cython-devel

changeset 1064:9d489341f992

sizeof() works on cdef attributes and cimported types
author Robert Bradshaw <robertwb@math.washington.edu>
date Sat Aug 16 16:30:15 2008 -0700 (3 years ago)
parents b1516f6bf662
children f92f158b855b
files Cython/Compiler/ExprNodes.py tests/compile/pylong.pyx
line diff
1.1 --- a/Cython/Compiler/ExprNodes.py Sat Aug 16 15:36:23 2008 -0700 1.2 +++ b/Cython/Compiler/ExprNodes.py Sat Aug 16 16:30:15 2008 -0700 1.3 @@ -3086,6 +3086,20 @@ 1.4 subexprs = [] 1.5 1.6 def analyse_types(self, env): 1.7 + # we may have incorrectly interpreted a dotted name as a type rather than an attribute 1.8 + # this could be better handled by more uniformly treating types as runtime-available objects 1.9 + if self.base_type.module_path: 1.10 + path = self.base_type.module_path 1.11 + obj = env.lookup(path[0]) 1.12 + if obj.as_module is None: 1.13 + operand = NameNode(pos=self.pos, name=path[0]) 1.14 + for attr in path[1:]: 1.15 + operand = AttributeNode(pos=self.pos, obj=operand, attribute=attr) 1.16 + operand = AttributeNode(pos=self.pos, obj=operand, attribute=self.base_type.name) 1.17 + self.operand = operand 1.18 + self.__class__ = SizeofVarNode 1.19 + self.analyse_types(env) 1.20 + return 1.21 base_type = self.base_type.analyse(env) 1.22 _, arg_type = self.declarator.analyse(base_type, env) 1.23 self.arg_type = arg_type
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/tests/compile/pylong.pyx Sat Aug 16 16:30:15 2008 -0700 2.3 @@ -0,0 +1,20 @@ 2.4 +cdef extern from "Python.h": 2.5 + ctypedef struct PyTypeObject: 2.6 + pass 2.7 + 2.8 + ctypedef struct PyObject: 2.9 + Py_ssize_t ob_refcnt 2.10 + PyTypeObject *ob_type 2.11 + 2.12 +cdef extern from "longintrepr.h": 2.13 + cdef struct _longobject: 2.14 + int ob_refcnt 2.15 + PyTypeObject *ob_type 2.16 + int ob_size 2.17 + unsigned int *ob_digit 2.18 + 2.19 +def test(temp = long(0)): 2.20 + cdef _longobject *l 2.21 + l = <_longobject *> temp 2.22 + print sizeof(l.ob_size) 2.23 + print sizeof(l.ob_digit[0])