From b5bfda6d8c16caa29f0c1e6e06ec3fddba76ed80 Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Wed, 25 Mar 2009 22:33:50 -0700 Subject: [PATCH] FIx #252, bad C struct identifiers --- Cython/Compiler/Symtab.py | 17 +++++++++++++++++ tests/compile/bad_c_struct_T252.pyx | 8 ++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/compile/bad_c_struct_T252.pyx diff --git a/Cython/Compiler/Symtab.py b/Cython/Compiler/Symtab.py index 58b57c85..6636dd1b 100644 --- a/Cython/Compiler/Symtab.py +++ b/Cython/Compiler/Symtab.py @@ -23,6 +23,19 @@ except NameError: possible_identifier = re.compile(ur"(?![0-9])\w+$", re.U).match nice_identifier = re.compile('^[a-zA-Z0-0_]+$').match +ansi_c_keywords = set( +['auto', 'break', 'case', 'char', 'const', 'continue', 'default', 'do', + 'double', 'else', 'enum', 'extern', 'float', 'for', 'goto', 'if', + 'int', 'long', 'register', 'return', 'short', 'signed', 'sizeof', + 'static', 'struct', 'switch', 'typedef', 'union', 'unsigned', 'void', + 'volatile', 'while']) + +def c_safe_identifier(cname): + # There are some C limitations on struct entry names. + if cname[:2] == '__' or cname in ansi_c_keywords: + cname = Naming.pyrex_prefix + cname + return cname + class BufferAux(object): writable_needed = False @@ -1252,6 +1265,8 @@ class StructOrUnionScope(Scope): # Add an entry for an attribute. if not cname: cname = name + if visibility == 'private': + cname = c_safe_identifier(cname) if type.is_cfunction: type = PyrexTypes.CPtrType(type) entry = self.declare(name, cname, type, pos, visibility) @@ -1387,6 +1402,8 @@ class CClassScope(ClassScope): % name) if not cname: cname = name + if visibility == 'private': + cname = c_safe_identifier(cname) entry = self.declare(name, cname, type, pos, visibility) entry.is_variable = 1 self.var_entries.append(entry) diff --git a/tests/compile/bad_c_struct_T252.pyx b/tests/compile/bad_c_struct_T252.pyx new file mode 100644 index 00000000..0f5b959e --- /dev/null +++ b/tests/compile/bad_c_struct_T252.pyx @@ -0,0 +1,8 @@ +cdef f(void=None): + pass + +cdef struct foo: + int void + +cdef class Foo: + cdef int void -- 2.26.2