1 # HG changeset patch |
|
2 # User Armin Rigo <[email protected]> |
|
3 # Date 1396624834 -7200 |
|
4 # Node ID facaf5de29c9e8cdaa148112ece17a4205cf2d84 |
|
5 # Parent 0457fbb2d45243a93cb8ed242646d7487bd36dc4 |
|
6 Issue #142: don't generate C files that use '$' in identifiers. |
|
7 |
|
8 diff --git a/cffi/vengine_cpy.py b/cffi/vengine_cpy.py |
|
9 --- a/cffi/vengine_cpy.py |
|
10 +++ b/cffi/vengine_cpy.py |
|
11 @@ -632,13 +632,18 @@ |
|
12 # ---------- |
|
13 # enums |
|
14 |
|
15 + def _enum_funcname(self, prefix, name): |
|
16 + # "$enum_$1" => "___D_enum____D_1" |
|
17 + name = name.replace('$', '___D_') |
|
18 + return '_cffi_e_%s_%s' % (prefix, name) |
|
19 + |
|
20 def _generate_cpy_enum_decl(self, tp, name, prefix='enum'): |
|
21 if tp.partial: |
|
22 for enumerator in tp.enumerators: |
|
23 self._generate_cpy_const(True, enumerator, delayed=False) |
|
24 return |
|
25 # |
|
26 - funcname = '_cffi_e_%s_%s' % (prefix, name) |
|
27 + funcname = self._enum_funcname(prefix, name) |
|
28 prnt = self._prnt |
|
29 prnt('static int %s(PyObject *lib)' % funcname) |
|
30 prnt('{') |
|
31 diff --git a/cffi/vengine_gen.py b/cffi/vengine_gen.py |
|
32 --- a/cffi/vengine_gen.py |
|
33 +++ b/cffi/vengine_gen.py |
|
34 @@ -410,13 +410,18 @@ |
|
35 # ---------- |
|
36 # enums |
|
37 |
|
38 + def _enum_funcname(self, prefix, name): |
|
39 + # "$enum_$1" => "___D_enum____D_1" |
|
40 + name = name.replace('$', '___D_') |
|
41 + return '_cffi_e_%s_%s' % (prefix, name) |
|
42 + |
|
43 def _generate_gen_enum_decl(self, tp, name, prefix='enum'): |
|
44 if tp.partial: |
|
45 for enumerator in tp.enumerators: |
|
46 self._generate_gen_const(True, enumerator) |
|
47 return |
|
48 # |
|
49 - funcname = '_cffi_e_%s_%s' % (prefix, name) |
|
50 + funcname = self._enum_funcname(prefix, name) |
|
51 self.export_symbols.append(funcname) |
|
52 prnt = self._prnt |
|
53 prnt('int %s(char *out_error)' % funcname) |
|
54 @@ -453,7 +458,7 @@ |
|
55 else: |
|
56 BType = self.ffi._typeof_locked("char[]")[0] |
|
57 BFunc = self.ffi._typeof_locked("int(*)(char*)")[0] |
|
58 - funcname = '_cffi_e_%s_%s' % (prefix, name) |
|
59 + funcname = self._enum_funcname(prefix, name) |
|
60 function = module.load_function(BFunc, funcname) |
|
61 p = self.ffi.new(BType, 256) |
|
62 if function(p) < 0: |
|
63 diff --git a/testing/test_verify.py b/testing/test_verify.py |
|
64 --- a/testing/test_verify.py |
|
65 +++ b/testing/test_verify.py |
|
66 @@ -1,4 +1,4 @@ |
|
67 -import py |
|
68 +import py, re |
|
69 import sys, os, math, weakref |
|
70 from cffi import FFI, VerificationError, VerificationMissing, model |
|
71 from testing.support import * |
|
72 @@ -29,6 +29,24 @@ |
|
73 def setup_module(): |
|
74 import cffi.verifier |
|
75 cffi.verifier.cleanup_tmpdir() |
|
76 + # |
|
77 + # check that no $ sign is produced in the C file; it used to be the |
|
78 + # case that anonymous enums would produce '$enum_$1', which was |
|
79 + # used as part of a function name. GCC accepts such names, but it's |
|
80 + # apparently non-standard. |
|
81 + _r_comment = re.compile(r"/\*.*?\*/|//.*?$", re.DOTALL | re.MULTILINE) |
|
82 + _r_string = re.compile(r'\".*?\"') |
|
83 + def _write_source_and_check(self, file=None): |
|
84 + base_write_source(self, file) |
|
85 + if file is None: |
|
86 + f = open(self.sourcefilename) |
|
87 + data = f.read() |
|
88 + f.close() |
|
89 + data = _r_comment.sub(' ', data) |
|
90 + data = _r_string.sub('"skipped"', data) |
|
91 + assert '$' not in data |
|
92 + base_write_source = cffi.verifier.Verifier._write_source |
|
93 + cffi.verifier.Verifier._write_source = _write_source_and_check |
|
94 |
|
95 |
|
96 def test_module_type(): |
|