sizeof() works on cdef attributes and cimported types
authorRobert Bradshaw <robertwb@math.washington.edu>
Sat, 16 Aug 2008 23:30:15 +0000 (16:30 -0700)
committerRobert Bradshaw <robertwb@math.washington.edu>
Sat, 16 Aug 2008 23:30:15 +0000 (16:30 -0700)
Cython/Compiler/ExprNodes.py
tests/compile/pylong.pyx [new file with mode: 0644]

index 1aca04bcfcc0acae9b888dec5747003c31afc224..253ff749333d3db4989415352b1aa9d617ddb56c 100644 (file)
@@ -3086,6 +3086,20 @@ class SizeofTypeNode(SizeofNode):
     subexprs = []
     
     def analyse_types(self, env):
+        # we may have incorrectly interpreted a dotted name as a type rather than an attribute
+        # this could be better handled by more uniformly treating types as runtime-available objects
+        if self.base_type.module_path:
+            path = self.base_type.module_path
+            obj = env.lookup(path[0])
+            if obj.as_module is None:
+                operand = NameNode(pos=self.pos, name=path[0])
+                for attr in path[1:]:
+                    operand = AttributeNode(pos=self.pos, obj=operand, attribute=attr)
+                operand = AttributeNode(pos=self.pos, obj=operand, attribute=self.base_type.name)
+                self.operand = operand
+                self.__class__ = SizeofVarNode
+                self.analyse_types(env)
+                return
         base_type = self.base_type.analyse(env)
         _, arg_type = self.declarator.analyse(base_type, env)
         self.arg_type = arg_type
diff --git a/tests/compile/pylong.pyx b/tests/compile/pylong.pyx
new file mode 100644 (file)
index 0000000..710938c
--- /dev/null
@@ -0,0 +1,20 @@
+cdef extern from "Python.h":
+    ctypedef struct PyTypeObject:
+        pass
+
+    ctypedef struct PyObject:
+        Py_ssize_t ob_refcnt 
+        PyTypeObject *ob_type
+
+cdef extern from "longintrepr.h":
+    cdef struct _longobject:
+        int ob_refcnt 
+        PyTypeObject *ob_type
+        int ob_size
+        unsigned int *ob_digit
+
+def test(temp = long(0)):
+    cdef _longobject *l 
+    l = <_longobject *> temp 
+    print sizeof(l.ob_size) 
+    print sizeof(l.ob_digit[0])