cython-devel

changeset 1906:e4ca38a675cb

FIx #252, bad C struct identifiers
author Robert Bradshaw <robertwb@math.washington.edu>
date Wed Mar 25 22:33:50 2009 -0700 (2 years ago)
parents 65b78cc40069
children 0610bab5f30b
files Cython/Compiler/Symtab.py tests/compile/bad_c_struct_T252.pyx
line diff
1.1 --- a/Cython/Compiler/Symtab.py Wed Mar 25 21:56:52 2009 -0700 1.2 +++ b/Cython/Compiler/Symtab.py Wed Mar 25 22:33:50 2009 -0700 1.3 @@ -23,6 +23,19 @@ 1.4 possible_identifier = re.compile(ur"(?![0-9])\w+$", re.U).match 1.5 nice_identifier = re.compile('^[a-zA-Z0-0_]+$').match 1.6 1.7 +ansi_c_keywords = set( 1.8 +['auto', 'break', 'case', 'char', 'const', 'continue', 'default', 'do', 1.9 + 'double', 'else', 'enum', 'extern', 'float', 'for', 'goto', 'if', 1.10 + 'int', 'long', 'register', 'return', 'short', 'signed', 'sizeof', 1.11 + 'static', 'struct', 'switch', 'typedef', 'union', 'unsigned', 'void', 1.12 + 'volatile', 'while']) 1.13 + 1.14 +def c_safe_identifier(cname): 1.15 + # There are some C limitations on struct entry names. 1.16 + if cname[:2] == '__' or cname in ansi_c_keywords: 1.17 + cname = Naming.pyrex_prefix + cname 1.18 + return cname 1.19 + 1.20 class BufferAux(object): 1.21 writable_needed = False 1.22 1.23 @@ -1252,6 +1265,8 @@ 1.24 # Add an entry for an attribute. 1.25 if not cname: 1.26 cname = name 1.27 + if visibility == 'private': 1.28 + cname = c_safe_identifier(cname) 1.29 if type.is_cfunction: 1.30 type = PyrexTypes.CPtrType(type) 1.31 entry = self.declare(name, cname, type, pos, visibility) 1.32 @@ -1387,6 +1402,8 @@ 1.33 % name) 1.34 if not cname: 1.35 cname = name 1.36 + if visibility == 'private': 1.37 + cname = c_safe_identifier(cname) 1.38 entry = self.declare(name, cname, type, pos, visibility) 1.39 entry.is_variable = 1 1.40 self.var_entries.append(entry)
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/tests/compile/bad_c_struct_T252.pyx Wed Mar 25 22:33:50 2009 -0700 2.3 @@ -0,0 +1,8 @@ 2.4 +cdef f(void=None): 2.5 + pass 2.6 + 2.7 +cdef struct foo: 2.8 + int void 2.9 + 2.10 +cdef class Foo: 2.11 + cdef int void