cython-devel

changeset 2118:97069bd04411

Ticket #355 - ctypedef class ordering
author Robert Bradshaw <robertwb@math.washington.edu>
date Thu Sep 10 19:33:50 2009 -0700 (3 years ago)
parents d4b478a9ee95
children 10e8f628ed98
files Cython/Compiler/ModuleNode.py Cython/Compiler/PyrexTypes.py Cython/Compiler/Symtab.py tests/compile/ctypedef_public_class_T355.pxd tests/compile/ctypedef_public_class_T355.pyx tests/errors/e_ctypedefforward.pyx
line diff
1.1 --- a/Cython/Compiler/ModuleNode.py Thu Sep 10 19:33:47 2009 -0700 1.2 +++ b/Cython/Compiler/ModuleNode.py Thu Sep 10 19:33:50 2009 -0700 1.3 @@ -811,6 +811,9 @@ 1.4 "%s;" % 1.5 attr.type.declaration_code(attr.cname)) 1.6 code.putln(footer) 1.7 + if type.objtypedef_cname is not None: 1.8 + # Only for exposing public typedef name. 1.9 + code.putln("typedef struct %s %s;" % (type.objstruct_cname, type.objtypedef_cname)) 1.10 1.11 def generate_global_declarations(self, env, code, definition): 1.12 code.putln("")
2.1 --- a/Cython/Compiler/PyrexTypes.py Thu Sep 10 19:33:47 2009 -0700 2.2 +++ b/Cython/Compiler/PyrexTypes.py Thu Sep 10 19:33:50 2009 -0700 2.3 @@ -368,6 +368,7 @@ 2.4 # base_type PyExtensionType or None 2.5 # module_name string or None Qualified name of defining module 2.6 # objstruct_cname string Name of PyObject struct 2.7 + # objtypedef_cname string Name of PyObject struct typedef 2.8 # typeobj_cname string or None C code fragment referring to type object 2.9 # typeptr_cname string or None Name of pointer to external type object 2.10 # vtabslot_cname string Name of C method table member 2.11 @@ -378,6 +379,8 @@ 2.12 is_extension_type = 1 2.13 has_attributes = 1 2.14 2.15 + objtypedef_cname = None 2.16 + 2.17 def __init__(self, name, typedef_flag, base_type): 2.18 self.name = name 2.19 self.scope = None
3.1 --- a/Cython/Compiler/Symtab.py Thu Sep 10 19:33:47 2009 -0700 3.2 +++ b/Cython/Compiler/Symtab.py Thu Sep 10 19:33:50 2009 -0700 3.3 @@ -1010,6 +1010,15 @@ 3.4 module_name = None, base_type = None, objstruct_cname = None, 3.5 typeobj_cname = None, visibility = 'private', typedef_flag = 0, api = 0, 3.6 buffer_defaults = None): 3.7 + # If this is a non-extern typedef class, expose the typedef, but use 3.8 + # the non-typedef struct internally to avoid needing forward 3.9 + # declarations for anonymous structs. 3.10 + if typedef_flag and visibility != 'extern': 3.11 + objtypedef_cname = objstruct_cname 3.12 + objstruct_cname = None 3.13 + typedef_flag = 0 3.14 + else: 3.15 + objtypedef_cname = None 3.16 # 3.17 # Look for previous declaration as a type 3.18 # 3.19 @@ -1034,6 +1043,8 @@ 3.20 type = PyrexTypes.PyExtensionType(name, typedef_flag, base_type) 3.21 type.pos = pos 3.22 type.buffer_defaults = buffer_defaults 3.23 + if objtypedef_cname is not None: 3.24 + type.objtypedef_cname = objtypedef_cname 3.25 if visibility == 'extern': 3.26 type.module_name = module_name 3.27 else: 3.28 @@ -1045,7 +1056,7 @@ 3.29 if objstruct_cname: 3.30 type.objstruct_cname = objstruct_cname 3.31 elif not entry.in_cinclude: 3.32 - type.objstruct_cname = self.mangle(Naming.objstruct_prefix, name) 3.33 + type.objstruct_cname = self.mangle(Naming.objstruct_prefix, name) 3.34 else: 3.35 error(entry.pos, 3.36 "Object name required for 'public' or 'extern' C class")
4.1 --- a/tests/compile/ctypedef_public_class_T355.pxd Thu Sep 10 19:33:47 2009 -0700 4.2 +++ b/tests/compile/ctypedef_public_class_T355.pxd Thu Sep 10 19:33:50 2009 -0700 4.3 @@ -2,4 +2,4 @@ 4.4 cdef public double seconds 4.5 4.6 ctypedef public class Event [type MyEvent_Type, object MyEventObject]: 4.7 - cdef public Time time 4.8 \ No newline at end of file 4.9 + cdef public Time time
5.1 --- a/tests/compile/ctypedef_public_class_T355.pyx Thu Sep 10 19:33:47 2009 -0700 5.2 +++ b/tests/compile/ctypedef_public_class_T355.pyx Thu Sep 10 19:33:50 2009 -0700 5.3 @@ -4,4 +4,4 @@ 5.4 5.5 ctypedef public class Event [type MyEvent_Type, object MyEventObject]: 5.6 def __init__(self, Time time): 5.7 - self.time = time 5.8 \ No newline at end of file 5.9 + self.time = time
6.1 --- a/tests/errors/e_ctypedefforward.pyx Thu Sep 10 19:33:47 2009 -0700 6.2 +++ b/tests/errors/e_ctypedefforward.pyx Thu Sep 10 19:33:50 2009 -0700 6.3 @@ -1,5 +1,4 @@ 6.4 ctypedef struct Spam 6.5 -ctypedef class Eggs 6.6 6.7 cdef extern from *: 6.8 ctypedef struct Ham 6.9 @@ -7,12 +6,7 @@ 6.10 ctypedef struct Spam: 6.11 int i 6.12 6.13 -ctypedef class Eggs: 6.14 - pass 6.15 - 6.16 ctypedef struct Spam 6.17 -ctypedef class Eggs 6.18 _ERRORS = u""" 6.19 1:0: Forward-referenced type must use 'cdef', not 'ctypedef' 6.20 -2:0: Forward-referenced type must use 'cdef', not 'ctypedef' 6.21 """