From: Robert Bradshaw Date: Sat, 16 Aug 2008 23:30:15 +0000 (-0700) Subject: sizeof() works on cdef attributes and cimported types X-Git-Tag: 0.9.8.1~10 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=5225e53d4f23cf7093baa9e128b61881e69f9ea5;p=cython.git sizeof() works on cdef attributes and cimported types --- diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 1aca04bc..253ff749 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -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 index 00000000..710938c1 --- /dev/null +++ b/tests/compile/pylong.pyx @@ -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])