--- /dev/null
+_adapt.pxi
+adapt.pxi
+build_ctao.py
+build_mpi.py
+build_petsc.py
+build_tao.py
+build_test.py
+cgen.py
+cparse.py
+dump.py
+genpyx.py
+ir.py
+lexer.py
+node.py
+numarray.pxi
+parse_core.py
+signature.py
+trace.py
+work_unit.py
+MANIFEST
+TODO
--- /dev/null
+
+pyxelator TODO:
+===============
+
+.) callback functions: these are working but it needs to be thought out some more. Exceptions are not propagated from callbacks.
+.) no nice module structure: it's all in one fat module.
+.) extern symbols and macros are a little tricky to wrap, some have been hardcoded (!!) in petscmacros.py.
+.) type checking
+.) upcasting
+.) adapt system memory usage audit: record and check every malloc/free/memcpy/memcmp
+ and every memory access using those nasty void* pointers
+
+21/10/05
+
+
+
--- /dev/null
+# code autogenerated from ./cgen.py
+
+cdef class _CChar(_CObject)
+cdef class _CSChar(_CObject)
+cdef class _CUChar(_CObject)
+cdef class _CShort(_CObject)
+cdef class _CUShort(_CObject)
+cdef class _CInt(_CObject)
+cdef class _CUInt(_CObject)
+cdef class _CLong(_CObject)
+cdef class _CULong(_CObject)
+cdef class _CLLong(_CObject)
+cdef class _CULLong(_CObject)
+cdef class _CFloat(_CObject)
+cdef class _CDouble(_CObject)
+cdef class _CLDouble(_CObject)
+
+
+#cdef public class _CChar(_CObject)[ object AdaptObj_CChar, type AdaptType_CChar ]:
+cdef class _CChar(_CObject):
+ def __init__( self, value = None, addr = None ):
+ cdef char*p
+ cdef long c_addr
+ cdef _CObject cobject
+ if addr is None:
+ p = <char*>pyx_calloc(1,sizeof(char))
+ self.p = <void*>p
+ if isinstance(value,CObject):
+ if isinstance(value,_CChar):
+ cobject = value
+ p[0] = (<char*>cobject.p)[0]
+ else:
+ value.init_CChar(self)
+ elif value is not None:
+ p[0] = value
+ else:
+ p[0] = 0
+ self.malloced = True
+ else:
+ c_addr = addr
+ self.p = <void*>c_addr
+ self.malloced = False
+ def __int__( self ):
+ cdef char *p
+ assert self.p != NULL
+ p = <char*>self.p
+ return int(p[0])
+ def __float__( self ):
+ cdef char *p
+ assert self.p != NULL
+ p = <char*>self.p
+ return float(p[0])
+ def __add__( x, y ):
+ cdef char val
+ cdef _CChar self
+ if not isinstance(x,CChar):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<char*>self.p)[0]
+ val = val + y
+ return CChar(val)
+ def __sub__( x, y ):
+ cdef char val
+ cdef _CChar self
+ if not isinstance(x,CChar):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<char*>self.p)[0]
+ val = val - y
+ return CChar(val)
+ def __mul__( x, y ):
+ cdef char val
+ cdef _CChar self
+ if not isinstance(x,CChar):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<char*>self.p)[0]
+ val = val + y
+ return CChar(val)
+ def __repr__( self ):
+ cdef long c_addr
+ cdef char c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<char*>self.p)[0]
+ value = ", value = %s" % str( c_val )
+ return "%s( addr = %s%s )" % (self.__class__.__name__, hex(c_addr), value)
+ def set_value( self, value ):
+ if isinstance(value,CObject):
+ value.init_CChar(self)
+ else:
+ (<char*>self.p)[0] = value
+ def get_value( self ):
+ return (<char*>self.p)[0]
+ def to_CChar(self):
+ return self
+ def init_from( self, _CChar cobject ):
+ " init self from a CChar instance "
+# if not self.is_compatible(cobject):
+ if not isinstance( cobject, CChar ):
+ raise TypeError("%s cannot be set from %s" % (self.__class__, cobject))
+ (<char*>self.p)[0] = (<char*>cobject.p)[0]
+ def get_basetype( self ):
+ return CChar
+# def sizeof( self ):
+# return sizeof(char)
+# def is_compatible( self, _CObject cobject ):
+# return isinstance( cobject, CChar )
+ def __str__( self ):
+ cdef int a
+ a = (<char*>self.p)[0]
+ s = PyString_FromFormat('%c', a)
+ return s
+ def from_CChar( self, _CChar cobject ):
+ " set self from a CChar instance"
+ (<char*>self.p)[0] = <char>(<char*>cobject.p)[0]
+ def init_CChar( self, _CChar cobject ):
+ " set a CChar instance from self "
+ (<char*>cobject.p)[0] = <char>(<char*>self.p)[0]
+ def from_CSChar( self, _CSChar cobject ):
+ " set self from a CSChar instance"
+ (<char*>self.p)[0] = <char>(<signed char*>cobject.p)[0]
+ def init_CSChar( self, _CSChar cobject ):
+ " set a CSChar instance from self "
+ (<signed char*>cobject.p)[0] = <signed char>(<char*>self.p)[0]
+ def to_CSChar( self ):
+ " cast self to new CSChar instance "
+ cdef char c_src
+ cdef signed char c_tgt
+ cdef _CChar py_src
+ cdef _CSChar py_tgt
+ c_src = (<char*>self.p)[0]
+ c_tgt = <signed char>c_src
+ py_tgt = CSChar()
+ (<signed char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUChar( self, _CUChar cobject ):
+ " set self from a CUChar instance"
+ (<char*>self.p)[0] = <char>(<unsigned char*>cobject.p)[0]
+ def init_CUChar( self, _CUChar cobject ):
+ " set a CUChar instance from self "
+ (<unsigned char*>cobject.p)[0] = <unsigned char>(<char*>self.p)[0]
+ def to_CUChar( self ):
+ " cast self to new CUChar instance "
+ cdef char c_src
+ cdef unsigned char c_tgt
+ cdef _CChar py_src
+ cdef _CUChar py_tgt
+ c_src = (<char*>self.p)[0]
+ c_tgt = <unsigned char>c_src
+ py_tgt = CUChar()
+ (<unsigned char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CShort( self, _CShort cobject ):
+ " set self from a CShort instance"
+ (<char*>self.p)[0] = <char>(<short*>cobject.p)[0]
+ def init_CShort( self, _CShort cobject ):
+ " set a CShort instance from self "
+ (<short*>cobject.p)[0] = <short>(<char*>self.p)[0]
+ def to_CShort( self ):
+ " cast self to new CShort instance "
+ cdef char c_src
+ cdef short c_tgt
+ cdef _CChar py_src
+ cdef _CShort py_tgt
+ c_src = (<char*>self.p)[0]
+ c_tgt = <short>c_src
+ py_tgt = CShort()
+ (<short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUShort( self, _CUShort cobject ):
+ " set self from a CUShort instance"
+ (<char*>self.p)[0] = <char>(<unsigned short*>cobject.p)[0]
+ def init_CUShort( self, _CUShort cobject ):
+ " set a CUShort instance from self "
+ (<unsigned short*>cobject.p)[0] = <unsigned short>(<char*>self.p)[0]
+ def to_CUShort( self ):
+ " cast self to new CUShort instance "
+ cdef char c_src
+ cdef unsigned short c_tgt
+ cdef _CChar py_src
+ cdef _CUShort py_tgt
+ c_src = (<char*>self.p)[0]
+ c_tgt = <unsigned short>c_src
+ py_tgt = CUShort()
+ (<unsigned short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CInt( self, _CInt cobject ):
+ " set self from a CInt instance"
+ (<char*>self.p)[0] = <char>(<int*>cobject.p)[0]
+ def init_CInt( self, _CInt cobject ):
+ " set a CInt instance from self "
+ (<int*>cobject.p)[0] = <int>(<char*>self.p)[0]
+ def to_CInt( self ):
+ " cast self to new CInt instance "
+ cdef char c_src
+ cdef int c_tgt
+ cdef _CChar py_src
+ cdef _CInt py_tgt
+ c_src = (<char*>self.p)[0]
+ c_tgt = <int>c_src
+ py_tgt = CInt()
+ (<int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUInt( self, _CUInt cobject ):
+ " set self from a CUInt instance"
+ (<char*>self.p)[0] = <char>(<unsigned int*>cobject.p)[0]
+ def init_CUInt( self, _CUInt cobject ):
+ " set a CUInt instance from self "
+ (<unsigned int*>cobject.p)[0] = <unsigned int>(<char*>self.p)[0]
+ def to_CUInt( self ):
+ " cast self to new CUInt instance "
+ cdef char c_src
+ cdef unsigned int c_tgt
+ cdef _CChar py_src
+ cdef _CUInt py_tgt
+ c_src = (<char*>self.p)[0]
+ c_tgt = <unsigned int>c_src
+ py_tgt = CUInt()
+ (<unsigned int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLong( self, _CLong cobject ):
+ " set self from a CLong instance"
+ (<char*>self.p)[0] = <char>(<long*>cobject.p)[0]
+ def init_CLong( self, _CLong cobject ):
+ " set a CLong instance from self "
+ (<long*>cobject.p)[0] = <long>(<char*>self.p)[0]
+ def to_CLong( self ):
+ " cast self to new CLong instance "
+ cdef char c_src
+ cdef long c_tgt
+ cdef _CChar py_src
+ cdef _CLong py_tgt
+ c_src = (<char*>self.p)[0]
+ c_tgt = <long>c_src
+ py_tgt = CLong()
+ (<long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULong( self, _CULong cobject ):
+ " set self from a CULong instance"
+ (<char*>self.p)[0] = <char>(<unsigned long*>cobject.p)[0]
+ def init_CULong( self, _CULong cobject ):
+ " set a CULong instance from self "
+ (<unsigned long*>cobject.p)[0] = <unsigned long>(<char*>self.p)[0]
+ def to_CULong( self ):
+ " cast self to new CULong instance "
+ cdef char c_src
+ cdef unsigned long c_tgt
+ cdef _CChar py_src
+ cdef _CULong py_tgt
+ c_src = (<char*>self.p)[0]
+ c_tgt = <unsigned long>c_src
+ py_tgt = CULong()
+ (<unsigned long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLLong( self, _CLLong cobject ):
+ " set self from a CLLong instance"
+ (<char*>self.p)[0] = <char>(<long long*>cobject.p)[0]
+ def init_CLLong( self, _CLLong cobject ):
+ " set a CLLong instance from self "
+ (<long long*>cobject.p)[0] = <long long>(<char*>self.p)[0]
+ def to_CLLong( self ):
+ " cast self to new CLLong instance "
+ cdef char c_src
+ cdef long long c_tgt
+ cdef _CChar py_src
+ cdef _CLLong py_tgt
+ c_src = (<char*>self.p)[0]
+ c_tgt = <long long>c_src
+ py_tgt = CLLong()
+ (<long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULLong( self, _CULLong cobject ):
+ " set self from a CULLong instance"
+ (<char*>self.p)[0] = <char>(<unsigned long long*>cobject.p)[0]
+ def init_CULLong( self, _CULLong cobject ):
+ " set a CULLong instance from self "
+ (<unsigned long long*>cobject.p)[0] = <unsigned long long>(<char*>self.p)[0]
+ def to_CULLong( self ):
+ " cast self to new CULLong instance "
+ cdef char c_src
+ cdef unsigned long long c_tgt
+ cdef _CChar py_src
+ cdef _CULLong py_tgt
+ c_src = (<char*>self.p)[0]
+ c_tgt = <unsigned long long>c_src
+ py_tgt = CULLong()
+ (<unsigned long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CFloat( self, _CFloat cobject ):
+ " set self from a CFloat instance"
+ (<char*>self.p)[0] = <char>(<float*>cobject.p)[0]
+ def init_CFloat( self, _CFloat cobject ):
+ " set a CFloat instance from self "
+ (<float*>cobject.p)[0] = <float>(<char*>self.p)[0]
+ def to_CFloat( self ):
+ " cast self to new CFloat instance "
+ cdef char c_src
+ cdef float c_tgt
+ cdef _CChar py_src
+ cdef _CFloat py_tgt
+ c_src = (<char*>self.p)[0]
+ c_tgt = <float>c_src
+ py_tgt = CFloat()
+ (<float*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CDouble( self, _CDouble cobject ):
+ " set self from a CDouble instance"
+ (<char*>self.p)[0] = <char>(<double*>cobject.p)[0]
+ def init_CDouble( self, _CDouble cobject ):
+ " set a CDouble instance from self "
+ (<double*>cobject.p)[0] = <double>(<char*>self.p)[0]
+ def to_CDouble( self ):
+ " cast self to new CDouble instance "
+ cdef char c_src
+ cdef double c_tgt
+ cdef _CChar py_src
+ cdef _CDouble py_tgt
+ c_src = (<char*>self.p)[0]
+ c_tgt = <double>c_src
+ py_tgt = CDouble()
+ (<double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLDouble( self, _CLDouble cobject ):
+ " set self from a CLDouble instance"
+ (<char*>self.p)[0] = <char>(<long double*>cobject.p)[0]
+ def init_CLDouble( self, _CLDouble cobject ):
+ " set a CLDouble instance from self "
+ (<long double*>cobject.p)[0] = <long double>(<char*>self.p)[0]
+ def to_CLDouble( self ):
+ " cast self to new CLDouble instance "
+ cdef char c_src
+ cdef long double c_tgt
+ cdef _CChar py_src
+ cdef _CLDouble py_tgt
+ c_src = (<char*>self.p)[0]
+ c_tgt = <long double>c_src
+ py_tgt = CLDouble()
+ (<long double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+CChar = Meta('CChar', (_CChar,), {} )
+CChar.basetype = CChar
+CChar.sizeof = sizeof(char)
+
+#cdef public class _CSChar(_CObject)[ object AdaptObj_CSChar, type AdaptType_CSChar ]:
+cdef class _CSChar(_CObject):
+ def __init__( self, value = None, addr = None ):
+ cdef signed char*p
+ cdef long c_addr
+ cdef _CObject cobject
+ if addr is None:
+ p = <signed char*>pyx_calloc(1,sizeof(signed char))
+ self.p = <void*>p
+ if isinstance(value,CObject):
+ if isinstance(value,_CSChar):
+ cobject = value
+ p[0] = (<signed char*>cobject.p)[0]
+ else:
+ value.init_CSChar(self)
+ elif value is not None:
+ p[0] = value
+ else:
+ p[0] = 0
+ self.malloced = True
+ else:
+ c_addr = addr
+ self.p = <void*>c_addr
+ self.malloced = False
+ def __int__( self ):
+ cdef signed char *p
+ assert self.p != NULL
+ p = <signed char*>self.p
+ return int(p[0])
+ def __float__( self ):
+ cdef signed char *p
+ assert self.p != NULL
+ p = <signed char*>self.p
+ return float(p[0])
+ def __add__( x, y ):
+ cdef signed char val
+ cdef _CSChar self
+ if not isinstance(x,CSChar):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<signed char*>self.p)[0]
+ val = val + y
+ return CSChar(val)
+ def __sub__( x, y ):
+ cdef signed char val
+ cdef _CSChar self
+ if not isinstance(x,CSChar):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<signed char*>self.p)[0]
+ val = val - y
+ return CSChar(val)
+ def __mul__( x, y ):
+ cdef signed char val
+ cdef _CSChar self
+ if not isinstance(x,CSChar):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<signed char*>self.p)[0]
+ val = val + y
+ return CSChar(val)
+ def __repr__( self ):
+ cdef long c_addr
+ cdef signed char c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<signed char*>self.p)[0]
+ value = ", value = %s" % str( c_val )
+ return "%s( addr = %s%s )" % (self.__class__.__name__, hex(c_addr), value)
+ def set_value( self, value ):
+ if isinstance(value,CObject):
+ value.init_CSChar(self)
+ else:
+ (<signed char*>self.p)[0] = value
+ def get_value( self ):
+ return (<signed char*>self.p)[0]
+ def to_CSChar(self):
+ return self
+ def init_from( self, _CSChar cobject ):
+ " init self from a CSChar instance "
+# if not self.is_compatible(cobject):
+ if not isinstance( cobject, CSChar ):
+ raise TypeError("%s cannot be set from %s" % (self.__class__, cobject))
+ (<signed char*>self.p)[0] = (<signed char*>cobject.p)[0]
+ def get_basetype( self ):
+ return CSChar
+# def sizeof( self ):
+# return sizeof(signed char)
+# def is_compatible( self, _CObject cobject ):
+# return isinstance( cobject, CSChar )
+ def __str__( self ):
+ cdef long c_addr
+ cdef signed char c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<signed char*>self.p)[0]
+ value = str( c_val )
+ return value
+ def from_CChar( self, _CChar cobject ):
+ " set self from a CChar instance"
+ (<signed char*>self.p)[0] = <signed char>(<char*>cobject.p)[0]
+ def init_CChar( self, _CChar cobject ):
+ " set a CChar instance from self "
+ (<char*>cobject.p)[0] = <char>(<signed char*>self.p)[0]
+ def to_CChar( self ):
+ " cast self to new CChar instance "
+ cdef signed char c_src
+ cdef char c_tgt
+ cdef _CSChar py_src
+ cdef _CChar py_tgt
+ c_src = (<signed char*>self.p)[0]
+ c_tgt = <char>c_src
+ py_tgt = CChar()
+ (<char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CSChar( self, _CSChar cobject ):
+ " set self from a CSChar instance"
+ (<signed char*>self.p)[0] = <signed char>(<signed char*>cobject.p)[0]
+ def init_CSChar( self, _CSChar cobject ):
+ " set a CSChar instance from self "
+ (<signed char*>cobject.p)[0] = <signed char>(<signed char*>self.p)[0]
+ def from_CUChar( self, _CUChar cobject ):
+ " set self from a CUChar instance"
+ (<signed char*>self.p)[0] = <signed char>(<unsigned char*>cobject.p)[0]
+ def init_CUChar( self, _CUChar cobject ):
+ " set a CUChar instance from self "
+ (<unsigned char*>cobject.p)[0] = <unsigned char>(<signed char*>self.p)[0]
+ def to_CUChar( self ):
+ " cast self to new CUChar instance "
+ cdef signed char c_src
+ cdef unsigned char c_tgt
+ cdef _CSChar py_src
+ cdef _CUChar py_tgt
+ c_src = (<signed char*>self.p)[0]
+ c_tgt = <unsigned char>c_src
+ py_tgt = CUChar()
+ (<unsigned char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CShort( self, _CShort cobject ):
+ " set self from a CShort instance"
+ (<signed char*>self.p)[0] = <signed char>(<short*>cobject.p)[0]
+ def init_CShort( self, _CShort cobject ):
+ " set a CShort instance from self "
+ (<short*>cobject.p)[0] = <short>(<signed char*>self.p)[0]
+ def to_CShort( self ):
+ " cast self to new CShort instance "
+ cdef signed char c_src
+ cdef short c_tgt
+ cdef _CSChar py_src
+ cdef _CShort py_tgt
+ c_src = (<signed char*>self.p)[0]
+ c_tgt = <short>c_src
+ py_tgt = CShort()
+ (<short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUShort( self, _CUShort cobject ):
+ " set self from a CUShort instance"
+ (<signed char*>self.p)[0] = <signed char>(<unsigned short*>cobject.p)[0]
+ def init_CUShort( self, _CUShort cobject ):
+ " set a CUShort instance from self "
+ (<unsigned short*>cobject.p)[0] = <unsigned short>(<signed char*>self.p)[0]
+ def to_CUShort( self ):
+ " cast self to new CUShort instance "
+ cdef signed char c_src
+ cdef unsigned short c_tgt
+ cdef _CSChar py_src
+ cdef _CUShort py_tgt
+ c_src = (<signed char*>self.p)[0]
+ c_tgt = <unsigned short>c_src
+ py_tgt = CUShort()
+ (<unsigned short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CInt( self, _CInt cobject ):
+ " set self from a CInt instance"
+ (<signed char*>self.p)[0] = <signed char>(<int*>cobject.p)[0]
+ def init_CInt( self, _CInt cobject ):
+ " set a CInt instance from self "
+ (<int*>cobject.p)[0] = <int>(<signed char*>self.p)[0]
+ def to_CInt( self ):
+ " cast self to new CInt instance "
+ cdef signed char c_src
+ cdef int c_tgt
+ cdef _CSChar py_src
+ cdef _CInt py_tgt
+ c_src = (<signed char*>self.p)[0]
+ c_tgt = <int>c_src
+ py_tgt = CInt()
+ (<int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUInt( self, _CUInt cobject ):
+ " set self from a CUInt instance"
+ (<signed char*>self.p)[0] = <signed char>(<unsigned int*>cobject.p)[0]
+ def init_CUInt( self, _CUInt cobject ):
+ " set a CUInt instance from self "
+ (<unsigned int*>cobject.p)[0] = <unsigned int>(<signed char*>self.p)[0]
+ def to_CUInt( self ):
+ " cast self to new CUInt instance "
+ cdef signed char c_src
+ cdef unsigned int c_tgt
+ cdef _CSChar py_src
+ cdef _CUInt py_tgt
+ c_src = (<signed char*>self.p)[0]
+ c_tgt = <unsigned int>c_src
+ py_tgt = CUInt()
+ (<unsigned int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLong( self, _CLong cobject ):
+ " set self from a CLong instance"
+ (<signed char*>self.p)[0] = <signed char>(<long*>cobject.p)[0]
+ def init_CLong( self, _CLong cobject ):
+ " set a CLong instance from self "
+ (<long*>cobject.p)[0] = <long>(<signed char*>self.p)[0]
+ def to_CLong( self ):
+ " cast self to new CLong instance "
+ cdef signed char c_src
+ cdef long c_tgt
+ cdef _CSChar py_src
+ cdef _CLong py_tgt
+ c_src = (<signed char*>self.p)[0]
+ c_tgt = <long>c_src
+ py_tgt = CLong()
+ (<long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULong( self, _CULong cobject ):
+ " set self from a CULong instance"
+ (<signed char*>self.p)[0] = <signed char>(<unsigned long*>cobject.p)[0]
+ def init_CULong( self, _CULong cobject ):
+ " set a CULong instance from self "
+ (<unsigned long*>cobject.p)[0] = <unsigned long>(<signed char*>self.p)[0]
+ def to_CULong( self ):
+ " cast self to new CULong instance "
+ cdef signed char c_src
+ cdef unsigned long c_tgt
+ cdef _CSChar py_src
+ cdef _CULong py_tgt
+ c_src = (<signed char*>self.p)[0]
+ c_tgt = <unsigned long>c_src
+ py_tgt = CULong()
+ (<unsigned long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLLong( self, _CLLong cobject ):
+ " set self from a CLLong instance"
+ (<signed char*>self.p)[0] = <signed char>(<long long*>cobject.p)[0]
+ def init_CLLong( self, _CLLong cobject ):
+ " set a CLLong instance from self "
+ (<long long*>cobject.p)[0] = <long long>(<signed char*>self.p)[0]
+ def to_CLLong( self ):
+ " cast self to new CLLong instance "
+ cdef signed char c_src
+ cdef long long c_tgt
+ cdef _CSChar py_src
+ cdef _CLLong py_tgt
+ c_src = (<signed char*>self.p)[0]
+ c_tgt = <long long>c_src
+ py_tgt = CLLong()
+ (<long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULLong( self, _CULLong cobject ):
+ " set self from a CULLong instance"
+ (<signed char*>self.p)[0] = <signed char>(<unsigned long long*>cobject.p)[0]
+ def init_CULLong( self, _CULLong cobject ):
+ " set a CULLong instance from self "
+ (<unsigned long long*>cobject.p)[0] = <unsigned long long>(<signed char*>self.p)[0]
+ def to_CULLong( self ):
+ " cast self to new CULLong instance "
+ cdef signed char c_src
+ cdef unsigned long long c_tgt
+ cdef _CSChar py_src
+ cdef _CULLong py_tgt
+ c_src = (<signed char*>self.p)[0]
+ c_tgt = <unsigned long long>c_src
+ py_tgt = CULLong()
+ (<unsigned long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CFloat( self, _CFloat cobject ):
+ " set self from a CFloat instance"
+ (<signed char*>self.p)[0] = <signed char>(<float*>cobject.p)[0]
+ def init_CFloat( self, _CFloat cobject ):
+ " set a CFloat instance from self "
+ (<float*>cobject.p)[0] = <float>(<signed char*>self.p)[0]
+ def to_CFloat( self ):
+ " cast self to new CFloat instance "
+ cdef signed char c_src
+ cdef float c_tgt
+ cdef _CSChar py_src
+ cdef _CFloat py_tgt
+ c_src = (<signed char*>self.p)[0]
+ c_tgt = <float>c_src
+ py_tgt = CFloat()
+ (<float*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CDouble( self, _CDouble cobject ):
+ " set self from a CDouble instance"
+ (<signed char*>self.p)[0] = <signed char>(<double*>cobject.p)[0]
+ def init_CDouble( self, _CDouble cobject ):
+ " set a CDouble instance from self "
+ (<double*>cobject.p)[0] = <double>(<signed char*>self.p)[0]
+ def to_CDouble( self ):
+ " cast self to new CDouble instance "
+ cdef signed char c_src
+ cdef double c_tgt
+ cdef _CSChar py_src
+ cdef _CDouble py_tgt
+ c_src = (<signed char*>self.p)[0]
+ c_tgt = <double>c_src
+ py_tgt = CDouble()
+ (<double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLDouble( self, _CLDouble cobject ):
+ " set self from a CLDouble instance"
+ (<signed char*>self.p)[0] = <signed char>(<long double*>cobject.p)[0]
+ def init_CLDouble( self, _CLDouble cobject ):
+ " set a CLDouble instance from self "
+ (<long double*>cobject.p)[0] = <long double>(<signed char*>self.p)[0]
+ def to_CLDouble( self ):
+ " cast self to new CLDouble instance "
+ cdef signed char c_src
+ cdef long double c_tgt
+ cdef _CSChar py_src
+ cdef _CLDouble py_tgt
+ c_src = (<signed char*>self.p)[0]
+ c_tgt = <long double>c_src
+ py_tgt = CLDouble()
+ (<long double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+CSChar = Meta('CSChar', (_CSChar,), {} )
+CSChar.basetype = CSChar
+CSChar.sizeof = sizeof(signed char)
+
+#cdef public class _CUChar(_CObject)[ object AdaptObj_CUChar, type AdaptType_CUChar ]:
+cdef class _CUChar(_CObject):
+ def __init__( self, value = None, addr = None ):
+ cdef unsigned char*p
+ cdef long c_addr
+ cdef _CObject cobject
+ if addr is None:
+ p = <unsigned char*>pyx_calloc(1,sizeof(unsigned char))
+ self.p = <void*>p
+ if isinstance(value,CObject):
+ if isinstance(value,_CUChar):
+ cobject = value
+ p[0] = (<unsigned char*>cobject.p)[0]
+ else:
+ value.init_CUChar(self)
+ elif value is not None:
+ p[0] = value
+ else:
+ p[0] = 0
+ self.malloced = True
+ else:
+ c_addr = addr
+ self.p = <void*>c_addr
+ self.malloced = False
+ def __int__( self ):
+ cdef unsigned char *p
+ assert self.p != NULL
+ p = <unsigned char*>self.p
+ return int(p[0])
+ def __float__( self ):
+ cdef unsigned char *p
+ assert self.p != NULL
+ p = <unsigned char*>self.p
+ return float(p[0])
+ def __add__( x, y ):
+ cdef unsigned char val
+ cdef _CUChar self
+ if not isinstance(x,CUChar):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<unsigned char*>self.p)[0]
+ val = val + y
+ return CUChar(val)
+ def __sub__( x, y ):
+ cdef unsigned char val
+ cdef _CUChar self
+ if not isinstance(x,CUChar):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<unsigned char*>self.p)[0]
+ val = val - y
+ return CUChar(val)
+ def __mul__( x, y ):
+ cdef unsigned char val
+ cdef _CUChar self
+ if not isinstance(x,CUChar):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<unsigned char*>self.p)[0]
+ val = val + y
+ return CUChar(val)
+ def __repr__( self ):
+ cdef long c_addr
+ cdef unsigned char c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<unsigned char*>self.p)[0]
+ value = ", value = %s" % str( c_val )
+ return "%s( addr = %s%s )" % (self.__class__.__name__, hex(c_addr), value)
+ def set_value( self, value ):
+ if isinstance(value,CObject):
+ value.init_CUChar(self)
+ else:
+ (<unsigned char*>self.p)[0] = value
+ def get_value( self ):
+ return (<unsigned char*>self.p)[0]
+ def to_CUChar(self):
+ return self
+ def init_from( self, _CUChar cobject ):
+ " init self from a CUChar instance "
+# if not self.is_compatible(cobject):
+ if not isinstance( cobject, CUChar ):
+ raise TypeError("%s cannot be set from %s" % (self.__class__, cobject))
+ (<unsigned char*>self.p)[0] = (<unsigned char*>cobject.p)[0]
+ def get_basetype( self ):
+ return CUChar
+# def sizeof( self ):
+# return sizeof(unsigned char)
+# def is_compatible( self, _CObject cobject ):
+# return isinstance( cobject, CUChar )
+ def __str__( self ):
+ cdef long c_addr
+ cdef unsigned char c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<unsigned char*>self.p)[0]
+ value = str( c_val )
+ return value
+ def from_CChar( self, _CChar cobject ):
+ " set self from a CChar instance"
+ (<unsigned char*>self.p)[0] = <unsigned char>(<char*>cobject.p)[0]
+ def init_CChar( self, _CChar cobject ):
+ " set a CChar instance from self "
+ (<char*>cobject.p)[0] = <char>(<unsigned char*>self.p)[0]
+ def to_CChar( self ):
+ " cast self to new CChar instance "
+ cdef unsigned char c_src
+ cdef char c_tgt
+ cdef _CUChar py_src
+ cdef _CChar py_tgt
+ c_src = (<unsigned char*>self.p)[0]
+ c_tgt = <char>c_src
+ py_tgt = CChar()
+ (<char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CSChar( self, _CSChar cobject ):
+ " set self from a CSChar instance"
+ (<unsigned char*>self.p)[0] = <unsigned char>(<signed char*>cobject.p)[0]
+ def init_CSChar( self, _CSChar cobject ):
+ " set a CSChar instance from self "
+ (<signed char*>cobject.p)[0] = <signed char>(<unsigned char*>self.p)[0]
+ def to_CSChar( self ):
+ " cast self to new CSChar instance "
+ cdef unsigned char c_src
+ cdef signed char c_tgt
+ cdef _CUChar py_src
+ cdef _CSChar py_tgt
+ c_src = (<unsigned char*>self.p)[0]
+ c_tgt = <signed char>c_src
+ py_tgt = CSChar()
+ (<signed char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUChar( self, _CUChar cobject ):
+ " set self from a CUChar instance"
+ (<unsigned char*>self.p)[0] = <unsigned char>(<unsigned char*>cobject.p)[0]
+ def init_CUChar( self, _CUChar cobject ):
+ " set a CUChar instance from self "
+ (<unsigned char*>cobject.p)[0] = <unsigned char>(<unsigned char*>self.p)[0]
+ def from_CShort( self, _CShort cobject ):
+ " set self from a CShort instance"
+ (<unsigned char*>self.p)[0] = <unsigned char>(<short*>cobject.p)[0]
+ def init_CShort( self, _CShort cobject ):
+ " set a CShort instance from self "
+ (<short*>cobject.p)[0] = <short>(<unsigned char*>self.p)[0]
+ def to_CShort( self ):
+ " cast self to new CShort instance "
+ cdef unsigned char c_src
+ cdef short c_tgt
+ cdef _CUChar py_src
+ cdef _CShort py_tgt
+ c_src = (<unsigned char*>self.p)[0]
+ c_tgt = <short>c_src
+ py_tgt = CShort()
+ (<short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUShort( self, _CUShort cobject ):
+ " set self from a CUShort instance"
+ (<unsigned char*>self.p)[0] = <unsigned char>(<unsigned short*>cobject.p)[0]
+ def init_CUShort( self, _CUShort cobject ):
+ " set a CUShort instance from self "
+ (<unsigned short*>cobject.p)[0] = <unsigned short>(<unsigned char*>self.p)[0]
+ def to_CUShort( self ):
+ " cast self to new CUShort instance "
+ cdef unsigned char c_src
+ cdef unsigned short c_tgt
+ cdef _CUChar py_src
+ cdef _CUShort py_tgt
+ c_src = (<unsigned char*>self.p)[0]
+ c_tgt = <unsigned short>c_src
+ py_tgt = CUShort()
+ (<unsigned short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CInt( self, _CInt cobject ):
+ " set self from a CInt instance"
+ (<unsigned char*>self.p)[0] = <unsigned char>(<int*>cobject.p)[0]
+ def init_CInt( self, _CInt cobject ):
+ " set a CInt instance from self "
+ (<int*>cobject.p)[0] = <int>(<unsigned char*>self.p)[0]
+ def to_CInt( self ):
+ " cast self to new CInt instance "
+ cdef unsigned char c_src
+ cdef int c_tgt
+ cdef _CUChar py_src
+ cdef _CInt py_tgt
+ c_src = (<unsigned char*>self.p)[0]
+ c_tgt = <int>c_src
+ py_tgt = CInt()
+ (<int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUInt( self, _CUInt cobject ):
+ " set self from a CUInt instance"
+ (<unsigned char*>self.p)[0] = <unsigned char>(<unsigned int*>cobject.p)[0]
+ def init_CUInt( self, _CUInt cobject ):
+ " set a CUInt instance from self "
+ (<unsigned int*>cobject.p)[0] = <unsigned int>(<unsigned char*>self.p)[0]
+ def to_CUInt( self ):
+ " cast self to new CUInt instance "
+ cdef unsigned char c_src
+ cdef unsigned int c_tgt
+ cdef _CUChar py_src
+ cdef _CUInt py_tgt
+ c_src = (<unsigned char*>self.p)[0]
+ c_tgt = <unsigned int>c_src
+ py_tgt = CUInt()
+ (<unsigned int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLong( self, _CLong cobject ):
+ " set self from a CLong instance"
+ (<unsigned char*>self.p)[0] = <unsigned char>(<long*>cobject.p)[0]
+ def init_CLong( self, _CLong cobject ):
+ " set a CLong instance from self "
+ (<long*>cobject.p)[0] = <long>(<unsigned char*>self.p)[0]
+ def to_CLong( self ):
+ " cast self to new CLong instance "
+ cdef unsigned char c_src
+ cdef long c_tgt
+ cdef _CUChar py_src
+ cdef _CLong py_tgt
+ c_src = (<unsigned char*>self.p)[0]
+ c_tgt = <long>c_src
+ py_tgt = CLong()
+ (<long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULong( self, _CULong cobject ):
+ " set self from a CULong instance"
+ (<unsigned char*>self.p)[0] = <unsigned char>(<unsigned long*>cobject.p)[0]
+ def init_CULong( self, _CULong cobject ):
+ " set a CULong instance from self "
+ (<unsigned long*>cobject.p)[0] = <unsigned long>(<unsigned char*>self.p)[0]
+ def to_CULong( self ):
+ " cast self to new CULong instance "
+ cdef unsigned char c_src
+ cdef unsigned long c_tgt
+ cdef _CUChar py_src
+ cdef _CULong py_tgt
+ c_src = (<unsigned char*>self.p)[0]
+ c_tgt = <unsigned long>c_src
+ py_tgt = CULong()
+ (<unsigned long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLLong( self, _CLLong cobject ):
+ " set self from a CLLong instance"
+ (<unsigned char*>self.p)[0] = <unsigned char>(<long long*>cobject.p)[0]
+ def init_CLLong( self, _CLLong cobject ):
+ " set a CLLong instance from self "
+ (<long long*>cobject.p)[0] = <long long>(<unsigned char*>self.p)[0]
+ def to_CLLong( self ):
+ " cast self to new CLLong instance "
+ cdef unsigned char c_src
+ cdef long long c_tgt
+ cdef _CUChar py_src
+ cdef _CLLong py_tgt
+ c_src = (<unsigned char*>self.p)[0]
+ c_tgt = <long long>c_src
+ py_tgt = CLLong()
+ (<long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULLong( self, _CULLong cobject ):
+ " set self from a CULLong instance"
+ (<unsigned char*>self.p)[0] = <unsigned char>(<unsigned long long*>cobject.p)[0]
+ def init_CULLong( self, _CULLong cobject ):
+ " set a CULLong instance from self "
+ (<unsigned long long*>cobject.p)[0] = <unsigned long long>(<unsigned char*>self.p)[0]
+ def to_CULLong( self ):
+ " cast self to new CULLong instance "
+ cdef unsigned char c_src
+ cdef unsigned long long c_tgt
+ cdef _CUChar py_src
+ cdef _CULLong py_tgt
+ c_src = (<unsigned char*>self.p)[0]
+ c_tgt = <unsigned long long>c_src
+ py_tgt = CULLong()
+ (<unsigned long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CFloat( self, _CFloat cobject ):
+ " set self from a CFloat instance"
+ (<unsigned char*>self.p)[0] = <unsigned char>(<float*>cobject.p)[0]
+ def init_CFloat( self, _CFloat cobject ):
+ " set a CFloat instance from self "
+ (<float*>cobject.p)[0] = <float>(<unsigned char*>self.p)[0]
+ def to_CFloat( self ):
+ " cast self to new CFloat instance "
+ cdef unsigned char c_src
+ cdef float c_tgt
+ cdef _CUChar py_src
+ cdef _CFloat py_tgt
+ c_src = (<unsigned char*>self.p)[0]
+ c_tgt = <float>c_src
+ py_tgt = CFloat()
+ (<float*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CDouble( self, _CDouble cobject ):
+ " set self from a CDouble instance"
+ (<unsigned char*>self.p)[0] = <unsigned char>(<double*>cobject.p)[0]
+ def init_CDouble( self, _CDouble cobject ):
+ " set a CDouble instance from self "
+ (<double*>cobject.p)[0] = <double>(<unsigned char*>self.p)[0]
+ def to_CDouble( self ):
+ " cast self to new CDouble instance "
+ cdef unsigned char c_src
+ cdef double c_tgt
+ cdef _CUChar py_src
+ cdef _CDouble py_tgt
+ c_src = (<unsigned char*>self.p)[0]
+ c_tgt = <double>c_src
+ py_tgt = CDouble()
+ (<double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLDouble( self, _CLDouble cobject ):
+ " set self from a CLDouble instance"
+ (<unsigned char*>self.p)[0] = <unsigned char>(<long double*>cobject.p)[0]
+ def init_CLDouble( self, _CLDouble cobject ):
+ " set a CLDouble instance from self "
+ (<long double*>cobject.p)[0] = <long double>(<unsigned char*>self.p)[0]
+ def to_CLDouble( self ):
+ " cast self to new CLDouble instance "
+ cdef unsigned char c_src
+ cdef long double c_tgt
+ cdef _CUChar py_src
+ cdef _CLDouble py_tgt
+ c_src = (<unsigned char*>self.p)[0]
+ c_tgt = <long double>c_src
+ py_tgt = CLDouble()
+ (<long double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+CUChar = Meta('CUChar', (_CUChar,), {} )
+CUChar.basetype = CUChar
+CUChar.sizeof = sizeof(unsigned char)
+
+#cdef public class _CShort(_CObject)[ object AdaptObj_CShort, type AdaptType_CShort ]:
+cdef class _CShort(_CObject):
+ def __init__( self, value = None, addr = None ):
+ cdef short*p
+ cdef long c_addr
+ cdef _CObject cobject
+ if addr is None:
+ p = <short*>pyx_calloc(1,sizeof(short))
+ self.p = <void*>p
+ if isinstance(value,CObject):
+ if isinstance(value,_CShort):
+ cobject = value
+ p[0] = (<short*>cobject.p)[0]
+ else:
+ value.init_CShort(self)
+ elif value is not None:
+ p[0] = value
+ else:
+ p[0] = 0
+ self.malloced = True
+ else:
+ c_addr = addr
+ self.p = <void*>c_addr
+ self.malloced = False
+ def __int__( self ):
+ cdef short *p
+ assert self.p != NULL
+ p = <short*>self.p
+ return int(p[0])
+ def __float__( self ):
+ cdef short *p
+ assert self.p != NULL
+ p = <short*>self.p
+ return float(p[0])
+ def __add__( x, y ):
+ cdef short val
+ cdef _CShort self
+ if not isinstance(x,CShort):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<short*>self.p)[0]
+ val = val + y
+ return CShort(val)
+ def __sub__( x, y ):
+ cdef short val
+ cdef _CShort self
+ if not isinstance(x,CShort):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<short*>self.p)[0]
+ val = val - y
+ return CShort(val)
+ def __mul__( x, y ):
+ cdef short val
+ cdef _CShort self
+ if not isinstance(x,CShort):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<short*>self.p)[0]
+ val = val + y
+ return CShort(val)
+ def __repr__( self ):
+ cdef long c_addr
+ cdef short c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<short*>self.p)[0]
+ value = ", value = %s" % str( c_val )
+ return "%s( addr = %s%s )" % (self.__class__.__name__, hex(c_addr), value)
+ def set_value( self, value ):
+ if isinstance(value,CObject):
+ value.init_CShort(self)
+ else:
+ (<short*>self.p)[0] = value
+ def get_value( self ):
+ return (<short*>self.p)[0]
+ def to_CShort(self):
+ return self
+ def init_from( self, _CShort cobject ):
+ " init self from a CShort instance "
+# if not self.is_compatible(cobject):
+ if not isinstance( cobject, CShort ):
+ raise TypeError("%s cannot be set from %s" % (self.__class__, cobject))
+ (<short*>self.p)[0] = (<short*>cobject.p)[0]
+ def get_basetype( self ):
+ return CShort
+# def sizeof( self ):
+# return sizeof(short)
+# def is_compatible( self, _CObject cobject ):
+# return isinstance( cobject, CShort )
+ def __str__( self ):
+ cdef long c_addr
+ cdef short c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<short*>self.p)[0]
+ value = str( c_val )
+ return value
+ def from_CChar( self, _CChar cobject ):
+ " set self from a CChar instance"
+ (<short*>self.p)[0] = <short>(<char*>cobject.p)[0]
+ def init_CChar( self, _CChar cobject ):
+ " set a CChar instance from self "
+ (<char*>cobject.p)[0] = <char>(<short*>self.p)[0]
+ def to_CChar( self ):
+ " cast self to new CChar instance "
+ cdef short c_src
+ cdef char c_tgt
+ cdef _CShort py_src
+ cdef _CChar py_tgt
+ c_src = (<short*>self.p)[0]
+ c_tgt = <char>c_src
+ py_tgt = CChar()
+ (<char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CSChar( self, _CSChar cobject ):
+ " set self from a CSChar instance"
+ (<short*>self.p)[0] = <short>(<signed char*>cobject.p)[0]
+ def init_CSChar( self, _CSChar cobject ):
+ " set a CSChar instance from self "
+ (<signed char*>cobject.p)[0] = <signed char>(<short*>self.p)[0]
+ def to_CSChar( self ):
+ " cast self to new CSChar instance "
+ cdef short c_src
+ cdef signed char c_tgt
+ cdef _CShort py_src
+ cdef _CSChar py_tgt
+ c_src = (<short*>self.p)[0]
+ c_tgt = <signed char>c_src
+ py_tgt = CSChar()
+ (<signed char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUChar( self, _CUChar cobject ):
+ " set self from a CUChar instance"
+ (<short*>self.p)[0] = <short>(<unsigned char*>cobject.p)[0]
+ def init_CUChar( self, _CUChar cobject ):
+ " set a CUChar instance from self "
+ (<unsigned char*>cobject.p)[0] = <unsigned char>(<short*>self.p)[0]
+ def to_CUChar( self ):
+ " cast self to new CUChar instance "
+ cdef short c_src
+ cdef unsigned char c_tgt
+ cdef _CShort py_src
+ cdef _CUChar py_tgt
+ c_src = (<short*>self.p)[0]
+ c_tgt = <unsigned char>c_src
+ py_tgt = CUChar()
+ (<unsigned char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CShort( self, _CShort cobject ):
+ " set self from a CShort instance"
+ (<short*>self.p)[0] = <short>(<short*>cobject.p)[0]
+ def init_CShort( self, _CShort cobject ):
+ " set a CShort instance from self "
+ (<short*>cobject.p)[0] = <short>(<short*>self.p)[0]
+ def from_CUShort( self, _CUShort cobject ):
+ " set self from a CUShort instance"
+ (<short*>self.p)[0] = <short>(<unsigned short*>cobject.p)[0]
+ def init_CUShort( self, _CUShort cobject ):
+ " set a CUShort instance from self "
+ (<unsigned short*>cobject.p)[0] = <unsigned short>(<short*>self.p)[0]
+ def to_CUShort( self ):
+ " cast self to new CUShort instance "
+ cdef short c_src
+ cdef unsigned short c_tgt
+ cdef _CShort py_src
+ cdef _CUShort py_tgt
+ c_src = (<short*>self.p)[0]
+ c_tgt = <unsigned short>c_src
+ py_tgt = CUShort()
+ (<unsigned short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CInt( self, _CInt cobject ):
+ " set self from a CInt instance"
+ (<short*>self.p)[0] = <short>(<int*>cobject.p)[0]
+ def init_CInt( self, _CInt cobject ):
+ " set a CInt instance from self "
+ (<int*>cobject.p)[0] = <int>(<short*>self.p)[0]
+ def to_CInt( self ):
+ " cast self to new CInt instance "
+ cdef short c_src
+ cdef int c_tgt
+ cdef _CShort py_src
+ cdef _CInt py_tgt
+ c_src = (<short*>self.p)[0]
+ c_tgt = <int>c_src
+ py_tgt = CInt()
+ (<int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUInt( self, _CUInt cobject ):
+ " set self from a CUInt instance"
+ (<short*>self.p)[0] = <short>(<unsigned int*>cobject.p)[0]
+ def init_CUInt( self, _CUInt cobject ):
+ " set a CUInt instance from self "
+ (<unsigned int*>cobject.p)[0] = <unsigned int>(<short*>self.p)[0]
+ def to_CUInt( self ):
+ " cast self to new CUInt instance "
+ cdef short c_src
+ cdef unsigned int c_tgt
+ cdef _CShort py_src
+ cdef _CUInt py_tgt
+ c_src = (<short*>self.p)[0]
+ c_tgt = <unsigned int>c_src
+ py_tgt = CUInt()
+ (<unsigned int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLong( self, _CLong cobject ):
+ " set self from a CLong instance"
+ (<short*>self.p)[0] = <short>(<long*>cobject.p)[0]
+ def init_CLong( self, _CLong cobject ):
+ " set a CLong instance from self "
+ (<long*>cobject.p)[0] = <long>(<short*>self.p)[0]
+ def to_CLong( self ):
+ " cast self to new CLong instance "
+ cdef short c_src
+ cdef long c_tgt
+ cdef _CShort py_src
+ cdef _CLong py_tgt
+ c_src = (<short*>self.p)[0]
+ c_tgt = <long>c_src
+ py_tgt = CLong()
+ (<long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULong( self, _CULong cobject ):
+ " set self from a CULong instance"
+ (<short*>self.p)[0] = <short>(<unsigned long*>cobject.p)[0]
+ def init_CULong( self, _CULong cobject ):
+ " set a CULong instance from self "
+ (<unsigned long*>cobject.p)[0] = <unsigned long>(<short*>self.p)[0]
+ def to_CULong( self ):
+ " cast self to new CULong instance "
+ cdef short c_src
+ cdef unsigned long c_tgt
+ cdef _CShort py_src
+ cdef _CULong py_tgt
+ c_src = (<short*>self.p)[0]
+ c_tgt = <unsigned long>c_src
+ py_tgt = CULong()
+ (<unsigned long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLLong( self, _CLLong cobject ):
+ " set self from a CLLong instance"
+ (<short*>self.p)[0] = <short>(<long long*>cobject.p)[0]
+ def init_CLLong( self, _CLLong cobject ):
+ " set a CLLong instance from self "
+ (<long long*>cobject.p)[0] = <long long>(<short*>self.p)[0]
+ def to_CLLong( self ):
+ " cast self to new CLLong instance "
+ cdef short c_src
+ cdef long long c_tgt
+ cdef _CShort py_src
+ cdef _CLLong py_tgt
+ c_src = (<short*>self.p)[0]
+ c_tgt = <long long>c_src
+ py_tgt = CLLong()
+ (<long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULLong( self, _CULLong cobject ):
+ " set self from a CULLong instance"
+ (<short*>self.p)[0] = <short>(<unsigned long long*>cobject.p)[0]
+ def init_CULLong( self, _CULLong cobject ):
+ " set a CULLong instance from self "
+ (<unsigned long long*>cobject.p)[0] = <unsigned long long>(<short*>self.p)[0]
+ def to_CULLong( self ):
+ " cast self to new CULLong instance "
+ cdef short c_src
+ cdef unsigned long long c_tgt
+ cdef _CShort py_src
+ cdef _CULLong py_tgt
+ c_src = (<short*>self.p)[0]
+ c_tgt = <unsigned long long>c_src
+ py_tgt = CULLong()
+ (<unsigned long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CFloat( self, _CFloat cobject ):
+ " set self from a CFloat instance"
+ (<short*>self.p)[0] = <short>(<float*>cobject.p)[0]
+ def init_CFloat( self, _CFloat cobject ):
+ " set a CFloat instance from self "
+ (<float*>cobject.p)[0] = <float>(<short*>self.p)[0]
+ def to_CFloat( self ):
+ " cast self to new CFloat instance "
+ cdef short c_src
+ cdef float c_tgt
+ cdef _CShort py_src
+ cdef _CFloat py_tgt
+ c_src = (<short*>self.p)[0]
+ c_tgt = <float>c_src
+ py_tgt = CFloat()
+ (<float*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CDouble( self, _CDouble cobject ):
+ " set self from a CDouble instance"
+ (<short*>self.p)[0] = <short>(<double*>cobject.p)[0]
+ def init_CDouble( self, _CDouble cobject ):
+ " set a CDouble instance from self "
+ (<double*>cobject.p)[0] = <double>(<short*>self.p)[0]
+ def to_CDouble( self ):
+ " cast self to new CDouble instance "
+ cdef short c_src
+ cdef double c_tgt
+ cdef _CShort py_src
+ cdef _CDouble py_tgt
+ c_src = (<short*>self.p)[0]
+ c_tgt = <double>c_src
+ py_tgt = CDouble()
+ (<double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLDouble( self, _CLDouble cobject ):
+ " set self from a CLDouble instance"
+ (<short*>self.p)[0] = <short>(<long double*>cobject.p)[0]
+ def init_CLDouble( self, _CLDouble cobject ):
+ " set a CLDouble instance from self "
+ (<long double*>cobject.p)[0] = <long double>(<short*>self.p)[0]
+ def to_CLDouble( self ):
+ " cast self to new CLDouble instance "
+ cdef short c_src
+ cdef long double c_tgt
+ cdef _CShort py_src
+ cdef _CLDouble py_tgt
+ c_src = (<short*>self.p)[0]
+ c_tgt = <long double>c_src
+ py_tgt = CLDouble()
+ (<long double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+CShort = Meta('CShort', (_CShort,), {} )
+CShort.basetype = CShort
+CShort.sizeof = sizeof(short)
+
+#cdef public class _CUShort(_CObject)[ object AdaptObj_CUShort, type AdaptType_CUShort ]:
+cdef class _CUShort(_CObject):
+ def __init__( self, value = None, addr = None ):
+ cdef unsigned short*p
+ cdef long c_addr
+ cdef _CObject cobject
+ if addr is None:
+ p = <unsigned short*>pyx_calloc(1,sizeof(unsigned short))
+ self.p = <void*>p
+ if isinstance(value,CObject):
+ if isinstance(value,_CUShort):
+ cobject = value
+ p[0] = (<unsigned short*>cobject.p)[0]
+ else:
+ value.init_CUShort(self)
+ elif value is not None:
+ p[0] = value
+ else:
+ p[0] = 0
+ self.malloced = True
+ else:
+ c_addr = addr
+ self.p = <void*>c_addr
+ self.malloced = False
+ def __int__( self ):
+ cdef unsigned short *p
+ assert self.p != NULL
+ p = <unsigned short*>self.p
+ return int(p[0])
+ def __float__( self ):
+ cdef unsigned short *p
+ assert self.p != NULL
+ p = <unsigned short*>self.p
+ return float(p[0])
+ def __add__( x, y ):
+ cdef unsigned short val
+ cdef _CUShort self
+ if not isinstance(x,CUShort):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<unsigned short*>self.p)[0]
+ val = val + y
+ return CUShort(val)
+ def __sub__( x, y ):
+ cdef unsigned short val
+ cdef _CUShort self
+ if not isinstance(x,CUShort):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<unsigned short*>self.p)[0]
+ val = val - y
+ return CUShort(val)
+ def __mul__( x, y ):
+ cdef unsigned short val
+ cdef _CUShort self
+ if not isinstance(x,CUShort):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<unsigned short*>self.p)[0]
+ val = val + y
+ return CUShort(val)
+ def __repr__( self ):
+ cdef long c_addr
+ cdef unsigned short c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<unsigned short*>self.p)[0]
+ value = ", value = %s" % str( c_val )
+ return "%s( addr = %s%s )" % (self.__class__.__name__, hex(c_addr), value)
+ def set_value( self, value ):
+ if isinstance(value,CObject):
+ value.init_CUShort(self)
+ else:
+ (<unsigned short*>self.p)[0] = value
+ def get_value( self ):
+ return (<unsigned short*>self.p)[0]
+ def to_CUShort(self):
+ return self
+ def init_from( self, _CUShort cobject ):
+ " init self from a CUShort instance "
+# if not self.is_compatible(cobject):
+ if not isinstance( cobject, CUShort ):
+ raise TypeError("%s cannot be set from %s" % (self.__class__, cobject))
+ (<unsigned short*>self.p)[0] = (<unsigned short*>cobject.p)[0]
+ def get_basetype( self ):
+ return CUShort
+# def sizeof( self ):
+# return sizeof(unsigned short)
+# def is_compatible( self, _CObject cobject ):
+# return isinstance( cobject, CUShort )
+ def __str__( self ):
+ cdef long c_addr
+ cdef unsigned short c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<unsigned short*>self.p)[0]
+ value = str( c_val )
+ return value
+ def from_CChar( self, _CChar cobject ):
+ " set self from a CChar instance"
+ (<unsigned short*>self.p)[0] = <unsigned short>(<char*>cobject.p)[0]
+ def init_CChar( self, _CChar cobject ):
+ " set a CChar instance from self "
+ (<char*>cobject.p)[0] = <char>(<unsigned short*>self.p)[0]
+ def to_CChar( self ):
+ " cast self to new CChar instance "
+ cdef unsigned short c_src
+ cdef char c_tgt
+ cdef _CUShort py_src
+ cdef _CChar py_tgt
+ c_src = (<unsigned short*>self.p)[0]
+ c_tgt = <char>c_src
+ py_tgt = CChar()
+ (<char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CSChar( self, _CSChar cobject ):
+ " set self from a CSChar instance"
+ (<unsigned short*>self.p)[0] = <unsigned short>(<signed char*>cobject.p)[0]
+ def init_CSChar( self, _CSChar cobject ):
+ " set a CSChar instance from self "
+ (<signed char*>cobject.p)[0] = <signed char>(<unsigned short*>self.p)[0]
+ def to_CSChar( self ):
+ " cast self to new CSChar instance "
+ cdef unsigned short c_src
+ cdef signed char c_tgt
+ cdef _CUShort py_src
+ cdef _CSChar py_tgt
+ c_src = (<unsigned short*>self.p)[0]
+ c_tgt = <signed char>c_src
+ py_tgt = CSChar()
+ (<signed char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUChar( self, _CUChar cobject ):
+ " set self from a CUChar instance"
+ (<unsigned short*>self.p)[0] = <unsigned short>(<unsigned char*>cobject.p)[0]
+ def init_CUChar( self, _CUChar cobject ):
+ " set a CUChar instance from self "
+ (<unsigned char*>cobject.p)[0] = <unsigned char>(<unsigned short*>self.p)[0]
+ def to_CUChar( self ):
+ " cast self to new CUChar instance "
+ cdef unsigned short c_src
+ cdef unsigned char c_tgt
+ cdef _CUShort py_src
+ cdef _CUChar py_tgt
+ c_src = (<unsigned short*>self.p)[0]
+ c_tgt = <unsigned char>c_src
+ py_tgt = CUChar()
+ (<unsigned char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CShort( self, _CShort cobject ):
+ " set self from a CShort instance"
+ (<unsigned short*>self.p)[0] = <unsigned short>(<short*>cobject.p)[0]
+ def init_CShort( self, _CShort cobject ):
+ " set a CShort instance from self "
+ (<short*>cobject.p)[0] = <short>(<unsigned short*>self.p)[0]
+ def to_CShort( self ):
+ " cast self to new CShort instance "
+ cdef unsigned short c_src
+ cdef short c_tgt
+ cdef _CUShort py_src
+ cdef _CShort py_tgt
+ c_src = (<unsigned short*>self.p)[0]
+ c_tgt = <short>c_src
+ py_tgt = CShort()
+ (<short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUShort( self, _CUShort cobject ):
+ " set self from a CUShort instance"
+ (<unsigned short*>self.p)[0] = <unsigned short>(<unsigned short*>cobject.p)[0]
+ def init_CUShort( self, _CUShort cobject ):
+ " set a CUShort instance from self "
+ (<unsigned short*>cobject.p)[0] = <unsigned short>(<unsigned short*>self.p)[0]
+ def from_CInt( self, _CInt cobject ):
+ " set self from a CInt instance"
+ (<unsigned short*>self.p)[0] = <unsigned short>(<int*>cobject.p)[0]
+ def init_CInt( self, _CInt cobject ):
+ " set a CInt instance from self "
+ (<int*>cobject.p)[0] = <int>(<unsigned short*>self.p)[0]
+ def to_CInt( self ):
+ " cast self to new CInt instance "
+ cdef unsigned short c_src
+ cdef int c_tgt
+ cdef _CUShort py_src
+ cdef _CInt py_tgt
+ c_src = (<unsigned short*>self.p)[0]
+ c_tgt = <int>c_src
+ py_tgt = CInt()
+ (<int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUInt( self, _CUInt cobject ):
+ " set self from a CUInt instance"
+ (<unsigned short*>self.p)[0] = <unsigned short>(<unsigned int*>cobject.p)[0]
+ def init_CUInt( self, _CUInt cobject ):
+ " set a CUInt instance from self "
+ (<unsigned int*>cobject.p)[0] = <unsigned int>(<unsigned short*>self.p)[0]
+ def to_CUInt( self ):
+ " cast self to new CUInt instance "
+ cdef unsigned short c_src
+ cdef unsigned int c_tgt
+ cdef _CUShort py_src
+ cdef _CUInt py_tgt
+ c_src = (<unsigned short*>self.p)[0]
+ c_tgt = <unsigned int>c_src
+ py_tgt = CUInt()
+ (<unsigned int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLong( self, _CLong cobject ):
+ " set self from a CLong instance"
+ (<unsigned short*>self.p)[0] = <unsigned short>(<long*>cobject.p)[0]
+ def init_CLong( self, _CLong cobject ):
+ " set a CLong instance from self "
+ (<long*>cobject.p)[0] = <long>(<unsigned short*>self.p)[0]
+ def to_CLong( self ):
+ " cast self to new CLong instance "
+ cdef unsigned short c_src
+ cdef long c_tgt
+ cdef _CUShort py_src
+ cdef _CLong py_tgt
+ c_src = (<unsigned short*>self.p)[0]
+ c_tgt = <long>c_src
+ py_tgt = CLong()
+ (<long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULong( self, _CULong cobject ):
+ " set self from a CULong instance"
+ (<unsigned short*>self.p)[0] = <unsigned short>(<unsigned long*>cobject.p)[0]
+ def init_CULong( self, _CULong cobject ):
+ " set a CULong instance from self "
+ (<unsigned long*>cobject.p)[0] = <unsigned long>(<unsigned short*>self.p)[0]
+ def to_CULong( self ):
+ " cast self to new CULong instance "
+ cdef unsigned short c_src
+ cdef unsigned long c_tgt
+ cdef _CUShort py_src
+ cdef _CULong py_tgt
+ c_src = (<unsigned short*>self.p)[0]
+ c_tgt = <unsigned long>c_src
+ py_tgt = CULong()
+ (<unsigned long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLLong( self, _CLLong cobject ):
+ " set self from a CLLong instance"
+ (<unsigned short*>self.p)[0] = <unsigned short>(<long long*>cobject.p)[0]
+ def init_CLLong( self, _CLLong cobject ):
+ " set a CLLong instance from self "
+ (<long long*>cobject.p)[0] = <long long>(<unsigned short*>self.p)[0]
+ def to_CLLong( self ):
+ " cast self to new CLLong instance "
+ cdef unsigned short c_src
+ cdef long long c_tgt
+ cdef _CUShort py_src
+ cdef _CLLong py_tgt
+ c_src = (<unsigned short*>self.p)[0]
+ c_tgt = <long long>c_src
+ py_tgt = CLLong()
+ (<long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULLong( self, _CULLong cobject ):
+ " set self from a CULLong instance"
+ (<unsigned short*>self.p)[0] = <unsigned short>(<unsigned long long*>cobject.p)[0]
+ def init_CULLong( self, _CULLong cobject ):
+ " set a CULLong instance from self "
+ (<unsigned long long*>cobject.p)[0] = <unsigned long long>(<unsigned short*>self.p)[0]
+ def to_CULLong( self ):
+ " cast self to new CULLong instance "
+ cdef unsigned short c_src
+ cdef unsigned long long c_tgt
+ cdef _CUShort py_src
+ cdef _CULLong py_tgt
+ c_src = (<unsigned short*>self.p)[0]
+ c_tgt = <unsigned long long>c_src
+ py_tgt = CULLong()
+ (<unsigned long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CFloat( self, _CFloat cobject ):
+ " set self from a CFloat instance"
+ (<unsigned short*>self.p)[0] = <unsigned short>(<float*>cobject.p)[0]
+ def init_CFloat( self, _CFloat cobject ):
+ " set a CFloat instance from self "
+ (<float*>cobject.p)[0] = <float>(<unsigned short*>self.p)[0]
+ def to_CFloat( self ):
+ " cast self to new CFloat instance "
+ cdef unsigned short c_src
+ cdef float c_tgt
+ cdef _CUShort py_src
+ cdef _CFloat py_tgt
+ c_src = (<unsigned short*>self.p)[0]
+ c_tgt = <float>c_src
+ py_tgt = CFloat()
+ (<float*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CDouble( self, _CDouble cobject ):
+ " set self from a CDouble instance"
+ (<unsigned short*>self.p)[0] = <unsigned short>(<double*>cobject.p)[0]
+ def init_CDouble( self, _CDouble cobject ):
+ " set a CDouble instance from self "
+ (<double*>cobject.p)[0] = <double>(<unsigned short*>self.p)[0]
+ def to_CDouble( self ):
+ " cast self to new CDouble instance "
+ cdef unsigned short c_src
+ cdef double c_tgt
+ cdef _CUShort py_src
+ cdef _CDouble py_tgt
+ c_src = (<unsigned short*>self.p)[0]
+ c_tgt = <double>c_src
+ py_tgt = CDouble()
+ (<double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLDouble( self, _CLDouble cobject ):
+ " set self from a CLDouble instance"
+ (<unsigned short*>self.p)[0] = <unsigned short>(<long double*>cobject.p)[0]
+ def init_CLDouble( self, _CLDouble cobject ):
+ " set a CLDouble instance from self "
+ (<long double*>cobject.p)[0] = <long double>(<unsigned short*>self.p)[0]
+ def to_CLDouble( self ):
+ " cast self to new CLDouble instance "
+ cdef unsigned short c_src
+ cdef long double c_tgt
+ cdef _CUShort py_src
+ cdef _CLDouble py_tgt
+ c_src = (<unsigned short*>self.p)[0]
+ c_tgt = <long double>c_src
+ py_tgt = CLDouble()
+ (<long double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+CUShort = Meta('CUShort', (_CUShort,), {} )
+CUShort.basetype = CUShort
+CUShort.sizeof = sizeof(unsigned short)
+
+#cdef public class _CInt(_CObject)[ object AdaptObj_CInt, type AdaptType_CInt ]:
+cdef class _CInt(_CObject):
+ def __init__( self, value = None, addr = None ):
+ cdef int*p
+ cdef long c_addr
+ cdef _CObject cobject
+ if addr is None:
+ p = <int*>pyx_calloc(1,sizeof(int))
+ self.p = <void*>p
+ if isinstance(value,CObject):
+ if isinstance(value,_CInt):
+ cobject = value
+ p[0] = (<int*>cobject.p)[0]
+ else:
+ value.init_CInt(self)
+ elif value is not None:
+ p[0] = value
+ else:
+ p[0] = 0
+ self.malloced = True
+ else:
+ c_addr = addr
+ self.p = <void*>c_addr
+ self.malloced = False
+ def __int__( self ):
+ cdef int *p
+ assert self.p != NULL
+ p = <int*>self.p
+ return int(p[0])
+ def __float__( self ):
+ cdef int *p
+ assert self.p != NULL
+ p = <int*>self.p
+ return float(p[0])
+ def __add__( x, y ):
+ cdef int val
+ cdef _CInt self
+ if not isinstance(x,CInt):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<int*>self.p)[0]
+ val = val + y
+ return CInt(val)
+ def __sub__( x, y ):
+ cdef int val
+ cdef _CInt self
+ if not isinstance(x,CInt):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<int*>self.p)[0]
+ val = val - y
+ return CInt(val)
+ def __mul__( x, y ):
+ cdef int val
+ cdef _CInt self
+ if not isinstance(x,CInt):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<int*>self.p)[0]
+ val = val + y
+ return CInt(val)
+ def __repr__( self ):
+ cdef long c_addr
+ cdef int c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<int*>self.p)[0]
+ value = ", value = %s" % str( c_val )
+ return "%s( addr = %s%s )" % (self.__class__.__name__, hex(c_addr), value)
+ def set_value( self, value ):
+ if isinstance(value,CObject):
+ value.init_CInt(self)
+ else:
+ (<int*>self.p)[0] = value
+ def get_value( self ):
+ return (<int*>self.p)[0]
+ def to_CInt(self):
+ return self
+ def init_from( self, _CInt cobject ):
+ " init self from a CInt instance "
+# if not self.is_compatible(cobject):
+ if not isinstance( cobject, CInt ):
+ raise TypeError("%s cannot be set from %s" % (self.__class__, cobject))
+ (<int*>self.p)[0] = (<int*>cobject.p)[0]
+ def get_basetype( self ):
+ return CInt
+# def sizeof( self ):
+# return sizeof(int)
+# def is_compatible( self, _CObject cobject ):
+# return isinstance( cobject, CInt )
+ def __str__( self ):
+ cdef long c_addr
+ cdef int c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<int*>self.p)[0]
+ value = str( c_val )
+ return value
+# def __add__( x, y ):
+# cdef _CInt z
+# if isinstance(x,CInt):
+# z = x
+# return (<int*>z.p)[0] + y
+# elif isinstance(y,CInt):
+# z = y
+# return x + (<int*>z.p)[0]
+# return NotImplemented
+# def __sub__( x, y ):
+# cdef _CInt z
+# if isinstance(x,CInt):
+# z = x
+# return (<int*>z.p)[0] - y
+# elif isinstance(y,CInt):
+# z = y
+# return x - (<int*>z.p)[0]
+# return NotImplemented
+ def from_CChar( self, _CChar cobject ):
+ " set self from a CChar instance"
+ (<int*>self.p)[0] = <int>(<char*>cobject.p)[0]
+ def init_CChar( self, _CChar cobject ):
+ " set a CChar instance from self "
+ (<char*>cobject.p)[0] = <char>(<int*>self.p)[0]
+ def to_CChar( self ):
+ " cast self to new CChar instance "
+ cdef int c_src
+ cdef char c_tgt
+ cdef _CInt py_src
+ cdef _CChar py_tgt
+ c_src = (<int*>self.p)[0]
+ c_tgt = <char>c_src
+ py_tgt = CChar()
+ (<char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CSChar( self, _CSChar cobject ):
+ " set self from a CSChar instance"
+ (<int*>self.p)[0] = <int>(<signed char*>cobject.p)[0]
+ def init_CSChar( self, _CSChar cobject ):
+ " set a CSChar instance from self "
+ (<signed char*>cobject.p)[0] = <signed char>(<int*>self.p)[0]
+ def to_CSChar( self ):
+ " cast self to new CSChar instance "
+ cdef int c_src
+ cdef signed char c_tgt
+ cdef _CInt py_src
+ cdef _CSChar py_tgt
+ c_src = (<int*>self.p)[0]
+ c_tgt = <signed char>c_src
+ py_tgt = CSChar()
+ (<signed char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUChar( self, _CUChar cobject ):
+ " set self from a CUChar instance"
+ (<int*>self.p)[0] = <int>(<unsigned char*>cobject.p)[0]
+ def init_CUChar( self, _CUChar cobject ):
+ " set a CUChar instance from self "
+ (<unsigned char*>cobject.p)[0] = <unsigned char>(<int*>self.p)[0]
+ def to_CUChar( self ):
+ " cast self to new CUChar instance "
+ cdef int c_src
+ cdef unsigned char c_tgt
+ cdef _CInt py_src
+ cdef _CUChar py_tgt
+ c_src = (<int*>self.p)[0]
+ c_tgt = <unsigned char>c_src
+ py_tgt = CUChar()
+ (<unsigned char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CShort( self, _CShort cobject ):
+ " set self from a CShort instance"
+ (<int*>self.p)[0] = <int>(<short*>cobject.p)[0]
+ def init_CShort( self, _CShort cobject ):
+ " set a CShort instance from self "
+ (<short*>cobject.p)[0] = <short>(<int*>self.p)[0]
+ def to_CShort( self ):
+ " cast self to new CShort instance "
+ cdef int c_src
+ cdef short c_tgt
+ cdef _CInt py_src
+ cdef _CShort py_tgt
+ c_src = (<int*>self.p)[0]
+ c_tgt = <short>c_src
+ py_tgt = CShort()
+ (<short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUShort( self, _CUShort cobject ):
+ " set self from a CUShort instance"
+ (<int*>self.p)[0] = <int>(<unsigned short*>cobject.p)[0]
+ def init_CUShort( self, _CUShort cobject ):
+ " set a CUShort instance from self "
+ (<unsigned short*>cobject.p)[0] = <unsigned short>(<int*>self.p)[0]
+ def to_CUShort( self ):
+ " cast self to new CUShort instance "
+ cdef int c_src
+ cdef unsigned short c_tgt
+ cdef _CInt py_src
+ cdef _CUShort py_tgt
+ c_src = (<int*>self.p)[0]
+ c_tgt = <unsigned short>c_src
+ py_tgt = CUShort()
+ (<unsigned short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CInt( self, _CInt cobject ):
+ " set self from a CInt instance"
+ (<int*>self.p)[0] = <int>(<int*>cobject.p)[0]
+ def init_CInt( self, _CInt cobject ):
+ " set a CInt instance from self "
+ (<int*>cobject.p)[0] = <int>(<int*>self.p)[0]
+ def from_CUInt( self, _CUInt cobject ):
+ " set self from a CUInt instance"
+ (<int*>self.p)[0] = <int>(<unsigned int*>cobject.p)[0]
+ def init_CUInt( self, _CUInt cobject ):
+ " set a CUInt instance from self "
+ (<unsigned int*>cobject.p)[0] = <unsigned int>(<int*>self.p)[0]
+ def to_CUInt( self ):
+ " cast self to new CUInt instance "
+ cdef int c_src
+ cdef unsigned int c_tgt
+ cdef _CInt py_src
+ cdef _CUInt py_tgt
+ c_src = (<int*>self.p)[0]
+ c_tgt = <unsigned int>c_src
+ py_tgt = CUInt()
+ (<unsigned int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLong( self, _CLong cobject ):
+ " set self from a CLong instance"
+ (<int*>self.p)[0] = <int>(<long*>cobject.p)[0]
+ def init_CLong( self, _CLong cobject ):
+ " set a CLong instance from self "
+ (<long*>cobject.p)[0] = <long>(<int*>self.p)[0]
+ def to_CLong( self ):
+ " cast self to new CLong instance "
+ cdef int c_src
+ cdef long c_tgt
+ cdef _CInt py_src
+ cdef _CLong py_tgt
+ c_src = (<int*>self.p)[0]
+ c_tgt = <long>c_src
+ py_tgt = CLong()
+ (<long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULong( self, _CULong cobject ):
+ " set self from a CULong instance"
+ (<int*>self.p)[0] = <int>(<unsigned long*>cobject.p)[0]
+ def init_CULong( self, _CULong cobject ):
+ " set a CULong instance from self "
+ (<unsigned long*>cobject.p)[0] = <unsigned long>(<int*>self.p)[0]
+ def to_CULong( self ):
+ " cast self to new CULong instance "
+ cdef int c_src
+ cdef unsigned long c_tgt
+ cdef _CInt py_src
+ cdef _CULong py_tgt
+ c_src = (<int*>self.p)[0]
+ c_tgt = <unsigned long>c_src
+ py_tgt = CULong()
+ (<unsigned long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLLong( self, _CLLong cobject ):
+ " set self from a CLLong instance"
+ (<int*>self.p)[0] = <int>(<long long*>cobject.p)[0]
+ def init_CLLong( self, _CLLong cobject ):
+ " set a CLLong instance from self "
+ (<long long*>cobject.p)[0] = <long long>(<int*>self.p)[0]
+ def to_CLLong( self ):
+ " cast self to new CLLong instance "
+ cdef int c_src
+ cdef long long c_tgt
+ cdef _CInt py_src
+ cdef _CLLong py_tgt
+ c_src = (<int*>self.p)[0]
+ c_tgt = <long long>c_src
+ py_tgt = CLLong()
+ (<long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULLong( self, _CULLong cobject ):
+ " set self from a CULLong instance"
+ (<int*>self.p)[0] = <int>(<unsigned long long*>cobject.p)[0]
+ def init_CULLong( self, _CULLong cobject ):
+ " set a CULLong instance from self "
+ (<unsigned long long*>cobject.p)[0] = <unsigned long long>(<int*>self.p)[0]
+ def to_CULLong( self ):
+ " cast self to new CULLong instance "
+ cdef int c_src
+ cdef unsigned long long c_tgt
+ cdef _CInt py_src
+ cdef _CULLong py_tgt
+ c_src = (<int*>self.p)[0]
+ c_tgt = <unsigned long long>c_src
+ py_tgt = CULLong()
+ (<unsigned long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CFloat( self, _CFloat cobject ):
+ " set self from a CFloat instance"
+ (<int*>self.p)[0] = <int>(<float*>cobject.p)[0]
+ def init_CFloat( self, _CFloat cobject ):
+ " set a CFloat instance from self "
+ (<float*>cobject.p)[0] = <float>(<int*>self.p)[0]
+ def to_CFloat( self ):
+ " cast self to new CFloat instance "
+ cdef int c_src
+ cdef float c_tgt
+ cdef _CInt py_src
+ cdef _CFloat py_tgt
+ c_src = (<int*>self.p)[0]
+ c_tgt = <float>c_src
+ py_tgt = CFloat()
+ (<float*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CDouble( self, _CDouble cobject ):
+ " set self from a CDouble instance"
+ (<int*>self.p)[0] = <int>(<double*>cobject.p)[0]
+ def init_CDouble( self, _CDouble cobject ):
+ " set a CDouble instance from self "
+ (<double*>cobject.p)[0] = <double>(<int*>self.p)[0]
+ def to_CDouble( self ):
+ " cast self to new CDouble instance "
+ cdef int c_src
+ cdef double c_tgt
+ cdef _CInt py_src
+ cdef _CDouble py_tgt
+ c_src = (<int*>self.p)[0]
+ c_tgt = <double>c_src
+ py_tgt = CDouble()
+ (<double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLDouble( self, _CLDouble cobject ):
+ " set self from a CLDouble instance"
+ (<int*>self.p)[0] = <int>(<long double*>cobject.p)[0]
+ def init_CLDouble( self, _CLDouble cobject ):
+ " set a CLDouble instance from self "
+ (<long double*>cobject.p)[0] = <long double>(<int*>self.p)[0]
+ def to_CLDouble( self ):
+ " cast self to new CLDouble instance "
+ cdef int c_src
+ cdef long double c_tgt
+ cdef _CInt py_src
+ cdef _CLDouble py_tgt
+ c_src = (<int*>self.p)[0]
+ c_tgt = <long double>c_src
+ py_tgt = CLDouble()
+ (<long double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+CInt = Meta('CInt', (_CInt,), {} )
+CInt.basetype = CInt
+CInt.sizeof = sizeof(int)
+
+#cdef public class _CUInt(_CObject)[ object AdaptObj_CUInt, type AdaptType_CUInt ]:
+cdef class _CUInt(_CObject):
+ def __init__( self, value = None, addr = None ):
+ cdef unsigned int*p
+ cdef long c_addr
+ cdef _CObject cobject
+ if addr is None:
+ p = <unsigned int*>pyx_calloc(1,sizeof(unsigned int))
+ self.p = <void*>p
+ if isinstance(value,CObject):
+ if isinstance(value,_CUInt):
+ cobject = value
+ p[0] = (<unsigned int*>cobject.p)[0]
+ else:
+ value.init_CUInt(self)
+ elif value is not None:
+ p[0] = value
+ else:
+ p[0] = 0
+ self.malloced = True
+ else:
+ c_addr = addr
+ self.p = <void*>c_addr
+ self.malloced = False
+ def __int__( self ):
+ cdef unsigned int *p
+ assert self.p != NULL
+ p = <unsigned int*>self.p
+ return int(p[0])
+ def __float__( self ):
+ cdef unsigned int *p
+ assert self.p != NULL
+ p = <unsigned int*>self.p
+ return float(p[0])
+ def __add__( x, y ):
+ cdef unsigned int val
+ cdef _CUInt self
+ if not isinstance(x,CUInt):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<unsigned int*>self.p)[0]
+ val = val + y
+ return CUInt(val)
+ def __sub__( x, y ):
+ cdef unsigned int val
+ cdef _CUInt self
+ if not isinstance(x,CUInt):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<unsigned int*>self.p)[0]
+ val = val - y
+ return CUInt(val)
+ def __mul__( x, y ):
+ cdef unsigned int val
+ cdef _CUInt self
+ if not isinstance(x,CUInt):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<unsigned int*>self.p)[0]
+ val = val + y
+ return CUInt(val)
+ def __repr__( self ):
+ cdef long c_addr
+ cdef unsigned int c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<unsigned int*>self.p)[0]
+ value = ", value = %s" % str( c_val )
+ return "%s( addr = %s%s )" % (self.__class__.__name__, hex(c_addr), value)
+ def set_value( self, value ):
+ if isinstance(value,CObject):
+ value.init_CUInt(self)
+ else:
+ (<unsigned int*>self.p)[0] = value
+ def get_value( self ):
+ return (<unsigned int*>self.p)[0]
+ def to_CUInt(self):
+ return self
+ def init_from( self, _CUInt cobject ):
+ " init self from a CUInt instance "
+# if not self.is_compatible(cobject):
+ if not isinstance( cobject, CUInt ):
+ raise TypeError("%s cannot be set from %s" % (self.__class__, cobject))
+ (<unsigned int*>self.p)[0] = (<unsigned int*>cobject.p)[0]
+ def get_basetype( self ):
+ return CUInt
+# def sizeof( self ):
+# return sizeof(unsigned int)
+# def is_compatible( self, _CObject cobject ):
+# return isinstance( cobject, CUInt )
+ def __str__( self ):
+ cdef long c_addr
+ cdef unsigned int c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<unsigned int*>self.p)[0]
+ value = str( c_val )
+ return value
+ def from_CChar( self, _CChar cobject ):
+ " set self from a CChar instance"
+ (<unsigned int*>self.p)[0] = <unsigned int>(<char*>cobject.p)[0]
+ def init_CChar( self, _CChar cobject ):
+ " set a CChar instance from self "
+ (<char*>cobject.p)[0] = <char>(<unsigned int*>self.p)[0]
+ def to_CChar( self ):
+ " cast self to new CChar instance "
+ cdef unsigned int c_src
+ cdef char c_tgt
+ cdef _CUInt py_src
+ cdef _CChar py_tgt
+ c_src = (<unsigned int*>self.p)[0]
+ c_tgt = <char>c_src
+ py_tgt = CChar()
+ (<char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CSChar( self, _CSChar cobject ):
+ " set self from a CSChar instance"
+ (<unsigned int*>self.p)[0] = <unsigned int>(<signed char*>cobject.p)[0]
+ def init_CSChar( self, _CSChar cobject ):
+ " set a CSChar instance from self "
+ (<signed char*>cobject.p)[0] = <signed char>(<unsigned int*>self.p)[0]
+ def to_CSChar( self ):
+ " cast self to new CSChar instance "
+ cdef unsigned int c_src
+ cdef signed char c_tgt
+ cdef _CUInt py_src
+ cdef _CSChar py_tgt
+ c_src = (<unsigned int*>self.p)[0]
+ c_tgt = <signed char>c_src
+ py_tgt = CSChar()
+ (<signed char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUChar( self, _CUChar cobject ):
+ " set self from a CUChar instance"
+ (<unsigned int*>self.p)[0] = <unsigned int>(<unsigned char*>cobject.p)[0]
+ def init_CUChar( self, _CUChar cobject ):
+ " set a CUChar instance from self "
+ (<unsigned char*>cobject.p)[0] = <unsigned char>(<unsigned int*>self.p)[0]
+ def to_CUChar( self ):
+ " cast self to new CUChar instance "
+ cdef unsigned int c_src
+ cdef unsigned char c_tgt
+ cdef _CUInt py_src
+ cdef _CUChar py_tgt
+ c_src = (<unsigned int*>self.p)[0]
+ c_tgt = <unsigned char>c_src
+ py_tgt = CUChar()
+ (<unsigned char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CShort( self, _CShort cobject ):
+ " set self from a CShort instance"
+ (<unsigned int*>self.p)[0] = <unsigned int>(<short*>cobject.p)[0]
+ def init_CShort( self, _CShort cobject ):
+ " set a CShort instance from self "
+ (<short*>cobject.p)[0] = <short>(<unsigned int*>self.p)[0]
+ def to_CShort( self ):
+ " cast self to new CShort instance "
+ cdef unsigned int c_src
+ cdef short c_tgt
+ cdef _CUInt py_src
+ cdef _CShort py_tgt
+ c_src = (<unsigned int*>self.p)[0]
+ c_tgt = <short>c_src
+ py_tgt = CShort()
+ (<short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUShort( self, _CUShort cobject ):
+ " set self from a CUShort instance"
+ (<unsigned int*>self.p)[0] = <unsigned int>(<unsigned short*>cobject.p)[0]
+ def init_CUShort( self, _CUShort cobject ):
+ " set a CUShort instance from self "
+ (<unsigned short*>cobject.p)[0] = <unsigned short>(<unsigned int*>self.p)[0]
+ def to_CUShort( self ):
+ " cast self to new CUShort instance "
+ cdef unsigned int c_src
+ cdef unsigned short c_tgt
+ cdef _CUInt py_src
+ cdef _CUShort py_tgt
+ c_src = (<unsigned int*>self.p)[0]
+ c_tgt = <unsigned short>c_src
+ py_tgt = CUShort()
+ (<unsigned short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CInt( self, _CInt cobject ):
+ " set self from a CInt instance"
+ (<unsigned int*>self.p)[0] = <unsigned int>(<int*>cobject.p)[0]
+ def init_CInt( self, _CInt cobject ):
+ " set a CInt instance from self "
+ (<int*>cobject.p)[0] = <int>(<unsigned int*>self.p)[0]
+ def to_CInt( self ):
+ " cast self to new CInt instance "
+ cdef unsigned int c_src
+ cdef int c_tgt
+ cdef _CUInt py_src
+ cdef _CInt py_tgt
+ c_src = (<unsigned int*>self.p)[0]
+ c_tgt = <int>c_src
+ py_tgt = CInt()
+ (<int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUInt( self, _CUInt cobject ):
+ " set self from a CUInt instance"
+ (<unsigned int*>self.p)[0] = <unsigned int>(<unsigned int*>cobject.p)[0]
+ def init_CUInt( self, _CUInt cobject ):
+ " set a CUInt instance from self "
+ (<unsigned int*>cobject.p)[0] = <unsigned int>(<unsigned int*>self.p)[0]
+ def from_CLong( self, _CLong cobject ):
+ " set self from a CLong instance"
+ (<unsigned int*>self.p)[0] = <unsigned int>(<long*>cobject.p)[0]
+ def init_CLong( self, _CLong cobject ):
+ " set a CLong instance from self "
+ (<long*>cobject.p)[0] = <long>(<unsigned int*>self.p)[0]
+ def to_CLong( self ):
+ " cast self to new CLong instance "
+ cdef unsigned int c_src
+ cdef long c_tgt
+ cdef _CUInt py_src
+ cdef _CLong py_tgt
+ c_src = (<unsigned int*>self.p)[0]
+ c_tgt = <long>c_src
+ py_tgt = CLong()
+ (<long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULong( self, _CULong cobject ):
+ " set self from a CULong instance"
+ (<unsigned int*>self.p)[0] = <unsigned int>(<unsigned long*>cobject.p)[0]
+ def init_CULong( self, _CULong cobject ):
+ " set a CULong instance from self "
+ (<unsigned long*>cobject.p)[0] = <unsigned long>(<unsigned int*>self.p)[0]
+ def to_CULong( self ):
+ " cast self to new CULong instance "
+ cdef unsigned int c_src
+ cdef unsigned long c_tgt
+ cdef _CUInt py_src
+ cdef _CULong py_tgt
+ c_src = (<unsigned int*>self.p)[0]
+ c_tgt = <unsigned long>c_src
+ py_tgt = CULong()
+ (<unsigned long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLLong( self, _CLLong cobject ):
+ " set self from a CLLong instance"
+ (<unsigned int*>self.p)[0] = <unsigned int>(<long long*>cobject.p)[0]
+ def init_CLLong( self, _CLLong cobject ):
+ " set a CLLong instance from self "
+ (<long long*>cobject.p)[0] = <long long>(<unsigned int*>self.p)[0]
+ def to_CLLong( self ):
+ " cast self to new CLLong instance "
+ cdef unsigned int c_src
+ cdef long long c_tgt
+ cdef _CUInt py_src
+ cdef _CLLong py_tgt
+ c_src = (<unsigned int*>self.p)[0]
+ c_tgt = <long long>c_src
+ py_tgt = CLLong()
+ (<long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULLong( self, _CULLong cobject ):
+ " set self from a CULLong instance"
+ (<unsigned int*>self.p)[0] = <unsigned int>(<unsigned long long*>cobject.p)[0]
+ def init_CULLong( self, _CULLong cobject ):
+ " set a CULLong instance from self "
+ (<unsigned long long*>cobject.p)[0] = <unsigned long long>(<unsigned int*>self.p)[0]
+ def to_CULLong( self ):
+ " cast self to new CULLong instance "
+ cdef unsigned int c_src
+ cdef unsigned long long c_tgt
+ cdef _CUInt py_src
+ cdef _CULLong py_tgt
+ c_src = (<unsigned int*>self.p)[0]
+ c_tgt = <unsigned long long>c_src
+ py_tgt = CULLong()
+ (<unsigned long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CFloat( self, _CFloat cobject ):
+ " set self from a CFloat instance"
+ (<unsigned int*>self.p)[0] = <unsigned int>(<float*>cobject.p)[0]
+ def init_CFloat( self, _CFloat cobject ):
+ " set a CFloat instance from self "
+ (<float*>cobject.p)[0] = <float>(<unsigned int*>self.p)[0]
+ def to_CFloat( self ):
+ " cast self to new CFloat instance "
+ cdef unsigned int c_src
+ cdef float c_tgt
+ cdef _CUInt py_src
+ cdef _CFloat py_tgt
+ c_src = (<unsigned int*>self.p)[0]
+ c_tgt = <float>c_src
+ py_tgt = CFloat()
+ (<float*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CDouble( self, _CDouble cobject ):
+ " set self from a CDouble instance"
+ (<unsigned int*>self.p)[0] = <unsigned int>(<double*>cobject.p)[0]
+ def init_CDouble( self, _CDouble cobject ):
+ " set a CDouble instance from self "
+ (<double*>cobject.p)[0] = <double>(<unsigned int*>self.p)[0]
+ def to_CDouble( self ):
+ " cast self to new CDouble instance "
+ cdef unsigned int c_src
+ cdef double c_tgt
+ cdef _CUInt py_src
+ cdef _CDouble py_tgt
+ c_src = (<unsigned int*>self.p)[0]
+ c_tgt = <double>c_src
+ py_tgt = CDouble()
+ (<double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLDouble( self, _CLDouble cobject ):
+ " set self from a CLDouble instance"
+ (<unsigned int*>self.p)[0] = <unsigned int>(<long double*>cobject.p)[0]
+ def init_CLDouble( self, _CLDouble cobject ):
+ " set a CLDouble instance from self "
+ (<long double*>cobject.p)[0] = <long double>(<unsigned int*>self.p)[0]
+ def to_CLDouble( self ):
+ " cast self to new CLDouble instance "
+ cdef unsigned int c_src
+ cdef long double c_tgt
+ cdef _CUInt py_src
+ cdef _CLDouble py_tgt
+ c_src = (<unsigned int*>self.p)[0]
+ c_tgt = <long double>c_src
+ py_tgt = CLDouble()
+ (<long double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+CUInt = Meta('CUInt', (_CUInt,), {} )
+CUInt.basetype = CUInt
+CUInt.sizeof = sizeof(unsigned int)
+
+#cdef public class _CLong(_CObject)[ object AdaptObj_CLong, type AdaptType_CLong ]:
+cdef class _CLong(_CObject):
+ def __init__( self, value = None, addr = None ):
+ cdef long*p
+ cdef long c_addr
+ cdef _CObject cobject
+ if addr is None:
+ p = <long*>pyx_calloc(1,sizeof(long))
+ self.p = <void*>p
+ if isinstance(value,CObject):
+ if isinstance(value,_CLong):
+ cobject = value
+ p[0] = (<long*>cobject.p)[0]
+ else:
+ value.init_CLong(self)
+ elif value is not None:
+ p[0] = value
+ else:
+ p[0] = 0
+ self.malloced = True
+ else:
+ c_addr = addr
+ self.p = <void*>c_addr
+ self.malloced = False
+ def __int__( self ):
+ cdef long *p
+ assert self.p != NULL
+ p = <long*>self.p
+ return int(p[0])
+ def __float__( self ):
+ cdef long *p
+ assert self.p != NULL
+ p = <long*>self.p
+ return float(p[0])
+ def __add__( x, y ):
+ cdef long val
+ cdef _CLong self
+ if not isinstance(x,CLong):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<long*>self.p)[0]
+ val = val + y
+ return CLong(val)
+ def __sub__( x, y ):
+ cdef long val
+ cdef _CLong self
+ if not isinstance(x,CLong):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<long*>self.p)[0]
+ val = val - y
+ return CLong(val)
+ def __mul__( x, y ):
+ cdef long val
+ cdef _CLong self
+ if not isinstance(x,CLong):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<long*>self.p)[0]
+ val = val + y
+ return CLong(val)
+ def __repr__( self ):
+ cdef long c_addr
+ cdef long c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<long*>self.p)[0]
+ value = ", value = %s" % str( c_val )
+ return "%s( addr = %s%s )" % (self.__class__.__name__, hex(c_addr), value)
+ def set_value( self, value ):
+ if isinstance(value,CObject):
+ value.init_CLong(self)
+ else:
+ (<long*>self.p)[0] = value
+ def get_value( self ):
+ return (<long*>self.p)[0]
+ def to_CLong(self):
+ return self
+ def init_from( self, _CLong cobject ):
+ " init self from a CLong instance "
+# if not self.is_compatible(cobject):
+ if not isinstance( cobject, CLong ):
+ raise TypeError("%s cannot be set from %s" % (self.__class__, cobject))
+ (<long*>self.p)[0] = (<long*>cobject.p)[0]
+ def get_basetype( self ):
+ return CLong
+# def sizeof( self ):
+# return sizeof(long)
+# def is_compatible( self, _CObject cobject ):
+# return isinstance( cobject, CLong )
+ def __str__( self ):
+ cdef long c_addr
+ cdef long c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<long*>self.p)[0]
+ value = str( c_val )
+ return value
+ def from_CChar( self, _CChar cobject ):
+ " set self from a CChar instance"
+ (<long*>self.p)[0] = <long>(<char*>cobject.p)[0]
+ def init_CChar( self, _CChar cobject ):
+ " set a CChar instance from self "
+ (<char*>cobject.p)[0] = <char>(<long*>self.p)[0]
+ def to_CChar( self ):
+ " cast self to new CChar instance "
+ cdef long c_src
+ cdef char c_tgt
+ cdef _CLong py_src
+ cdef _CChar py_tgt
+ c_src = (<long*>self.p)[0]
+ c_tgt = <char>c_src
+ py_tgt = CChar()
+ (<char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CSChar( self, _CSChar cobject ):
+ " set self from a CSChar instance"
+ (<long*>self.p)[0] = <long>(<signed char*>cobject.p)[0]
+ def init_CSChar( self, _CSChar cobject ):
+ " set a CSChar instance from self "
+ (<signed char*>cobject.p)[0] = <signed char>(<long*>self.p)[0]
+ def to_CSChar( self ):
+ " cast self to new CSChar instance "
+ cdef long c_src
+ cdef signed char c_tgt
+ cdef _CLong py_src
+ cdef _CSChar py_tgt
+ c_src = (<long*>self.p)[0]
+ c_tgt = <signed char>c_src
+ py_tgt = CSChar()
+ (<signed char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUChar( self, _CUChar cobject ):
+ " set self from a CUChar instance"
+ (<long*>self.p)[0] = <long>(<unsigned char*>cobject.p)[0]
+ def init_CUChar( self, _CUChar cobject ):
+ " set a CUChar instance from self "
+ (<unsigned char*>cobject.p)[0] = <unsigned char>(<long*>self.p)[0]
+ def to_CUChar( self ):
+ " cast self to new CUChar instance "
+ cdef long c_src
+ cdef unsigned char c_tgt
+ cdef _CLong py_src
+ cdef _CUChar py_tgt
+ c_src = (<long*>self.p)[0]
+ c_tgt = <unsigned char>c_src
+ py_tgt = CUChar()
+ (<unsigned char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CShort( self, _CShort cobject ):
+ " set self from a CShort instance"
+ (<long*>self.p)[0] = <long>(<short*>cobject.p)[0]
+ def init_CShort( self, _CShort cobject ):
+ " set a CShort instance from self "
+ (<short*>cobject.p)[0] = <short>(<long*>self.p)[0]
+ def to_CShort( self ):
+ " cast self to new CShort instance "
+ cdef long c_src
+ cdef short c_tgt
+ cdef _CLong py_src
+ cdef _CShort py_tgt
+ c_src = (<long*>self.p)[0]
+ c_tgt = <short>c_src
+ py_tgt = CShort()
+ (<short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUShort( self, _CUShort cobject ):
+ " set self from a CUShort instance"
+ (<long*>self.p)[0] = <long>(<unsigned short*>cobject.p)[0]
+ def init_CUShort( self, _CUShort cobject ):
+ " set a CUShort instance from self "
+ (<unsigned short*>cobject.p)[0] = <unsigned short>(<long*>self.p)[0]
+ def to_CUShort( self ):
+ " cast self to new CUShort instance "
+ cdef long c_src
+ cdef unsigned short c_tgt
+ cdef _CLong py_src
+ cdef _CUShort py_tgt
+ c_src = (<long*>self.p)[0]
+ c_tgt = <unsigned short>c_src
+ py_tgt = CUShort()
+ (<unsigned short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CInt( self, _CInt cobject ):
+ " set self from a CInt instance"
+ (<long*>self.p)[0] = <long>(<int*>cobject.p)[0]
+ def init_CInt( self, _CInt cobject ):
+ " set a CInt instance from self "
+ (<int*>cobject.p)[0] = <int>(<long*>self.p)[0]
+ def to_CInt( self ):
+ " cast self to new CInt instance "
+ cdef long c_src
+ cdef int c_tgt
+ cdef _CLong py_src
+ cdef _CInt py_tgt
+ c_src = (<long*>self.p)[0]
+ c_tgt = <int>c_src
+ py_tgt = CInt()
+ (<int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUInt( self, _CUInt cobject ):
+ " set self from a CUInt instance"
+ (<long*>self.p)[0] = <long>(<unsigned int*>cobject.p)[0]
+ def init_CUInt( self, _CUInt cobject ):
+ " set a CUInt instance from self "
+ (<unsigned int*>cobject.p)[0] = <unsigned int>(<long*>self.p)[0]
+ def to_CUInt( self ):
+ " cast self to new CUInt instance "
+ cdef long c_src
+ cdef unsigned int c_tgt
+ cdef _CLong py_src
+ cdef _CUInt py_tgt
+ c_src = (<long*>self.p)[0]
+ c_tgt = <unsigned int>c_src
+ py_tgt = CUInt()
+ (<unsigned int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLong( self, _CLong cobject ):
+ " set self from a CLong instance"
+ (<long*>self.p)[0] = <long>(<long*>cobject.p)[0]
+ def init_CLong( self, _CLong cobject ):
+ " set a CLong instance from self "
+ (<long*>cobject.p)[0] = <long>(<long*>self.p)[0]
+ def from_CULong( self, _CULong cobject ):
+ " set self from a CULong instance"
+ (<long*>self.p)[0] = <long>(<unsigned long*>cobject.p)[0]
+ def init_CULong( self, _CULong cobject ):
+ " set a CULong instance from self "
+ (<unsigned long*>cobject.p)[0] = <unsigned long>(<long*>self.p)[0]
+ def to_CULong( self ):
+ " cast self to new CULong instance "
+ cdef long c_src
+ cdef unsigned long c_tgt
+ cdef _CLong py_src
+ cdef _CULong py_tgt
+ c_src = (<long*>self.p)[0]
+ c_tgt = <unsigned long>c_src
+ py_tgt = CULong()
+ (<unsigned long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLLong( self, _CLLong cobject ):
+ " set self from a CLLong instance"
+ (<long*>self.p)[0] = <long>(<long long*>cobject.p)[0]
+ def init_CLLong( self, _CLLong cobject ):
+ " set a CLLong instance from self "
+ (<long long*>cobject.p)[0] = <long long>(<long*>self.p)[0]
+ def to_CLLong( self ):
+ " cast self to new CLLong instance "
+ cdef long c_src
+ cdef long long c_tgt
+ cdef _CLong py_src
+ cdef _CLLong py_tgt
+ c_src = (<long*>self.p)[0]
+ c_tgt = <long long>c_src
+ py_tgt = CLLong()
+ (<long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULLong( self, _CULLong cobject ):
+ " set self from a CULLong instance"
+ (<long*>self.p)[0] = <long>(<unsigned long long*>cobject.p)[0]
+ def init_CULLong( self, _CULLong cobject ):
+ " set a CULLong instance from self "
+ (<unsigned long long*>cobject.p)[0] = <unsigned long long>(<long*>self.p)[0]
+ def to_CULLong( self ):
+ " cast self to new CULLong instance "
+ cdef long c_src
+ cdef unsigned long long c_tgt
+ cdef _CLong py_src
+ cdef _CULLong py_tgt
+ c_src = (<long*>self.p)[0]
+ c_tgt = <unsigned long long>c_src
+ py_tgt = CULLong()
+ (<unsigned long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CFloat( self, _CFloat cobject ):
+ " set self from a CFloat instance"
+ (<long*>self.p)[0] = <long>(<float*>cobject.p)[0]
+ def init_CFloat( self, _CFloat cobject ):
+ " set a CFloat instance from self "
+ (<float*>cobject.p)[0] = <float>(<long*>self.p)[0]
+ def to_CFloat( self ):
+ " cast self to new CFloat instance "
+ cdef long c_src
+ cdef float c_tgt
+ cdef _CLong py_src
+ cdef _CFloat py_tgt
+ c_src = (<long*>self.p)[0]
+ c_tgt = <float>c_src
+ py_tgt = CFloat()
+ (<float*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CDouble( self, _CDouble cobject ):
+ " set self from a CDouble instance"
+ (<long*>self.p)[0] = <long>(<double*>cobject.p)[0]
+ def init_CDouble( self, _CDouble cobject ):
+ " set a CDouble instance from self "
+ (<double*>cobject.p)[0] = <double>(<long*>self.p)[0]
+ def to_CDouble( self ):
+ " cast self to new CDouble instance "
+ cdef long c_src
+ cdef double c_tgt
+ cdef _CLong py_src
+ cdef _CDouble py_tgt
+ c_src = (<long*>self.p)[0]
+ c_tgt = <double>c_src
+ py_tgt = CDouble()
+ (<double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLDouble( self, _CLDouble cobject ):
+ " set self from a CLDouble instance"
+ (<long*>self.p)[0] = <long>(<long double*>cobject.p)[0]
+ def init_CLDouble( self, _CLDouble cobject ):
+ " set a CLDouble instance from self "
+ (<long double*>cobject.p)[0] = <long double>(<long*>self.p)[0]
+ def to_CLDouble( self ):
+ " cast self to new CLDouble instance "
+ cdef long c_src
+ cdef long double c_tgt
+ cdef _CLong py_src
+ cdef _CLDouble py_tgt
+ c_src = (<long*>self.p)[0]
+ c_tgt = <long double>c_src
+ py_tgt = CLDouble()
+ (<long double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+CLong = Meta('CLong', (_CLong,), {} )
+CLong.basetype = CLong
+CLong.sizeof = sizeof(long)
+
+#cdef public class _CULong(_CObject)[ object AdaptObj_CULong, type AdaptType_CULong ]:
+cdef class _CULong(_CObject):
+ def __init__( self, value = None, addr = None ):
+ cdef unsigned long*p
+ cdef long c_addr
+ cdef _CObject cobject
+ if addr is None:
+ p = <unsigned long*>pyx_calloc(1,sizeof(unsigned long))
+ self.p = <void*>p
+ if isinstance(value,CObject):
+ if isinstance(value,_CULong):
+ cobject = value
+ p[0] = (<unsigned long*>cobject.p)[0]
+ else:
+ value.init_CULong(self)
+ elif value is not None:
+ p[0] = value
+ else:
+ p[0] = 0
+ self.malloced = True
+ else:
+ c_addr = addr
+ self.p = <void*>c_addr
+ self.malloced = False
+ def __int__( self ):
+ cdef unsigned long *p
+ assert self.p != NULL
+ p = <unsigned long*>self.p
+ return int(p[0])
+ def __float__( self ):
+ cdef unsigned long *p
+ assert self.p != NULL
+ p = <unsigned long*>self.p
+ return float(p[0])
+ def __add__( x, y ):
+ cdef unsigned long val
+ cdef _CULong self
+ if not isinstance(x,CULong):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<unsigned long*>self.p)[0]
+ val = val + y
+ return CULong(val)
+ def __sub__( x, y ):
+ cdef unsigned long val
+ cdef _CULong self
+ if not isinstance(x,CULong):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<unsigned long*>self.p)[0]
+ val = val - y
+ return CULong(val)
+ def __mul__( x, y ):
+ cdef unsigned long val
+ cdef _CULong self
+ if not isinstance(x,CULong):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<unsigned long*>self.p)[0]
+ val = val + y
+ return CULong(val)
+ def __repr__( self ):
+ cdef long c_addr
+ cdef unsigned long c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<unsigned long*>self.p)[0]
+ value = ", value = %s" % str( c_val )
+ return "%s( addr = %s%s )" % (self.__class__.__name__, hex(c_addr), value)
+ def set_value( self, value ):
+ if isinstance(value,CObject):
+ value.init_CULong(self)
+ else:
+ (<unsigned long*>self.p)[0] = value
+ def get_value( self ):
+ return (<unsigned long*>self.p)[0]
+ def to_CULong(self):
+ return self
+ def init_from( self, _CULong cobject ):
+ " init self from a CULong instance "
+# if not self.is_compatible(cobject):
+ if not isinstance( cobject, CULong ):
+ raise TypeError("%s cannot be set from %s" % (self.__class__, cobject))
+ (<unsigned long*>self.p)[0] = (<unsigned long*>cobject.p)[0]
+ def get_basetype( self ):
+ return CULong
+# def sizeof( self ):
+# return sizeof(unsigned long)
+# def is_compatible( self, _CObject cobject ):
+# return isinstance( cobject, CULong )
+ def __str__( self ):
+ cdef long c_addr
+ cdef unsigned long c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<unsigned long*>self.p)[0]
+ value = str( c_val )
+ return value
+ def from_CChar( self, _CChar cobject ):
+ " set self from a CChar instance"
+ (<unsigned long*>self.p)[0] = <unsigned long>(<char*>cobject.p)[0]
+ def init_CChar( self, _CChar cobject ):
+ " set a CChar instance from self "
+ (<char*>cobject.p)[0] = <char>(<unsigned long*>self.p)[0]
+ def to_CChar( self ):
+ " cast self to new CChar instance "
+ cdef unsigned long c_src
+ cdef char c_tgt
+ cdef _CULong py_src
+ cdef _CChar py_tgt
+ c_src = (<unsigned long*>self.p)[0]
+ c_tgt = <char>c_src
+ py_tgt = CChar()
+ (<char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CSChar( self, _CSChar cobject ):
+ " set self from a CSChar instance"
+ (<unsigned long*>self.p)[0] = <unsigned long>(<signed char*>cobject.p)[0]
+ def init_CSChar( self, _CSChar cobject ):
+ " set a CSChar instance from self "
+ (<signed char*>cobject.p)[0] = <signed char>(<unsigned long*>self.p)[0]
+ def to_CSChar( self ):
+ " cast self to new CSChar instance "
+ cdef unsigned long c_src
+ cdef signed char c_tgt
+ cdef _CULong py_src
+ cdef _CSChar py_tgt
+ c_src = (<unsigned long*>self.p)[0]
+ c_tgt = <signed char>c_src
+ py_tgt = CSChar()
+ (<signed char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUChar( self, _CUChar cobject ):
+ " set self from a CUChar instance"
+ (<unsigned long*>self.p)[0] = <unsigned long>(<unsigned char*>cobject.p)[0]
+ def init_CUChar( self, _CUChar cobject ):
+ " set a CUChar instance from self "
+ (<unsigned char*>cobject.p)[0] = <unsigned char>(<unsigned long*>self.p)[0]
+ def to_CUChar( self ):
+ " cast self to new CUChar instance "
+ cdef unsigned long c_src
+ cdef unsigned char c_tgt
+ cdef _CULong py_src
+ cdef _CUChar py_tgt
+ c_src = (<unsigned long*>self.p)[0]
+ c_tgt = <unsigned char>c_src
+ py_tgt = CUChar()
+ (<unsigned char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CShort( self, _CShort cobject ):
+ " set self from a CShort instance"
+ (<unsigned long*>self.p)[0] = <unsigned long>(<short*>cobject.p)[0]
+ def init_CShort( self, _CShort cobject ):
+ " set a CShort instance from self "
+ (<short*>cobject.p)[0] = <short>(<unsigned long*>self.p)[0]
+ def to_CShort( self ):
+ " cast self to new CShort instance "
+ cdef unsigned long c_src
+ cdef short c_tgt
+ cdef _CULong py_src
+ cdef _CShort py_tgt
+ c_src = (<unsigned long*>self.p)[0]
+ c_tgt = <short>c_src
+ py_tgt = CShort()
+ (<short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUShort( self, _CUShort cobject ):
+ " set self from a CUShort instance"
+ (<unsigned long*>self.p)[0] = <unsigned long>(<unsigned short*>cobject.p)[0]
+ def init_CUShort( self, _CUShort cobject ):
+ " set a CUShort instance from self "
+ (<unsigned short*>cobject.p)[0] = <unsigned short>(<unsigned long*>self.p)[0]
+ def to_CUShort( self ):
+ " cast self to new CUShort instance "
+ cdef unsigned long c_src
+ cdef unsigned short c_tgt
+ cdef _CULong py_src
+ cdef _CUShort py_tgt
+ c_src = (<unsigned long*>self.p)[0]
+ c_tgt = <unsigned short>c_src
+ py_tgt = CUShort()
+ (<unsigned short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CInt( self, _CInt cobject ):
+ " set self from a CInt instance"
+ (<unsigned long*>self.p)[0] = <unsigned long>(<int*>cobject.p)[0]
+ def init_CInt( self, _CInt cobject ):
+ " set a CInt instance from self "
+ (<int*>cobject.p)[0] = <int>(<unsigned long*>self.p)[0]
+ def to_CInt( self ):
+ " cast self to new CInt instance "
+ cdef unsigned long c_src
+ cdef int c_tgt
+ cdef _CULong py_src
+ cdef _CInt py_tgt
+ c_src = (<unsigned long*>self.p)[0]
+ c_tgt = <int>c_src
+ py_tgt = CInt()
+ (<int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUInt( self, _CUInt cobject ):
+ " set self from a CUInt instance"
+ (<unsigned long*>self.p)[0] = <unsigned long>(<unsigned int*>cobject.p)[0]
+ def init_CUInt( self, _CUInt cobject ):
+ " set a CUInt instance from self "
+ (<unsigned int*>cobject.p)[0] = <unsigned int>(<unsigned long*>self.p)[0]
+ def to_CUInt( self ):
+ " cast self to new CUInt instance "
+ cdef unsigned long c_src
+ cdef unsigned int c_tgt
+ cdef _CULong py_src
+ cdef _CUInt py_tgt
+ c_src = (<unsigned long*>self.p)[0]
+ c_tgt = <unsigned int>c_src
+ py_tgt = CUInt()
+ (<unsigned int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLong( self, _CLong cobject ):
+ " set self from a CLong instance"
+ (<unsigned long*>self.p)[0] = <unsigned long>(<long*>cobject.p)[0]
+ def init_CLong( self, _CLong cobject ):
+ " set a CLong instance from self "
+ (<long*>cobject.p)[0] = <long>(<unsigned long*>self.p)[0]
+ def to_CLong( self ):
+ " cast self to new CLong instance "
+ cdef unsigned long c_src
+ cdef long c_tgt
+ cdef _CULong py_src
+ cdef _CLong py_tgt
+ c_src = (<unsigned long*>self.p)[0]
+ c_tgt = <long>c_src
+ py_tgt = CLong()
+ (<long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULong( self, _CULong cobject ):
+ " set self from a CULong instance"
+ (<unsigned long*>self.p)[0] = <unsigned long>(<unsigned long*>cobject.p)[0]
+ def init_CULong( self, _CULong cobject ):
+ " set a CULong instance from self "
+ (<unsigned long*>cobject.p)[0] = <unsigned long>(<unsigned long*>self.p)[0]
+ def from_CLLong( self, _CLLong cobject ):
+ " set self from a CLLong instance"
+ (<unsigned long*>self.p)[0] = <unsigned long>(<long long*>cobject.p)[0]
+ def init_CLLong( self, _CLLong cobject ):
+ " set a CLLong instance from self "
+ (<long long*>cobject.p)[0] = <long long>(<unsigned long*>self.p)[0]
+ def to_CLLong( self ):
+ " cast self to new CLLong instance "
+ cdef unsigned long c_src
+ cdef long long c_tgt
+ cdef _CULong py_src
+ cdef _CLLong py_tgt
+ c_src = (<unsigned long*>self.p)[0]
+ c_tgt = <long long>c_src
+ py_tgt = CLLong()
+ (<long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULLong( self, _CULLong cobject ):
+ " set self from a CULLong instance"
+ (<unsigned long*>self.p)[0] = <unsigned long>(<unsigned long long*>cobject.p)[0]
+ def init_CULLong( self, _CULLong cobject ):
+ " set a CULLong instance from self "
+ (<unsigned long long*>cobject.p)[0] = <unsigned long long>(<unsigned long*>self.p)[0]
+ def to_CULLong( self ):
+ " cast self to new CULLong instance "
+ cdef unsigned long c_src
+ cdef unsigned long long c_tgt
+ cdef _CULong py_src
+ cdef _CULLong py_tgt
+ c_src = (<unsigned long*>self.p)[0]
+ c_tgt = <unsigned long long>c_src
+ py_tgt = CULLong()
+ (<unsigned long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CFloat( self, _CFloat cobject ):
+ " set self from a CFloat instance"
+ (<unsigned long*>self.p)[0] = <unsigned long>(<float*>cobject.p)[0]
+ def init_CFloat( self, _CFloat cobject ):
+ " set a CFloat instance from self "
+ (<float*>cobject.p)[0] = <float>(<unsigned long*>self.p)[0]
+ def to_CFloat( self ):
+ " cast self to new CFloat instance "
+ cdef unsigned long c_src
+ cdef float c_tgt
+ cdef _CULong py_src
+ cdef _CFloat py_tgt
+ c_src = (<unsigned long*>self.p)[0]
+ c_tgt = <float>c_src
+ py_tgt = CFloat()
+ (<float*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CDouble( self, _CDouble cobject ):
+ " set self from a CDouble instance"
+ (<unsigned long*>self.p)[0] = <unsigned long>(<double*>cobject.p)[0]
+ def init_CDouble( self, _CDouble cobject ):
+ " set a CDouble instance from self "
+ (<double*>cobject.p)[0] = <double>(<unsigned long*>self.p)[0]
+ def to_CDouble( self ):
+ " cast self to new CDouble instance "
+ cdef unsigned long c_src
+ cdef double c_tgt
+ cdef _CULong py_src
+ cdef _CDouble py_tgt
+ c_src = (<unsigned long*>self.p)[0]
+ c_tgt = <double>c_src
+ py_tgt = CDouble()
+ (<double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLDouble( self, _CLDouble cobject ):
+ " set self from a CLDouble instance"
+ (<unsigned long*>self.p)[0] = <unsigned long>(<long double*>cobject.p)[0]
+ def init_CLDouble( self, _CLDouble cobject ):
+ " set a CLDouble instance from self "
+ (<long double*>cobject.p)[0] = <long double>(<unsigned long*>self.p)[0]
+ def to_CLDouble( self ):
+ " cast self to new CLDouble instance "
+ cdef unsigned long c_src
+ cdef long double c_tgt
+ cdef _CULong py_src
+ cdef _CLDouble py_tgt
+ c_src = (<unsigned long*>self.p)[0]
+ c_tgt = <long double>c_src
+ py_tgt = CLDouble()
+ (<long double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+CULong = Meta('CULong', (_CULong,), {} )
+CULong.basetype = CULong
+CULong.sizeof = sizeof(unsigned long)
+
+#cdef public class _CLLong(_CObject)[ object AdaptObj_CLLong, type AdaptType_CLLong ]:
+cdef class _CLLong(_CObject):
+ def __init__( self, value = None, addr = None ):
+ cdef long long*p
+ cdef long c_addr
+ cdef _CObject cobject
+ if addr is None:
+ p = <long long*>pyx_calloc(1,sizeof(long long))
+ self.p = <void*>p
+ if isinstance(value,CObject):
+ if isinstance(value,_CLLong):
+ cobject = value
+ p[0] = (<long long*>cobject.p)[0]
+ else:
+ value.init_CLLong(self)
+ elif value is not None:
+ p[0] = value
+ else:
+ p[0] = 0
+ self.malloced = True
+ else:
+ c_addr = addr
+ self.p = <void*>c_addr
+ self.malloced = False
+ def __int__( self ):
+ cdef long long *p
+ assert self.p != NULL
+ p = <long long*>self.p
+ return int(p[0])
+ def __float__( self ):
+ cdef long long *p
+ assert self.p != NULL
+ p = <long long*>self.p
+ return float(p[0])
+ def __add__( x, y ):
+ cdef long long val
+ cdef _CLLong self
+ if not isinstance(x,CLLong):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<long long*>self.p)[0]
+ val = val + y
+ return CLLong(val)
+ def __sub__( x, y ):
+ cdef long long val
+ cdef _CLLong self
+ if not isinstance(x,CLLong):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<long long*>self.p)[0]
+ val = val - y
+ return CLLong(val)
+ def __mul__( x, y ):
+ cdef long long val
+ cdef _CLLong self
+ if not isinstance(x,CLLong):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<long long*>self.p)[0]
+ val = val + y
+ return CLLong(val)
+ def __repr__( self ):
+ cdef long c_addr
+ cdef long long c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<long long*>self.p)[0]
+ value = ", value = %s" % str( c_val )
+ return "%s( addr = %s%s )" % (self.__class__.__name__, hex(c_addr), value)
+ def set_value( self, value ):
+ if isinstance(value,CObject):
+ value.init_CLLong(self)
+ else:
+ (<long long*>self.p)[0] = value
+ def get_value( self ):
+ return (<long long*>self.p)[0]
+ def to_CLLong(self):
+ return self
+ def init_from( self, _CLLong cobject ):
+ " init self from a CLLong instance "
+# if not self.is_compatible(cobject):
+ if not isinstance( cobject, CLLong ):
+ raise TypeError("%s cannot be set from %s" % (self.__class__, cobject))
+ (<long long*>self.p)[0] = (<long long*>cobject.p)[0]
+ def get_basetype( self ):
+ return CLLong
+# def sizeof( self ):
+# return sizeof(long long)
+# def is_compatible( self, _CObject cobject ):
+# return isinstance( cobject, CLLong )
+ def __str__( self ):
+ cdef long c_addr
+ cdef long long c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<long long*>self.p)[0]
+ value = str( c_val )
+ return value
+ def from_CChar( self, _CChar cobject ):
+ " set self from a CChar instance"
+ (<long long*>self.p)[0] = <long long>(<char*>cobject.p)[0]
+ def init_CChar( self, _CChar cobject ):
+ " set a CChar instance from self "
+ (<char*>cobject.p)[0] = <char>(<long long*>self.p)[0]
+ def to_CChar( self ):
+ " cast self to new CChar instance "
+ cdef long long c_src
+ cdef char c_tgt
+ cdef _CLLong py_src
+ cdef _CChar py_tgt
+ c_src = (<long long*>self.p)[0]
+ c_tgt = <char>c_src
+ py_tgt = CChar()
+ (<char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CSChar( self, _CSChar cobject ):
+ " set self from a CSChar instance"
+ (<long long*>self.p)[0] = <long long>(<signed char*>cobject.p)[0]
+ def init_CSChar( self, _CSChar cobject ):
+ " set a CSChar instance from self "
+ (<signed char*>cobject.p)[0] = <signed char>(<long long*>self.p)[0]
+ def to_CSChar( self ):
+ " cast self to new CSChar instance "
+ cdef long long c_src
+ cdef signed char c_tgt
+ cdef _CLLong py_src
+ cdef _CSChar py_tgt
+ c_src = (<long long*>self.p)[0]
+ c_tgt = <signed char>c_src
+ py_tgt = CSChar()
+ (<signed char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUChar( self, _CUChar cobject ):
+ " set self from a CUChar instance"
+ (<long long*>self.p)[0] = <long long>(<unsigned char*>cobject.p)[0]
+ def init_CUChar( self, _CUChar cobject ):
+ " set a CUChar instance from self "
+ (<unsigned char*>cobject.p)[0] = <unsigned char>(<long long*>self.p)[0]
+ def to_CUChar( self ):
+ " cast self to new CUChar instance "
+ cdef long long c_src
+ cdef unsigned char c_tgt
+ cdef _CLLong py_src
+ cdef _CUChar py_tgt
+ c_src = (<long long*>self.p)[0]
+ c_tgt = <unsigned char>c_src
+ py_tgt = CUChar()
+ (<unsigned char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CShort( self, _CShort cobject ):
+ " set self from a CShort instance"
+ (<long long*>self.p)[0] = <long long>(<short*>cobject.p)[0]
+ def init_CShort( self, _CShort cobject ):
+ " set a CShort instance from self "
+ (<short*>cobject.p)[0] = <short>(<long long*>self.p)[0]
+ def to_CShort( self ):
+ " cast self to new CShort instance "
+ cdef long long c_src
+ cdef short c_tgt
+ cdef _CLLong py_src
+ cdef _CShort py_tgt
+ c_src = (<long long*>self.p)[0]
+ c_tgt = <short>c_src
+ py_tgt = CShort()
+ (<short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUShort( self, _CUShort cobject ):
+ " set self from a CUShort instance"
+ (<long long*>self.p)[0] = <long long>(<unsigned short*>cobject.p)[0]
+ def init_CUShort( self, _CUShort cobject ):
+ " set a CUShort instance from self "
+ (<unsigned short*>cobject.p)[0] = <unsigned short>(<long long*>self.p)[0]
+ def to_CUShort( self ):
+ " cast self to new CUShort instance "
+ cdef long long c_src
+ cdef unsigned short c_tgt
+ cdef _CLLong py_src
+ cdef _CUShort py_tgt
+ c_src = (<long long*>self.p)[0]
+ c_tgt = <unsigned short>c_src
+ py_tgt = CUShort()
+ (<unsigned short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CInt( self, _CInt cobject ):
+ " set self from a CInt instance"
+ (<long long*>self.p)[0] = <long long>(<int*>cobject.p)[0]
+ def init_CInt( self, _CInt cobject ):
+ " set a CInt instance from self "
+ (<int*>cobject.p)[0] = <int>(<long long*>self.p)[0]
+ def to_CInt( self ):
+ " cast self to new CInt instance "
+ cdef long long c_src
+ cdef int c_tgt
+ cdef _CLLong py_src
+ cdef _CInt py_tgt
+ c_src = (<long long*>self.p)[0]
+ c_tgt = <int>c_src
+ py_tgt = CInt()
+ (<int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUInt( self, _CUInt cobject ):
+ " set self from a CUInt instance"
+ (<long long*>self.p)[0] = <long long>(<unsigned int*>cobject.p)[0]
+ def init_CUInt( self, _CUInt cobject ):
+ " set a CUInt instance from self "
+ (<unsigned int*>cobject.p)[0] = <unsigned int>(<long long*>self.p)[0]
+ def to_CUInt( self ):
+ " cast self to new CUInt instance "
+ cdef long long c_src
+ cdef unsigned int c_tgt
+ cdef _CLLong py_src
+ cdef _CUInt py_tgt
+ c_src = (<long long*>self.p)[0]
+ c_tgt = <unsigned int>c_src
+ py_tgt = CUInt()
+ (<unsigned int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLong( self, _CLong cobject ):
+ " set self from a CLong instance"
+ (<long long*>self.p)[0] = <long long>(<long*>cobject.p)[0]
+ def init_CLong( self, _CLong cobject ):
+ " set a CLong instance from self "
+ (<long*>cobject.p)[0] = <long>(<long long*>self.p)[0]
+ def to_CLong( self ):
+ " cast self to new CLong instance "
+ cdef long long c_src
+ cdef long c_tgt
+ cdef _CLLong py_src
+ cdef _CLong py_tgt
+ c_src = (<long long*>self.p)[0]
+ c_tgt = <long>c_src
+ py_tgt = CLong()
+ (<long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULong( self, _CULong cobject ):
+ " set self from a CULong instance"
+ (<long long*>self.p)[0] = <long long>(<unsigned long*>cobject.p)[0]
+ def init_CULong( self, _CULong cobject ):
+ " set a CULong instance from self "
+ (<unsigned long*>cobject.p)[0] = <unsigned long>(<long long*>self.p)[0]
+ def to_CULong( self ):
+ " cast self to new CULong instance "
+ cdef long long c_src
+ cdef unsigned long c_tgt
+ cdef _CLLong py_src
+ cdef _CULong py_tgt
+ c_src = (<long long*>self.p)[0]
+ c_tgt = <unsigned long>c_src
+ py_tgt = CULong()
+ (<unsigned long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLLong( self, _CLLong cobject ):
+ " set self from a CLLong instance"
+ (<long long*>self.p)[0] = <long long>(<long long*>cobject.p)[0]
+ def init_CLLong( self, _CLLong cobject ):
+ " set a CLLong instance from self "
+ (<long long*>cobject.p)[0] = <long long>(<long long*>self.p)[0]
+ def from_CULLong( self, _CULLong cobject ):
+ " set self from a CULLong instance"
+ (<long long*>self.p)[0] = <long long>(<unsigned long long*>cobject.p)[0]
+ def init_CULLong( self, _CULLong cobject ):
+ " set a CULLong instance from self "
+ (<unsigned long long*>cobject.p)[0] = <unsigned long long>(<long long*>self.p)[0]
+ def to_CULLong( self ):
+ " cast self to new CULLong instance "
+ cdef long long c_src
+ cdef unsigned long long c_tgt
+ cdef _CLLong py_src
+ cdef _CULLong py_tgt
+ c_src = (<long long*>self.p)[0]
+ c_tgt = <unsigned long long>c_src
+ py_tgt = CULLong()
+ (<unsigned long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CFloat( self, _CFloat cobject ):
+ " set self from a CFloat instance"
+ (<long long*>self.p)[0] = <long long>(<float*>cobject.p)[0]
+ def init_CFloat( self, _CFloat cobject ):
+ " set a CFloat instance from self "
+ (<float*>cobject.p)[0] = <float>(<long long*>self.p)[0]
+ def to_CFloat( self ):
+ " cast self to new CFloat instance "
+ cdef long long c_src
+ cdef float c_tgt
+ cdef _CLLong py_src
+ cdef _CFloat py_tgt
+ c_src = (<long long*>self.p)[0]
+ c_tgt = <float>c_src
+ py_tgt = CFloat()
+ (<float*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CDouble( self, _CDouble cobject ):
+ " set self from a CDouble instance"
+ (<long long*>self.p)[0] = <long long>(<double*>cobject.p)[0]
+ def init_CDouble( self, _CDouble cobject ):
+ " set a CDouble instance from self "
+ (<double*>cobject.p)[0] = <double>(<long long*>self.p)[0]
+ def to_CDouble( self ):
+ " cast self to new CDouble instance "
+ cdef long long c_src
+ cdef double c_tgt
+ cdef _CLLong py_src
+ cdef _CDouble py_tgt
+ c_src = (<long long*>self.p)[0]
+ c_tgt = <double>c_src
+ py_tgt = CDouble()
+ (<double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLDouble( self, _CLDouble cobject ):
+ " set self from a CLDouble instance"
+ (<long long*>self.p)[0] = <long long>(<long double*>cobject.p)[0]
+ def init_CLDouble( self, _CLDouble cobject ):
+ " set a CLDouble instance from self "
+ (<long double*>cobject.p)[0] = <long double>(<long long*>self.p)[0]
+ def to_CLDouble( self ):
+ " cast self to new CLDouble instance "
+ cdef long long c_src
+ cdef long double c_tgt
+ cdef _CLLong py_src
+ cdef _CLDouble py_tgt
+ c_src = (<long long*>self.p)[0]
+ c_tgt = <long double>c_src
+ py_tgt = CLDouble()
+ (<long double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+CLLong = Meta('CLLong', (_CLLong,), {} )
+CLLong.basetype = CLLong
+CLLong.sizeof = sizeof(long long)
+
+#cdef public class _CULLong(_CObject)[ object AdaptObj_CULLong, type AdaptType_CULLong ]:
+cdef class _CULLong(_CObject):
+ def __init__( self, value = None, addr = None ):
+ cdef unsigned long long*p
+ cdef long c_addr
+ cdef _CObject cobject
+ if addr is None:
+ p = <unsigned long long*>pyx_calloc(1,sizeof(unsigned long long))
+ self.p = <void*>p
+ if isinstance(value,CObject):
+ if isinstance(value,_CULLong):
+ cobject = value
+ p[0] = (<unsigned long long*>cobject.p)[0]
+ else:
+ value.init_CULLong(self)
+ elif value is not None:
+ p[0] = value
+ else:
+ p[0] = 0
+ self.malloced = True
+ else:
+ c_addr = addr
+ self.p = <void*>c_addr
+ self.malloced = False
+ def __int__( self ):
+ cdef unsigned long long *p
+ assert self.p != NULL
+ p = <unsigned long long*>self.p
+ return int(p[0])
+ def __float__( self ):
+ cdef unsigned long long *p
+ assert self.p != NULL
+ p = <unsigned long long*>self.p
+ return float(p[0])
+ def __add__( x, y ):
+ cdef unsigned long long val
+ cdef _CULLong self
+ if not isinstance(x,CULLong):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<unsigned long long*>self.p)[0]
+ val = val + y
+ return CULLong(val)
+ def __sub__( x, y ):
+ cdef unsigned long long val
+ cdef _CULLong self
+ if not isinstance(x,CULLong):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<unsigned long long*>self.p)[0]
+ val = val - y
+ return CULLong(val)
+ def __mul__( x, y ):
+ cdef unsigned long long val
+ cdef _CULLong self
+ if not isinstance(x,CULLong):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<unsigned long long*>self.p)[0]
+ val = val + y
+ return CULLong(val)
+ def __repr__( self ):
+ cdef long c_addr
+ cdef unsigned long long c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<unsigned long long*>self.p)[0]
+ value = ", value = %s" % str( c_val )
+ return "%s( addr = %s%s )" % (self.__class__.__name__, hex(c_addr), value)
+ def set_value( self, value ):
+ if isinstance(value,CObject):
+ value.init_CULLong(self)
+ else:
+ (<unsigned long long*>self.p)[0] = value
+ def get_value( self ):
+ return (<unsigned long long*>self.p)[0]
+ def to_CULLong(self):
+ return self
+ def init_from( self, _CULLong cobject ):
+ " init self from a CULLong instance "
+# if not self.is_compatible(cobject):
+ if not isinstance( cobject, CULLong ):
+ raise TypeError("%s cannot be set from %s" % (self.__class__, cobject))
+ (<unsigned long long*>self.p)[0] = (<unsigned long long*>cobject.p)[0]
+ def get_basetype( self ):
+ return CULLong
+# def sizeof( self ):
+# return sizeof(unsigned long long)
+# def is_compatible( self, _CObject cobject ):
+# return isinstance( cobject, CULLong )
+ def __str__( self ):
+ cdef long c_addr
+ cdef unsigned long long c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<unsigned long long*>self.p)[0]
+ value = str( c_val )
+ return value
+ def from_CChar( self, _CChar cobject ):
+ " set self from a CChar instance"
+ (<unsigned long long*>self.p)[0] = <unsigned long long>(<char*>cobject.p)[0]
+ def init_CChar( self, _CChar cobject ):
+ " set a CChar instance from self "
+ (<char*>cobject.p)[0] = <char>(<unsigned long long*>self.p)[0]
+ def to_CChar( self ):
+ " cast self to new CChar instance "
+ cdef unsigned long long c_src
+ cdef char c_tgt
+ cdef _CULLong py_src
+ cdef _CChar py_tgt
+ c_src = (<unsigned long long*>self.p)[0]
+ c_tgt = <char>c_src
+ py_tgt = CChar()
+ (<char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CSChar( self, _CSChar cobject ):
+ " set self from a CSChar instance"
+ (<unsigned long long*>self.p)[0] = <unsigned long long>(<signed char*>cobject.p)[0]
+ def init_CSChar( self, _CSChar cobject ):
+ " set a CSChar instance from self "
+ (<signed char*>cobject.p)[0] = <signed char>(<unsigned long long*>self.p)[0]
+ def to_CSChar( self ):
+ " cast self to new CSChar instance "
+ cdef unsigned long long c_src
+ cdef signed char c_tgt
+ cdef _CULLong py_src
+ cdef _CSChar py_tgt
+ c_src = (<unsigned long long*>self.p)[0]
+ c_tgt = <signed char>c_src
+ py_tgt = CSChar()
+ (<signed char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUChar( self, _CUChar cobject ):
+ " set self from a CUChar instance"
+ (<unsigned long long*>self.p)[0] = <unsigned long long>(<unsigned char*>cobject.p)[0]
+ def init_CUChar( self, _CUChar cobject ):
+ " set a CUChar instance from self "
+ (<unsigned char*>cobject.p)[0] = <unsigned char>(<unsigned long long*>self.p)[0]
+ def to_CUChar( self ):
+ " cast self to new CUChar instance "
+ cdef unsigned long long c_src
+ cdef unsigned char c_tgt
+ cdef _CULLong py_src
+ cdef _CUChar py_tgt
+ c_src = (<unsigned long long*>self.p)[0]
+ c_tgt = <unsigned char>c_src
+ py_tgt = CUChar()
+ (<unsigned char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CShort( self, _CShort cobject ):
+ " set self from a CShort instance"
+ (<unsigned long long*>self.p)[0] = <unsigned long long>(<short*>cobject.p)[0]
+ def init_CShort( self, _CShort cobject ):
+ " set a CShort instance from self "
+ (<short*>cobject.p)[0] = <short>(<unsigned long long*>self.p)[0]
+ def to_CShort( self ):
+ " cast self to new CShort instance "
+ cdef unsigned long long c_src
+ cdef short c_tgt
+ cdef _CULLong py_src
+ cdef _CShort py_tgt
+ c_src = (<unsigned long long*>self.p)[0]
+ c_tgt = <short>c_src
+ py_tgt = CShort()
+ (<short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUShort( self, _CUShort cobject ):
+ " set self from a CUShort instance"
+ (<unsigned long long*>self.p)[0] = <unsigned long long>(<unsigned short*>cobject.p)[0]
+ def init_CUShort( self, _CUShort cobject ):
+ " set a CUShort instance from self "
+ (<unsigned short*>cobject.p)[0] = <unsigned short>(<unsigned long long*>self.p)[0]
+ def to_CUShort( self ):
+ " cast self to new CUShort instance "
+ cdef unsigned long long c_src
+ cdef unsigned short c_tgt
+ cdef _CULLong py_src
+ cdef _CUShort py_tgt
+ c_src = (<unsigned long long*>self.p)[0]
+ c_tgt = <unsigned short>c_src
+ py_tgt = CUShort()
+ (<unsigned short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CInt( self, _CInt cobject ):
+ " set self from a CInt instance"
+ (<unsigned long long*>self.p)[0] = <unsigned long long>(<int*>cobject.p)[0]
+ def init_CInt( self, _CInt cobject ):
+ " set a CInt instance from self "
+ (<int*>cobject.p)[0] = <int>(<unsigned long long*>self.p)[0]
+ def to_CInt( self ):
+ " cast self to new CInt instance "
+ cdef unsigned long long c_src
+ cdef int c_tgt
+ cdef _CULLong py_src
+ cdef _CInt py_tgt
+ c_src = (<unsigned long long*>self.p)[0]
+ c_tgt = <int>c_src
+ py_tgt = CInt()
+ (<int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUInt( self, _CUInt cobject ):
+ " set self from a CUInt instance"
+ (<unsigned long long*>self.p)[0] = <unsigned long long>(<unsigned int*>cobject.p)[0]
+ def init_CUInt( self, _CUInt cobject ):
+ " set a CUInt instance from self "
+ (<unsigned int*>cobject.p)[0] = <unsigned int>(<unsigned long long*>self.p)[0]
+ def to_CUInt( self ):
+ " cast self to new CUInt instance "
+ cdef unsigned long long c_src
+ cdef unsigned int c_tgt
+ cdef _CULLong py_src
+ cdef _CUInt py_tgt
+ c_src = (<unsigned long long*>self.p)[0]
+ c_tgt = <unsigned int>c_src
+ py_tgt = CUInt()
+ (<unsigned int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLong( self, _CLong cobject ):
+ " set self from a CLong instance"
+ (<unsigned long long*>self.p)[0] = <unsigned long long>(<long*>cobject.p)[0]
+ def init_CLong( self, _CLong cobject ):
+ " set a CLong instance from self "
+ (<long*>cobject.p)[0] = <long>(<unsigned long long*>self.p)[0]
+ def to_CLong( self ):
+ " cast self to new CLong instance "
+ cdef unsigned long long c_src
+ cdef long c_tgt
+ cdef _CULLong py_src
+ cdef _CLong py_tgt
+ c_src = (<unsigned long long*>self.p)[0]
+ c_tgt = <long>c_src
+ py_tgt = CLong()
+ (<long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULong( self, _CULong cobject ):
+ " set self from a CULong instance"
+ (<unsigned long long*>self.p)[0] = <unsigned long long>(<unsigned long*>cobject.p)[0]
+ def init_CULong( self, _CULong cobject ):
+ " set a CULong instance from self "
+ (<unsigned long*>cobject.p)[0] = <unsigned long>(<unsigned long long*>self.p)[0]
+ def to_CULong( self ):
+ " cast self to new CULong instance "
+ cdef unsigned long long c_src
+ cdef unsigned long c_tgt
+ cdef _CULLong py_src
+ cdef _CULong py_tgt
+ c_src = (<unsigned long long*>self.p)[0]
+ c_tgt = <unsigned long>c_src
+ py_tgt = CULong()
+ (<unsigned long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLLong( self, _CLLong cobject ):
+ " set self from a CLLong instance"
+ (<unsigned long long*>self.p)[0] = <unsigned long long>(<long long*>cobject.p)[0]
+ def init_CLLong( self, _CLLong cobject ):
+ " set a CLLong instance from self "
+ (<long long*>cobject.p)[0] = <long long>(<unsigned long long*>self.p)[0]
+ def to_CLLong( self ):
+ " cast self to new CLLong instance "
+ cdef unsigned long long c_src
+ cdef long long c_tgt
+ cdef _CULLong py_src
+ cdef _CLLong py_tgt
+ c_src = (<unsigned long long*>self.p)[0]
+ c_tgt = <long long>c_src
+ py_tgt = CLLong()
+ (<long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULLong( self, _CULLong cobject ):
+ " set self from a CULLong instance"
+ (<unsigned long long*>self.p)[0] = <unsigned long long>(<unsigned long long*>cobject.p)[0]
+ def init_CULLong( self, _CULLong cobject ):
+ " set a CULLong instance from self "
+ (<unsigned long long*>cobject.p)[0] = <unsigned long long>(<unsigned long long*>self.p)[0]
+ def from_CFloat( self, _CFloat cobject ):
+ " set self from a CFloat instance"
+ (<unsigned long long*>self.p)[0] = <unsigned long long>(<float*>cobject.p)[0]
+ def init_CFloat( self, _CFloat cobject ):
+ " set a CFloat instance from self "
+ (<float*>cobject.p)[0] = <float>(<unsigned long long*>self.p)[0]
+ def to_CFloat( self ):
+ " cast self to new CFloat instance "
+ cdef unsigned long long c_src
+ cdef float c_tgt
+ cdef _CULLong py_src
+ cdef _CFloat py_tgt
+ c_src = (<unsigned long long*>self.p)[0]
+ c_tgt = <float>c_src
+ py_tgt = CFloat()
+ (<float*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CDouble( self, _CDouble cobject ):
+ " set self from a CDouble instance"
+ (<unsigned long long*>self.p)[0] = <unsigned long long>(<double*>cobject.p)[0]
+ def init_CDouble( self, _CDouble cobject ):
+ " set a CDouble instance from self "
+ (<double*>cobject.p)[0] = <double>(<unsigned long long*>self.p)[0]
+ def to_CDouble( self ):
+ " cast self to new CDouble instance "
+ cdef unsigned long long c_src
+ cdef double c_tgt
+ cdef _CULLong py_src
+ cdef _CDouble py_tgt
+ c_src = (<unsigned long long*>self.p)[0]
+ c_tgt = <double>c_src
+ py_tgt = CDouble()
+ (<double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLDouble( self, _CLDouble cobject ):
+ " set self from a CLDouble instance"
+ (<unsigned long long*>self.p)[0] = <unsigned long long>(<long double*>cobject.p)[0]
+ def init_CLDouble( self, _CLDouble cobject ):
+ " set a CLDouble instance from self "
+ (<long double*>cobject.p)[0] = <long double>(<unsigned long long*>self.p)[0]
+ def to_CLDouble( self ):
+ " cast self to new CLDouble instance "
+ cdef unsigned long long c_src
+ cdef long double c_tgt
+ cdef _CULLong py_src
+ cdef _CLDouble py_tgt
+ c_src = (<unsigned long long*>self.p)[0]
+ c_tgt = <long double>c_src
+ py_tgt = CLDouble()
+ (<long double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+CULLong = Meta('CULLong', (_CULLong,), {} )
+CULLong.basetype = CULLong
+CULLong.sizeof = sizeof(unsigned long long)
+
+#cdef public class _CFloat(_CObject)[ object AdaptObj_CFloat, type AdaptType_CFloat ]:
+cdef class _CFloat(_CObject):
+ def __init__( self, value = None, addr = None ):
+ cdef float*p
+ cdef long c_addr
+ cdef _CObject cobject
+ if addr is None:
+ p = <float*>pyx_calloc(1,sizeof(float))
+ self.p = <void*>p
+ if isinstance(value,CObject):
+ if isinstance(value,_CFloat):
+ cobject = value
+ p[0] = (<float*>cobject.p)[0]
+ else:
+ value.init_CFloat(self)
+ elif value is not None:
+ p[0] = value
+ else:
+ p[0] = 0
+ self.malloced = True
+ else:
+ c_addr = addr
+ self.p = <void*>c_addr
+ self.malloced = False
+ def __int__( self ):
+ cdef float *p
+ assert self.p != NULL
+ p = <float*>self.p
+ return int(p[0])
+ def __float__( self ):
+ cdef float *p
+ assert self.p != NULL
+ p = <float*>self.p
+ return float(p[0])
+ def __add__( x, y ):
+ cdef float val
+ cdef _CFloat self
+ if not isinstance(x,CFloat):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<float*>self.p)[0]
+ val = val + y
+ return CFloat(val)
+ def __sub__( x, y ):
+ cdef float val
+ cdef _CFloat self
+ if not isinstance(x,CFloat):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<float*>self.p)[0]
+ val = val - y
+ return CFloat(val)
+ def __mul__( x, y ):
+ cdef float val
+ cdef _CFloat self
+ if not isinstance(x,CFloat):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<float*>self.p)[0]
+ val = val + y
+ return CFloat(val)
+ def __repr__( self ):
+ cdef long c_addr
+ cdef float c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<float*>self.p)[0]
+ value = ", value = %s" % str( c_val )
+ return "%s( addr = %s%s )" % (self.__class__.__name__, hex(c_addr), value)
+ def set_value( self, value ):
+ if isinstance(value,CObject):
+ value.init_CFloat(self)
+ else:
+ (<float*>self.p)[0] = value
+ def get_value( self ):
+ return (<float*>self.p)[0]
+ def to_CFloat(self):
+ return self
+ def init_from( self, _CFloat cobject ):
+ " init self from a CFloat instance "
+# if not self.is_compatible(cobject):
+ if not isinstance( cobject, CFloat ):
+ raise TypeError("%s cannot be set from %s" % (self.__class__, cobject))
+ (<float*>self.p)[0] = (<float*>cobject.p)[0]
+ def get_basetype( self ):
+ return CFloat
+# def sizeof( self ):
+# return sizeof(float)
+# def is_compatible( self, _CObject cobject ):
+# return isinstance( cobject, CFloat )
+ def __str__( self ):
+ cdef long c_addr
+ cdef float c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<float*>self.p)[0]
+ value = str( c_val )
+ return value
+ def from_CChar( self, _CChar cobject ):
+ " set self from a CChar instance"
+ (<float*>self.p)[0] = <float>(<char*>cobject.p)[0]
+ def init_CChar( self, _CChar cobject ):
+ " set a CChar instance from self "
+ (<char*>cobject.p)[0] = <char>(<float*>self.p)[0]
+ def to_CChar( self ):
+ " cast self to new CChar instance "
+ cdef float c_src
+ cdef char c_tgt
+ cdef _CFloat py_src
+ cdef _CChar py_tgt
+ c_src = (<float*>self.p)[0]
+ c_tgt = <char>c_src
+ py_tgt = CChar()
+ (<char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CSChar( self, _CSChar cobject ):
+ " set self from a CSChar instance"
+ (<float*>self.p)[0] = <float>(<signed char*>cobject.p)[0]
+ def init_CSChar( self, _CSChar cobject ):
+ " set a CSChar instance from self "
+ (<signed char*>cobject.p)[0] = <signed char>(<float*>self.p)[0]
+ def to_CSChar( self ):
+ " cast self to new CSChar instance "
+ cdef float c_src
+ cdef signed char c_tgt
+ cdef _CFloat py_src
+ cdef _CSChar py_tgt
+ c_src = (<float*>self.p)[0]
+ c_tgt = <signed char>c_src
+ py_tgt = CSChar()
+ (<signed char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUChar( self, _CUChar cobject ):
+ " set self from a CUChar instance"
+ (<float*>self.p)[0] = <float>(<unsigned char*>cobject.p)[0]
+ def init_CUChar( self, _CUChar cobject ):
+ " set a CUChar instance from self "
+ (<unsigned char*>cobject.p)[0] = <unsigned char>(<float*>self.p)[0]
+ def to_CUChar( self ):
+ " cast self to new CUChar instance "
+ cdef float c_src
+ cdef unsigned char c_tgt
+ cdef _CFloat py_src
+ cdef _CUChar py_tgt
+ c_src = (<float*>self.p)[0]
+ c_tgt = <unsigned char>c_src
+ py_tgt = CUChar()
+ (<unsigned char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CShort( self, _CShort cobject ):
+ " set self from a CShort instance"
+ (<float*>self.p)[0] = <float>(<short*>cobject.p)[0]
+ def init_CShort( self, _CShort cobject ):
+ " set a CShort instance from self "
+ (<short*>cobject.p)[0] = <short>(<float*>self.p)[0]
+ def to_CShort( self ):
+ " cast self to new CShort instance "
+ cdef float c_src
+ cdef short c_tgt
+ cdef _CFloat py_src
+ cdef _CShort py_tgt
+ c_src = (<float*>self.p)[0]
+ c_tgt = <short>c_src
+ py_tgt = CShort()
+ (<short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUShort( self, _CUShort cobject ):
+ " set self from a CUShort instance"
+ (<float*>self.p)[0] = <float>(<unsigned short*>cobject.p)[0]
+ def init_CUShort( self, _CUShort cobject ):
+ " set a CUShort instance from self "
+ (<unsigned short*>cobject.p)[0] = <unsigned short>(<float*>self.p)[0]
+ def to_CUShort( self ):
+ " cast self to new CUShort instance "
+ cdef float c_src
+ cdef unsigned short c_tgt
+ cdef _CFloat py_src
+ cdef _CUShort py_tgt
+ c_src = (<float*>self.p)[0]
+ c_tgt = <unsigned short>c_src
+ py_tgt = CUShort()
+ (<unsigned short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CInt( self, _CInt cobject ):
+ " set self from a CInt instance"
+ (<float*>self.p)[0] = <float>(<int*>cobject.p)[0]
+ def init_CInt( self, _CInt cobject ):
+ " set a CInt instance from self "
+ (<int*>cobject.p)[0] = <int>(<float*>self.p)[0]
+ def to_CInt( self ):
+ " cast self to new CInt instance "
+ cdef float c_src
+ cdef int c_tgt
+ cdef _CFloat py_src
+ cdef _CInt py_tgt
+ c_src = (<float*>self.p)[0]
+ c_tgt = <int>c_src
+ py_tgt = CInt()
+ (<int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUInt( self, _CUInt cobject ):
+ " set self from a CUInt instance"
+ (<float*>self.p)[0] = <float>(<unsigned int*>cobject.p)[0]
+ def init_CUInt( self, _CUInt cobject ):
+ " set a CUInt instance from self "
+ (<unsigned int*>cobject.p)[0] = <unsigned int>(<float*>self.p)[0]
+ def to_CUInt( self ):
+ " cast self to new CUInt instance "
+ cdef float c_src
+ cdef unsigned int c_tgt
+ cdef _CFloat py_src
+ cdef _CUInt py_tgt
+ c_src = (<float*>self.p)[0]
+ c_tgt = <unsigned int>c_src
+ py_tgt = CUInt()
+ (<unsigned int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLong( self, _CLong cobject ):
+ " set self from a CLong instance"
+ (<float*>self.p)[0] = <float>(<long*>cobject.p)[0]
+ def init_CLong( self, _CLong cobject ):
+ " set a CLong instance from self "
+ (<long*>cobject.p)[0] = <long>(<float*>self.p)[0]
+ def to_CLong( self ):
+ " cast self to new CLong instance "
+ cdef float c_src
+ cdef long c_tgt
+ cdef _CFloat py_src
+ cdef _CLong py_tgt
+ c_src = (<float*>self.p)[0]
+ c_tgt = <long>c_src
+ py_tgt = CLong()
+ (<long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULong( self, _CULong cobject ):
+ " set self from a CULong instance"
+ (<float*>self.p)[0] = <float>(<unsigned long*>cobject.p)[0]
+ def init_CULong( self, _CULong cobject ):
+ " set a CULong instance from self "
+ (<unsigned long*>cobject.p)[0] = <unsigned long>(<float*>self.p)[0]
+ def to_CULong( self ):
+ " cast self to new CULong instance "
+ cdef float c_src
+ cdef unsigned long c_tgt
+ cdef _CFloat py_src
+ cdef _CULong py_tgt
+ c_src = (<float*>self.p)[0]
+ c_tgt = <unsigned long>c_src
+ py_tgt = CULong()
+ (<unsigned long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLLong( self, _CLLong cobject ):
+ " set self from a CLLong instance"
+ (<float*>self.p)[0] = <float>(<long long*>cobject.p)[0]
+ def init_CLLong( self, _CLLong cobject ):
+ " set a CLLong instance from self "
+ (<long long*>cobject.p)[0] = <long long>(<float*>self.p)[0]
+ def to_CLLong( self ):
+ " cast self to new CLLong instance "
+ cdef float c_src
+ cdef long long c_tgt
+ cdef _CFloat py_src
+ cdef _CLLong py_tgt
+ c_src = (<float*>self.p)[0]
+ c_tgt = <long long>c_src
+ py_tgt = CLLong()
+ (<long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULLong( self, _CULLong cobject ):
+ " set self from a CULLong instance"
+ (<float*>self.p)[0] = <float>(<unsigned long long*>cobject.p)[0]
+ def init_CULLong( self, _CULLong cobject ):
+ " set a CULLong instance from self "
+ (<unsigned long long*>cobject.p)[0] = <unsigned long long>(<float*>self.p)[0]
+ def to_CULLong( self ):
+ " cast self to new CULLong instance "
+ cdef float c_src
+ cdef unsigned long long c_tgt
+ cdef _CFloat py_src
+ cdef _CULLong py_tgt
+ c_src = (<float*>self.p)[0]
+ c_tgt = <unsigned long long>c_src
+ py_tgt = CULLong()
+ (<unsigned long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CFloat( self, _CFloat cobject ):
+ " set self from a CFloat instance"
+ (<float*>self.p)[0] = <float>(<float*>cobject.p)[0]
+ def init_CFloat( self, _CFloat cobject ):
+ " set a CFloat instance from self "
+ (<float*>cobject.p)[0] = <float>(<float*>self.p)[0]
+ def from_CDouble( self, _CDouble cobject ):
+ " set self from a CDouble instance"
+ (<float*>self.p)[0] = <float>(<double*>cobject.p)[0]
+ def init_CDouble( self, _CDouble cobject ):
+ " set a CDouble instance from self "
+ (<double*>cobject.p)[0] = <double>(<float*>self.p)[0]
+ def to_CDouble( self ):
+ " cast self to new CDouble instance "
+ cdef float c_src
+ cdef double c_tgt
+ cdef _CFloat py_src
+ cdef _CDouble py_tgt
+ c_src = (<float*>self.p)[0]
+ c_tgt = <double>c_src
+ py_tgt = CDouble()
+ (<double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLDouble( self, _CLDouble cobject ):
+ " set self from a CLDouble instance"
+ (<float*>self.p)[0] = <float>(<long double*>cobject.p)[0]
+ def init_CLDouble( self, _CLDouble cobject ):
+ " set a CLDouble instance from self "
+ (<long double*>cobject.p)[0] = <long double>(<float*>self.p)[0]
+ def to_CLDouble( self ):
+ " cast self to new CLDouble instance "
+ cdef float c_src
+ cdef long double c_tgt
+ cdef _CFloat py_src
+ cdef _CLDouble py_tgt
+ c_src = (<float*>self.p)[0]
+ c_tgt = <long double>c_src
+ py_tgt = CLDouble()
+ (<long double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+CFloat = Meta('CFloat', (_CFloat,), {} )
+CFloat.basetype = CFloat
+CFloat.sizeof = sizeof(float)
+
+#cdef public class _CDouble(_CObject)[ object AdaptObj_CDouble, type AdaptType_CDouble ]:
+cdef class _CDouble(_CObject):
+ def __init__( self, value = None, addr = None ):
+ cdef double*p
+ cdef long c_addr
+ cdef _CObject cobject
+ if addr is None:
+ p = <double*>pyx_calloc(1,sizeof(double))
+ self.p = <void*>p
+ if isinstance(value,CObject):
+ if isinstance(value,_CDouble):
+ cobject = value
+ p[0] = (<double*>cobject.p)[0]
+ else:
+ value.init_CDouble(self)
+ elif value is not None:
+ p[0] = value
+ else:
+ p[0] = 0
+ self.malloced = True
+ else:
+ c_addr = addr
+ self.p = <void*>c_addr
+ self.malloced = False
+ def __int__( self ):
+ cdef double *p
+ assert self.p != NULL
+ p = <double*>self.p
+ return int(p[0])
+ def __float__( self ):
+ cdef double *p
+ assert self.p != NULL
+ p = <double*>self.p
+ return float(p[0])
+ def __add__( x, y ):
+ cdef double val
+ cdef _CDouble self
+ if not isinstance(x,CDouble):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<double*>self.p)[0]
+ val = val + y
+ return CDouble(val)
+ def __sub__( x, y ):
+ cdef double val
+ cdef _CDouble self
+ if not isinstance(x,CDouble):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<double*>self.p)[0]
+ val = val - y
+ return CDouble(val)
+ def __mul__( x, y ):
+ cdef double val
+ cdef _CDouble self
+ if not isinstance(x,CDouble):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<double*>self.p)[0]
+ val = val + y
+ return CDouble(val)
+ def __repr__( self ):
+ cdef long c_addr
+ cdef double c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<double*>self.p)[0]
+ value = ", value = %s" % str( c_val )
+ return "%s( addr = %s%s )" % (self.__class__.__name__, hex(c_addr), value)
+ def set_value( self, value ):
+ if isinstance(value,CObject):
+ value.init_CDouble(self)
+ else:
+ (<double*>self.p)[0] = value
+ def get_value( self ):
+ return (<double*>self.p)[0]
+ def to_CDouble(self):
+ return self
+ def init_from( self, _CDouble cobject ):
+ " init self from a CDouble instance "
+# if not self.is_compatible(cobject):
+ if not isinstance( cobject, CDouble ):
+ raise TypeError("%s cannot be set from %s" % (self.__class__, cobject))
+ (<double*>self.p)[0] = (<double*>cobject.p)[0]
+ def get_basetype( self ):
+ return CDouble
+# def sizeof( self ):
+# return sizeof(double)
+# def is_compatible( self, _CObject cobject ):
+# return isinstance( cobject, CDouble )
+ def __str__( self ):
+ cdef long c_addr
+ cdef double c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<double*>self.p)[0]
+ value = str( c_val )
+ return value
+ def from_CChar( self, _CChar cobject ):
+ " set self from a CChar instance"
+ (<double*>self.p)[0] = <double>(<char*>cobject.p)[0]
+ def init_CChar( self, _CChar cobject ):
+ " set a CChar instance from self "
+ (<char*>cobject.p)[0] = <char>(<double*>self.p)[0]
+ def to_CChar( self ):
+ " cast self to new CChar instance "
+ cdef double c_src
+ cdef char c_tgt
+ cdef _CDouble py_src
+ cdef _CChar py_tgt
+ c_src = (<double*>self.p)[0]
+ c_tgt = <char>c_src
+ py_tgt = CChar()
+ (<char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CSChar( self, _CSChar cobject ):
+ " set self from a CSChar instance"
+ (<double*>self.p)[0] = <double>(<signed char*>cobject.p)[0]
+ def init_CSChar( self, _CSChar cobject ):
+ " set a CSChar instance from self "
+ (<signed char*>cobject.p)[0] = <signed char>(<double*>self.p)[0]
+ def to_CSChar( self ):
+ " cast self to new CSChar instance "
+ cdef double c_src
+ cdef signed char c_tgt
+ cdef _CDouble py_src
+ cdef _CSChar py_tgt
+ c_src = (<double*>self.p)[0]
+ c_tgt = <signed char>c_src
+ py_tgt = CSChar()
+ (<signed char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUChar( self, _CUChar cobject ):
+ " set self from a CUChar instance"
+ (<double*>self.p)[0] = <double>(<unsigned char*>cobject.p)[0]
+ def init_CUChar( self, _CUChar cobject ):
+ " set a CUChar instance from self "
+ (<unsigned char*>cobject.p)[0] = <unsigned char>(<double*>self.p)[0]
+ def to_CUChar( self ):
+ " cast self to new CUChar instance "
+ cdef double c_src
+ cdef unsigned char c_tgt
+ cdef _CDouble py_src
+ cdef _CUChar py_tgt
+ c_src = (<double*>self.p)[0]
+ c_tgt = <unsigned char>c_src
+ py_tgt = CUChar()
+ (<unsigned char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CShort( self, _CShort cobject ):
+ " set self from a CShort instance"
+ (<double*>self.p)[0] = <double>(<short*>cobject.p)[0]
+ def init_CShort( self, _CShort cobject ):
+ " set a CShort instance from self "
+ (<short*>cobject.p)[0] = <short>(<double*>self.p)[0]
+ def to_CShort( self ):
+ " cast self to new CShort instance "
+ cdef double c_src
+ cdef short c_tgt
+ cdef _CDouble py_src
+ cdef _CShort py_tgt
+ c_src = (<double*>self.p)[0]
+ c_tgt = <short>c_src
+ py_tgt = CShort()
+ (<short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUShort( self, _CUShort cobject ):
+ " set self from a CUShort instance"
+ (<double*>self.p)[0] = <double>(<unsigned short*>cobject.p)[0]
+ def init_CUShort( self, _CUShort cobject ):
+ " set a CUShort instance from self "
+ (<unsigned short*>cobject.p)[0] = <unsigned short>(<double*>self.p)[0]
+ def to_CUShort( self ):
+ " cast self to new CUShort instance "
+ cdef double c_src
+ cdef unsigned short c_tgt
+ cdef _CDouble py_src
+ cdef _CUShort py_tgt
+ c_src = (<double*>self.p)[0]
+ c_tgt = <unsigned short>c_src
+ py_tgt = CUShort()
+ (<unsigned short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CInt( self, _CInt cobject ):
+ " set self from a CInt instance"
+ (<double*>self.p)[0] = <double>(<int*>cobject.p)[0]
+ def init_CInt( self, _CInt cobject ):
+ " set a CInt instance from self "
+ (<int*>cobject.p)[0] = <int>(<double*>self.p)[0]
+ def to_CInt( self ):
+ " cast self to new CInt instance "
+ cdef double c_src
+ cdef int c_tgt
+ cdef _CDouble py_src
+ cdef _CInt py_tgt
+ c_src = (<double*>self.p)[0]
+ c_tgt = <int>c_src
+ py_tgt = CInt()
+ (<int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUInt( self, _CUInt cobject ):
+ " set self from a CUInt instance"
+ (<double*>self.p)[0] = <double>(<unsigned int*>cobject.p)[0]
+ def init_CUInt( self, _CUInt cobject ):
+ " set a CUInt instance from self "
+ (<unsigned int*>cobject.p)[0] = <unsigned int>(<double*>self.p)[0]
+ def to_CUInt( self ):
+ " cast self to new CUInt instance "
+ cdef double c_src
+ cdef unsigned int c_tgt
+ cdef _CDouble py_src
+ cdef _CUInt py_tgt
+ c_src = (<double*>self.p)[0]
+ c_tgt = <unsigned int>c_src
+ py_tgt = CUInt()
+ (<unsigned int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLong( self, _CLong cobject ):
+ " set self from a CLong instance"
+ (<double*>self.p)[0] = <double>(<long*>cobject.p)[0]
+ def init_CLong( self, _CLong cobject ):
+ " set a CLong instance from self "
+ (<long*>cobject.p)[0] = <long>(<double*>self.p)[0]
+ def to_CLong( self ):
+ " cast self to new CLong instance "
+ cdef double c_src
+ cdef long c_tgt
+ cdef _CDouble py_src
+ cdef _CLong py_tgt
+ c_src = (<double*>self.p)[0]
+ c_tgt = <long>c_src
+ py_tgt = CLong()
+ (<long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULong( self, _CULong cobject ):
+ " set self from a CULong instance"
+ (<double*>self.p)[0] = <double>(<unsigned long*>cobject.p)[0]
+ def init_CULong( self, _CULong cobject ):
+ " set a CULong instance from self "
+ (<unsigned long*>cobject.p)[0] = <unsigned long>(<double*>self.p)[0]
+ def to_CULong( self ):
+ " cast self to new CULong instance "
+ cdef double c_src
+ cdef unsigned long c_tgt
+ cdef _CDouble py_src
+ cdef _CULong py_tgt
+ c_src = (<double*>self.p)[0]
+ c_tgt = <unsigned long>c_src
+ py_tgt = CULong()
+ (<unsigned long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLLong( self, _CLLong cobject ):
+ " set self from a CLLong instance"
+ (<double*>self.p)[0] = <double>(<long long*>cobject.p)[0]
+ def init_CLLong( self, _CLLong cobject ):
+ " set a CLLong instance from self "
+ (<long long*>cobject.p)[0] = <long long>(<double*>self.p)[0]
+ def to_CLLong( self ):
+ " cast self to new CLLong instance "
+ cdef double c_src
+ cdef long long c_tgt
+ cdef _CDouble py_src
+ cdef _CLLong py_tgt
+ c_src = (<double*>self.p)[0]
+ c_tgt = <long long>c_src
+ py_tgt = CLLong()
+ (<long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULLong( self, _CULLong cobject ):
+ " set self from a CULLong instance"
+ (<double*>self.p)[0] = <double>(<unsigned long long*>cobject.p)[0]
+ def init_CULLong( self, _CULLong cobject ):
+ " set a CULLong instance from self "
+ (<unsigned long long*>cobject.p)[0] = <unsigned long long>(<double*>self.p)[0]
+ def to_CULLong( self ):
+ " cast self to new CULLong instance "
+ cdef double c_src
+ cdef unsigned long long c_tgt
+ cdef _CDouble py_src
+ cdef _CULLong py_tgt
+ c_src = (<double*>self.p)[0]
+ c_tgt = <unsigned long long>c_src
+ py_tgt = CULLong()
+ (<unsigned long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CFloat( self, _CFloat cobject ):
+ " set self from a CFloat instance"
+ (<double*>self.p)[0] = <double>(<float*>cobject.p)[0]
+ def init_CFloat( self, _CFloat cobject ):
+ " set a CFloat instance from self "
+ (<float*>cobject.p)[0] = <float>(<double*>self.p)[0]
+ def to_CFloat( self ):
+ " cast self to new CFloat instance "
+ cdef double c_src
+ cdef float c_tgt
+ cdef _CDouble py_src
+ cdef _CFloat py_tgt
+ c_src = (<double*>self.p)[0]
+ c_tgt = <float>c_src
+ py_tgt = CFloat()
+ (<float*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CDouble( self, _CDouble cobject ):
+ " set self from a CDouble instance"
+ (<double*>self.p)[0] = <double>(<double*>cobject.p)[0]
+ def init_CDouble( self, _CDouble cobject ):
+ " set a CDouble instance from self "
+ (<double*>cobject.p)[0] = <double>(<double*>self.p)[0]
+ def from_CLDouble( self, _CLDouble cobject ):
+ " set self from a CLDouble instance"
+ (<double*>self.p)[0] = <double>(<long double*>cobject.p)[0]
+ def init_CLDouble( self, _CLDouble cobject ):
+ " set a CLDouble instance from self "
+ (<long double*>cobject.p)[0] = <long double>(<double*>self.p)[0]
+ def to_CLDouble( self ):
+ " cast self to new CLDouble instance "
+ cdef double c_src
+ cdef long double c_tgt
+ cdef _CDouble py_src
+ cdef _CLDouble py_tgt
+ c_src = (<double*>self.p)[0]
+ c_tgt = <long double>c_src
+ py_tgt = CLDouble()
+ (<long double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+CDouble = Meta('CDouble', (_CDouble,), {} )
+CDouble.basetype = CDouble
+CDouble.sizeof = sizeof(double)
+
+#cdef public class _CLDouble(_CObject)[ object AdaptObj_CLDouble, type AdaptType_CLDouble ]:
+cdef class _CLDouble(_CObject):
+ def __init__( self, value = None, addr = None ):
+ cdef long double*p
+ cdef long c_addr
+ cdef _CObject cobject
+ if addr is None:
+ p = <long double*>pyx_calloc(1,sizeof(long double))
+ self.p = <void*>p
+ if isinstance(value,CObject):
+ if isinstance(value,_CLDouble):
+ cobject = value
+ p[0] = (<long double*>cobject.p)[0]
+ else:
+ value.init_CLDouble(self)
+ elif value is not None:
+ p[0] = value
+ else:
+ p[0] = 0
+ self.malloced = True
+ else:
+ c_addr = addr
+ self.p = <void*>c_addr
+ self.malloced = False
+ def __int__( self ):
+ cdef long double *p
+ assert self.p != NULL
+ p = <long double*>self.p
+ return int(p[0])
+ def __float__( self ):
+ cdef long double *p
+ assert self.p != NULL
+ p = <long double*>self.p
+ return float(p[0])
+ def __add__( x, y ):
+ cdef long double val
+ cdef _CLDouble self
+ if not isinstance(x,CLDouble):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<long double*>self.p)[0]
+ val = val + y
+ return CLDouble(val)
+ def __sub__( x, y ):
+ cdef long double val
+ cdef _CLDouble self
+ if not isinstance(x,CLDouble):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<long double*>self.p)[0]
+ val = val - y
+ return CLDouble(val)
+ def __mul__( x, y ):
+ cdef long double val
+ cdef _CLDouble self
+ if not isinstance(x,CLDouble):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<long double*>self.p)[0]
+ val = val + y
+ return CLDouble(val)
+ def __repr__( self ):
+ cdef long c_addr
+ cdef long double c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<long double*>self.p)[0]
+ value = ", value = %s" % str( c_val )
+ return "%s( addr = %s%s )" % (self.__class__.__name__, hex(c_addr), value)
+ def set_value( self, value ):
+ if isinstance(value,CObject):
+ value.init_CLDouble(self)
+ else:
+ (<long double*>self.p)[0] = value
+ def get_value( self ):
+ return (<long double*>self.p)[0]
+ def to_CLDouble(self):
+ return self
+ def init_from( self, _CLDouble cobject ):
+ " init self from a CLDouble instance "
+# if not self.is_compatible(cobject):
+ if not isinstance( cobject, CLDouble ):
+ raise TypeError("%s cannot be set from %s" % (self.__class__, cobject))
+ (<long double*>self.p)[0] = (<long double*>cobject.p)[0]
+ def get_basetype( self ):
+ return CLDouble
+# def sizeof( self ):
+# return sizeof(long double)
+# def is_compatible( self, _CObject cobject ):
+# return isinstance( cobject, CLDouble )
+ def __str__( self ):
+ cdef long c_addr
+ cdef long double c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<long double*>self.p)[0]
+ value = str( c_val )
+ return value
+ def from_CChar( self, _CChar cobject ):
+ " set self from a CChar instance"
+ (<long double*>self.p)[0] = <long double>(<char*>cobject.p)[0]
+ def init_CChar( self, _CChar cobject ):
+ " set a CChar instance from self "
+ (<char*>cobject.p)[0] = <char>(<long double*>self.p)[0]
+ def to_CChar( self ):
+ " cast self to new CChar instance "
+ cdef long double c_src
+ cdef char c_tgt
+ cdef _CLDouble py_src
+ cdef _CChar py_tgt
+ c_src = (<long double*>self.p)[0]
+ c_tgt = <char>c_src
+ py_tgt = CChar()
+ (<char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CSChar( self, _CSChar cobject ):
+ " set self from a CSChar instance"
+ (<long double*>self.p)[0] = <long double>(<signed char*>cobject.p)[0]
+ def init_CSChar( self, _CSChar cobject ):
+ " set a CSChar instance from self "
+ (<signed char*>cobject.p)[0] = <signed char>(<long double*>self.p)[0]
+ def to_CSChar( self ):
+ " cast self to new CSChar instance "
+ cdef long double c_src
+ cdef signed char c_tgt
+ cdef _CLDouble py_src
+ cdef _CSChar py_tgt
+ c_src = (<long double*>self.p)[0]
+ c_tgt = <signed char>c_src
+ py_tgt = CSChar()
+ (<signed char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUChar( self, _CUChar cobject ):
+ " set self from a CUChar instance"
+ (<long double*>self.p)[0] = <long double>(<unsigned char*>cobject.p)[0]
+ def init_CUChar( self, _CUChar cobject ):
+ " set a CUChar instance from self "
+ (<unsigned char*>cobject.p)[0] = <unsigned char>(<long double*>self.p)[0]
+ def to_CUChar( self ):
+ " cast self to new CUChar instance "
+ cdef long double c_src
+ cdef unsigned char c_tgt
+ cdef _CLDouble py_src
+ cdef _CUChar py_tgt
+ c_src = (<long double*>self.p)[0]
+ c_tgt = <unsigned char>c_src
+ py_tgt = CUChar()
+ (<unsigned char*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CShort( self, _CShort cobject ):
+ " set self from a CShort instance"
+ (<long double*>self.p)[0] = <long double>(<short*>cobject.p)[0]
+ def init_CShort( self, _CShort cobject ):
+ " set a CShort instance from self "
+ (<short*>cobject.p)[0] = <short>(<long double*>self.p)[0]
+ def to_CShort( self ):
+ " cast self to new CShort instance "
+ cdef long double c_src
+ cdef short c_tgt
+ cdef _CLDouble py_src
+ cdef _CShort py_tgt
+ c_src = (<long double*>self.p)[0]
+ c_tgt = <short>c_src
+ py_tgt = CShort()
+ (<short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUShort( self, _CUShort cobject ):
+ " set self from a CUShort instance"
+ (<long double*>self.p)[0] = <long double>(<unsigned short*>cobject.p)[0]
+ def init_CUShort( self, _CUShort cobject ):
+ " set a CUShort instance from self "
+ (<unsigned short*>cobject.p)[0] = <unsigned short>(<long double*>self.p)[0]
+ def to_CUShort( self ):
+ " cast self to new CUShort instance "
+ cdef long double c_src
+ cdef unsigned short c_tgt
+ cdef _CLDouble py_src
+ cdef _CUShort py_tgt
+ c_src = (<long double*>self.p)[0]
+ c_tgt = <unsigned short>c_src
+ py_tgt = CUShort()
+ (<unsigned short*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CInt( self, _CInt cobject ):
+ " set self from a CInt instance"
+ (<long double*>self.p)[0] = <long double>(<int*>cobject.p)[0]
+ def init_CInt( self, _CInt cobject ):
+ " set a CInt instance from self "
+ (<int*>cobject.p)[0] = <int>(<long double*>self.p)[0]
+ def to_CInt( self ):
+ " cast self to new CInt instance "
+ cdef long double c_src
+ cdef int c_tgt
+ cdef _CLDouble py_src
+ cdef _CInt py_tgt
+ c_src = (<long double*>self.p)[0]
+ c_tgt = <int>c_src
+ py_tgt = CInt()
+ (<int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CUInt( self, _CUInt cobject ):
+ " set self from a CUInt instance"
+ (<long double*>self.p)[0] = <long double>(<unsigned int*>cobject.p)[0]
+ def init_CUInt( self, _CUInt cobject ):
+ " set a CUInt instance from self "
+ (<unsigned int*>cobject.p)[0] = <unsigned int>(<long double*>self.p)[0]
+ def to_CUInt( self ):
+ " cast self to new CUInt instance "
+ cdef long double c_src
+ cdef unsigned int c_tgt
+ cdef _CLDouble py_src
+ cdef _CUInt py_tgt
+ c_src = (<long double*>self.p)[0]
+ c_tgt = <unsigned int>c_src
+ py_tgt = CUInt()
+ (<unsigned int*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLong( self, _CLong cobject ):
+ " set self from a CLong instance"
+ (<long double*>self.p)[0] = <long double>(<long*>cobject.p)[0]
+ def init_CLong( self, _CLong cobject ):
+ " set a CLong instance from self "
+ (<long*>cobject.p)[0] = <long>(<long double*>self.p)[0]
+ def to_CLong( self ):
+ " cast self to new CLong instance "
+ cdef long double c_src
+ cdef long c_tgt
+ cdef _CLDouble py_src
+ cdef _CLong py_tgt
+ c_src = (<long double*>self.p)[0]
+ c_tgt = <long>c_src
+ py_tgt = CLong()
+ (<long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULong( self, _CULong cobject ):
+ " set self from a CULong instance"
+ (<long double*>self.p)[0] = <long double>(<unsigned long*>cobject.p)[0]
+ def init_CULong( self, _CULong cobject ):
+ " set a CULong instance from self "
+ (<unsigned long*>cobject.p)[0] = <unsigned long>(<long double*>self.p)[0]
+ def to_CULong( self ):
+ " cast self to new CULong instance "
+ cdef long double c_src
+ cdef unsigned long c_tgt
+ cdef _CLDouble py_src
+ cdef _CULong py_tgt
+ c_src = (<long double*>self.p)[0]
+ c_tgt = <unsigned long>c_src
+ py_tgt = CULong()
+ (<unsigned long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLLong( self, _CLLong cobject ):
+ " set self from a CLLong instance"
+ (<long double*>self.p)[0] = <long double>(<long long*>cobject.p)[0]
+ def init_CLLong( self, _CLLong cobject ):
+ " set a CLLong instance from self "
+ (<long long*>cobject.p)[0] = <long long>(<long double*>self.p)[0]
+ def to_CLLong( self ):
+ " cast self to new CLLong instance "
+ cdef long double c_src
+ cdef long long c_tgt
+ cdef _CLDouble py_src
+ cdef _CLLong py_tgt
+ c_src = (<long double*>self.p)[0]
+ c_tgt = <long long>c_src
+ py_tgt = CLLong()
+ (<long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CULLong( self, _CULLong cobject ):
+ " set self from a CULLong instance"
+ (<long double*>self.p)[0] = <long double>(<unsigned long long*>cobject.p)[0]
+ def init_CULLong( self, _CULLong cobject ):
+ " set a CULLong instance from self "
+ (<unsigned long long*>cobject.p)[0] = <unsigned long long>(<long double*>self.p)[0]
+ def to_CULLong( self ):
+ " cast self to new CULLong instance "
+ cdef long double c_src
+ cdef unsigned long long c_tgt
+ cdef _CLDouble py_src
+ cdef _CULLong py_tgt
+ c_src = (<long double*>self.p)[0]
+ c_tgt = <unsigned long long>c_src
+ py_tgt = CULLong()
+ (<unsigned long long*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CFloat( self, _CFloat cobject ):
+ " set self from a CFloat instance"
+ (<long double*>self.p)[0] = <long double>(<float*>cobject.p)[0]
+ def init_CFloat( self, _CFloat cobject ):
+ " set a CFloat instance from self "
+ (<float*>cobject.p)[0] = <float>(<long double*>self.p)[0]
+ def to_CFloat( self ):
+ " cast self to new CFloat instance "
+ cdef long double c_src
+ cdef float c_tgt
+ cdef _CLDouble py_src
+ cdef _CFloat py_tgt
+ c_src = (<long double*>self.p)[0]
+ c_tgt = <float>c_src
+ py_tgt = CFloat()
+ (<float*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CDouble( self, _CDouble cobject ):
+ " set self from a CDouble instance"
+ (<long double*>self.p)[0] = <long double>(<double*>cobject.p)[0]
+ def init_CDouble( self, _CDouble cobject ):
+ " set a CDouble instance from self "
+ (<double*>cobject.p)[0] = <double>(<long double*>self.p)[0]
+ def to_CDouble( self ):
+ " cast self to new CDouble instance "
+ cdef long double c_src
+ cdef double c_tgt
+ cdef _CLDouble py_src
+ cdef _CDouble py_tgt
+ c_src = (<long double*>self.p)[0]
+ c_tgt = <double>c_src
+ py_tgt = CDouble()
+ (<double*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+ def from_CLDouble( self, _CLDouble cobject ):
+ " set self from a CLDouble instance"
+ (<long double*>self.p)[0] = <long double>(<long double*>cobject.p)[0]
+ def init_CLDouble( self, _CLDouble cobject ):
+ " set a CLDouble instance from self "
+ (<long double*>cobject.p)[0] = <long double>(<long double*>self.p)[0]
+CLDouble = Meta('CLDouble', (_CLDouble,), {} )
+CLDouble.basetype = CLDouble
+CLDouble.sizeof = sizeof(long double)
--- /dev/null
+
+
+cdef extern from *:
+ ctypedef unsigned int __c_size_t "size_t"
+
+cdef extern from "object.h":
+ ctypedef class __builtin__.type [object PyHeapTypeObject]:
+ pass
+
+cdef class Meta(type):
+ pass
+
+cdef class _CObject:
+#cdef public class _CObject [ object AdaptObj_CObject, type AdaptType_CObject ]:
+ cdef void*p
+ cdef int malloced
+ cdef public object incref # incref what we are pointing to
+
+cdef class _CPointer(_CObject):
+ pass
+
+cdef class _CArray(_CPointer):
+ cdef public __c_size_t el_size # sizeof what we point to # XX class attr ??
+ cdef __c_size_t nmemb
+ cdef void *items
+
+cdef class _CChar(_CObject):
+ pass
+cdef class _CSChar(_CObject):
+ pass
+cdef class _CUChar(_CObject):
+ pass
+cdef class _CShort(_CObject):
+ pass
+cdef class _CUShort(_CObject):
+ pass
+cdef class _CInt(_CObject):
+ pass
+cdef class _CUInt(_CObject):
+ pass
+cdef class _CLong(_CObject):
+ pass
+cdef class _CULong(_CObject):
+ pass
+cdef class _CLLong(_CObject):
+ pass
+cdef class _CULLong(_CObject):
+ pass
+cdef class _CFloat(_CObject):
+ pass
+cdef class _CDouble(_CObject):
+ pass
+cdef class _CLDouble(_CObject):
+ pass
+cdef class _CStruct(_CObject):
+ pass
--- /dev/null
+# Copyright (c) 2005, National ICT Australia
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the
+# distribution.
+# * Neither the name of National ICT Australia nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#cdef extern from *:
+# ctypedef unsigned int __c_size_t "size_t"
+
+cdef extern from "string.h":
+ void *__c_memcpy "memcpy" ( void*, void*, __c_size_t )
+ int __c_memcmp "memcmp" ( void*, void*, __c_size_t )
+
+# XX implement custom fast malloc (we do lots of little mallocs) XX
+# ...profile...
+cdef extern from "stdlib.h":
+ void *malloc(__c_size_t)
+ void *calloc(__c_size_t, __c_size_t)
+ void free(void*)
+
+cdef extern from "Python.h":
+ ctypedef struct PyObject:
+ int ob_refcnt
+ void Py_INCREF(object o)
+ void Py_DECREF(object o)
+ object PyString_FromFormat(char *format, ...)
+# PyObject* PyObject_Call( PyObject *callable_object, PyObject *args, PyObject *kw)
+ object PyObject_Call( object callable_object, object args, object kw)
+
+cdef void *pyx_malloc( __c_size_t size ):
+ return malloc(size)
+cdef void *pyx_calloc( __c_size_t nmemb, __c_size_t size ):
+ return calloc( nmemb, size )
+cdef void pyx_free( void *p ):
+ free(p)
+
+cdef void *pyx_memcpy( void* tgt, void* src, __c_size_t n ):
+ cdef void *res
+# print "pyx_memcpy", <long>tgt, <long>src, n
+ res = __c_memcpy(tgt,src,n)
+ return res
+
+cdef int pyx_memcmp( void* tgt, void* src, __c_size_t n ):
+ cdef int res
+# print "pyx_memcmp", <long>tgt, <long>src, n
+ res = __c_memcmp(tgt,src,n)
+ return res
+
+#cdef __c_PetscErrorCode petsc_malloc( __c_size_t a,int lineno,char *function,char *filename,char *dir,void**result):
+# result[0]=pyx_malloc( a )
+#
+#cdef __c_PetscErrorCode petsc_free( void *aa,int line,char *function,char *file,char *dir):
+# pyx_free(aa)
+#
+#__c_PetscSetMalloc( petsc_malloc, petsc_free )
+
+
+
+###############################################################################
+# XX we should mangle all these names a bit more ...
+# XX or stick in a submodule ?
+
+#
+# pyrex generates bad code for "unsigned long"
+# which we would like to use as a pointer type
+# so we just use "long".
+#
+# SystemError: ../Objects/longobject.c:240: bad argument to internal function:
+# unsigned long PyLong_AsUnsignedLong(PyObject *vv)
+#
+
+cdef extern from "object.h":
+ ctypedef class __builtin__.type [object PyHeapTypeObject]:
+ pass
+
+cdef class Meta(type)
+
+# We want eg. CInt.pointer to give a class which is the type "pointer to CInt"
+# so we inherit from above classes
+cdef class Meta(type):
+ property array:
+ def __get__( cls ):
+ name = "CArray"+cls.__name__
+ try:
+ return globals()[name]
+ except KeyError:
+ tp = Meta( name, (CArray,), {} )
+ tp.target = cls # class attribute
+ tp.sizeof = None # array's don't have a size until they get a length (instantiated)
+ globals()[name] = tp
+ return tp
+ property pointer:
+ def __get__( cls ):
+ name = "CPointer"+cls.__name__
+ try:
+ return globals()[name]
+ except KeyError:
+ tp = Meta( name, (CPointer,), {} )
+ tp.target = cls # class attribute
+ globals()[name] = tp
+ return tp
+
+def py2cobject( item ):
+ if type(item) in (int,):
+ return CInt(item) # really should be CLong
+ if type(item) == float:
+ return CDouble(item)
+ if type(item) == str:
+ return CStr(item)
+
+cdef class _CObject
+#cdef public class _CObject [ object AdaptObj_CObject, type AdaptType_CObject ]
+
+cdef class _CPointer(_CObject)
+#cdef public class _CPointer(_CObject) [ object AdaptObj_CPointer, type AdaptType_CPointer ]
+
+cdef class _CObject:
+#cdef public class _CObject [ object AdaptObj_CObject, type AdaptType_CObject ]:
+# cdef void*p
+# cdef int malloced
+# cdef public object incref # incref what we are pointing to
+ def __init__( self, value = None, addr = None ):
+ """
+ CObject( [value], [addr] )
+ value : a compatible CObject or native python object
+ addr : a memory location for a c-object
+ """
+ cdef long c_addr
+ assert value is None
+ if addr is not None:
+ c_addr = addr
+ else:
+ c_addr = 0
+ self.p = <void*>c_addr
+ self.malloced = False
+ self.incref = None
+ def __int__( self ):
+ # XX assumes we are pointing to a long
+ cdef long *p
+ assert self.p != NULL
+ p = <long*>self.p
+ return p[0]
+ def get_value( self ): # == __int__
+ # XX assumes we are pointing to a long
+ cdef long *p
+ assert self.p != NULL
+ p = <long*>self.p
+ return p[0]
+ def __nonzero__( self ):
+ return bool( self.get_value() )
+ def set_value( self, value ):
+ # XX assumes we are pointing to a long
+ cdef long *p
+ assert self.p != NULL
+ p = <long*>self.p
+ p[0] = value
+ def clone( self ):
+ return self.__class__( self )
+ property addr:
+ def __get__(self):
+ cdef long c_addr
+ cdef _CPointer p
+ c_addr = <long>self.p
+# p = CPointer( value = c_addr )
+ p = type(self).pointer( c_addr )
+ p.incref = self # incref
+ return p
+ def cast_to( self, cls ):
+ return cls( self )
+ def get_basetype( self ):
+ return _CObject
+ def is_compatible( self, _CObject cobject ):
+# print "_CObject.is_compatible", type(cobject), self.basetype
+ return isinstance( cobject, self.basetype )
+ def __str__( self ):
+ cdef long c_addr
+ c_addr = <long>self.p
+ val = ""
+ if c_addr != 0:
+ val = ", val = %s" % hex(self.get_value())
+ return "%s( addr = %s%s )" % (self.__class__.__name__, hex(c_addr), val)
+ def __repr__( self ):
+ return str(self)
+# def __cmp__( _CObject self, _CObject other ): # XX it's not (self, other) XX
+# # XX assumes we are pointing to a long
+# cdef long *p1, *p2
+# assert self.p != NULL
+# p1 = <long*>self.p
+# assert other.p != NULL
+# p2 = <long*>other.p
+# print "__equ__", p1[0], p2[0]
+# return max(-1, min(1, p1[0] - p2[0]))
+ def __cmp__( x, y ):
+ print x.__class__, '__cmp__', y.__class__
+ assert isinstance(x,_CObject) # x may not be self ... huh ?
+ if not isinstance(y,_CObject):
+ y = py2cobject(y)
+ if x.sizeof == y.sizeof:
+ res = x.addr.memcmp( y.addr, 1 )
+ else:
+ res = -1 # ?
+ def __richcmp__( x, y, int op ):
+# print '__richcmp__', x.__class__, y.__class__, op
+ cdef int cmp
+ assert isinstance(x,_CObject) # x may not be self ... huh ?
+ if not isinstance(y,_CObject):
+ y = py2cobject(y)
+ if y is None:
+ # hmmm...
+ if op in (0,3,4): return True
+ else: return False
+ if x.sizeof == y.sizeof:
+ cmp = x.addr.memcmp( y.addr, 1 )
+ else:
+ cmp = -1 # ?
+ if op == 0: # x<y
+ return cmp<0
+ elif op == 1: # x<=y
+ return cmp<=0
+ elif op == 2: # x==y
+ return cmp==0
+ elif op == 3: # x!=y
+ return cmp!=0
+ elif op == 4: # x>y
+ return cmp>0
+ elif op == 5: # x>=y
+ return cmp>=0
+ assert 0, op
+ def __dealloc__( self ):
+ if self.malloced:
+# print "__dealloc__", hex(<long>self.p)
+ pyx_free( self.p )
+#CObject = Meta( 'CObject', (_CObject,), {} )
+CObject = _CObject
+CVoid = Meta( 'CObject', (_CObject,), {} )
+CVoid.basetype = CVoid
+
+# more types:
+include "_adapt.pxi"
+
+cdef class _CPointer(_CObject):
+#cdef public class _CPointer(_CObject) [ object AdaptObj_CPointer, type AdaptType_CPointer ]:
+ def __init__( self, value = None, addr = None ):
+ """
+ CPointer( [value], [addr] )
+ value : a CPointer
+ addr : address of a c-pointer
+ """
+ cdef long c_value
+ cdef void**p
+ cdef long c_addr
+ cdef _CObject cobject
+ if addr is None:
+ p = <void**>pyx_calloc(1,sizeof(void*))
+ self.p = <void*>p
+ if isinstance(value,_CObject):
+ cobject = value
+ if not self.is_compatible( value ):
+ raise TypeError("%s cannot be set from %s" % (self.__class__, value))
+ c_value = (<long*>cobject.p)[0]
+ elif value is not None:
+ c_value = value
+ else:
+ c_value = 0
+# print self.__class__.__name__, "__init__( value = ", hex(c_value), ")"
+ p[0] = <void*>c_value
+ self.malloced = True
+ else:
+ c_addr = addr
+ self.p = <void*>c_addr
+ self.malloced = False
+ # XX make .target a property that sets el_size once and for all ? XX
+# def sizeof( self ):
+# return sizeof(void*)
+ def memcpy( _CPointer self, _CPointer source, n_items ):
+ " ob.memcpy( source, size ): copy <n_items> items of mem from <source> "
+ assert self.get_value() != 0
+ assert source.get_value() != 0
+# print "_CPointer.memcpy", self, source, n_items
+ pyx_memcpy( (<void**>self.p)[0], (<void**>source.p)[0], n_items * self.target.sizeof )
+ def memcmp( _CPointer self, _CPointer other, n_items ):
+ " ob.memcmp( other, size ): compare <n_items> items of mem with <other> "
+ assert self.get_value() != 0
+ assert other.get_value() != 0
+# print "_CPointer.memcmp", self, other, n_items
+ res = pyx_memcmp( (<void**>self.p)[0], (<void**>other.p)[0], n_items * self.target.sizeof )
+ return res
+ def __setitem__( self, idx, item ):
+ item = self.target(item)
+ p = self.target.pointer(self)
+ p = p.__iadd__( idx )
+ p.memcpy( item.addr, 1 )
+ def __getitem__( self, idx ):
+ item = self.target()
+ p = self.target.pointer(self)
+ p = p.__iadd__( idx )
+ item.addr.memcpy( p, 1 )
+ return item
+ def is_compatible( self, _CObject cobject ):
+# if not type(tgt_class)==Meta:
+# tgt_class = type(tgt_class)
+ src_class = self.__class__
+ tgt_class = cobject.__class__
+ assert src_class==_CObject or type(src_class)==Meta
+ assert tgt_class==_CObject or type(tgt_class)==Meta
+ while issubclass(src_class, _CPointer) and issubclass(tgt_class, _CPointer):
+# print "is_compatible", str(src_class), str(tgt_class)
+ assert issubclass(src_class,_CPointer)
+ assert issubclass(tgt_class,_CPointer)
+ src_class = src_class.target
+ tgt_class = tgt_class.target
+# print "is_compatible", str(src_class), str(tgt_class)
+# return _self.is_compatible(cobject) # isinstance(cobject, self.basetype)
+ if tgt_class == CObject:
+ res = True
+ elif not issubclass(src_class, _CPointer):
+ res = src_class().is_compatible( tgt_class() )
+ else:
+ res = tgt_class().is_compatible( src_class() )
+# print "is_compatible:", res
+ return res
+ def init_from( self, _CPointer cobject ):
+ if not self.is_compatible(cobject):
+ raise TypeError("%s cannot be set from %s" % (self.__class__, cobject))
+ (<void**>self.p)[0] = (<void**>cobject.p)[0]
+ def get_basetype( self ): # use an attribute ?
+ return self.__class__
+ def __int__( self ):
+ cdef void**p
+ cdef long c_addr
+ c_addr = <long>( (<void**>self.p)[0] )
+# print "CPointer.__int__:", c_addr
+ return c_addr
+ property deref:
+ def __get__( self ):
+ cdef long**p
+ p = <long**>self.p
+ if hasattr(self,"target"):
+ ob = self.target( addr = <long>p[0] ) # sets malloced=False
+ ob.incref = self
+ else:
+ ob = CPointer( value = p[0][0] )
+ return ob
+ def __iadd__( self, other ):
+ cdef long sz
+ cdef _CPointer cobject
+ cdef char *cp
+ sz = self.target.sizeof * other
+ cobject = self
+ cp = (<char**>cobject.p)[0]
+ cp = cp + sz
+ (<char**>cobject.p)[0] = cp
+ return self # hmmm, build a new one ?
+ def __add__( _CPointer self, other ):
+ cdef long c_addr
+ assert int(other) == other, "can't add type %s to pointer (need integer type)"%type(other)
+ c_addr = (<long*>self.p)[0]
+ c_addr = c_addr + self.target.sizeof * other
+ return self.__class__( value = c_addr )
+ def __call__( self, *args ):
+ ob = self.deref
+ if isinstance(ob,CFunction):
+ return ob(*args)
+ raise TypeError( "%s not callable"%self )
+
+CPointer = Meta( 'CPointer', (_CPointer,), {} )
+CPointer.target = CObject
+CPointer.basetype = CPointer
+CPointer.sizeof = sizeof(void*)
+null = CPointer( 0 )
+
+cdef class _CArray(_CPointer):
+# cdef public __c_size_t el_size # sizeof what we point to # XX class attr ??
+# cdef __c_size_t nmemb
+# cdef void *items # == self.p (XX duplicate XX)
+ def __init__( self, arg=None ): # XX addr ?
+ " arg: init list or size "
+ cdef long c_addr
+ if arg is not None:
+ if type(arg) in (int,long):
+ nmemb = arg
+ value = None
+ else:
+ nmemb = len(arg)
+ value = arg
+ el_size = self.target.sizeof
+ self.items = <void*>pyx_calloc(nmemb, el_size)
+ c_addr = <long>self.items
+ _CPointer.__init__( self, value = c_addr )
+ self.el_size = el_size
+ self.nmemb = nmemb
+ # print "%s.__init__: nmemb=%s, el_size=%s, target=%s, sizeof(target)=%s"\
+ # % ( self.__class__.__name__, nmemb, el_size, self.target, self.target.sizeof )
+ self.sizeof = self.el_size * nmemb
+ if value is not None:
+ for idx, item in enumerate(value):
+ self[idx] = item
+ else:
+ self.items = NULL
+# print self
+# def sizeof( self ):
+# return self.el_size * nmemb # is this right ??
+ def __len__( self ):
+ return self.nmemb
+ def __dealloc__( self ):
+ if self.items != NULL:
+ pyx_free( self.items )
+# CPointer.__dealloc__(self) # ??
+ def __str__( self ):
+ return "%s(%s)" % ( self.__class__.__name__, list(self) )
+# def is_compatible( self, _CObject cobject ):
+# # XX check array sizes
+# # right now this is all handled from _CPointer XX
+CArray = Meta( 'CArray', (_CArray,), {} )
+# CArray.basetype ... not used
+
+def get_slice_size( int start, int stop, int step ):
+ if stop<=start: return 0
+ if (stop-start)%step == 0:
+ return (stop-start)/step
+ return (stop-start)/step + 1
+
+cdef class _CArrayCIntRange(_CArray): # derive from CArrayCInt
+ def __init__( self, int start, int stop, int step ):
+ cdef int count
+ cdef int *p
+ cdef int nmemb
+ nmemb = get_slice_size(start,stop,step)
+ p = <int*>pyx_malloc( nmemb * sizeof(int) )
+ self.nmemb = nmemb
+ count = 0
+ while nmemb:
+ p[count] = count + start
+ count = count + 1
+ nmemb = nmemb - 1
+ self.items = p
+ addr = <long>self.items
+ _CPointer.__init__( self, value = addr )
+ self.el_size = sizeof(int)
+CArrayCIntRange = Meta( 'CArrayCIntRange', (_CArrayCIntRange,), {} )
+CArrayCIntRange.target = CInt
+
+class CStr(CChar.array):
+ def __init__( self, s ):
+ CChar.array.__init__( self, len(s)+1 )
+ idx = 0
+ for c in s:
+ self[idx] = ord(c)
+ idx = idx + 1
+ self[idx] = 0
+ def str( _CObject self ):
+ if self.p != NULL:
+ p = (<char**>self.p)[0]
+ else:
+ p = "<NULL>"
+ return p
+
+
+cdef class _CStruct(_CObject):
+ pass
+CStruct = Meta( 'CStruct', (_CStruct,), {} )
+CStruct.basetype = CStruct
+
+cdef class _CUnion(_CObject):
+ pass
+CUnion = Meta( 'CUnion', (_CUnion,), {} )
+CUnion.basetype = CUnion
+
+#cdef class _CEnum(_CInt):
+# pass
+#CEnum = Meta( 'CEnum', (_CEnum,), {} )
+CEnum = Meta( 'CEnum', (CInt,), {} )
+CEnum.basetype = CInt
+
+
+cdef class _CFunction(_CObject):
+ " a callable pointer "
+# cdef void (*p)()
+ cdef object arg_tps
+ def __init__(self, *arg_tps):
+ _CObject.__init__(self)
+ self.arg_tps = arg_tps
+ def check_args(self, *args):
+ if len(self.arg_tps)!=len(args):
+ raise TypeError("xxx")
+ for i, arg in enumerate(args):
+ # check types here
+ arg_tp = self.arg_tps[i]
+ if arg_tp is not None:
+ pass
+# this is an abstract base class, subclasses define __call__:
+# def __call__(self, *args):
+# cdef c_arg0
+# ret = (<int (*)(int)>self.p)( c_arg0 )
+# return ret
+CFunction = Meta( 'CFunction', (_CFunction,), {} )
+CFunction.basetype = CFunction
+
+cdef class _CFunctionThief(_CFunction):
+ " this function steals references to python objects "
+ cdef object refs
+ def __init__(self, *arg_tps):
+ _CFunction.__init__(self,*arg_tps)
+ self.refs = {} # map pyfunc to incref'd tuple
+ def hold( self, pyfunc, data ):
+ data = (pyfunc, data)
+ self.refs[pyfunc] = data # incref
+ return data
+ def release( self, pyfunc ):
+ del self.refs[pyfunc] # decref
+CFunctionThief = Meta( 'CFunctionThief', (_CFunctionThief,), {} )
+CFunctionThief.basetype = CFunctionThief
+
+def _build_function_cls( *sig ):
+ # use '0', '1' as begin/end markers
+ # use '_' as separator
+ toks = []
+ for tp in sig:
+ toks.append( tp.__class__.__name__ )
+ name = 'CFunction0' + '_'.join( toks ) + '1'
+ try:
+ return globals()[name]
+ except KeyError:
+ tp = Meta( name, (CFunction,), {} ) # we use the ordinary Meta
+ tp._sig = sig # class attribute
+ globals()[name] = tp
+ return tp
+
+cdef class FunctionMeta(Meta):
+ property sig:
+ def __get__( cls ):
+ return _build_function_cls
+CFunction = FunctionMeta( 'CFunction', (_CFunction,), {} )
+CFunction.basetype = CFunction
+
+
+#cdef class _CPyObject(_CPointer):
+#CPyObject = Meta( 'CPyObject', (_CPyObject), {} )
+
+class CPyObject(CPointer):
+ def __init__( self, value=None, addr=None ):
+ cdef void* p
+ if value is not None:
+ if not isinstance(value,_CObject):
+ p = <void*>value
+ Py_INCREF(value)
+ CPointer.__init__( self, <long>p )
+ else:
+ CPointer.__init__( self, addr=addr )
+ def __call__( _CPointer self, *args, **kw ):
+ cdef object o, result
+ cdef void *p
+ p = (<void**>self.p)[0]
+ o = <object>p
+ result = PyObject_Call( o, args, kw )
+ return result
+ def get_ob( _CPointer self ):
+ # get the actual python object
+ cdef void *p
+ p = (<void**>self.p)[0]
+ o = <object>p
+ return o
+ ob = property(get_ob)
+ def get_refcnt( _CPointer self ):
+ cdef PyObject*p
+ p = (<PyObject**>self.p)[0]
+ assert p != NULL
+ return p[0].ob_refcnt
+ refcnt = property(get_refcnt)
+# def incref( _CPointer self ):
+# cdef PyObject*p
+# p = (<PyObject**>self.p)[0]
+# assert p != NULL
+# Py_INCREF(p)
+# def decref( _CPointer self ):
+# cdef PyObject*p
+# p = (<PyObject**>self.p)[0]
+# assert p != NULL
+# Py_DECREF(p)
+ def incref( _CPointer self ):
+ cdef void *p
+ p = (<void**>self.p)[0]
+ o = <object>p
+ Py_INCREF(o)
+ def decref( _CPointer self ):
+ cdef void *p
+ p = (<void**>self.p)[0]
+ o = <object>p
+ Py_DECREF(o)
+ def __del__( _CPointer self ):
+ self.decref()
+
+import sys
+__module__ = sys.modules[__name__]
+
+def __sizeof(cls):
+ return cls.sizeof
+__module__.sizeof=__sizeof
+
+###############################################################################
+# XX mangle these c names ?
+
+cdef cobject_from_pointer( void *p ):
+ cdef long addr
+ addr = <long>p
+ ob = CPointer( value = addr )
+ return ob
+
+cdef void *pointer_from_cobject( _CObject ob ):
+ return ob.p
+
+cdef char *get_charp( ob ):
+ cdef _CObject _ob
+ cdef char*p
+ if type(ob)==str:
+ p = ob
+ elif type(ob) in (int,long):
+ assert ob==0
+ p = <char*>ob
+ else:
+ _ob = ob
+ p = (<char**>_ob.p)[0]
+# print "get_charp", ob, hex(<long>p)
+ return p
+
+def str_from_charp( _CPointer ob ):
+ cdef char*p
+ p = (<char**>ob.p)[0]
+ return p
+
+
+
--- /dev/null
+#!/usr/bin/env python
+
+template = \
+"""
+#cdef public class _%(pytypename)s(_CObject)[ object AdaptObj_%(pytypename)s, type AdaptType_%(pytypename)s ]:
+cdef class _%(pytypename)s(_CObject):
+ def __init__( self, value = None, addr = None ):
+ cdef %(ctypename)s*p
+ cdef long c_addr
+ cdef _CObject cobject
+ if addr is None:
+ p = <%(ctypename)s*>pyx_calloc(1,sizeof(%(ctypename)s))
+ self.p = <void*>p
+ if isinstance(value,CObject):
+ if isinstance(value,_%(pytypename)s):
+ cobject = value
+ p[0] = (<%(ctypename)s*>cobject.p)[0]
+ else:
+ value.init_%(pytypename)s(self)
+ elif value is not None:
+ p[0] = value
+ else:
+ p[0] = 0
+ self.malloced = True
+ else:
+ c_addr = addr
+ self.p = <void*>c_addr
+ self.malloced = False
+ def __int__( self ):
+ cdef %(ctypename)s *p
+ assert self.p != NULL
+ p = <%(ctypename)s*>self.p
+ return int(p[0])
+ def __float__( self ):
+ cdef %(ctypename)s *p
+ assert self.p != NULL
+ p = <%(ctypename)s*>self.p
+ return float(p[0])
+ def __add__( x, y ):
+ cdef %(ctypename)s val
+ cdef _%(pytypename)s self
+ if not isinstance(x,%(pytypename)s):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<%(ctypename)s*>self.p)[0]
+ val = val + y
+ return %(pytypename)s(val)
+ def __sub__( x, y ):
+ cdef %(ctypename)s val
+ cdef _%(pytypename)s self
+ if not isinstance(x,%(pytypename)s):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<%(ctypename)s*>self.p)[0]
+ val = val - y
+ return %(pytypename)s(val)
+ def __mul__( x, y ):
+ cdef %(ctypename)s val
+ cdef _%(pytypename)s self
+ if not isinstance(x,%(pytypename)s):
+ x,y = y,x
+ self = x
+ assert self.p != NULL
+ val = (<%(ctypename)s*>self.p)[0]
+ val = val + y
+ return %(pytypename)s(val)
+ def __repr__( self ):
+ cdef long c_addr
+ cdef %(ctypename)s c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<%(ctypename)s*>self.p)[0]
+ value = ", value = %%s" %% str( c_val )
+ return "%%s( addr = %%s%%s )" %% (self.__class__.__name__, hex(c_addr), value)
+ def set_value( self, value ):
+ if isinstance(value,CObject):
+ value.init_%(pytypename)s(self)
+ else:
+ (<%(ctypename)s*>self.p)[0] = value
+ def get_value( self ):
+ return (<%(ctypename)s*>self.p)[0]
+ def to_%(pytypename)s(self):
+ return self
+ def init_from( self, _%(pytypename)s cobject ):
+ " init self from a %(pytypename)s instance "
+# if not self.is_compatible(cobject):
+ if not isinstance( cobject, %(pytypename)s ):
+ raise TypeError("%%s cannot be set from %%s" %% (self.__class__, cobject))
+ (<%(ctypename)s*>self.p)[0] = (<%(ctypename)s*>cobject.p)[0]
+ def get_basetype( self ):
+ return %(pytypename)s
+# def sizeof( self ):
+# return sizeof(%(ctypename)s)
+# def is_compatible( self, _CObject cobject ):
+# return isinstance( cobject, %(pytypename)s )
+"""
+
+to_func = """\
+ def to_%(pytarget)s( self ):
+ " cast self to new %(pytarget)s instance "
+ cdef %(csource)s c_src
+ cdef %(ctarget)s c_tgt
+ cdef _%(pysource)s py_src
+ cdef _%(pytarget)s py_tgt
+ c_src = (<%(csource)s*>self.p)[0]
+ c_tgt = <%(ctarget)s>c_src
+ py_tgt = %(pytarget)s()
+ (<%(ctarget)s*>py_tgt.p)[0] = c_tgt
+ return py_tgt
+"""
+from_func = """\
+ def from_%(pytarget)s( self, _%(pytarget)s cobject ):
+ " set self from a %(pytarget)s instance"
+ (<%(csource)s*>self.p)[0] = <%(csource)s>(<%(ctarget)s*>cobject.p)[0]
+"""
+init_func = """\
+ def init_%(pytarget)s( self, _%(pytarget)s cobject ):
+ " set a %(pytarget)s instance from self "
+ (<%(ctarget)s*>cobject.p)[0] = <%(ctarget)s>(<%(csource)s*>self.p)[0]
+"""
+
+str_tmpl = """\
+ def __str__( self ):
+ cdef long c_addr
+ cdef %(ctypename)s c_val
+ c_addr = <long>self.p
+ value = ""
+ if c_addr != 0:
+ c_val = (<%(ctypename)s*>self.p)[0]
+ value = str( c_val )
+ return value
+"""
+
+tps = """\
+CChar char
+CSChar signed char
+CUChar unsigned char
+CShort short
+CUShort unsigned short
+CInt int
+CUInt unsigned int
+CLong long
+CULong unsigned long
+CLLong long long
+CULLong unsigned long long
+CFloat float
+CDouble double
+CLDouble long double """.split('\n')
+
+tps = [ ( tp.split()[0], ' '.join(tp.split()[1:]) ) for tp in tps ]
+
+tps = [ {'pytypename':tp[0], 'ctypename':tp[1]} for tp in tps ]
+
+#print tps
+
+import sys
+#pxifile = open("_adapt.pxi","w")
+pxifile = sys.argv[1]
+pxdfile = sys.argv[2]
+assert pxifile.endswith('.pxi')
+assert pxdfile.endswith('.pxd')
+pxifile = open(pxifile,"w")
+pxdfile = open(pxdfile,"w")
+
+print >>pxifile, "# code autogenerated from %s" % __file__
+print >>pxifile
+
+for tp in tps:
+# print 'cdef public class _%(pytypename)s(_CObject)[ object AdaptObj_%(pytypename)s, type AdaptType_%(pytypename)s ]' % tp
+ print >>pxifile, 'cdef class _%(pytypename)s(_CObject)' % tp
+print>>pxifile
+
+for tp in tps:
+ print >>pxifile, template % tp,
+# print '#', tp
+ if tp['pytypename'] == 'CChar':
+ print >>pxifile, """\
+ def __str__( self ):
+ cdef int a
+ a = (<char*>self.p)[0]
+ s = PyString_FromFormat('%c', a)
+ return s
+""",
+ else:
+ print >>pxifile, str_tmpl % tp,
+ if tp['pytypename'] == 'CInt':
+ print >>pxifile, """\
+# def __add__( x, y ):
+# cdef _CInt z
+# if isinstance(x,CInt):
+# z = x
+# return (<int*>z.p)[0] + y
+# elif isinstance(y,CInt):
+# z = y
+# return x + (<int*>z.p)[0]
+# return NotImplemented
+# def __sub__( x, y ):
+# cdef _CInt z
+# if isinstance(x,CInt):
+# z = x
+# return (<int*>z.p)[0] - y
+# elif isinstance(y,CInt):
+# z = y
+# return x - (<int*>z.p)[0]
+# return NotImplemented
+""",
+ for _tp in tps:
+ print >>pxifile, from_func % \
+ { 'csource':tp['ctypename'], 'pysource':tp['pytypename'],
+ 'ctarget':_tp['ctypename'], 'pytarget':_tp['pytypename'] },
+ print >>pxifile, init_func % \
+ { 'csource':tp['ctypename'], 'pysource':tp['pytypename'],
+ 'ctarget':_tp['ctypename'], 'pytarget':_tp['pytypename'] },
+ if _tp == tp:
+ continue
+ print >>pxifile, to_func % \
+ { 'csource':tp['ctypename'], 'pysource':tp['pytypename'],
+ 'ctarget':_tp['ctypename'], 'pytarget':_tp['pytypename'] },
+
+ print >>pxifile, "%(pytypename)s = Meta('%(pytypename)s', (_%(pytypename)s,), {} )" % tp
+ print >>pxifile, "%(pytypename)s.basetype = %(pytypename)s" % tp
+ print >>pxifile, "%(pytypename)s.sizeof = sizeof(%(ctypename)s)" % tp
+
+
+pxdheader = \
+"""
+
+cdef extern from *:
+ ctypedef unsigned int __c_size_t "size_t"
+
+cdef extern from "object.h":
+ ctypedef class __builtin__.type [object PyHeapTypeObject]:
+ pass
+
+cdef class Meta(type):
+ pass
+
+cdef class _CObject:
+#cdef public class _CObject [ object AdaptObj_CObject, type AdaptType_CObject ]:
+ cdef void*p
+ cdef int malloced
+ cdef public object incref # incref what we are pointing to
+
+cdef class _CPointer(_CObject):
+ pass
+
+cdef class _CArray(_CPointer):
+ cdef public __c_size_t el_size # sizeof what we point to # XX class attr ??
+ cdef __c_size_t nmemb
+ cdef void *items
+
+"""
+
+pxdfile.write( pxdheader )
+
+for tp in tps:
+# print 'cdef public class _%(pytypename)s(_CObject)[ object AdaptObj_%(pytypename)s, type AdaptType_%(pytypename)s ]' % tp
+ print >>pxdfile, 'cdef class _%(pytypename)s(_CObject):\n pass' % tp
+print >>pxdfile, 'cdef class _CStruct(_CObject):\n pass'
+
+
--- /dev/null
+#!/usr/bin/env python
+
+"""
+
+(c) 2002, 2003, 2004, 2005 Simon Burton <simon@arrowtheory.com>
+Released under GNU LGPL license.
+
+"""
+
+import sys
+
+from lexer import Lexer
+from parse_core import Symbols, Parser
+import node as node_module
+
+
+class Node(node_module.Node):
+
+ def is_typedef(self):
+ for x in self:
+ if isinstance(x,Node):
+ if x.is_typedef():
+ return 1
+ return 0
+
+ #def explain(self):
+ #l = []
+ #for x in self:
+ #if isinstance(x,Node):
+ #l.append(x.explain())
+ #else:
+ #l.append(str(x))
+ #return string.join(l," ")
+ ##(self.__class__.__name__,string.join(l) )
+
+ def psource(self):
+ if hasattr(self,'lines'):
+ print "# "+string.join(self.lines,"\n# ")+"\n"
+
+
+###################################################################
+#
+###################################################################
+#
+
+
+class BasicType(Node):
+ " int char short etc. "
+ def __init__(self,name):
+ Node.__init__(self,name)
+
+class Qualifier(Node):
+ """
+ """
+ def __init__(self,name):
+ Node.__init__(self,name)
+ self.name=name
+
+class StorageClass(Node):
+ """
+ """
+ def __init__(self,name):
+ Node.__init__(self,name)
+ self.name=name
+
+class Typedef(StorageClass):
+ """
+ """
+ def __init__(self,s='typedef'):
+ Node.__init__(self,s)
+ #def explain(self):
+ #return "type"
+
+class Ellipses(Node):
+ """
+ """
+ def __init__(self,s='...'):
+ Node.__init__(self,s)
+
+class GCCBuiltin(BasicType):
+ """
+ """
+ pass
+
+
+class Identifier(Node):
+ """
+ """
+ def __init__(self,name="",*items):
+ if name or 1:
+ Node.__init__(self,name,*items)
+ else:
+ Node.__init__(self)
+ self.name=name
+
+class Function(Node,Parser):
+ """
+ """
+ def __init__(self,*items):
+ Node.__init__(self,*items)
+
+ def parse(self,lexer,symbols):
+ symbols = Symbols(symbols)
+ args = ''
+ #lexer.get_token()
+ if lexer.tok != ')':
+ if not lexer.tok:
+ self.parse_error(lexer)
+ #lexer.unget_token() # unget start of decl
+ while lexer.tok != ')':
+ node = ParameterDeclaration()
+ node.parse(lexer,symbols)
+ self.append( node )
+ if lexer.tok != ')' and lexer.tok != ',':
+ self.parse_error(lexer)
+ if lexer.tok == ',':
+ lexer.get_token()
+ lexer.get_token()
+
+
+class Pointer(Node):
+ """
+ """
+ def __init__(self,*items):
+ Node.__init__(self,*items)
+
+class Array(Node,Parser):
+ """
+ """
+ def __init__(self,*items):
+ Node.__init__(self,*items)
+
+ def parse(self,lexer,symbols):
+ lexer.get_token() # a number or ']'
+ # XX
+ # HACK HACK: constant c expressions can appear in here:
+ # eg. [ 15 * sizeof (int) - 2 * sizeof (void *) ]
+ # XX
+ toks = []
+ while lexer.tok != ']':
+ #self.append( lexer.kind )
+ toks.append( lexer.tok )
+ lexer.get_token()
+ child = " ".join(toks)
+ if child == "":
+ child = None
+ self.append( child )
+ lexer.get_token() # read past the ']'
+
+class Tag(Node):
+ """
+ """
+ pass
+
+
+class Compound(Node,Parser):
+ "Struct or Union"
+
+ def __init__(self,*items,**kw):
+ Node.__init__(self,*items,**kw)
+
+ def parse(self,lexer,symbols):
+ symbols = Symbols(symbols)
+ tag = "" # anonymous
+ if lexer.tok != '{':
+ tag = lexer.tok
+ if not ( tag[0]=='_' or tag[0].isalpha() ):
+ self.parse_error(lexer ,"expected tag, got '%s'"%tag )
+ lexer.get_token()
+ if tag:
+ self.append(Tag(tag))
+ else:
+ self.append(Tag())
+ self.tag = tag
+ if lexer.tok == '{':
+ fieldlist = []
+ lexer.get_token()
+ if lexer.tok != '}':
+ if not lexer.tok: self.parse_error(lexer)
+ while lexer.tok != '}':
+ node = StructDeclaration()
+ node.parse(lexer,symbols)
+ fieldlist.append( node )
+ self += fieldlist
+ lexer.get_token()
+ if self.verbose:
+ print "%s.__init__() #<--"%(self)
+
+class Struct(Compound):
+ """
+ """
+ pass
+
+class Union(Compound):
+ """
+ """
+ pass
+
+class Enum(Node,Parser):
+ """
+ """
+ def __init__(self,*items,**kw):
+ Node.__init__(self,*items,**kw)
+
+ def parse(self,lexer,symbols):
+ tag = "" # anonymous
+ if lexer.tok != '{':
+ tag = lexer.tok
+ if not ( tag[0]=='_' or tag[0].isalpha() ):
+ self.parse_error(lexer ,"expected tag, got '%s'"%tag )
+ lexer.get_token()
+ if tag:
+ self.append(Tag(tag))
+ else:
+ self.append(Tag())
+ self.tag = tag
+ if lexer.tok == '{':
+ lexer.get_token()
+ if lexer.tok != '}': # XX dopey control flow
+ if not lexer.tok: # XX dopey control flow
+ self.parse_error(lexer) # XX dopey control flow
+ while lexer.tok != '}': # XX dopey control flow
+ if lexer.kind is not None:
+ self.expected_error(lexer ,"identifier" )
+ ident = Identifier(lexer.tok)
+ if symbols[ident[0]] is not None:
+ self.parse_error(lexer,"%s already defined."%ident[0])
+ symbols[ident[0]]=ident
+ self.append( ident )
+ lexer.get_token()
+ if lexer.tok == '=':
+ lexer.get_token()
+ # ConstantExpr
+ # XX hack hack XX
+ while lexer.tok!=',' and lexer.tok!='}':
+ lexer.get_token()
+# if type( lexer.kind ) is not int:
+# #self.parse_error(lexer ,"expected integer" )
+# # XX hack hack XX
+# while lexer.tok!=',' and lexer.tok!='}':
+# lexer.get_token()
+# else:
+# # put initializer into the Identifier
+# ident.append( lexer.kind )
+# lexer.get_token()
+ if lexer.tok != '}':
+ if lexer.tok != ',':
+ self.expected_error(lexer,"}",",")
+ lexer.get_token() # ','
+ lexer.get_token()
+ if self.verbose:
+ print "%s.__init__() #<--"%(self)
+
+
+
+class Declarator(Node,Parser):
+ """
+ """
+ def __init__(self,*items):
+ Node.__init__(self,*items)
+ self.ident = None
+
+ def parse(self,lexer,symbols):
+ #Parser.parse_enter(self,lexer)
+ stack = []
+ # read up to identifier, pushing tokens onto stack
+ self.ident = self.parse_identifier(lexer,symbols,stack)
+ self.name = ''
+ if self.ident is not None:
+ self.append( self.ident )
+ self.name = self.ident.name
+ # now read outwards from identifier
+ self.parse_declarator(lexer,symbols,stack)
+ #Parser.parse_leave(self,lexer)
+
+ def parse_identifier(self,lexer,symbols,stack):
+ if self.verbose:
+ print "%s.parse_identifier()"%self
+ ident = None
+ if lexer.tok != ';':
+ while lexer.tok and lexer.kind is not None:
+ stack.append( (lexer.tok, lexer.kind) )
+ lexer.get_token()
+ if lexer.tok:
+ ident = Identifier( lexer.tok )
+ #stack.append( (ident.name, ident) )
+ lexer.get_token()
+ if self.verbose:
+ print "%s.parse_identifier()=%s"%(self,repr(ident))
+ return ident
+
+ def parse_declarator(self,lexer,symbols,stack,level=0):
+ if self.verbose:
+ print " "*level+"%s.parse_declarator(%s) # --->"%\
+ (self,stack)
+ if lexer.tok == '[':
+ while lexer.tok == '[':
+ node = Array()
+ node.parse(lexer,symbols)
+ self.append(node)
+ if lexer.tok == '(':
+ self.parse_error(lexer ,"array of functions" )
+ elif lexer.tok == '(':
+ lexer.get_token()
+ node = Function()
+ node.parse(lexer,symbols)
+ self.append( node )
+ if lexer.tok == '(':
+ self.parse_error(lexer ,"function returns a function" )
+ if lexer.tok == '[':
+ self.parse_error(lexer ,"function returns an array" )
+ while stack:
+ tok, kind = stack[-1] # peek
+ if tok == '(':
+ stack.pop()
+ self.consume(lexer,')')
+ self.parse_declarator(lexer,symbols,stack,level+1)
+ elif tok == '*':
+ stack.pop()
+ self.append( Pointer() )
+ else:
+ tok, kind = stack.pop()
+ self.append( kind )
+ if self.verbose:
+ print " "*level+"%s.parse_declarator(%s) # <---"%\
+ (self,stack)
+
+
+class AbstractDeclarator(Declarator):
+ """ used in ParameterDeclaration; may lack an identifier """
+
+ def parse_identifier(self,lexer,symbols,stack):
+ if self.verbose:
+ print "%s.parse_identifier()"%self
+ ident = None
+ ident = Identifier()
+ while 1:
+ if lexer.tok == ';':
+ self.parse_error(lexer)
+ if lexer.tok == ')':
+ break
+ if lexer.tok == ',':
+ break
+ if lexer.tok == '[':
+ break
+ if lexer.kind is None:
+ #print "%s.new identifier"%self
+ ident = Identifier( lexer.tok )
+ lexer.get_token()
+ #stack.append( (ident.name, ident) )
+ break
+ stack.append( (lexer.tok, lexer.kind) )
+ lexer.get_token()
+ if self.verbose:
+ print "%s.parse_identifier()=%s"%(self,repr(ident))
+ return ident
+
+class FieldLength(Node):
+ """
+ """
+ pass
+
+class StructDeclarator(Declarator):
+ """
+ """
+ def parse(self,lexer,symbols):
+ if lexer.tok != ':':
+ Declarator.parse(self,lexer,symbols)
+ if lexer.tok == ':':
+ lexer.get_token()
+ # ConstantExpr
+ length = int(lexer.tok)
+ #print "length = ",length
+ self.append( FieldLength(length) )
+ lexer.get_token()
+
+class DeclarationSpecifiers(Node,Parser):
+ """
+ """
+ def __init__(self,*items):
+ Node.__init__(self,*items)
+
+ def __eq__(self,other):
+ " unordered (set/bag) equality "
+ if not isinstance(other,Node):
+ return 0
+ for i in range(len(self)):
+ if not self[i] in other:
+ return 0
+ for i in range(len(other)):
+ if not other[i] in self:
+ return 0
+ return 1
+
+ def parse(self,lexer,symbols):
+ self.parse_spec(lexer,symbols)
+ self.reverse()
+
+ def parse_spec(self,lexer,symbols):
+ typespec = None
+ while lexer.tok:
+ if isinstance( lexer.kind, TypeAlias ) or\
+ isinstance( lexer.kind, BasicType ):
+ if typespec is not None:
+ self.parse_error(lexer ,"type already specified as %s"\
+ %typespec )
+ typespec=lexer.kind
+ self.append( lexer.kind )
+ lexer.get_token()
+ elif isinstance( lexer.kind, Qualifier ):
+ self.append( lexer.kind )
+ lexer.get_token()
+ elif isinstance( lexer.kind, StorageClass ):
+ self.append( lexer.kind )
+ lexer.get_token()
+ elif lexer.tok=='struct':
+ lexer.get_token()
+ self.parse_struct(lexer,symbols)
+ break #?
+ elif lexer.tok=='union':
+ lexer.get_token()
+ self.parse_union(lexer,symbols)
+ break #?
+ elif lexer.tok=='enum':
+ lexer.get_token()
+ self.parse_enum(lexer,symbols)
+ break #?
+ elif lexer.kind is None:
+ # identifier
+ break
+ else:
+ break
+
+ def parse_struct(self,lexer,symbols):
+ if self.verbose:
+ print "%s.parse_struct()"%(self)
+ node = Struct()
+ node.parse(lexer,symbols)
+ _node = None
+ if node.tag:
+ _node = symbols.get_tag( node.tag )
+ if _node is not None:
+ if not isinstance( _node, Struct ):
+ self.parse_error(lexer,"tag defined as wrong kind")
+ if len(node)>1:
+ if len(_node)>1:
+ self.parse_error(lexer,"tag already defined as %s"%_node)
+ #symbols.set_tag( node.tag, node )
+ #else:
+ # refer to the previously defined struct
+ ##node = _node
+ #node = _node.clone()
+ if 0:
+ # refer to the previously defined struct
+ if len(node)==1:
+ _node = symbols.deep_get_tag( node.tag )
+ if _node is not None:
+ node=_node
+ # But what about any future reference to the struct ?
+ if node.tag:
+ symbols.set_tag( node.tag, node )
+ self.append( node )
+
+ def parse_union(self,lexer,symbols):
+ if self.verbose:
+ print "%s.parse_union(%s)"%(self,node)
+ node = Union()
+ node.parse(lexer,symbols)
+ _node = None
+ if node.tag:
+ _node = symbols.get_tag( node.tag )
+ if _node is not None:
+ if not isinstance( _node, Union ):
+ self.parse_error(lexer,"tag %s defined as wrong kind"%repr(node.tag))
+ if len(node)>1:
+ if len(_node)>1:
+ self.parse_error(lexer,"tag already defined as %s"%_node)
+ #symbols.set_tag( node.tag, node )
+ #else:
+ #node = _node
+ #if len(node)==1:
+ #_node = symbols.deep_get_tag( node.tag )
+ #if _node is not None:
+ #node=_node
+ if node.tag:
+ symbols.set_tag( node.tag, node )
+ self.append( node )
+
+ def parse_enum(self,lexer,symbols):
+ if self.verbose:
+ print "%s.parse_enum(%s)"%(self,node)
+ node = Enum()
+ node.parse(lexer,symbols)
+ _node = None
+ if node.tag:
+ _node = symbols.get_tag( node.tag )
+ if _node is not None:
+ if not isinstance( _node, Enum ):
+ self.parse_error(lexer,"tag defined as wrong kind")
+ if len(node)>1:
+ if len(_node)>1:
+ self.parse_error(lexer,"tag already defined as %s"%_node)
+ #symbols.set_tag( node.tag, node )
+ #else:
+ #node = _node
+ #if len(node)==1:
+ #_node = symbols.deep_get_tag( node.tag )
+ #if _node is not None:
+ #node=_node
+ if node.tag:
+ symbols.set_tag( node.tag, node )
+ self.append( node )
+
+ def is_typedef(self):
+ return self.find(Typedef) is not None
+
+ def needs_declarator(self):
+ for node in self:
+ if isinstance( node, Struct ):
+ return False
+ if isinstance( node, Enum ):
+ return False
+ if isinstance( node, Union ):
+ return False
+ return True
+
+
+
+class TypeSpecifiers(DeclarationSpecifiers):
+ " used in ParameterDeclaration "
+
+ def parse_spec(self,lexer,symbols):
+ typespec = None
+ while lexer.tok:
+ if isinstance( lexer.kind, TypeAlias ) or\
+ isinstance( lexer.kind, BasicType ):
+ if typespec is not None:
+ self.parse_error(lexer ,"type already specified as %s"\
+ %typespec )
+ typespec=lexer.kind
+ self.append( lexer.kind )
+ lexer.get_token()
+ elif isinstance( lexer.kind, Qualifier ):
+ self.append( lexer.kind )
+ lexer.get_token()
+ elif isinstance( lexer.kind, StorageClass ):
+ self.parse_error(lexer ,"'%s' cannot appear here"%lexer.tok )
+ elif lexer.tok=='struct':
+ lexer.get_token()
+ self.parse_struct(lexer,symbols)
+ break #?
+ elif lexer.tok=='union':
+ lexer.get_token()
+ self.parse_union(lexer,symbols)
+ break #?
+ elif lexer.tok=='enum':
+ lexer.get_token()
+ self.parse_enum(lexer,symbols)
+ break #?
+ elif lexer.kind is None:
+ # identifier
+ break
+ else:
+ break
+
+
+class Initializer(Node,Parser):
+ """
+ """
+ def __init__(self,*items):
+ Node.__init__(self,*items)
+
+ def parse(self,lexer,symbols):
+ self.parse_error(lexer,"not implemented")
+
+
+class TypeAlias(Node):
+ " typedefed things "
+
+ def __init__(self,name,decl=None):
+ Node.__init__(self,name)#,decl)
+ self.name=name
+ self.decl=decl
+
+
+class Declaration(Node,Parser):
+ """
+ """
+ def __init__(self,*items):
+ Node.__init__(self,*items)
+ #self.acted=False
+
+ def parse(self,lexer,symbols):
+ if not lexer.tok:
+ return
+ Parser.parse_enter(self,lexer)
+ declspec = DeclarationSpecifiers()
+ declspec.parse(lexer,symbols)
+ if len(declspec)==0:
+ if lexer.tok == ';':
+ lexer.get_token()
+ # empty declaration...
+ return
+ self.parse_error(lexer,
+ "expected specifiers, got '%s'"%lexer.tok )
+ self.append(declspec)
+ while 1:
+ decl = Declarator()
+ decl.parse(lexer,symbols)
+ if len(decl)==0:
+ if declspec.needs_declarator():
+ self.parse_error(lexer,
+ "expected declarator, got '%s'"%lexer.tok )
+ self.append(decl)
+ ident = decl.ident
+ if ident is not None:
+ #if len(ident):
+ # install symbol
+ node = symbols[ident[0]]
+ if node is not None:
+ # we allow functions to be defined (as same) again
+ #print node.deepstr(),'\n', self.deepstr()
+ _node = node.clone()
+ _node.delete(Identifier)
+ _self = self.clone()
+ _self.delete(Identifier)
+ if _node != _self:
+ self.parse_error(lexer,
+ "\n%s\n already defined as \n%s\n"%\
+ (self.deepstr(),node.deepstr()))
+ else:
+ if self.is_typedef():
+ #lexer.mktypedef( ident[0], self )
+ tp = TypeAlias(ident[0],decl)
+ lexer.mktypedef( ident[0], tp )
+ else:
+ symbols[ident[0]] = self
+ if lexer.tok == '=':
+ # parse initializer
+ lexer.get_token()
+ init = Initializer()
+ init.parse(lexer,symbols)
+ ident.append( init ) # as in Enum
+ #else: struct, union or enum
+ if lexer.tok == ';':
+ # no more declarators
+ break
+ if lexer.tok == '{':
+ # ! ahhh, function body !!!
+# sys.stderr.write(
+# "WARNING: function body found at line %s\n"%lexer.lno )
+ bcount = 1
+ while bcount:
+ lexer.get_brace_token()
+ if lexer.tok == '}':
+ bcount -= 1
+ if lexer.tok == '{':
+ bcount += 1
+ lexer.get_token()
+ Parser.parse_leave(self,lexer)
+ return
+ self.consume(lexer,',')
+ self.consume(lexer,';')
+ Parser.parse_leave(self,lexer)
+
+ def is_typedef(self):
+ spec=self[0]
+ assert isinstance(spec,DeclarationSpecifiers), self.deepstr()
+ return spec.is_typedef()
+
+
+class ParameterDeclaration(Declaration):
+ """
+ """
+ def parse(self,lexer,symbols):
+ typespec = TypeSpecifiers()
+ typespec.parse(lexer,symbols)
+ self.append(typespec)
+ decl = AbstractDeclarator()
+ decl.parse(lexer,symbols)
+ self.append(decl)
+ ident = decl.ident
+ if ident is not None and ident[0]:
+ node = symbols[ident[0]]
+ if node is not None:
+ self.parse_error(lexer,
+ "%s already defined as %s"%(ident,node))
+ else:
+ symbols[ident[0]] = self
+
+
+class StructDeclaration(Declaration):
+ """
+ """
+ def parse(self,lexer,symbols):
+ if not lexer.tok:
+ return
+ declspec = DeclarationSpecifiers()
+ declspec.parse(lexer,symbols)
+ self.append(declspec)
+ if len(declspec)==0:
+ if lexer.tok == ';':
+ lexer.get_token()
+ # empty declaration...
+ return
+ self.parse_error(lexer,
+ "expected specifiers, got '%s'"%lexer.tok )
+ while 1:
+ decl = StructDeclarator()
+ decl.parse(lexer,symbols)
+ if len(decl)==0:
+ self.parse_error(lexer,
+ "expected declarator, got '%s'"%lexer.tok )
+ self.append(decl)
+ ident = decl.ident
+ if ident is not None:
+ node = symbols[ident[0]]
+ if node is not None:
+ self.parse_error(lexer ,
+ "%s already defined as %s"%(ident,node))
+ else:
+ if declspec.is_typedef():
+ self.parse_error(lexer,"typedef in struct or union")
+ else:
+ symbols[ident[0]] = self
+ if lexer.tok == ';':
+ break
+ self.consume(lexer,',')
+ self.consume(lexer,';')
+
+
+class TransUnit(Node,Parser):
+ """
+ """
+ def __init__(self,*items,**kw):
+ Node.__init__(self,*items,**kw)
+
+ def parse(self,s,verbose=0):
+ self.symbols = Symbols()
+ self.lexer = Lexer(s,verbose=verbose) #,host=__module__)
+ node = None
+ while self.lexer.tok:
+ node=Declaration()
+ node.parse(self.lexer,self.symbols)
+ #sys.stderr.write( "# line %s\n"%self.lexer.lno )
+ if node:
+ self.append(node)
+ #node.psource()
+ #print node.deepstr(),'\n'
+ #node.act()
+
+ def strip(self,files):
+ " leave only the declarations from <files> "
+ i=0
+ while i<len(self):
+ if self[i].file in files:
+ i=i+1
+ else:
+ self.pop(i)
+
+ def strip_filter(self,cb):
+ " leave only the declarations such that cb(file) "
+ i=0
+ while i<len(self):
+ if cb(self[i].file):
+ i=i+1
+ else:
+ self.pop(i)
+
+ def assert_no_dups(self):
+ check={}
+ for node in self.nodes():
+ assert not check.has_key(id(node))
+ check[id(node)]=1
+
+
+
+try:
+ import NoModule
+ import psyco
+ from psyco.classes import *
+except ImportError:
+ class _psyco:
+ def jit(self): pass
+ def bind(self, f): pass
+ def proxy(self, f): return f
+ psyco = _psyco()
+psyco.bind( Lexer.get_token )
+psyco.bind( Node )
+
+def run0():
+ verbose = 0
+ if not sys.argv[1:]:
+ s = sys.stdin.read()
+ if sys.argv[1:]:
+ s = sys.argv[1]
+ #if sys.argv[2:]:
+ #verbose = int(sys.argv[2])
+ if 0:
+ import profile
+ profile.run('TransUnit(s)','prof.out')
+ import pstats
+ p=pstats.Stats('prof.out')
+ p.strip_dirs().sort_stats(-1).print_stats()
+ else:
+ node = TransUnit(verbose = 1 )
+ node.parse(s)
+ node.act(1,1,1)
+
+def run1():
+ cstr = "char *(*)() ,"
+ node = AbstractDeclarator()
+ node.parse( Lexer(cstr,True), Symbols() )
+ print node.deepstr()
+
+if __name__=="__main__":
+ pass
+
+
--- /dev/null
+#!/usr/bin/env python
+
+#
+# Copyright (c) 2005, National ICT Australia
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the
+# distribution.
+# * Neither the name of National ICT Australia nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+# File : crest/utils/dump.py
+#
+# Author : Simon Burton (simon@arrowtheory.com)
+#
+# Created : 28 March 2005
+#
+# Updated :
+#
+
+"""
+Some heavy duty debug tools here...
+"""
+
+import sys
+import logging
+import gc
+
+def dump( expr="", info="", indent="" ):
+ """
+ print calling function name,
+ and python expression string <expr>, as evaluated in the calling function.
+ Note: we split <expr> on commas.
+ """
+ try:
+ assert 0
+ except:
+ tp, val, tb = sys.exc_info()
+ frame = tb.tb_frame
+ frame = frame.f_back # our caller
+ name = frame.f_code.co_name
+ if name=='?':
+ name=''
+ else:
+ name=name+": "
+ data = eval( expr, frame.f_globals, frame.f_locals )
+ depth = 0 # of stack
+ while frame.f_back:
+ depth += 1
+ frame = frame.f_back
+ exprs = expr.split(',')
+ if len(exprs)==1:
+ data = (data,)
+ data = tuple([ repr(x) for x in data ])
+ print indent*depth + name + info + ( ",".join( ["%s=%%s"%x for x in exprs] ) % data )
+
+
+def test_dump():
+ # this is our function we are debugging
+ def foo( a, b ):
+ c = a + b
+ dump( "a, b, a+b, c", "c should be a+b: " )
+ # now we call it
+ foo( 5, 77 )
+
+class Tracer(object):
+ singleton = None
+ def __init__(self, callback, data, *filenames):
+ """
+ trace execution and call the callback on each line
+ """
+ if Tracer.singleton is not None:
+ raise Exception, "Only one trace object supported at a time"
+ Tracer.singleton = self
+ self.filenames = filenames
+ self.callback = callback
+ self.data = data
+ sys.settrace(self)
+ def trace( self, frame, event, arg ):
+ code = frame.f_code
+# print "trace:", event, arg, frame.f_lineno, frame.f_locals
+ name, lineno, filename = code.co_name, frame.f_lineno, code.co_filename
+ lcl, gbl = frame.f_locals, frame.f_globals
+ depth = -1 # depth of stack
+ while frame.f_back:
+ depth += 1
+ frame = frame.f_back
+ msg = " "*depth+"%s:%s in %s()"%(filename, lineno, name)
+ self.callback( self.data, filename, lineno, name, lcl, gbl )
+ def trace_local( self, frame, event, arg ):
+ code = frame.f_code
+# print "trace_local:", event, arg, frame.f_lineno, frame.f_locals
+ name, lineno, filename = code.co_name, frame.f_lineno, code.co_filename
+ lcl, gbl = frame.f_locals, frame.f_globals
+ self.callback( self.data, filename, lineno, name, lcl, gbl )
+# return None
+ return self.trace_local
+ def __call__( self, frame, event, arg ):
+# print "trace:", event, arg, frame.f_locals
+ code = frame.f_code
+ filename = code.co_filename
+# print filename
+ if not self.filenames or filename.split('/')[-1] in self.filenames:
+ self.trace(frame,event,arg)
+ return self.trace_local
+ def fini(self):
+ sys.settrace(None)
+ Tracer.singleton = None
+
+
+class Coverage(Tracer):
+ def __init__(self, filename):
+ """
+ """
+ Tracer.__init__( self, self.callback, {}, filename )
+ self.filename = filename
+ def callback( self, data, filename, lno, name, *args ):
+ data[lno] = name
+ def save( self ):
+# print 'save:', self.data
+ ifile = open( self.filename )
+ filename = self.filename[:-3]+'.cov.py' # keep .py extension
+ ofile = open( filename, 'w+' )
+ lno = 1
+ while 1:
+ line = ifile.readline()
+ if not line:
+ break
+ c = ' '
+ if lno in self.data:
+ c = '+'
+ ofile.write( c + line )
+ lno += 1
+
+class Watcher(Tracer):
+ def __init__(self, expr, everyline=False, gbls={}, lcls={}, *filenames):
+ Tracer.__init__( self, self.callback, None, *filenames )
+ self.expr = expr
+ self.gbls = gbls
+ self.lcls = lcls
+ self.msg = ""
+ def callback(self, data, filename, lno, name, *args):
+ try:
+ msg = str( eval(self.expr, self.gbls, self.lcls) )
+ except Exception, e:
+ msg = str(e)
+ if msg != self.msg:
+ print "function:", name, ":", lno-1
+ print "%s: %s" % ( self.expr, msg )
+ self.msg = msg
+
+
+class TraceLogger(object): # inherit from Tracer ?
+ singleton = None
+ def __init__(self, *filenames):
+ " log all function calls originating in a file from filenames "
+ if TraceLogger.singleton is not None:
+ raise Exception, "Only one trace object supported at a time"
+ TraceLogger.singleton = self
+ self.filenames = filenames
+ self.loggers = {} # map names to loggers
+ self.hd = logging.StreamHandler(sys.stdout)
+# fm = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
+ fm = logging.Formatter('%(message)s')
+ self.hd.setFormatter(fm)
+ sys.settrace(self)
+ def get_logger(self, name):
+ logger = self.loggers.get(name, None)
+ if logger is None:
+ logger = logging.getLogger(name)
+ logger.addHandler( self.hd )
+ logger.setLevel(1)
+ return logger
+ def trace( self, frame, event, arg ):
+ code = frame.f_code
+# print "trace_local:", event, arg, frame.f_lineno, frame.f_locals
+ name, lineno, filename = code.co_name, frame.f_lineno, code.co_filename
+ lcl, gbl = frame.f_locals, frame.f_globals
+ depth = -1 # depth of stack
+ while frame.f_back:
+ depth += 1
+ frame = frame.f_back
+ msg = " "*depth+"%s:%s in %s()"%(filename, lineno, name)
+ self.get_logger( gbl.get("__name__","?") ).log(1, msg)
+ def trace_local( self, frame, event, arg ):
+ return None
+ return self.trace_local
+ def __call__( self, frame, event, arg ):
+# print "trace:", event, arg, frame.f_locals
+ code = frame.f_code
+ filename = code.co_filename
+ if not self.filenames or filename in self.filenames:
+ self.trace(frame,event,arg)
+# return self.trace_local
+ def fini(self):
+ sys.settrace(None)
+ TraceLogger.singleton = None
+
+
+def test_trace():
+ def fa(a,b):
+ return a+b
+ def fb(x,y):
+ z = fa(x,y)
+ return z
+ trace = TraceLogger(__file__)
+ sys.settrace(trace)
+ fb(3,4)
+ trace.fini()
+
+
+class MemLogger(object):
+ def __init__( self ):
+ gc.set_debug(gc.DEBUG_LEAK)
+ self.tank = {}
+ for ob in gc.get_objects():
+ self.tank[id(ob)]=None
+ self.watchtank={}
+
+ def watch(self, ob):
+ self.watchtank[id(ob)] = None
+
+ def dumpinfo(self, ob):
+ print id(ob), type(ob),
+ if hasattr(ob,'__name__'): print "\t", ob.__name__,
+ if hasattr(ob,'shape'): print "\t", ob.shape,
+ print
+
+ def dumpfrom(self, ob, depth=1, indent=0):
+ if depth==0: return
+ fromobs = gc.get_referrers(ob)
+ if fromobs:
+ for _ob in fromobs:
+ print ' '*indent+' ',; dumpinfo(_ob)
+ dumpfrom(_ob,depth-1,indent+1)
+
+ def find(self, ob, tp, tank=None):
+ " traverse object space, from ob, looking for object of type tp "
+ # XXX this needs to be breadth first XXX
+ if tank is None:
+ tank={id(ob):None}
+ # search = gc.get_referrers(ob)+gc.get_referents(ob)
+ search = gc.get_referrers(ob)
+ for _ob in search:
+ if not id(_ob) in tank:
+ tank[id(_ob)] = None # been here
+ if type(_ob)==tp:
+ return _ob # got it
+ _ob = self.find(_ob,tp,tank)
+ if _ob is not None:
+ return _ob
+
+ def check(self, msg=None):
+ " look for newly created objects "
+ foundone=False
+ newtank={}
+ for ob in gc.get_objects():
+ newtank[id(ob)]=None
+ if not self.tank.has_key(id(ob)): # and id(ob) in watchtank:
+ print " NEW:",
+ dumpinfo(ob)
+ # dumpfrom(ob,1,2)
+ # _ob = self.find(ob,type(ob))
+ # if _ob:
+ # print " found:", dumpinfo(_ob)
+ # toobs = gc.get_referents(ob)
+ # if toobs:
+ # print "\trefers to:"
+ # for _ob in toobs:
+ # print "\t ",; dumpinfo(_ob)
+ self.tank[id(ob)] = None
+ foundone = True
+ # for key in newtank:
+ # if not self.tank.has_key(key):
+ # print " DEL:", key
+ ## print "collect:", gc.collect() # == 0 (OK)
+ if foundone:
+ if msg: print "check:", msg
+ print "objects:", len(gc.get_objects())
+ print "--"*30
+ sleep(0.04)
+
+def test_mem():
+ #XXX
+ pass
+
+if __name__ == "__main__":
+ test_trace()
+
+
--- /dev/null
+#!/usr/bin/env python
+""" genpyx.py - parse c declarations
+
+(c) 2002, 2003, 2004, 2005 Simon Burton <simon@arrowtheory.com>
+Released under GNU LGPL license.
+
+version 0.xx
+
+This is a module of mixin classes for ir.py .
+
+Towards the end of ir.py our global class definitions
+are remapped to point to the class definitions in ir.py .
+So, for example, when we refer to Node we get ir.Node .
+
+"""
+
+import sys
+from datetime import datetime
+from sets import Set
+
+from dump import dump
+
+# XX use this Context class instead of all those kw dicts !! XX
+class Context(object):
+ " just a record (struct) "
+ def __init__( self, **kw ):
+ for key, value in kw.items():
+ setattr( self, key, value )
+ def __getattr__( self, name ):
+ return None # ?
+ def __getitem__( self, name ):
+ return getattr(self, name)
+
+class OStream(object):
+ def __init__( self, filename=None ):
+ self.filename = filename
+ self.tokens = []
+ self._indent = 0
+ def put( self, token="" ):
+ assert type(token) is str
+ self.tokens.append( token )
+ def startln( self, token="" ):
+ assert type(token) is str
+ self.tokens.append( ' '*self._indent + token )
+ def putln( self, ln="" ):
+ assert type(ln) is str
+ self.tokens.append( ' '*self._indent + ln + '\n')
+ def endln( self, token="" ):
+ assert type(token) is str
+ self.tokens.append( token + '\n')
+ def indent( self ):
+ self._indent += 1
+ def dedent( self ):
+ self._indent -= 1
+ assert self._indent >= 0, self._indent
+ def join( self ):
+ return ''.join( self.tokens )
+ def close( self ):
+ s = ''.join( self.tokens )
+ f = open( self.filename, 'w' )
+ f.write(s)
+
+#
+###############################################################################
+#
+
+class Node(object):
+ """
+ tree structure
+ """
+ _unique_id = 0
+ def get_unique_id(cls):
+ Node._unique_id += 1
+ return Node._unique_id
+ get_unique_id = classmethod(get_unique_id)
+
+# XX toks: use a tree of tokens: a list that can be push'ed and pop'ed XX
+ def pyxstr(self,toks=None,indent=0,**kw):
+ """
+ Build a list of tokens; return the joined tokens string
+ """
+# dump( "self.__class__.__name__,toks" )
+ if toks is None:
+ toks = []
+ for x in self:
+ if isinstance(x,Node):
+ x.pyxstr(toks, indent, **kw)
+ else:
+ toks.insert(0,str(x)+' ')
+ s = ''.join(toks)
+# dump( "self.__class__.__name__,s" )
+ return s
+
+ def pyx_wrapper(self, tp_tank, ostream=None, **kw):
+ if ostream is None:
+ ostream = OStream()
+ for node in self:
+ if isinstance(node, Node):
+ ostream = node.pyx_wrapper( tp_tank, ostream=ostream, **kw )
+ return ostream
+
+#
+#################################################
+
+class Named(object):
+ "has a .name property"
+ pass
+
+class BasicType(object):
+ "float double void char int"
+ pass
+
+class Qualifier(object):
+ "register signed unsigned short long const volatile inline"
+ def pyxstr(self,toks=None,indent=0,**kw):
+ if toks is None:
+ toks = []
+ x = self[0]
+ if x not in ( 'const','volatile','inline','register'): # ignore these
+ toks.insert(0,str(x)+' ')
+ s = ''.join(toks)
+ return s
+
+class StorageClass(object):
+ "extern static auto"
+ def pyxstr(self,toks=None,indent=0,**kw):
+ return ""
+
+class Ellipses(object):
+ "..."
+ pass
+
+class GCCBuiltin(BasicType):
+ "things with __builtin prefix"
+ pass
+
+class Identifier(object):
+ """
+ """
+ def pyxstr(self,toks=None,indent=0,**kw):
+ if toks is None:
+ toks=[]
+ if self.name:
+ toks.append( self.name )
+ return " ".join(toks)
+
+class TypeAlias(object):
+ """
+ typedefed things, eg. size_t
+ """
+ def pyxstr(self,toks=None,indent=0,cprefix="",**kw):
+# dump( "self.__class__.__name__,toks" )
+ if toks is None:
+ toks = []
+ for x in self:
+ if isinstance(x,Node):
+ x.pyxstr(toks, indent, cprefix=cprefix, **kw)
+ else:
+ s = str(x)+' '
+ if cprefix:
+ s = cprefix+s
+ toks.insert(0,s)
+ s = ''.join(toks)
+# dump( "self.__class__.__name__,s" )
+ return s
+
+class Function(object):
+ """
+ """
+ def pyxstr(self,toks,indent=0,**kw):
+ #print '%s.pyxstr(%s)'%(self,toks)
+ _toks=[]
+ assert len(self)
+ i=0
+ while isinstance(self[i],Declarator):
+ if not self[i].is_void():
+ _toks.append( self[i].pyxstr(indent=indent, **kw) )
+ i=i+1
+ toks.append( '(%s)'% ', '.join(_toks) )
+ while i<len(self):
+ self[i].pyxstr(toks, indent=indent, **kw)
+ i=i+1
+ return " ".join(toks)
+
+class Pointer(object):
+ """
+ """
+ def pyxstr(self,toks,indent=0,**kw):
+ assert len(self)
+ node=self[0]
+ toks.insert(0,'*')
+ if isinstance(node,Function):
+ toks.insert(0,'(')
+ toks.append(')')
+ elif isinstance(node,Array):
+ toks.insert(0,'(')
+ toks.append(')')
+ return Node.pyxstr(self,toks,indent, **kw)
+
+class Array(object):
+ """
+ """
+ def pyxstr(self,toks,indent=0,**kw):
+ if self.size is None:
+ toks.append('[]')
+ else:
+ try:
+ int(self.size)
+ toks.append('[%s]'%self.size)
+ except:
+ toks.append('[]')
+ return Node( *self[:-1] ).pyxstr( toks,indent, **kw )
+
+class Tag(object):
+ " the tag of a Struct, Union or Enum "
+ pass
+
+class Taged(object):
+ "Struct, Union or Enum "
+ pass
+
+class Compound(Taged):
+ "Struct or Union"
+ def pyxstr(self,_toks=None,indent=0,cprefix="",shadow_name=True,**kw):
+ if _toks is None:
+ _toks=[]
+ names = kw.get('names',{})
+ kw['names'] = names
+ tag_lookup = kw.get('tag_lookup')
+ if self.tag:
+ tag=self.tag.name
+ else:
+ tag = ''
+ if isinstance(self,Struct):
+ descr = 'struct'
+ elif isinstance(self,Union):
+ descr = 'union'
+ _node = names.get(self.tag.name,None)
+ if ( _node is not None and _node.has_members() ) or \
+ ( _node is not None and not self.has_members() ):
+ descr = '' # i am not defining myself here
+# print "Compound.pyxstr", tag
+# print self.deepstr()
+ if descr:
+ if cprefix and shadow_name:
+ tag = '%s%s "%s"'%(cprefix,tag,tag)
+ elif cprefix:
+ tag = cprefix+tag
+ toks = [ descr+' '+tag ] # struct foo
+ if self.has_members():
+ toks.append(':\n')
+ for decl in self[1:]: # XX self.members
+ toks.append( decl.pyxstr(indent=indent+1, cprefix=cprefix, shadow_name=shadow_name, **kw)+"\n" ) # shadow_name = False ?
+ elif not tag_lookup.get( self.tag.name, self ).has_members():
+ # define empty struct here, it's the best we're gonna get
+ toks.append(':\n')
+ toks.append( " "*(indent+1) + "pass\n" )
+ else:
+ if cprefix: # and shadow_name:
+ tag = cprefix+tag
+ toks = [ ' '+tag+' ' ] # foo
+ while toks:
+ _toks.insert( 0, toks.pop() )
+ return "".join( _toks )
+
+class Struct(Compound):
+ """
+ """
+ def pyx_wrapper(self, tp_tank, ostream=None, **kw):
+ cobjects = kw['cobjects']
+ ostream.putln( '# ' + self.cstr() )
+ name = self.tag and self.tag.name
+ if name and not name in cobjects:
+ ostream.putln("%s = Meta( '%s', (CStruct,), {} )" % ( name, name ))
+ cobjects[name] = self
+# ostream.putln( self.deepstr(comment=True) )
+ return ostream
+
+class Union(Compound):
+ """
+ """
+ pass
+
+
+class Enum(Taged):
+ """
+ """
+ def pyxstr(self,_toks=None,indent=0,cprefix="",shadow_name=True,**kw):
+ if _toks is None:
+ _toks=[]
+ names = kw.get('names',{})
+ kw['names'] = names
+ if self.tag:
+ tag=self.tag.name
+ else:
+ tag = ''
+ _node = names.get(self.tag.name,None)
+ if ( _node is not None and _node.has_members() ) or \
+ ( _node is not None and not self.has_members() ):
+ descr = '' # i am not defining myself here
+ else:
+ descr = 'enum'
+ if descr:
+# if not names.has_key(self.tag.name):
+ toks = [ descr+' '+tag ] # enum foo
+ toks.append(':\n')
+ idents = [ ident for ident in self.members if ident.name not in names ]
+ for ident in idents:
+ if cprefix and shadow_name:
+ ident = ident.clone()
+ ident.name = '%s%s "%s"' % ( cprefix, ident.name, ident.name )
+# else: assert 0
+ toks.append( ' '+' '*indent + ident.pyxstr(**kw)+",\n" )
+ names[ ident.name ] = ident
+ if not idents:
+ # empty enum def'n !
+ # assert 0 # should be handled by parents...
+ toks.append( ' '+' '*indent + "pass\n" )
+ else:
+ toks = [ ' '+tag+' ' ] # foo
+ while toks:
+ _toks.insert( 0, toks.pop() )
+ return "".join( _toks )
+
+#CChar char
+#CSChar signed char
+#CUChar unsigned char
+#CShort short
+#CUShort unsigned short
+#CInt int
+#CUInt unsigned int
+#CLong long
+#CULong unsigned long
+#CLLong long long
+#CULLong unsigned long long
+#CFloat float
+#CDouble double
+#CLDouble long double
+class TPSet(object):
+ char_tps = 'CChar CSChar CUChar'.split()
+ short_tps = 'CShort CUShort'.split()
+ int_tps = 'CInt CUInt CShort CUShort CLong CULong CLLong CULLong'.split()
+ signed_tps = 'CSChar CShort CInt CLong CLLong'.split()
+ unsigned_tps = 'CUInt CUChar CUShort CULong CULLong'.split()
+ long_tps = 'CLong CULong CLLong CULLong CLDouble'.split()
+ long_long_tps = 'CLLong CULLong'.split()
+ float_tps = ['CFloat']
+ double_tps = 'CDouble CLDouble'.split()
+ void_tps = ['CVoid']
+ all_tps = 'CVoid CChar CSChar CUChar CShort CUShort CInt CUInt CLong CULong CLLong CULLong CFloat CDouble CLDouble'.split()
+ def __init__( self ):
+ self.tps = self.all_tps
+ self.toks = []
+ def intersect( self, tps ):
+# print ' ', self.tps, tps
+ self.tps = [ tp for tp in tps if tp in self.tps ]
+# print ' ', self.tps
+ def parse( self, tok ):
+ attr = '%s_tps' % tok
+ if hasattr( self, attr ):
+ self.intersect( getattr(self,attr) )
+ if tok == 'long' and 'long' in self.toks:
+ self.intersect( self.long_long_tps )
+ self.toks.append(tok)
+# if not self.tps:
+# return 'CObject'
+ return self.tps[0]
+
+class VarAdaptor(object):
+ """
+ An adaptor generates code for converting between a python variable
+ and a c variable.
+ """
+ def __init__( self, ostream, py_varname = None, c_varname = None, node = None, cobjects = None ):
+ self.ostream = ostream
+ self.py_varname = py_varname
+ self.c_varname = c_varname
+# node = node.clone()
+ self.onode = node.clone() # original node
+ node = Declarator().init_from( node.clone() )
+ if c_varname is not None:
+ node.name = c_varname
+ # turn array's into pointer's :
+ for _node in node.nodes(): # bottom-up search
+ for idx in range(len(_node)):
+ if isinstance(_node[idx],Array):
+ _node[idx] = _node[idx].to_pointer()
+ node.invalidate() # so that we recompute cbasetype
+ self.node = node
+ self.bt = node.cbasetype() # WARNING: cbasetype may be cached
+ self.cobjects = cobjects
+ assert type(cobjects) is dict, repr(cobjects)
+ def declare_py_arg( self ):
+ if self.node.is_pyxnative():
+ self.ostream.put( self.py_varname )
+ elif self.node.is_pointer_to_char():
+ self.ostream.put( self.py_varname )
+ else:
+ self.ostream.put( "%s %s" % (self.node.pyx_adaptor_decl(self.cobjects), self.py_varname) )
+ def declare_cvar(self, names, tag_lookup, cprefix=""):
+ if not self.node.is_void():
+ self.ostream.putln( self.node.pyxstr( indent=0, names=names, tag_lookup=tag_lookup, cprefix=cprefix, shadow_name=False ) )
+# self.ostream.putln( "" )
+ def declare_pyvar(self): #, names, tag_lookup, cprefix=""):
+ if not self.node.is_void() and not self.node.is_pyxnative():
+ s = 'cdef %s %s' % ( self.node.pyx_adaptor_decl(self.cobjects), self.py_varname )
+ self.ostream.putln( s )
+# self.ostream.putln( "" )
+ def get_c_cast(self, names, tag_lookup, cprefix=""):
+ # doh! node is already a pointer !! so use onode:
+ node = self.onode.clone()
+ node.name = ""
+ cast = node.pyxstr(names=names,tag_lookup=tag_lookup, use_cdef = False, cprefix=cprefix, shadow_name=False)
+ return "<%s>"%cast
+ def get_c_type(self, names, tag_lookup, cprefix=""):
+ # doh! node is already a pointer !! so use onode:
+ node = self.onode.clone()
+ node.name = ""
+ tp = node.pyxstr(names=names,tag_lookup=tag_lookup, use_cdef = False, cprefix=cprefix, shadow_name=False)
+ return tp
+ def get_c_from_pyvar(self, names, tag_lookup, cprefix=""):
+ assert names != None
+ assert tag_lookup != None
+ if self.node.is_pyxnative():
+ s = self.py_varname
+ elif self.node.is_pointer_to_char():
+ s = "get_charp(%s)" % self.py_varname
+ else:
+ p_arg = self.node.pointer_to()
+ cast = p_arg.pyxstr(names=names,tag_lookup=tag_lookup, use_cdef = False, cprefix=cprefix, shadow_name=False)
+ s = "(<%s>(%s.p))[0]" % ( cast, self.py_varname )
+ if s.count('cdef'):
+ print p_arg.deepstr()
+ assert 0
+ return s
+ def assign_to_compound_member(self, compound_adaptor, names, tag_lookup, cprefix=""):
+ assert names != None
+ assert tag_lookup != None
+ if self.node.is_pyxnative() or self.node.is_pointer_to_char():
+ if self.node.is_pyxnative():
+ rhs = self.py_varname
+ else:
+ rhs = "get_charp(%s)" % self.py_varname
+ self.ostream.putln( "(<%s%s*>(%s.p))[0].%s = %s" %\
+ ( cprefix, compound_adaptor.node.name, compound_adaptor.py_varname,
+ self.onode.name, rhs ) )
+ else:
+ p_arg = self.node.pointer_to()
+ cast = p_arg.pyxstr(names=names,tag_lookup=tag_lookup, use_cdef = False, cprefix=cprefix, shadow_name=False)
+ rhs = "(<%s>(%s.p))" % ( cast, self.py_varname )
+ self.ostream.putln( "%smemcpy( &((<%s%s*>(%s.p))[0].%s), %s, sizeof(%s) )" %\
+ ( cprefix, cprefix, compound_adaptor.node.name, compound_adaptor.py_varname, self.onode.name,
+ rhs,
+ self.get_c_type(names, tag_lookup, cprefix),
+ ) )
+ def assign_from_compound_member(self, compound_adaptor, names, tag_lookup, cprefix=""):
+ assert names != None
+ assert tag_lookup != None
+ if self.node.is_pyxnative():
+ self.ostream.putln( "%s = (<%s%s*>(%s.p))[0].%s" %\
+ (
+ self.py_varname, cprefix, compound_adaptor.node.name, compound_adaptor.py_varname,
+ self.onode.name,
+ ) )
+ else:
+ p_arg = self.node.pointer_to()
+ cast = p_arg.pyxstr(names=names,tag_lookup=tag_lookup, use_cdef = False, cprefix=cprefix, shadow_name=False)
+ s = "(<%s>(%s.p))[0]" % ( cast, self.py_varname )
+ self.ostream.putln( "%s = (<%s%s*>(%s.p))[0].%s" %\
+ ( s,
+ cprefix, compound_adaptor.node.name, compound_adaptor.py_varname,
+ self.onode.name ))
+ def get_c_arg(self):
+ return self.c_varname
+ def assign_to_cvar(self, names, tag_lookup, cprefix=""):
+ s = "%s = %s" % ( self.c_varname, self.get_c_from_pyvar( names, tag_lookup, cprefix ) )
+ self.ostream.putln( s )
+ def assign_cvar_from( self, s ):
+ if not self.node.is_void():
+ s = '%s = %s' % ( self.c_varname, s )
+ self.ostream.putln( s )
+# def assign_pyvar_to( self, s ):
+# s = "%s = %s" % ( s,
+ def init_pyvar( self, cobjects ):
+ self.ostream.putln('%s = %s()' % ( self.py_varname, self.node.pyx_adaptor_name(cobjects) ) )
+
+ def assign_to_pyvar( self, cobjects, cprefix ):
+ if self.bt.is_pyxnative(): # warning: char* assumed null terminated
+ self.ostream.putln('%s = %s'%(self.py_varname, self.c_varname))
+ elif not self.bt.is_void():
+# self.ostream.putln('# ' + ret.cstr() )
+# self.ostream.putln('%s = %s()' % ( self.py_varname, self.node.pyx_adaptor_name(cobjects) ) )
+ self.init_pyvar( cobjects )
+# self.ostream.putln('%smemcpy( ret.p, &c_ret, ret.sizeof )'%cprefix)
+ self.ostream.putln('%smemcpy( %s.p, &%s, %s.sizeof )'%(cprefix, self.py_varname, self.c_varname, self.py_varname ))
+ def return_pyvar( self ):
+ if not self.bt.is_void():
+ self.ostream.putln('return %s' % self.py_varname)
+ def return_cvar( self ):
+ if not self.bt.is_void():
+ self.ostream.putln('return %s' % self.c_varname)
+
+class CallbackAdaptor(object):
+ """ Emit code for generating c-function (cdef) callback.
+ """
+ def __init__(self, ostream, node, name):
+ self.ostream = ostream
+ self.node = node # the function declaration
+ self.name = name # what we should call ourself
+ def define_func(self, names, tag_lookup, cprefix="", cobjects=None, **kw):
+ decl = self.node.deref() # clone
+ function = decl.function
+
+# self.ostream.putln()
+# self.ostream.putln('#'+self.node.cstr())
+# self.ostream.putln('#'+decl.cstr())
+ arg_adaptors = []
+ decl.name = self.name
+ if not function.deepfind(Ellipses):
+
+ # build adaptors for function arguments
+ for i, arg in enumerate(function.args):
+ arg.name = "c_arg%s"%i
+ py_varname = "arg%s"%i
+ adaptor = VarAdaptor( self.ostream, py_varname=py_varname, c_varname=arg.name, node=arg, cobjects=cobjects )
+ arg_adaptors.append( adaptor )
+ ret_adaptor = VarAdaptor( self.ostream, py_varname='ret', c_varname='c_ret', node=function.ret, cobjects=cobjects )
+
+ # declare function
+ self.ostream.putln( decl.pyxstr(indent=0, cprefix=cprefix, shadow_name=False, **kw ) + ': # callback' )
+ self.ostream.indent()
+ self.ostream.putln( '"%s"'%decl.cstr() )
+# self.ostream.putln( " ob = <object>c_arg%s" % i ) # the last arg is a PyObject*
+
+ # declare local c variables
+ for adaptor in arg_adaptors:
+ adaptor.declare_pyvar()
+ ret_adaptor.declare_cvar(names=names, tag_lookup=tag_lookup, cprefix=cprefix)
+
+ # convert to python types
+ for adaptor in arg_adaptors:
+ adaptor.assign_to_pyvar( cobjects, cprefix )
+ ret_adaptor.declare_pyvar()
+
+ # call python function
+ args = [adaptor.py_varname for adaptor in arg_adaptors]
+# for i, arg in enumerate(function.args[:-1]): # skip the last arg
+# for i, arg in enumerate(function.args):
+# if arg.is_pyxnative(): # use it directly
+# args.append('c_arg%s'%i)
+# else:
+# args.append('arg%s'%i)
+# self.ostream.putln( "arg%s = CPointer( value = <long>c_arg%s )" % (i, i) )
+ self.ostream.putln( "# manually add code for finding ob here #")
+ ret_adaptor.assign_cvar_from( "ob(%s)" % ','.join(args) )
+ ret_adaptor.assign_to_pyvar( cobjects=cobjects, cprefix=cprefix )
+
+ # return c variable
+ ret_adaptor.return_cvar()
+ self.ostream.dedent()
+ return self.ostream
+
+#class CallbackArgAdaptor(object):
+# """
+# Generate code for callback arguments.
+# These objects actually consists of two Adaptor's.
+# """
+# def __init__( self, ostream, func_adaptor, data_adaptor ):
+# self.ostream = ostream
+# self.func_adaptor = func_adaptor
+# self.data_adaptor = data_adaptor
+# def declare_py_arg( self ):
+## self.func_adaptor.declare_py_arg()
+# self.ostream.put( self.func_adaptor.py_varname )
+# self.ostream.put(', ')
+## self.data_adaptor.declare_py_arg()
+# self.ostream.put( self.data_adaptor.py_varname )
+# def declare_cvar(self, names, tag_lookup, cprefix=""):
+# self.func_adaptor.declare_cvar(names, tag_lookup, cprefix)
+# self.data_adaptor.declare_cvar(names, tag_lookup, cprefix)
+# def get_c_arg(self):
+# return "%s,%s"%(self.func_adaptor.get_c_arg(),self.data_adaptor.get_c_arg())
+# def assign_to_cvar(self, names, tag_lookup, cprefix=""):
+# # the c callback argument comes from a fixed c callback function
+# cbname = self.func_adaptor.onode.get_pyxtpname() # use the onode
+# self.ostream.putln( '%s = %s' % ( self.func_adaptor.c_varname, cbname ) )
+# # the void* data argument is the python callable
+## self.ostream.putln( '%s = <void*>%s' % ( self.data_adaptor.c_varname, self.data_adaptor.py_varname ) )
+# self.ostream.putln( '%s = <void*>%s' % ( self.data_adaptor.c_varname, self.func_adaptor.py_varname ) )
+
+class CallbackArgAdaptor(VarAdaptor):
+ def declare_cvar(self,*args,**kw):
+ pass
+ def assign_to_cvar(self,*args,**kw):
+ pass
+
+class FunctionAdaptor(object):
+ def __init__(self, ostream, node, special_stream):
+ self.ostream = ostream
+ self.node = node # the function declaration
+ self.special_stream = special_stream
+ def define_func(self, names=None, tag_lookup=None, cprefix="", cobjects=None, **kw):
+ # emit 'def' fn that calls this function
+ # XX how to call varargs functions ? va_args functions ?
+
+ # first we remove any void argument:
+ function = self.node.function
+ if len(function.args)==1 and function.args[0].is_void():
+ function = function.clone()
+ function.pop(0) # XX encapsulation
+
+ ostream = self.ostream
+ # check if there are function pointer args:
+ for arg in function.args:
+ if arg.is_pointer_to_fn():
+ ostream = self.special_stream # these are special, to be hand edited
+
+ arg_adaptors = []
+ i = 0
+ while i < len(function.args):
+ arg = function.args[i]
+# # callback hackery:
+# if arg.is_callback() and (i+1<len(function.args)) \
+# and (function.args[i+1].pointer) \
+# and (function.args[i+1].deref().is_void()):
+# py_varname = 'arg%s'%i
+# func_adaptor = VarAdaptor(
+# ostream, py_varname=py_varname, c_varname='c_'+py_varname, node=arg, cobjects=cobjects )
+# i += 1
+# arg = function.args[i]
+# py_varname = 'arg%s'%i
+# data_adaptor = VarAdaptor(
+# ostream, py_varname=py_varname, c_varname='c_'+py_varname, node=arg, cobjects=cobjects )
+# i += 1
+# adaptor = CallbackArgAdaptor( ostream, func_adaptor, data_adaptor )
+# arg_adaptors.append( adaptor )
+# return ostream
+ if arg.is_pointer_to_fn():
+ cbname = self.node.name + "_arg%s_callback"%i
+ adaptor = CallbackAdaptor( ostream, arg, cbname )
+ ostream = adaptor.define_func( names, tag_lookup, cprefix=cprefix, cobjects=cobjects, **kw )
+ py_varname = 'arg%s'%i
+# adaptor = VarAdaptor( ostream, py_varname=py_varname, c_varname='c_'+py_varname, node=arg, cobjects=cobjects )
+# adaptor = VarAdaptor( ostream, py_varname=None, c_varname=cbname, node=arg, cobjects=cobjects )
+ adaptor = CallbackArgAdaptor( ostream, py_varname=py_varname, c_varname=cbname, node=arg, cobjects=cobjects )
+ arg_adaptors.append( adaptor )
+ i += 1
+ else:
+ if arg.name and 0: # disabled; arg.name may be an illegal name
+ py_varname = arg.name
+ else:
+ py_varname = 'arg%s'%i
+ adaptor = VarAdaptor( ostream, py_varname=py_varname, c_varname='c_'+py_varname, node=arg, cobjects=cobjects )
+ arg_adaptors.append( adaptor )
+ i += 1
+ ret_adaptor = VarAdaptor( ostream, py_varname='ret', c_varname='c_ret', node=function.ret, cobjects=cobjects )
+
+ # declare the function:
+ ostream.startln('def %s(' % self.node.name )
+ for i, adaptor in enumerate(arg_adaptors):
+ adaptor.declare_py_arg()
+ if i+1 < len(arg_adaptors):
+ ostream.put( ', ' )
+ ostream.endln('):')
+ ostream.indent()
+ ostream.putln( '"%s"' % self.node.cstr() )
+
+ ret = function.ret
+ rtp = ret.cbasetype() # WARNING: cbasetype may be cached
+
+ # declare the local c vars:
+ for adaptor in arg_adaptors:
+ adaptor.declare_cvar(names=names, tag_lookup=tag_lookup, cprefix=cprefix)
+ ret_adaptor.declare_cvar(names=names, tag_lookup=tag_lookup, cprefix=cprefix)
+ ret_adaptor.declare_pyvar() #names=names, tag_lookup=tag_lookup, cprefix=cprefix)
+
+ # convert the python args:
+ for adaptor in arg_adaptors:
+ adaptor.assign_to_cvar(names=names, tag_lookup=tag_lookup, cprefix=cprefix)
+
+ # now call the function:
+ c_args = [adaptor.get_c_arg() for adaptor in arg_adaptors]
+ ret_adaptor.assign_cvar_from( '%s%s(%s)' % ( cprefix, self.node.name, ','.join(c_args) ) )
+
+ # now return result:
+ ret_adaptor.assign_to_pyvar( cobjects=cobjects, cprefix=cprefix )
+ ret_adaptor.return_pyvar()
+
+ ostream.dedent()
+ ostream.putln()
+ return ostream
+
+
+
+class Declarator(object):
+ def is_pyxnative( self ):
+ # pyrex handles char* too
+ # but i don't know if we should make this the default
+ # sometimes we want to send a NULL, so ... XX
+ self = self.cbasetype() # WARNING: cbasetype may be cached
+ if self.is_void():
+ return False
+ if self.is_primative():
+ return True
+ if self.enum:
+ return True
+# pointer = None
+# if self.pointer:
+# pointer = self.pointer
+# elif self.array:
+# pointer = self.array
+# if pointer and pointer.spec:
+# spec = pointer.spec
+# if BasicType("char") in spec and not Qualifier("unsigned") in spec:
+# # char*, const char*
+## print self.deepstr()
+# return True
+ return False
+
+ def _pyxstr( self, toks, indent, cprefix, use_cdef, shadow_name, **kw ):
+ " this is the common part of pyxstr that gets called from both Declarator and Typedef "
+ names = kw.get('names',{}) # what names have been defined ?
+ kw['names']=names
+ for node in self.nodes(): # depth-first
+ if isinstance(node,Taged):
+# print "Declarator.pyxstr", node.cstr()
+ if not node.tag.name:
+ node.tag.name = "_anon_%s" % Node.get_unique_id()
+ _node = names.get(node.tag.name,None)
+ if _node is None or node.has_members():
+ # either i am not defined at all, or this is my _real_ definition
+ # emit def'n of this node
+ toks.append( ' '*indent + 'cdef ' + node.pyxstr(indent=indent, cprefix=cprefix, shadow_name=shadow_name, **kw).strip() )
+ names[ node.tag.name ] = node
+ elif isinstance(node,GCCBuiltin) and node[0] not in names:
+# toks.append( ' '*indent + 'ctypedef long ' + node.pyxstr(indent=indent, **kw).strip() + ' # XX ??' ) # XX ??
+ toks.append( ' '*indent + 'struct __unknown_builtin ' )
+ toks.append( ' '*indent + 'ctypedef __unknown_builtin ' + node.pyxstr(indent=indent, **kw).strip() )
+ names[ node[0] ] = node
+ for idx, child in enumerate(node):
+ if type(child)==Array and not child.has_size():
+ # mutate this mystery array into a pointer XX method: Array.to_pointer()
+ node[idx] = Pointer()
+ node[idx].init_from( child ) # warning: shallow init
+ node[idx].pop() # pop the size element
+
+ def pyxstr(self,toks=None,indent=0,cprefix="",use_cdef=True,shadow_name=True,**kw):
+ " note: i do not check if my name is already in 'names' "
+ self = self.clone() # <----- NOTE
+ toks=[]
+ names = kw.get('names',{}) # what names have been defined ?
+ kw['names']=names
+
+ self._pyxstr( toks, indent, cprefix, use_cdef, shadow_name, **kw )
+
+ if self.name and not names.has_key( self.name ):
+ names[ self.name ] = self
+ if self.identifier is not None:
+ comment = ""
+ if self.name in python_kws:
+ comment = "#"
+ if cprefix and use_cdef and shadow_name:
+ # When we are defining this guy, we refer to it using the pyrex shadow syntax.
+ self.name = '%s%s "%s" ' % ( cprefix, self.name, self.name )
+ cdef = 'cdef '
+ if not use_cdef: cdef = '' # sometimes we don't want the cdef (eg. in a cast)
+ # this may need shadow_name=False:
+ toks.append( ' '*indent + comment + cdef + Node.pyxstr(self,indent=indent, cprefix=cprefix, **kw).strip() ) # + "(cprefix=%s)"%cprefix)
+ #else: i am just a struct def (so i already did that) # huh ?? XX bad comment
+# dump("self,l")
+# print self.deepstr()
+# dump("self")
+ return ' \n'.join(toks)
+
+# def get_pyxtpname( self ):
+# cstr = self.ctype().cstr()
+# name = ('__pyxtp_%s'%hash(cstr)).replace('-','m') # nasty...
+# return name
+
+ def get_pyxtpname( self ):
+ node = self.cbasetype() # WARNING: cbasetype may be cached
+# if node.pointer or node.array:
+ # function pointers need unique names
+ # because we make python callbacks with those names
+ if (node.pointer or node.array) and not node.is_pointer_to_fn():
+ name = "__pyxtp_pvoid"
+# elif isinstance(self,Typedef):
+# return self.name
+ else:
+ node.delete( StorageClass ) # hackery
+ node.deeprm( Qualifier("const") ) # hackery
+ cstr = node.cstr()
+# cstr = node.pyxstr( names = {}, tag_lookup = {}, use_cdef = False ) # XX redefines anon structs etc.
+ name = ('__pyxtp_%s'%hash(cstr)).replace('-','m') # XX nasty...
+ if cstr.replace(' ','').isalnum():
+ name = '__pyxtp_'+'_'.join( cstr.split() )
+ else:
+ name = ('__pyxtp_%s'%hash(cstr)).replace('-','m') # XX nasty...
+ return name
+
+# def handle_function_pointers(self, tp_tank, ostream, cprefix="", func_cb=None, root=None, **kw):
+# decl = self.deref()
+# fn = decl.function
+# if not fn.args:
+# return ostream # not a callback
+# arg = fn.args[-1]
+# if not arg.pointer or not arg.deref().is_void():
+# return ostream # not a callback
+#
+## ostream.putln()
+## ostream.putln('#'+self.cstr())
+## ostream.putln('#'+decl.cstr())
+#
+# name = self.get_pyxtpname()
+# tp_tank[name] = self
+# decl.name = name
+# if not fn.deepfind(Ellipses):
+# for i, arg in enumerate(fn.args):
+# arg.name = 'c_arg%s'%i
+## ostream.putln( decl.pyxstr(indent=0, cprefix="", **kw ) + ': # callback' )
+# ostream.putln( decl.pyxstr(indent=0, cprefix=cprefix, shadow_name=False, **kw ) + ': # callback' )
+# ostream.putln( ' "%s"'%decl.cstr() )
+# ostream.putln( " ob = <object>c_arg%s" % i ) # the last arg is a PyObject*
+# args = []
+# for i, arg in enumerate(fn.args[:-1]): # skip the last arg
+# if arg.is_pyxnative(): # use it directly
+# args.append('c_arg%s'%i)
+# else:
+# args.append('arg%s'%i)
+# ostream.putln( " arg%s = CPointer( value = <long>c_arg%s )" % (i, i) )
+# ostream.putln( " res = ob(%s)" % ','.join(args) )
+# if not fn.ret.is_void():
+# if fn.ret.is_pyxnative():
+# ostream.putln( " return res" )
+# return ostream
+
+ def pyx_adaptor_decl( self, cobjects ):
+ " return the name of the adaptor type for this node "
+ assert type(cobjects)==dict, repr(cobjects)
+ name = 'CObject'
+ node = self.cbasetype()
+ if node.struct:
+ name = 'CStruct'
+ elif node.pointer: # or node.array:
+ name = 'CPointer'
+ elif node.union:
+ name = 'CUnion'
+ elif node.enum:
+# name = 'CEnum'
+ name = 'CInt'
+ elif node.function: # what's this ?
+ name = 'CObject'
+ elif node.spec:
+# print self.spec
+ tpset = TPSet()
+ for _node in node.spec:
+ if type(_node) in (BasicType,Qualifier):
+ name = tpset.parse( _node.name )
+# print ' ', _node[0], name
+
+ return '_'+name
+
+
+ def pyx_adaptor_name( self, cobjects ):
+ " return the name of the adaptor class for this node "
+ assert type(cobjects)==dict
+ name = 'CObject'
+ if self.type_alias:
+ alias = self.type_alias
+ if alias.name in cobjects:
+ name = alias.name
+ elif self.struct:
+ name = self.struct.tag and self.struct.tag.name
+ if name is None or not name in cobjects:
+ name = 'CStruct'
+ elif self.pointer: # or self.array:
+ node = self
+ _name = ''
+ while node.pointer:
+ _name = _name + '.pointer'
+ node = node.deref()
+ name = node.pyx_adaptor_name( cobjects )
+ if name == 'CObject': # type doesn't have the .pointer property
+ name = 'CVoid' # class has the .pointer property
+ name = name + _name
+ elif self.union:
+ name = 'CUnion'
+ elif self.enum:
+ name = 'CEnum'
+ elif self.function:
+# name = 'CObject' # CFunction ?
+ name = 'CFunction' # CFunction ?
+ elif self.spec:
+# print self.spec
+ tpset = TPSet()
+ for node in self.spec:
+ if type(node) in (BasicType,Qualifier):
+ name = tpset.parse( node.name )
+# print ' ', node[0], name
+
+ return name
+
+ def pyx_wrapper(self, tp_tank, ostream=None, names=None, fnames=None, cprefix="", func_cb=None, special_stream=None, root=None, **kw):
+
+# todo
+# 2. all Typedefs emit Meta(pyx_adaptor_name(self)) statements
+# 3. struct Declarators emit Meta(CStruct) statements
+# 4. extern Declarators with pointer type have .target set correctly
+# by calling the relevant .pointer Meta method
+
+ if root is None:
+ root = self
+ # depth-first:
+ ostream = Node.pyx_wrapper(self, tp_tank, ostream, names=names, fnames=fnames, cprefix=cprefix, func_cb=None, root=root, **kw) # do children
+ modname = kw['modname']
+ tag_lookup = kw['tag_lookup']
+ macros = kw['macros']
+ cobjects = kw['cobjects']
+
+ nodebt = self.cbasetype() # WARNING: cbasetype may be cached
+ cstr = nodebt.cstr()
+ name = self.get_pyxtpname()
+# ostream.putln( self.deepstr(comment=True, nl="", indent="") )
+# if tp_tank.has_key(name):
+## assert tp_tank[name] == nodebt, (tp_tank[name], nodebt)
+# # move along.. nothing to see here..
+# pass
+# elif nodebt.function:
+# pass
+# elif nodebt.enum:
+# pass
+# elif nodebt.compound: # struct or union
+# pass # XX
+# elif nodebt.is_pointer_to_fn():
+## ostream = self.handle_function_pointers(tp_tank, ostream, cprefix=cprefix, func_cb=None, root=root, **kw)
+# pass
+
+ if type(self)!=Declarator:
+ pass
+ elif self.name is None:
+ pass
+# elif self.function and not self.name in fnames and not self.deepfind(Ellipses):
+ elif self.function and not self.name in fnames and not self.function.is_varargs():
+ fnames[self.name] = self
+ if not func_cb:
+ func_adaptor = FunctionAdaptor( ostream, self, special_stream )
+ else:
+ func_adaptor = func_cb( ostream, self )
+ func_adaptor.define_func( names=names, tag_lookup=tag_lookup, cprefix=cprefix, cobjects=cobjects )
+ elif nodebt.is_pyxnative():
+ self.pyxsym(ostream=ostream, names=names, tag_lookup=tag_lookup, cprefix=cprefix, modname=modname, cobjects=cobjects)
+ else:
+ ostream.putln( '# ' + self.cstr() )
+ # cannot take address of array:
+ if not self.function and not self.array and self.name:
+ # we can expose these as CObject's too
+ self.pyxsym(ostream=ostream, names=names, tag_lookup=tag_lookup, cprefix=cprefix, modname=modname, cobjects=cobjects)
+
+ return ostream
+
+ def pyxsym(self, ostream, names=None, tag_lookup=None, cprefix="", modname=None, cobjects=None):
+ assert self.name is not None, self.deepstr()
+ ostream.putln( '# ' + self.cstr() )
+# This cdef is no good: it does not expose a python object
+# and we can't reliably set a global var
+# ostream.putln( 'cdef %s %s' % ( self.pyx_adaptor_decl(cobjects), self.name ) ) # _CObject
+# ostream.putln( '%s = %s()' % (self.name, self.pyx_adaptor_name(cobjects)) )
+# ostream.putln( '%s.p = <void*>&%s' % (self.name, cprefix+self.name) )
+# # expose a python object:
+# ostream.putln( '%s.%s = %s' % (modname,self.name, self.name) )
+ ostream.putln( '%s = %s( addr = <long>&%s )' % (self.name, self.pyx_adaptor_name(cobjects), cprefix+self.name) )
+ return ostream
+
+
+class Typedef(Declarator):
+ def pyxstr(self,toks=None,indent=0,cprefix="",use_cdef=True,shadow_name=True,**kw): # shadow_name=True
+ " warning: i do not check if my name is already in 'names' "
+ assert shadow_name == True
+ self = self.clone() # <----- NOTE
+ toks=[]
+ names = kw.get('names',{}) # what names have been defined ?
+ kw['names']=names
+
+# if self.tagged and not self.tagged.tag.name:
+# # "typedef struct {...} foo;" => "typedef struct foo {...} foo;"
+# # (to be emitted in the node loop below, and suppressed in the final toks.append)
+# self.tagged.tag = Tag( self.name ) # this is how pyrex does it: tag.name == self.name
+# XX that doesn't work (the resulting c fails to compile) XX
+
+ self._pyxstr( toks, indent, cprefix, use_cdef, shadow_name, **kw )
+
+# print self.deepstr()
+# dump("self")
+ if self.name and not names.has_key( self.name ):
+ names[ self.name ] = self
+ if not (self.tagged and self.name == self.tagged.tag.name):
+ comment = ""
+ if self.name in python_kws:
+ comment = "#"
+ #if cprefix:
+ # self.name = '%s%s "%s" ' % ( cprefix, self.name, self.name ) # XX pyrex can't do this
+ if cprefix: # shadow_name=True
+ # My c-name gets this prefix. See also TypeAlias.pyxstr(): it also prepends the cprefix.
+ self.name = '%s%s "%s" ' % ( cprefix, self.name, self.name )
+ toks.append( ' '*indent + comment + 'ctypedef ' + Node.pyxstr(self,indent=indent, cprefix=cprefix, **kw).strip() )
+# dump("self,l")
+ return ' \n'.join(toks)
+
+ def pyx_wrapper(self, tp_tank, ostream=None, names=None, fnames=None, cprefix="", func_cb=None, root=None, **kw):
+# todo
+# 2. all Typedefs emit Meta(pyx_adaptor_name(self)) statements
+# 3. struct Declarators emit Meta(CStruct) statements
+# 4. extern Declarators with pointer type have .target set correctly
+# by calling the relevant .pointer Meta method
+
+ if root is None: # hmm, we aren't using this ATM.. (going to use it for doing callbacks i think)
+ root = self
+ # depth-first:
+ ostream = Node.pyx_wrapper(self, tp_tank, ostream, names=names, fnames=fnames, cprefix=cprefix, func_cb=None, root=root, **kw) # do children
+ modname = kw['modname']
+ tag_lookup = kw['tag_lookup']
+ macros = kw['macros']
+ cobjects = kw['cobjects']
+
+ nodebt = self.cbasetype() # WARNING: cbasetype may be cached
+ name = self.get_pyxtpname()
+# ostream.putln( self.deepstr(comment=True, nl="", indent="") )
+ if tp_tank.has_key(name):
+# assert tp_tank[name] == nodebt, (tp_tank[name], nodebt)
+ # move along.. nothing to see here..
+ pass
+ elif nodebt.function:
+ pass
+ elif nodebt.enum:
+ pass
+# elif nodebt.pointer or nodebt.array:
+# pass # we just malloc a void*
+ elif nodebt.compound:
+ pass
+ elif nodebt.is_pointer_to_fn():
+# ostream = self.handle_function_pointers(tp_tank, ostream, cprefix=cprefix, func_cb=None, root=root, **kw)
+ pass
+
+ if self.name: # and not self.name[0]=='_': # we skip the underscored names
+ ostream.putln( '' )
+ ostream.putln( '# ' + self.cstr() )
+ name = self.name
+ adaptor_name = self.pyx_adaptor_name( cobjects )
+ cobjects[name] = self # typedef struct foo foo; is ok, i think..
+ if nodebt.compound:
+ ostream.putln("class %s(%s):" %(name,adaptor_name))
+ ostream.indent()
+ ostream.putln("__metaclass__ = Meta")
+# assert self.compound.tag.name in tag_lookup, self.compound.tag.name
+ compound = nodebt.compound
+ if self.compound.tag.name:
+ compound = tag_lookup[ self.compound.tag.name ]
+ compound_adaptor = VarAdaptor( ostream, py_varname='self', c_varname=None, node=self, cobjects=cobjects )
+# print compound.cstr()
+ for member in compound.members:
+ ostream.putln("#"+member.cstr())
+ arg_adaptor = VarAdaptor( ostream, py_varname='value', c_varname='c_value', node=member, cobjects=cobjects )
+
+ # set method:
+ ostream.startln("def set_%s( _CStruct self, " % member.name)
+ arg_adaptor.declare_py_arg()
+ ostream.endln(' ):')
+ ostream.indent()
+# ostream.putln( "%s.%s = %s" %\
+# ( compound_adaptor.get_c_from_pyvar(names, tag_lookup, cprefix ),
+# member.name,
+# arg_adaptor.get_c_from_pyvar(names, tag_lookup, cprefix) ) )
+# # doesn't work for arrays:
+# ostream.putln( "(<%s%s*>(%s.p))[0].%s = %s%s" %\
+# ( cprefix, self.name, compound_adaptor.py_varname,
+# member.name,
+# arg_adaptor.get_c_cast(names, tag_lookup, cprefix),
+# arg_adaptor.get_c_from_pyvar(names, tag_lookup, cprefix) ) )
+# ostream.putln( "memcpy( &((<%s%s*>(%s.p))[0].%s), %s, sizeof(%s) )" %\
+# ( cprefix, self.name, compound_adaptor.py_varname, member.name,
+# arg_adaptor.get_caddr_from_pyvar(names, tag_lookup, cprefix),
+# arg_adaptor.get_c_type(names, tag_lookup, cprefix),
+# ) )
+ arg_adaptor.assign_to_compound_member( compound_adaptor, names, tag_lookup, cprefix )
+ ostream.dedent()
+
+ # get method:
+ ostream.putln("def get_%s( _CStruct self ):" % member.name)
+ ostream.indent()
+ arg_adaptor.declare_pyvar() #names=names, tag_lookup=tag_lookup, cprefix=cprefix)
+ arg_adaptor.init_pyvar( cobjects )
+ arg_adaptor.assign_from_compound_member( compound_adaptor, names, tag_lookup, cprefix )
+ arg_adaptor.return_pyvar()
+ ostream.dedent()
+
+ ostream.putln("%s = property(get_%s,set_%s)" % ((member.name,)*3))
+ ostream.dedent()
+ ostream.putln("%s.sizeof = sizeof(%s%s)" % (name,cprefix,name))
+ else:
+ ostream.putln("%s = Meta( '%s', (%s,), {} )" % ( name, name, adaptor_name ))
+ if self.enum:
+ for member in self.enum.members:
+ # for an Enum these are all Identifiers
+ ostream.putln( '%s.%s = %s(%s)' % ( name, member.name, name, cprefix+member.name ) )
+ ostream.putln( '%s = %s' % ( member.name, cprefix+member.name ) )
+ # ostream.putln( self.deepstr(comment=True) )
+
+ return ostream
+
+
+class AbstractDeclarator(Declarator):
+ """ used in Function; may lack an identifier """
+ def pyxstr(self,toks=None,indent=0,**kw):
+ if self.name in python_kws:
+ # Would be better to do this in __init__, but our subclass doesn't call our __init__.
+ self.name = '_' + self.name
+# return ' '*indent + Node.pyxstr(self,toks,indent, **kw).strip()
+ return Node.pyxstr(self,toks,indent, **kw).strip()
+ def pyx_wrapper(self, tp_tank, ostream=None, **kw):
+ nodebt = self.cbasetype()
+ name = self.get_pyxtpname()
+# ostream.putln( self.deepstr(comment=True, nl="", indent="") )
+ if tp_tank.has_key(name):
+ pass
+# elif nodebt.is_pointer_to_fn():
+# assert self.is_pointer_to_fn(), self.cstr()
+# ostream = self.handle_function_pointers(tp_tank, ostream, **kw)
+# # bad idea: if there is a struct/enum as an argument, it gets redefined as a "new" struct/enum
+# #ostream = nodebt.handle_function_pointers(tp_tank, ostream, **kw)
+
+# if self.find( Ellipses ) is None and not self.is_void():
+# ostream = Declarator.pyx_wrapper(self, tp_tank, ostream=ostream, **kw)
+ return ostream
+
+
+class FieldLength(object):
+ """
+ """
+ def pyxstr(self,toks,indent,**kw):
+ pass
+
+
+class StructDeclarator(Declarator): # also used in Union
+ """
+ """
+ def pyxstr(self,toks=None,indent=0,**kw):
+ comment = ""
+ if self.name in python_kws:
+ comment = "#"
+ return ' '*indent + comment + Node.pyxstr(self,toks,indent, **kw).strip()
+ def pyx_wrapper(self, tp_tank, ostream=None, cprefix="", **kw):
+ # disabled for now
+ return ostream
+ if self.field_length is None:
+ ostream = Declarator.pyx_wrapper( self, tp_tank, ostream, cprefix=cprefix, **kw )
+ return ostream
+
+class DeclarationSpecifiers(object):
+ """
+ """
+ pass
+
+class TypeSpecifiers(DeclarationSpecifiers):
+ """
+ """
+ pass
+
+class Initializer(object):
+ """
+ """
+ pass
+
+class Declaration(object):
+ """
+ """
+ pass
+
+class ParameterDeclaration(Declaration):
+ """
+ """
+ pass
+
+class StructDeclaration(Declaration):
+ """
+ """
+ pass
+
+class TransUnit(object):
+ """
+ Top level node.
+ """
+ def pyxstr(self, filenames, modname, macros = {}, names = None, func_cb=None, **kw):
+ cprefix = "__c_"
+ # names: one global namespace:
+ # all identifiers (not abstract identifiers), tags and enum elements
+ # map names to Declarator, Enum element, ... what else?
+ if names is None:
+ names = {} # XX update from python_kws ??
+ decls = self.pyx_decls(filenames,modname,macros,names,func_cb,cprefix)
+ wrappers, special = self.pyx_wrappers(filenames,modname,macros,names,func_cb,cprefix)
+ return decls, wrappers, special
+
+ def pyx_decls(self, filenames, modname, macros = {}, names = None, func_cb=None, cprefix="", **kw):
+ # PART 1: emit extern declarations
+ ostream = OStream()
+ now = datetime.today()
+ ostream.putln( now.strftime('# Code generated by pyxelator on %x at %X') + '\n' )
+ ostream.putln("# PART 1: extern declarations")
+ for filename in filenames:
+ ostream.putln( 'cdef extern from "%s":\n pass\n' % filename )
+ ostream.putln( 'cdef extern from *:' )
+ file = None # current file
+ ostream.putln(""" ctypedef long %ssize_t "size_t" # XX pyrex can't handle unsigned long """ % cprefix )
+ ostream.putln(""" cdef void* %smemcpy "memcpy"(void*dest,void*src,__c_size_t n) """ % cprefix )
+ names['size_t']=None
+ names['memcpy']=None
+ for node in self:
+ ostream.putln('')
+ ostream.putln(' # ' + node.cstr() )
+ assert node.marked
+ comment = False
+ if node.name and node.name in names:
+ comment = True # redeclaration
+# ostream.putln( node.deepstr( comment=True ) )
+ s = node.pyxstr(indent=1, names=names, tag_lookup = self.tag_lookup, cprefix=cprefix, **kw)
+ if s.split():
+ if comment:
+ s = "#"+s.replace( '\n', '\n#' ) + " # redeclaration "
+ if node.file != file:
+ file = node.file
+# ostream.putln( 'cdef extern from "%s":' % file )
+ ostream.putln( ' # "%s"' % file )
+ ostream.putln( s )
+ ostream.putln('\n')
+# s = '\n'.join(toks)
+ return ostream.join()
+
+ def pyx_wrappers(self, filenames, modname, macros = {}, names = None, func_cb=None, cprefix="", **kw):
+ ostream = OStream()
+ special_stream = OStream() # this output is for template code that needs hand-tweaking
+ now = datetime.today()
+ ostream.putln( now.strftime('# Code generated by pyxelator on %x at %X') + '\n' )
+# ostream.putln('include "adapt.pxi"')
+ ostream.putln('cimport adapt')
+ adapt_cls = "CStruct CArray CChar CDouble CFloat CInt CLDouble CLLong CLong CObject CPointer CSChar CShort CUChar CUInt CULLong CULong CUShort ".split()
+ ostream.putln('from adapt cimport '+','.join(['_'+a for a in adapt_cls]))
+ adapt_cls = "CArray CArrayCIntRange CChar CDouble CEnum CFloat CFunction CInt CLDouble CLLong CLong CObject CPointer CSChar CShort CStr CStruct CUChar CUInt CULLong CULong CUShort CUnion CVoid CPyObject FunctionMeta Meta get_slice_size null py2cobject str_from_charp".split()
+ ostream.putln('from adapt import '+','.join(adapt_cls))
+ ostream.putln('include "%s_special.pxi"'%modname)
+ ostream.put(\
+"""
+
+cdef char *get_charp( ob ):
+ cdef _CObject _ob
+ cdef char*p
+ if type(ob)==str:
+ p = ob
+ elif type(ob) in (int,long):
+ assert ob==0
+ p = <char*>ob
+ else:
+ _ob = ob
+ p = (<char**>_ob.p)[0]
+# print "get_charp", ob, hex(<long>p)
+ return p
+
+""")
+
+# ostream.putln("""
+##import sys
+##%s = sys.modules["%s"] # this doesn't always work ... ??
+#
+##import sys
+##thismod = sys.modules[__name__]
+##thismod.foo = 1234
+#""" % (modname,modname) )
+
+ ostream.putln("# wrappers")
+ fnames = {}
+ tp_tank = {}
+ cobjects = {} # map each adaptor name (CObject subclass) to Node that created it
+ for node in self:
+ node.pyx_wrapper(tp_tank, ostream=ostream, names=names, fnames=fnames, tag_lookup=self.tag_lookup, cprefix=cprefix, cobjects=cobjects, modname=modname, macros=macros, func_cb=func_cb, special_stream = special_stream )
+
+ ostream.putln('\n')
+# s = '\n'.join(toks)
+# sys.stderr.write( "# %s lines\n" % s.count('\n') )
+
+ return ostream.join(), special_stream.join()
+
+# XX warn when we find a python keyword XX
+python_kws = """
+break continue del except exec finally pass print raise
+return try global assert lambda yield
+for while if elif else and in is not or import from """.split()
+python_kws = dict( zip( python_kws, (None,)*len(python_kws) ) )
+
+
--- /dev/null
+#!/usr/bin/env python
+""" ir.py - parse c declarations
+
+(c) 2002, 2003, 2004, 2005 Simon Burton <simon@arrowtheory.com>
+Released under GNU LGPL license.
+
+version 0.xx
+
+"""
+
+import sys
+#import cPickle as pickle
+import pickle
+
+from dump import dump
+
+#from lexer import Lexer
+from parse_core import Symbols #, Parser
+import node as node_module
+import cparse
+import genpyx
+
+class Node(genpyx.Node, node_module.Node):
+ """
+ tree structure
+ """
+ def __init__( self, *args, **kw ):
+ node_module.Node.__init__( self, *args, **kw )
+ self._marked = False
+ def get_marked( self ):
+ return self._marked
+ def set_marked( self, marked ):
+# if marked:
+# print "MARK", self
+ self._marked = marked
+ marked = property( get_marked, set_marked )
+
+# def __getstate__( self ):
+# return self.__class__, tuple( [ item.__getstate__() for item in self ] )
+# def __setstate__( self, state ):
+# cls, states = state
+# states = list(states)
+# for idx, state in enumerate(states):
+# items[idx] = items[idx].__setstate__(
+ def __getstate__(self):
+ return str(self)
+ def __setstate__(self, state):
+ Node.__init__(self)
+ self[:] = eval(state)
+
+# _unique_id = 0
+# def get_unique_id(cls):
+# Node._unique_id += 1
+# return Node._unique_id
+# get_unique_id = classmethod(get_unique_id)
+
+ def __hash__( self ):
+ return hash( tuple([hash(type(self))]+[hash(item) for item in self]) )
+
+ def clone(self):
+ l = []
+ for item in self:
+ if isinstance(item,Node):
+ item = item.clone()
+ l.append(item)
+ return self.__class__(*l, **self.__dict__)
+
+ def init_from( self, other ): # class method ?
+ # Warning: shallow init
+ self[:] = other
+ self.__dict__.update( other.__dict__ )
+ return self
+
+# def is_struct(self):
+# for x in self:
+# if isinstance(x,Node):
+# if x.is_struct():
+# return 1
+# return 0
+
+
+ #def explain(self):
+ #l = []
+ #for x in self:
+ #if isinstance(x,Node):
+ #l.append(x.explain())
+ #else:
+ #l.append(str(x))
+ #return string.join(l," ")
+ ##(self.__class__.__name__,string.join(l) )
+
+ def psource(self):
+ if hasattr(self,'lines'):
+# print "# "+string.join(self.lines,"\n# ")+"\n"
+ print "# "+"\n# ".join(self.lines)+"\n"
+
+ def cstr(self,l=None):
+ """
+ Build a list of tokens; return the joined tokens string
+ """
+# dump( "self.__class__.__name__,l" )
+ if l is None:
+ l = []
+ for x in self:
+ if isinstance(x,Node):
+ x.cstr(l)
+ else:
+ l.insert(0,str(x)+' ')
+ s = ''.join(l)
+# dump( "self.__class__.__name__,s" )
+ return s
+
+ def ctype(self): # anon_clone
+ " return clone of self without identifiers "
+ #print "%s.ctype()"%self
+ l=[]
+ for x in self:
+ if isinstance(x,Node):
+ l.append(x.ctype())
+ else:
+ l.append(x)
+ #print "%s.__class__(*%s)"%(self,l)
+ return self.__class__(*l, **self.__dict__) # XX **self.__dict__ ?
+
+ def cbasetype(self):
+ " return ctype with all TypeAlias's replaced "
+ # WARNING: we cache results (so do not mutate self!!)
+ l=[]
+ for x in self:
+ if isinstance(x,Node):
+ l.append(x.cbasetype())
+ else:
+ l.append(x)
+ #print "%s.__class__(*%s)"%(self,l)
+ return self.__class__(*l, **self.__dict__) # XX **self.__dict__ ?
+
+ def signature( self, tank=None ):
+ if tank is None:
+ tank = {}
+ for node in self.nodes():
+ if not tank.has_key( type(node) ):
+ tank[ type(node) ] = {}
+ type(node).tank = tank[type(node)]
+ shape = tuple( [ type(_node).__name__ for _node in node ] )
+ if not tank[type(node)].has_key(shape):
+ tank[type(node)][shape] = []
+ tank[type(node)][shape].append( node )
+ return tank
+
+ def psig( self, tank=None ):
+ if tank is None:
+ tank = {}
+ tank = self.signature(tank)
+ for key in tank.keys():
+ print key.__name__
+ for shape in tank[key].keys():
+ print " ", shape
+
+#
+#################################################
+
+class Named(genpyx.Named, Node):
+ " has a .name property "
+ def get_name(self):
+ if self:
+ assert type(self[0])==str
+ return self[0]
+ return None
+ def set_name(self, name):
+ if self:
+ self[0] = name
+ else:
+ self.append(name)
+ name = property(get_name,set_name)
+
+
+class BasicType(genpyx.BasicType, Named):
+ "float double void char int"
+ pass
+
+class Qualifier(genpyx.Qualifier, Named):
+ "register signed unsigned short long const volatile inline"
+ pass
+
+class StorageClass(genpyx.StorageClass, Named):
+ "extern static auto"
+ pass
+
+class Ellipses(genpyx.Ellipses, Named):
+ "..."
+ pass
+
+class GCCBuiltin(genpyx.GCCBuiltin, BasicType):
+ "things with __builtin prefix"
+ pass
+
+class Identifier(genpyx.Identifier, Named):
+ """
+ shape = +( str, +ConstExpr )
+ """
+ #def explain(self):
+ #if len(self)==1:
+ #return "%s"%self.name
+ #else:
+ #return "%s initialized to %s"%(self.name,
+ #Node(self[1]).explain()) # will handle Initializer
+
+# def ctype(self):
+# return self.__class__(*self[1:]) #.clone() ?
+
+# def get_name(self):
+# if self:
+# return self[0]
+# def set_name(self, name):
+# if self:
+# self[0] = name
+# else:
+# self.append(name)
+# name = property(get_name,set_name)
+
+ def cstr(self,l=None):
+ if l is None:
+ l=[]
+ if len(self)>1:
+ assert len(self)==2
+ l.append( '%s = %s'%(self[0],self[1]) )
+ elif len(self)==1:
+ l.append( str(self[0]) )
+ return " ".join(l)
+
+class TypeAlias(genpyx.TypeAlias, Named):
+ """
+ typedefed things, eg. size_t
+
+ """
+ def cbasetype( self ):
+ node = self.typedef.cbasetype().get_rest()
+# dump("node")
+ return node
+
+class Function(genpyx.Function, Node):
+ """
+ """
+ #def explain(self):
+ #if len(self):
+ #return "function (%s), returning"%\
+ #", ".join( map(lambda x:x.explain(),self) )
+ #else:
+ #return "function returning"
+
+ def cstr(self,l):
+ #print '%s.cstr(%s)'%(self,l)
+ _l=[]
+ assert len(self)
+ i=0
+ while isinstance(self[i],Declarator):
+ _l.append( self[i].cstr() )
+ i=i+1
+ l.append( '(%s)'% ', '.join(_l) )
+ while i<len(self):
+ self[i].cstr(l)
+ i=i+1
+ return " ".join(l)
+
+ def return_type(self):
+ node = self[-1]
+ #assert isinstance(node,DeclarationSpecifiers)
+ return Declarator( Identifier(), node )
+ ret = property(return_type)
+
+ def get_args(self):
+ args = [ arg for arg in self[:-1] if not arg.is_void() ]
+ return args
+ args = property(get_args)
+
+ def arg_types(self):
+ return [ AbstractDeclarator().init_from( arg.ctype() ) for arg in self[:-1]]
+
+ def is_varargs(self):
+ for node in self.nodes():
+ if isinstance(node,Ellipses) or 'va_list' in node:
+# print self, 'is_varargs'
+ return True
+# print self, 'is_varargs'
+ return False
+# return fn.deepfind(Ellipses) or fn.deepfind('va_list')
+
+ def ctype(self):
+ return Function(*self.arg_types()+[self[-1]]) # XX self[-1].ctype
+
+
+class Pointer(genpyx.Pointer, Node):
+ """
+ """
+ def get_spec(self):
+ if type(self[0])==TypeSpecifiers: # isinstance ??
+ return self[0]
+ spec = property(get_spec)
+
+ #def explain(self):
+ #return "pointer to"
+
+ def cstr(self,l):
+ assert len(self)
+ node=self[0]
+ l.insert(0,'*')
+ if isinstance(node,Function):
+ l.insert(0,'(')
+ l.append(')')
+ elif isinstance(node,Array):
+ l.insert(0,'(')
+ l.append(')')
+ return Node.cstr(self,l)
+
+class Array(genpyx.Array, Node):
+ """
+ """
+ #def explain(self):
+ #s=''
+ #if len(self):
+ #if type(self[0])==int:
+ #s='0 to %s '%(self[0]-1)
+ #return "array %sof"%s
+ def has_size(self):
+ try:
+ int(self.size)
+ return True
+ except:
+ return False
+
+ def get_size(self):
+ if type(self[-1])==str:
+ try: return int(self[-1])
+ except: return self[-1]
+ return self[-1] # None
+ size = property(get_size)
+
+ def get_spec(self):
+ if type(self[0])==TypeSpecifiers: # isinstance ??
+ return self[0]
+ spec = property(get_spec)
+
+ def to_pointer(self):
+ node = Pointer()
+ node.init_from( self.clone() )
+ node.pop() # pop the size element
+ return node
+
+ def cstr(self,l):
+ if self.size is None:
+ l.append('[]')
+ else:
+ l.append('[%s]'%self.size)
+ return Node( *self[:-1] ).cstr( l )
+
+class Tag(genpyx.Tag, Named):
+ " the tag of a Struct, Union or Enum "
+ pass
+
+class Taged(genpyx.Taged, Node):
+ "Struct, Union or Enum "
+ def get_tag(self):
+ if len(self):
+ tag = self[0]
+ assert type(tag)==Tag # isinstance ??
+ else:
+ tag = None
+ return tag
+ def set_tag(self,tag):
+ if len(self):
+ self[0] = tag
+ else:
+ self.append(tag)
+ tag = property( get_tag, set_tag )
+ def has_members(self):
+ return len(self)>1 # more than just a tag
+ def get_members(self):
+ return self[1:]
+ members = property(get_members) # fields ?
+
+ def ctype(self):
+ if not self.tag.name:
+ #print "# WARNING : anonymous struct " # OK i think
+ return self.clone()
+# self = self.clone()
+# return self[:1] # just the tag
+ return self.__class__( self.tag, **self.__dict__ ) # just the Tag
+# return self.__class__( *self, **self.__dict__ )
+
+ def cbasetype(self):
+ return self.ctype() # is this enough ???
+# return Node.cbasetype(self) # XX lookup my tag if i am empty ..?
+
+
+class Compound(genpyx.Compound, Taged):
+ "Struct or Union"
+
+ def cstr(self,_l=None):
+ assert isinstance( self[0], Tag )
+ tag=''
+ if len(self[0]):
+ tag=' '+self[0][0]
+ if isinstance(self,Struct):
+ l=[ 'struct%s '%tag ]
+ elif isinstance(self,Union):
+ l=[ 'union%s '%tag ]
+ if len(self)>1:
+ l.append(' { ')
+ for decl in self[1:]:
+ l.append( decl.cstr()+"; " )
+ l.append('} ')
+ if _l is None:
+ _l=[]
+ while l:
+ _l.insert( 0, l.pop() )
+ # XX empty struct with no tag -> "struct" XX
+ return "".join( _l )
+
+ def ctype(self):
+ tp = Taged.ctype(self)
+ for i in range(1,len(tp)):
+ tp[i] = StructDeclarator().init_from( tp[i] )
+ return tp
+
+class Struct(genpyx.Struct, Compound):
+ """
+ """
+ pass
+
+
+class Union(genpyx.Union, Compound):
+ """
+ """
+ pass
+
+
+class Enum(genpyx.Enum, Taged):
+ """
+ """
+ def cstr(self,_l=None):
+ assert isinstance( self[0], Tag )
+ tag=''
+ if len(self[0]):
+ tag=' '+self[0][0]
+ l=[ 'enum%s '%tag ]
+ if len(self)>1:
+ l.append(' { ')
+ for node in self[1:]:
+ l.append( node.cstr()+', ' )
+ l.append('} ')
+ if _l is None:
+ _l=[]
+ while l:
+ _l.insert( 0, l.pop() )
+ return ''.join( _l )
+
+class Declarator(genpyx.Declarator, Node):
+ """
+ """
+
+ def __eq__(self,other):
+ " unordered equality "
+ # ordering sometimes gets lost when we do a cbasetype
+ if not isinstance(other,Node):
+ return False
+ a, b = self[:], other[:]
+ a.sort()
+ b.sort()
+ return a == b
+
+ def __hash__( self ):
+ hs = [hash(item) for item in self]
+ hs.sort()
+ return hash( tuple([hash(type(self))]+hs) )
+
+ def transform(self):
+ return
+
+ def get_identifier(self):
+ if len(self)>1:
+ return self[0]
+ def set_identifier(self, identifier):
+ if len(self)>1:
+ self[0] = identifier
+ else:
+ self.insert(0,identifier)
+ identifier = property(get_identifier,set_identifier)
+
+ def get_spec(self):
+ spec = self[-1]
+ if type(spec)==TypeSpecifiers: # isinstance ??
+ return spec
+ spec = property(get_spec)
+
+ def get_type_alias(self):
+ if self.spec:
+ if isinstance(self.spec[0], TypeAlias):
+ return self.spec[0]
+ type_alias = property(get_type_alias)
+
+ def get_tagged(self):
+ if self.spec:
+ return self.spec.tagged # i am a tagged
+ tagged = property(get_tagged)
+
+ def get_compound(self):
+ if self.spec:
+ return self.spec.compound # i am a compound
+ compound = property(get_compound)
+
+ def get_struct(self):
+ if self.spec:
+ return self.spec.struct # i am a struct
+ struct = property(get_struct)
+
+ def get_union(self):
+ if self.spec:
+ return self.spec.union # i am a union
+ union = property(get_union)
+
+ def get_enum(self):
+ if self.spec:
+ return self.spec.enum # i am an enum
+ enum = property(get_enum)
+
+ def get_function(self):
+ if len(self)>1 and type(self[1])==Function: # isinstance ??
+ return self[1]
+ function = property(get_function)
+
+ def get_pointer(self):
+ if len(self)>1 and type(self[1])==Pointer: # isinstance ??
+ return self[1]
+ pointer = property(get_pointer)
+
+ def get_array(self):
+ if len(self)>1 and type(self[1])==Array: # isinstance ??
+ return self[1]
+ array = property(get_array)
+
+ def get_name(self):
+ if self.identifier:
+ return self.identifier.name
+ def set_name(self, name):
+ assert self.identifier is not None
+ self.identifier.name = name
+ name = property(get_name, set_name)
+
+ def get_rest(self): # XX needs a better name
+ if len(self)>1:
+ return self[1]
+ return self[0]
+
+ def pointer_to( self ):
+ " return Declarator pointing to self's type "
+ decl = Declarator(Identifier(), Pointer(self.get_rest().clone()))
+ return decl
+
+ def deref( self ):
+ " return (clone of) Declarator that self is pointing to "
+ node = self.ctype() # clone
+ pointer = node.pointer or node.array
+ assert pointer, "cannot dereference non-pointer"
+ node[1:2] = pointer
+ return node
+
+ def is_void(self):
+ return self.spec and BasicType('void') in self.spec
+
+ def is_pointer_to_fn(self):
+ return self.pointer and self.deref().function
+
+ def is_pointer_to_char(self):
+# return self.ctype() == TransUnit("char *a;").transform()[0].ctype()
+ node = self.pointer or self.array
+ if node:
+ spec = node.spec
+ if spec and BasicType('char') in spec and not BasicType('unsigned') in spec:
+ return True
+ return False
+
+ def is_callback(self):
+ " i am a pointer to a function whose last arg is void* "
+ if self.is_pointer_to_fn():
+ fn = self.deref().function
+ if fn.args:
+ arg = fn.args[-1]
+ if arg.pointer and arg.deref().is_void():
+ return True
+
+ def is_complete( self, tag_lookup ):
+ if self.tagged and self.tagged.tag.name in tag_lookup and not tag_lookup[self.tagged.tag.name].has_members():
+ return False
+ return True
+
+ def is_primative( self ):
+ "i am a char,short,int,float,double... "
+ spec = self.cbasetype().spec
+ return spec and spec.find(BasicType)
+
+ def is_pyxnative( self ):
+ # pyrex handles char* too
+ # but i don't know if we should make this the default
+ # sometimes we want to send a NULL, so ... XXX
+ self = self.cbasetype()
+ if self.is_void():
+ return False
+ if self.is_primative():
+ return True
+ if self.enum:
+ return True
+# pointer = None
+# if self.pointer:
+# pointer = self.pointer
+# elif self.array:
+# pointer = self.array
+# if pointer and pointer.spec:
+# spec = pointer.spec
+# if BasicType("char") in spec and not Qualifier("unsigned") in spec:
+# # char*, const char*
+## print self.deepstr()
+# return True
+ return False
+
+ def cstr(self,l=None):
+ return Node.cstr(self,l).strip()
+
+ def ctype(self):
+ decl=Declarator()
+ decl.init_from( self.clone() )
+ decl.identifier = Identifier()
+ for i in range(1,len(decl)):
+ decl[i]=decl[i].ctype()
+ return decl
+
+ def cbasetype(self):
+ # WARNING: we cache results (so do not mutate self!!)
+ try:
+ # this cache improves performance by 50%
+ return self.__cbasetype.clone()
+ except AttributeError:
+ pass
+ decl = self.ctype() # gets rid of Identifier names
+ for i, node in enumerate(decl):
+ decl[i] = decl[i].cbasetype()
+# return decl.get_rest()
+
+ done = False
+ while not done:
+ done = True
+ nodes = decl.deepfilter( TypeSpecifiers )
+# dump( "nodes" )
+ for node in nodes:
+ if node.deepfind( TypeSpecifiers ) != node:
+ # this node has another TypeSpecifier;
+# dump( "node" )
+ decl.expose_node( node )
+ done = False
+ break # start again...
+
+# dump( 'self' )
+# dump( 'decl' )
+
+ # each TypeSpecifier needs to absorb primitive siblings (StorageClass, BasicType etc.)
+ nodes = decl.deepfilter( TypeSpecifiers )
+ for node in nodes:
+ parent = decl.get_parent(node)
+ i = 0
+ while i < len(parent):
+ assert not type(parent[i]) in (TypeAlias, Enum, Struct, Union)
+ if type(parent[i]) in (StorageClass, BasicType, Qualifier):
+ node.append( parent.pop(i) )
+ else:
+ i = i + 1
+
+ self.__cbasetype = decl.clone()
+ return decl
+
+ def invalidate(self):
+ # flush cache, etc.
+ try:
+ del self.__cbasetype
+ except AttributeError:
+ pass
+
+ def declare_str(self,name):
+ " return c string declaring name with same type as self "
+ tp = self.ctype()
+ tp.name = name
+ return tp.cstr()+";"
+
+class Typedef(genpyx.Typedef, Declarator):
+ def cstr(self,l=None):
+ return 'typedef ' + Declarator.cstr(self,l) #.strip()
+
+class AbstractDeclarator(genpyx.AbstractDeclarator, Declarator):
+ """ used in Function; may lack an identifier """
+
+ #def cstr(self,l=None):
+ #return Node.cstr(self,l)
+
+# def ctype(self):
+# # _type_ ignores the name of our identifier
+# return Node.ctype(self)
+
+class FieldLength(genpyx.FieldLength, Node):
+ """
+ """
+ #def explain(self):
+ #return ""
+
+ def cstr(self,l):
+ l.append(':%s'%self[0])
+
+class StructDeclarator(genpyx.StructDeclarator, Declarator): # also used in Union
+ """
+ """
+ #def explain(self):
+ #flen = self.find(FieldLength)
+ #if flen is not None:
+ #i = self.index(flen)
+ #self.pop(i)
+ #s = Declarator.explain(self)
+ #self.insert(i,flen)
+ #width = flen[0]
+ #if width > 0:
+ #return s+" bitfield %s wide"%width
+ #else:
+ #return s+" alignment bitfield"
+ #else:
+ #return Declarator.explain(self)
+# def ctype(self):
+# return self
+ def get_field_length(self):
+ if len(self)>1 and isinstance( self[1], FieldLength ):
+ return self[1]
+ field_length = property(get_field_length)
+
+
+class DeclarationSpecifiers(genpyx.DeclarationSpecifiers, Node):
+#class TypeSpecifiers(Node):
+ """
+ """
+ def __eq__(self,other):
+ " unordered equality "
+ if not isinstance(other,Node):
+ return False
+ a, b = self[:], other[:]
+ a.sort()
+ b.sort()
+ return a == b
+
+ def __hash__( self ):
+ hs = [hash(item) for item in self]
+ hs.sort()
+ return hash( tuple([hash(type(self))]+hs) )
+
+# def is_struct(self):
+# return self.find(Struct) is not None
+
+
+class TypeSpecifiers(genpyx.TypeSpecifiers, DeclarationSpecifiers):
+ """
+ """
+ def get_tagged(self):
+ if self and isinstance(self[0],Taged):
+ return self[0]
+ tagged = property(get_tagged)
+
+ def get_compound(self):
+ if self and isinstance(self[0],Compound):
+ return self[0]
+ compound = property(get_compound)
+
+ def get_struct(self):
+ if self and isinstance(self[0],Struct):
+ return self[0]
+ struct = property(get_struct)
+
+ def get_union(self):
+ if self and isinstance(self[0],Union):
+ return self[0]
+ union = property(get_union)
+
+ def get_enum(self):
+ if self and isinstance(self[0],Enum):
+ return self[0]
+ enum = property(get_enum)
+
+ def cbasetype(self):
+ node = Node.cbasetype(self)
+# node.expose( TypeSpecifiers )
+# if node.deepfind(TypeSpecifiers) != node:
+ return node
+
+class Initializer(genpyx.Initializer, Node):
+ """
+ """
+ pass
+
+
+
+class Declaration(genpyx.Declaration, Node):
+ """
+ """
+ def do_spec(self):
+ " distribute DeclarationSpecifiers over each Declarator "
+ spec=self[0]
+ assert isinstance(spec,DeclarationSpecifiers), spec.deepstr()
+ self.pop(0)
+ for declarator in self:
+ assert isinstance(declarator,Declarator)
+ #if isinstance(declarator,DeclarationSpecifiers #huh?
+ ##for node in spec:
+ ##declarator.append(node.clone())
+ declarator.append(spec)
+
+ def transform(self):
+ # children go first
+ for node in self.nodes():
+ if isinstance(node,Declaration):
+ node.do_spec()
+ node.file = self.file # overkill ?
+ self.expose(Declaration)
+
+ #def explain(self):
+ #return string.join([x.explain() for x in self],", ")
+ #return string.join(map(lambda x:x.explain(),self),", ")
+
+
+class ParameterDeclaration(genpyx.ParameterDeclaration, Declaration):
+ """
+ """
+ pass
+
+
+class StructDeclaration(genpyx.StructDeclaration, Declaration):
+ """
+ """
+ pass
+
+
+class TransUnit(genpyx.TransUnit, Node):
+ """
+ Top level node.
+ """
+ def __init__( self, item ): # XX __init__ uses different signature ! XX
+ if type(item)==str:
+ node = cparse.TransUnit()
+ node.parse(item)
+ else:
+ node = item
+ assert isinstance( node, cparse.TransUnit ), str(node)
+ Node.__init__(self)
+ self[:] = [ self.convert(child) for child in node ]
+ self.__dict__.update( node.__dict__ )
+ assert "name" not in node.__dict__
+
+ self.syms = {} # map identifier names to their Declarator's
+ self.typedefs = {} # map names to Typedef's
+ self.tag_lookup = {} # map struct, union, enum tags to Taged's
+
+ # XX should call transform here XX
+
+# print self.deepstr()
+ def __getstate__( self ):
+ nodes = tuple( [ repr(node) for node in self ] )
+ typedefs = tuple( [ (key,repr(val)) for key,val in self.typedefs.items() ] )
+ return nodes, typedefs
+ def __setstate__( self, state ):
+ Node.__init__(self)
+ nodes, typedefs = state
+ nodes = [ eval(node) for node in nodes ]
+ self[:] = nodes
+ typedefs = [ (key,eval(val)) for key,val in typedefs ]
+ self.typedefs = dict(typedefs)
+
+ def convert( self, node ):
+# dump("node.__class__, node")
+# name = node.__class__.__name__
+# cls = globals()[ name ]
+ cls = cls_lookup[ type(node) ]
+ _node = cls()
+ for child in node:
+# dump("child")
+ if isinstance(child, node_module.Node):
+ child = self.convert( child )
+# dump("child")
+ else:
+ assert child is None or type(child) in (str, int), type(child)
+ _node.append( child )
+ _node.__dict__.update( node.__dict__ )
+# dump("_node.__class__, _node")
+ return _node
+
+ def strip(self,files):
+ " leave only the declarations from <files> "
+ i=0
+ while i<len(self):
+ if self[i].file in files:
+ i=i+1
+ else:
+ self.pop(i)
+
+ def mark(self,cb,verbose=False):
+ " mark our child nodes such that cb(node).. mark dependants too. prune unmarked objects. "
+ # mark the nodes:
+ for node in self:
+ node.marked = cb(self, node)
+ if verbose and node.marked:
+ print '1:', node.cstr()
+ # propagate dependancy:
+ i=len(self)
+ while i:
+ i-=1 # we go backwards
+ for node in self[i].nodes(): # bottom-up search
+ if verbose and self[i].marked and not node.marked:
+ print '2:', str(node), '<--', self[i].cstr()
+ node.marked = self[i].marked or node.marked
+ if type(node)==TypeAlias:
+ if verbose and node.marked and not node.typedef.marked:
+ print '3:', node.typedef.cstr(), '<--', node.cstr()
+ node.typedef.marked = node.typedef.marked or node.marked
+ if isinstance(node, Taged):
+ if node.tag.name in self.tag_lookup:
+ _node = self.tag_lookup[ node.tag.name ] # look-up the def'n
+ if verbose and node.marked and not _node.marked:
+ print '4:', _node.cstr(), '<--', self[i].cstr()
+# _node.marked = _node.marked or self[i].marked
+ _node.marked = _node.marked or node.marked
+# else:
+# # this guy has no tag
+# print "lost tag:", self[i].cstr()
+
+ # XX struct defs acquire marks from members, but XX
+ # XX ordinary definitions do not XX
+# if node.marked and not self[i].marked:
+# # one of my descendants is marked
+# if verbose:
+# print '5:', self[i].cstr(), '<--', node.cstr()
+# self[i].marked = True
+# if verbose:
+# for node in self:
+# print '-'*79
+# if node.enum:
+# print str(node.marked) + ': ' + node.cstr()
+ # prune:
+ f = open(".tmp/pruned.txt","w")
+ f.write("// This file autogenerated by '%s' .\n"%__file__)
+ f.write("// List of functions pruned from parse tree, for various reasons.\n\n")
+ i=0
+ while i<len(self):
+ if not self[i].marked:
+ if verbose: print 'pop:', self[i].cstr()
+ f.write( self[i].cstr() + "\n" )
+ self.pop(i)
+# elif self[i].compound:
+# # XXXX for now, rip out all struct members XXXX
+# self[i].compound[1:] = [] # XX encapsulation
+# i = i + 1
+ else:
+ i = i + 1
+ for key, value in self.syms.items():
+ if not value.marked:
+ del self.syms[key]
+ for key, value in self.typedefs.items():
+ if not value.marked:
+ del self.typedefs[key]
+ for key, value in self.tag_lookup.items():
+ if not value.marked:
+ del self.tag_lookup[key]
+# sys.exit(1)
+
+ def assert_no_dups(self):
+ check={}
+ for node in self.nodes():
+ assert not check.has_key(id(node))
+ check[id(node)]=1
+
+ def transform(self, verbose=False, test_parse=False, test_types=False ):
+ i=0
+ while i < len(self):
+ if verbose: print "##"*25
+ declaration=self[i]
+
+ if verbose: declaration.psource()
+ if verbose: print declaration.deepstr(),'\n'
+ assert isinstance(declaration,Declaration)
+ if verbose: print "# expose declarators from declaration"
+
+ # STAGE 1
+ declaration.transform()
+
+ if verbose: print declaration.deepstr(),'\n'
+ self[i:i+1] = declaration # expose declarators from declaration
+
+ for j in range(len(declaration)):
+ declarator=self[i]
+
+ assert isinstance(declarator,Declarator)
+ if verbose: print "# declarator.transform()"
+
+ # STAGE 2
+ declarator.transform()
+
+ if verbose: print declarator.deepstr(),'\n'
+ if verbose: print "# self.visit_declarator(declarator)"
+
+ # STAGE 3
+ self[i] = declarator = self.visit_declarator(declarator)
+
+ # STAGE 4
+ if declarator.name:
+ if isinstance(declarator, Typedef):
+ self.typedefs[ declarator.name ] = declarator
+ else:
+ self.syms[ declarator.name ] = declarator
+
+ for node in declarator.nodes():
+ if isinstance(node,Taged) and node.tag.name:
+ assert type(node.tag.name)==str, node.deepstr()
+ taged = self.tag_lookup.get( node.tag.name, None )
+ if taged is None:
+ self.tag_lookup[ node.tag.name ] = node
+ elif not taged.has_members():
+ # this is (maybe) the definition of this tag
+ self.tag_lookup[ node.tag.name ] = node
+
+ # Annotate the TypeAlias's
+ for node in declarator.deepfilter( TypeAlias ):
+ name = node[0]
+ assert type( name ) == str
+ node.typedef = self.typedefs[ name ]
+# dump("node, name, node.typedef")
+
+ if verbose: print declarator.deepstr(),'\n'
+ #print declarator.ctype().deepstr(),'\n'
+ #assert declarator.clone() == declarator
+
+ ###################################################
+ # TESTS:
+ if test_parse:
+ # test that parse of cstr gives same answer
+ cstr = declarator.cstr()+';\n'
+ if verbose: print '# '+cstr.replace('\n','\n# ')
+ #print
+ if isinstance(declarator,Typedef):
+ name = declarator[0][0]
+ assert type(name)==str
+ self.lexer.rmtypedef( name )
+ declaration = cparse.Declaration()
+ self.lexer.lex( cstr )
+ #print self.lexer.err_string()
+ declaration.parse( self.lexer, Symbols() ) # use new name-space
+ #declaration.parse( Lexer( cstr ), Symbols() )
+ declaration = self.convert(declaration)
+ declaration.transform()
+ assert len(declaration)==1
+ decl=declaration[0]
+ decl.transform()
+ decl = self.visit_declarator(decl)
+ if decl!=declarator:
+ if verbose: print "#???????????"
+ if verbose: print decl.deepstr(),'\n\n'
+ #if verbose: print declaration.deepstr(),'\n\n'
+ #assert 0
+ elif verbose: print '# OK\n'
+
+ if test_types:
+ node = declarator.ctype()
+ declare_str= node.declare_str("my_name")
+ if verbose: print "# declarator.ctype() "
+ if verbose: print node.deepstr(),"\n"
+ if verbose: print "#",declare_str.replace('\n','\n# '), '\n'
+
+ i=i+1
+ return self
+
+ def visit(self,node):
+ #print 'visit(%s)'%node
+ for _node in node:
+ if isinstance(_node,Declarator):
+ _node = self.visit_declarator(_node) # XX replace _node
+ elif isinstance(_node,Node):
+ _node = self.visit(_node) # XX replace _node
+ return node
+
+ def visit_declarator(self,decl):
+ assert isinstance(decl,Declarator)
+
+ # STAGE 3.a
+ tp = decl.deepfind(Typedef)
+ if tp is not None:
+ decl.deeprm(tp)
+ tp.init_from( decl ) # warning: shallow init
+ decl = tp
+
+ # STAGE 3.b
+ i=len(decl)
+ # accumulate nodes (they become the children of decl)
+ children=[]
+ while i:
+ i=i-1
+ node=decl.pop(i)
+ if isinstance(node,Declarator):
+ node = self.visit_declarator(node) # replace node
+ else:
+ node = self.visit(node) # replace node
+ if isinstance(node,Pointer):
+ node+=children
+ children=[node]
+ elif isinstance(node,Function):
+ node+=children
+ children=[node]
+ elif isinstance(node,Array):
+ while children:
+ node.insert(0,children.pop())
+ children=[node]
+ # array size (if any) at end
+ #elif isinstance(node,Identifier):
+ #node+=children
+ #children=[node]
+ else:
+ # accumulate
+ children.insert(0,node)
+ decl[:]=children
+ return decl
+
+ cstr = None
+ ctype = None
+ cbasetype = None
+
+
+# XX warn when we find a python keyword XX
+python_kws = """
+break continue del except exec finally pass print raise
+return try global assert lambda yield
+for while if elif else and in is not or import from """.split()
+python_kws = dict( zip( python_kws, (None,)*len(python_kws) ) )
+
+# XX this is not smart enough to respect inheritance
+# eg. TypeSpecifiers(DeclarationSpecifiers)
+# XX
+#gbl = globals()
+#clss = []
+#for key, val in gbl.items():
+# if type(val)==type:
+# if issubclass(val,Node) and val != Node:
+# clss.append( val )
+#for cls in clss:
+# name = cls.__name__
+# sibling = getattr( genpyx, name )
+# print " +++ ", gbl[name], gbl[name].__name__
+# gbl[name] = type( name, (cls, sibling), {} )
+# print " +++ ", gbl[name], gbl[name].__name__
+# assert issubclass( gbl[name], sibling )
+
+# remap the global class definitions in genpyx to
+# point to the definitions in this module
+gbl = globals()
+for key, val in gbl.items():
+ if type(val)==type:
+ if issubclass(val,Node):
+ setattr( genpyx, key, val )
+assert genpyx.Node == Node
+
+cls_lookup = {
+# Node : Node ,
+ cparse.BasicType : BasicType ,
+ cparse.Qualifier : Qualifier ,
+ cparse.StorageClass : StorageClass ,
+ cparse.Ellipses : Ellipses ,
+ cparse.GCCBuiltin : GCCBuiltin ,
+ cparse.Identifier : Identifier ,
+ cparse.TypeAlias : TypeAlias ,
+ cparse.Function : Function ,
+ cparse.Pointer : Pointer ,
+ cparse.Array : Array ,
+ cparse.Tag : Tag ,
+ cparse.Compound : Compound ,
+ cparse.Struct : Struct ,
+ cparse.Union : Union ,
+ cparse.Enum : Enum ,
+ cparse.Declarator : Declarator ,
+ cparse.Typedef : Typedef ,
+ cparse.AbstractDeclarator : AbstractDeclarator ,
+ cparse.FieldLength : FieldLength ,
+ cparse.StructDeclarator : StructDeclarator ,
+ cparse.DeclarationSpecifiers : TypeSpecifiers ,
+ cparse.TypeSpecifiers : TypeSpecifiers ,
+ cparse.Initializer : Initializer ,
+ cparse.Declaration : Declaration ,
+ cparse.ParameterDeclaration : ParameterDeclaration ,
+ cparse.StructDeclaration : StructDeclaration ,
+ cparse.TransUnit : TransUnit ,
+}
+
+
--- /dev/null
+#!/usr/bin/env python
+""" cdecl.py - parse c declarations
+
+(c) 2002, 2003, 2004, 2005 Simon Burton <simon@arrowtheory.com>
+Released under GNU LGPL license.
+
+version 0.xx
+
+"""
+
+import sys
+import string
+import types
+import copy
+
+#from cparse import BasicType, Qualifier, StorageClass, Typedef, Ellipses, GCCBuiltin
+#from cparse import *
+
+import cparse as host
+
+class LexError(Exception):
+ pass
+
+class Lexer(object):
+ def __init__(self,s="",verbose=0,**kw):
+ self.verbose = verbose
+ self.lookup = {} # a map for keywords and typedefs
+ for t in \
+ "float double void char int".split():
+ self.lookup[t] = host.BasicType( t )
+ for t in \
+ "register signed unsigned short long const volatile inline".split(): # inline here ???
+ self.lookup[t] = host.Qualifier( t )
+ for t in "extern static auto".split():
+ self.lookup[t] = host.StorageClass( t )
+ self.lookup['typedef'] = host.Typedef()
+ #self.lookup['__inline__'] = host.GCCBuiltin('__inline__')
+ #self.lookup['__extension__'] = host.Qualifier('__extension__')
+ self.lookup['...'] = host.Ellipses()
+ if s:
+ self.lex(s)
+ for key in kw.keys():
+ self.__dict__[key] = kw[key]
+
+ def lex(self,s):
+ self.stack = None
+ self.lines = s.splitlines()
+ self.set_state("","",0,0)
+ self.so_file = ""
+ self._newline()
+ self.get_token() # start
+
+ def mktypedef(self,tok,node):
+ if self.verbose:
+ print "%s.mktypedef(%s,%s)"%(self,tok,node)
+ self.lookup[ tok ] = node
+
+ def rmtypedef(self,tok):
+ " used in round trip testing "
+# print "# rmtypedef(%s)"%tok
+ assert isinstance( self.lookup[ tok ], host.Node ) # existance
+ del self.lookup[ tok ]
+
+ def _get_kind(self,tok):
+ #print '_get_kind(%s)'%tok,self.lookup
+ try:
+ return self.lookup[tok]
+ #return self.lookup[tok].clone()
+ except KeyError:
+ if tok.startswith("__builtin"):
+ node = host.GCCBuiltin(tok)
+ self.lookup[tok] = node
+ return node
+ #elif tok in ( "__extension__", ):
+ #node = GCCBuiltin(tok)
+ #self.lookup[tok] = node
+ #return node
+ return None
+
+ def _newline(self):
+ while self.lno < len(self.lines):
+ line = self.lines[self.lno]
+ if not line or line[0] != "#":
+ break
+ l = line.split('"')
+ assert len(l)>=2
+ self.so_file = l[1]
+ #self.so_lno = int( l[0].split()[1] )
+ #sys.stderr.write("# %s %s: %s\n"%(so_lno,so_file,l))
+ self.lno+=1
+
+ def get_brace_token( self ):
+ self.push_state()
+ ident_chars0 = string.letters+"_"
+ ident_chars1 = string.letters+string.digits+"_"
+ tok, kind = "", ""
+ while self.lno < len(self.lines):
+ s = self.lines[self.lno]
+ i=self.col
+ while i < len(s):
+ if s[i] not in '{}':
+ i=i+1
+ continue
+ else:
+ tok = s[i]
+ kind = tok
+ self.col = i+1
+ break
+ # keep moving
+ #sys.stderr.write( "lexer ignoring '%s'\n"%s[i] )
+ i=i+1
+ if i==len(s):
+ # nothing found
+ assert tok == ""
+ self.col=0
+ self.lno+=1
+ self._newline()
+ else:
+ assert tok
+ break
+ self.set_state(tok,kind,self.lno,self.col)
+
+ def get_token(self):
+ self.push_state()
+ ident_chars0 = string.letters+"_"
+ ident_chars1 = string.letters+string.digits+"_"
+ tok, kind = "", ""
+ while self.lno < len(self.lines):
+ s = self.lines[self.lno]
+ i=self.col
+ while i < len(s):
+ if s[i].isspace():
+ i=i+1
+ continue
+ #if s[i] in ident_chars0:
+ if s[i].isalpha() or s[i]=='_':
+ # identifier
+ j=i+1
+ while j<len(s):
+ if s[j] in ident_chars1:
+ j=j+1
+ else:
+ break
+ tok = s[i:j]
+ self.col = j
+ kind = self._get_kind(tok)
+ break
+ if s[i].isdigit() or \
+ (i+1<len(s) and s[i] in '+-.' and s[i+1].isdigit()):
+ # number literal
+ is_float = s[i]=='.'
+ is_hex = s[i:i+2]=='0x'
+ if is_hex:
+ i=i+2
+ assert s[i].isdigit(), self.err_string()
+ j=i+1
+ while j<len(s):
+ #print "lex ",repr(s[i]),is_float
+ if s[j].isdigit() or (is_hex and s[j] in "abcdefABCDEF"):
+ j=j+1
+ elif s[j]=='.' and not is_float:
+ assert not is_hex
+ j=j+1
+ is_float=1
+ else:
+ break
+ tok = s[i:j]
+ self.col = j
+ if is_float:
+ kind = float(tok)
+ elif is_hex:
+ kind = int(tok,16)
+ else:
+ kind = int(tok)
+ break
+ if s[i:i+3]=='...':
+ # ellipses
+ #sys.stderr.write( "ELLIPSES "+str(self.get_state()) )
+ tok = s[i:i+3]
+ kind = self._get_kind(tok)
+ self.col = i+3
+ break
+ if s[i] in '*/{}()[]:;,=+-~.<>|&':
+ tok = s[i]
+ kind = tok
+ self.col = i+1
+ break
+ if s[i] == "'":
+ j = i+2
+ while j<len(s) and s[j]!="'":
+ j+=1
+ if j==len(s):
+ raise LexError( self.err_string() + "unterminated char constant" )
+ tok = s[i:j+1]
+ self.col = j+1
+ kind = s[i:j+1]
+ break
+ # keep moving
+ #sys.stderr.write( "lexer ignoring '%s'\n"%s[i] )
+ sys.stderr.write( "lexer ignoring '%s' lno=%d\n"%(s[i],self.lno+1) )
+ i=i+1
+ # end while i < len(s)
+ if i==len(s):
+ # nothing found, go to next line
+ assert tok == ""
+ self.col=0
+ self.lno+=1
+ self._newline()
+ else:
+ # we got one
+ assert tok
+ break
+ # end while self.lno < len(self.lines):
+ self.set_state(tok,kind,self.lno,self.col)
+
+ def err_string(self):
+ "Return helpful error string :)"
+ return self.lines[self.lno]+"\n"+" "*self.col+"^\n"
+
+ def push_state(self):
+ self.stack = self.get_state() # a short stack :)
+ #self.stack.push( self.get_state() )
+
+ def unget_token(self):
+ assert self.stack is not None
+ self.set_state(*self.stack)
+ self.stack = None
+
+ def set_state(self,tok,kind,lno,col):
+ if self.verbose:
+ print "tok,kind,lno,col = ",(tok,kind,lno,col)
+ self.tok = tok
+ self.kind = kind
+ self.lno = lno # line
+ self.col = col # column
+
+ def get_state(self):
+ return self.tok,self.kind,self.lno,self.col
+
+ def get_file(self):
+ return self.so_file
+
+###################################################################
+#
+###################################################################
+#
+
+
--- /dev/null
+#!/usr/bin/env python
+""" cdecl.py - parse c declarations
+
+(c) 2002, 2003, 2004, 2005 Simon Burton <simon@arrowtheory.com>
+Released under GNU LGPL license.
+
+version 0.xx
+
+"""
+
+import string
+
+
+class Node(list):
+ " A node in a parse tree "
+
+ def __init__(self,*items,**kw):
+ list.__init__( self, items )
+ self.lock1 = 0 # these two should be properties (simplifies serializing)
+ self.lock2 = 0
+ self.verbose = 0
+ for key in kw.keys():
+ self.__dict__[key] = kw[key]
+
+ def __str__(self):
+ attrs = []
+ for item in self:
+ if isinstance(item,Node):
+ attrs.append( str(item) )
+ else:
+ attrs.append( repr(item) )
+ attrs = ','.join(attrs)
+ return "%s(%s)"%(self.__class__.__name__,attrs)
+
+ def safe_repr( self, tank ):
+ tank[ str(self) ] = None
+ attrs = []
+ for item in self:
+ if isinstance(item,Node):
+ attrs.append( item.safe_repr(tank) ) # can we use repr here ?
+ else:
+ attrs.append( repr(item) )
+ # this is the dangerous bit:
+ for key, val in self.__dict__.items():
+ if isinstance(val,Node):
+ if str(val) not in tank:
+ attrs.append( '%s=%s'%(key,val.safe_repr(tank)) )
+ else:
+ attrs.append( '%s=%s'%(key,repr(val)) )
+ attrs = ','.join(attrs)
+ return "%s(%s)"%(self.__class__.__name__,attrs)
+
+ def __repr__(self):
+# attrs = ','.join( [repr(item) for item in self] + \
+# [ '%s=%s'%(key,repr(val)) for key,val in self.__dict__.items() ] )
+# return "%s%s"%(self.__class__.__name__,tuple(attrs))
+ return self.safe_repr({})
+
+ def __eq__(self,other):
+ if not isinstance(other,Node):
+ return 0
+ if len(self)!=len(other):
+ return 0
+ for i in range(len(self)):
+ if not self[i]==other[i]:
+ return 0
+ return 1
+
+ def __ne__(self,other):
+ return not self==other
+
+ def filter(self,cls):
+ return [x for x in self if isinstance(x,cls)]
+ #return filter( lambda x:isinstance(x,cls), self )
+
+ def deepfilter(self,cls):
+ " bottom-up "
+ return [x for x in self.nodes() if isinstance(x,cls)]
+
+ def find(self,cls):
+ for x in self:
+ if isinstance(x,cls):
+ return x
+ return None
+
+ def deepfind(self,cls):
+ " bottom-up isinstance search "
+ for x in self:
+ if isinstance(x,Node):
+ if isinstance(x,cls):
+ return x
+ node = x.deepfind(cls)
+ if node is not None:
+ return node
+ if isinstance(self,cls):
+ return self
+ return None
+
+ def leaves(self):
+ for i in self:
+ if isinstance( i, Node ):
+ for j in i.leaves():
+ yield j
+ else:
+ yield i
+
+ def nodes(self):
+ " bottom-up iteration "
+ for i in self:
+ if isinstance( i, Node ):
+ for j in i.nodes():
+ yield j
+ yield self
+
+ def deeplen(self):
+ i=0
+ if not self.lock2:
+ self.lock2=1
+ for item in self:
+ i+=1
+ if isinstance(item,Node):
+ i+=item.deeplen()
+ self.lock2=0
+ else:
+ i+=1
+ return i
+
+ def deepstr(self,level=0,comment=False,nl='\n',indent=' '):
+ if self.deeplen() < 4:
+ nl = ""; indent = ""
+# else:
+# nl="\n"; indent = " "
+ s = []
+ if not self.lock1:
+ self.lock1=1
+ for item in self:
+ if isinstance(item,Node):
+ s.append( indent*(level+1)+item.deepstr(level+1,False,nl,indent) )
+ else:
+ s.append( indent*(level+1)+repr(item) )
+ self.lock1=0
+ else:
+ for item in self:
+ if isinstance(item,Node):
+ s.append( indent*(level+1)+"<recursion...>" )
+ else:
+ s.append( indent*(level+1)+"%s"%repr(item) )
+ s = "%s(%s)"%(self.__class__.__name__,nl+string.join(s,","+nl))
+ if comment:
+ s = '#' + s.replace('\n','\n#')
+ return s
+
+ def clone(self):
+ items = []
+ for item in self:
+ if isinstance(item,Node):
+ item = item.clone()
+ items.append(item)
+ # we skip any attributes...
+ return self.__class__(*items)
+
+ def fastclone(self):
+ # XX is it faster ???
+# print "clone"
+ nodes = [self]
+ idxs = [0]
+ itemss = [ [] ]
+ while nodes:
+ assert len(nodes)==len(idxs)==len(itemss)
+ node = nodes[-1]
+ items = itemss[-1]
+ assert idxs[-1] == len(items)
+ while idxs[-1]==len(node):
+ # pop
+ _node = node.__class__( *items )
+ _node.__dict__.update( node.__dict__ )
+ nodes.pop(-1)
+ idxs.pop(-1)
+ itemss.pop(-1)
+ if not nodes:
+# for node0 in self.nodes():
+# for node1 in _node.nodes():
+# assert node0 is not node1
+# assert _node == self
+ return _node # Done !!
+ node = nodes[-1]
+ items = itemss[-1]
+ items.append(_node) # set
+ idxs[-1] += 1
+ assert idxs[-1] == len(items)
+# assert idxs[-1] < len(node), str( (node,nodes,idxs,itemss) )
+
+ _node = node[ idxs[-1] ]
+ # while idxs[-1]<len(node):
+ if isinstance(_node,Node):
+ # push
+ nodes.append( _node )
+ idxs.append( 0 )
+ itemss.append( [] )
+ else:
+ # next
+ items.append(_node)
+ idxs[-1] += 1
+ assert idxs[-1] == len(items)
+
+ def expose(self,cls):
+ ' expose children of any <cls> instance '
+ # children first
+ for x in self:
+ if isinstance(x,Node):
+ x.expose(cls)
+ # now the tricky bit
+ i=0
+ while i < len(self):
+ if isinstance(self[i],cls):
+ node=self.pop(i)
+ for x in node:
+ assert not isinstance(x,cls)
+ # pass on some attributes
+ if hasattr(node,'lines') and not hasattr(x,'lines'):
+ x.lines=node.lines
+ if hasattr(node,'file') and not hasattr(x,'file'):
+ x.file=node.file
+ self.insert(i,x) # expose
+ i=i+1
+ assert i<=len(self)
+ else:
+ i=i+1
+
+ def get_parent( self, item ): # XX 25% CPU time here XX
+ assert self != item
+ if item in self:
+ return self
+ for child in self:
+ if isinstance(child, Node):
+ parent = child.get_parent(item)
+ if parent is not None:
+ return parent
+ return None
+
+ def expose_node( self, item ):
+ assert self != item
+ parent = self.get_parent(item)
+ idx = parent.index( item )
+ parent[idx:idx+1] = item[:]
+
+ def delete(self,cls):
+ ' delete any <cls> subtree '
+ for x in self:
+ if isinstance(x,Node):
+ x.delete(cls)
+ # now the tricky bit
+ i=0
+ while i < len(self):
+ if isinstance(self[i],cls):
+ self.pop(i)
+ else:
+ i=i+1
+
+ def deeprm(self,item):
+ ' remove any items matching <item> '
+ for x in self:
+ if isinstance(x,Node):
+ x.deeprm(item)
+ # now the tricky bit
+ i=0
+ while i < len(self):
+ if self[i] == item:
+ self.pop(i)
+ else:
+ i=i+1
+
+ def idem(self,cls):
+ " <cls> is made idempotent "
+ # children first
+ for x in self:
+ if isinstance(x,Node):
+ x.idem(cls)
+ if isinstance(self,cls):
+ # now the tricky bit
+ i=0
+ while i < len(self):
+ if isinstance(self[i],cls):
+ node = self.pop(i)
+ for x in node:
+ assert not isinstance(x,cls)
+ self.insert(i,x) # idempotent
+ i=i+1
+ assert i<=len(self)
+ else:
+ i=i+1
+
+if __name__=="__main__":
+ node = Node( 'a', Node(1,2), Node(Node(Node(),1)) )
+
+ print node
+ print node.clone()
+
+
+
+
--- /dev/null
+#!/usr/bin/env python
+# Copyright (c) 2005, National ICT Australia
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the
+# distribution.
+# * Neither the name of National ICT Australia nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+# Some helper routines from the Python API
+cdef extern from "Python.h":
+ # For parsing tuples
+ int PyArg_ParseTuple(object args, char *format, ...)
+
+ # To access tuples
+ object PyTuple_New(int)
+ int PyTuple_SetItem(object, int, object)
+ object PyTuple_GetItem(object, int)
+ object PyTuple_GET_ITEM(object, int)
+ int PyTuple_Size(object tuple)
+
+ int Py_DECREF(object)
+ int Py_INCREF(object)
+
+ # To access integers
+# object PyInt_FromLong(long)
+
+ # To access double
+ object PyFloat_FromDouble(double)
+
+ # To access strings
+ object PyString_FromStringAndSize(char *s, int len)
+ char *PyString_AsString(object string)
+ int PyString_AsStringAndSize(object obj, char **buffer, int *length)
+ object PyString_FromString(char *)
+
+ object PyBuffer_FromMemory(void *ptr, int size)
+ object PyBuffer_FromReadWriteMemory(void *ptr, int size)
+
+#cdef enum:
+ #MAXDIM = 40
+
+# Structs and functions from numarray
+cdef extern from "numarray/numarray.h":
+ #ctypedef Complex64
+
+ ctypedef enum NumRequirements:
+ NUM_CONTIGUOUS
+ NUM_NOTSWAPPED
+ NUM_ALIGNED
+ NUM_WRITABLE
+ NUM_C_ARRAY
+ NUM_UNCONVERTED
+
+ ctypedef enum NumarrayByteOrder:
+ NUM_LITTLE_ENDIAN
+ NUM_BIG_ENDIAN
+
+ cdef enum:
+ UNCONVERTED
+ C_ARRAY
+
+ ctypedef enum NumarrayType:
+ tAny
+ tBool
+ tInt8
+ tUInt8
+ tInt16
+ tUInt16
+ tInt32
+ tUInt32
+ tInt64
+ tUInt64
+ tFloat32
+ tFloat64
+ tComplex32
+ tComplex64
+ tObject
+ tDefault
+ tLong
+
+ # Declaration for the PyArrayObject
+ struct PyArray_Descr:
+ int type_num, elsize
+ char type
+
+ #ctypedef class numarray.numarraycore.NumArray [object PyArrayObject]:
+ ctypedef class numarray._numarray._numarray [object PyArrayObject]:
+ # Compatibility with Numeric
+ cdef char *data
+ cdef int nd
+ #cdef int dimensions[MAXDIM], strides[MAXDIM]
+ cdef int *dimensions, *strides
+ cdef object base
+ cdef PyArray_Descr *descr
+ cdef int flags
+ # New attributes for numarray objects
+ cdef object _data # object must meet buffer API
+ cdef object _shadows # ill-behaved original array.
+ cdef int nstrides # elements in strides array
+ cdef long byteoffset # offset into buffer where array data begins
+ cdef long bytestride # basic seperation of elements in bytes
+ cdef long itemsize # length of 1 element in bytes
+ cdef char byteorder # NUM_BIG_ENDIAN, NUM_LITTLE_ENDIAN
+ cdef char _aligned # test override flag
+ cdef char _contiguous # test override flag
+
+# Functions from numarray API
+cdef extern from "numarray/numarray.h":
+ ctypedef int maybelong
+cdef extern from "numarray/libnumarray.h":
+ void import_libnumarray()
+ object NA_NewAll(
+ int ndim, maybelong* shape, NumarrayType type, void* buffer,
+ maybelong byteoffset, maybelong bytestride, int byteorder, int aligned, int writeable )
+ object NA_NewAllStrides(
+ int ndim, maybelong* shape, maybelong* strides, NumarrayType type, void* buffer,
+ int byteorder, int byteoffset, int aligned, int writeable )
+ object NA_New( void* buffer, NumarrayType type, int ndim,... )
+ object NA_Empty( int ndim, maybelong* shape, NumarrayType type )
+ object NA_NewArray( void* buffer, NumarrayType type, int ndim, ... )
+ object NA_vNewArray( void* buffer, NumarrayType type, int ndim, maybelong *shape )
+
+ object NA_InputArray (object, NumarrayType, int)
+ object NA_OutputArray (object, NumarrayType, int)
+ object NA_IoArray (object, NumarrayType, int)
+# object PyArray_FromDimsAndData(int nd, int *dims, int type, char *data)
+# object PyArray_FromDims(int nd, int *d, int type)
+ object NA_NewAllFromBuffer(int ndim, maybelong *shape, NumarrayType type, object buffer, maybelong byteoffset, maybelong bytestride, int byteorder, int aligned, int writable)
+ object NA_updateDataPtr(object)
+ object NA_getPythonScalar(object, long)
+ object NA_setFromPythonScalar(object, int, object)
+ #object NA_getPythonScalar(PyArrayObject, long)
+ #object NA_setFromPythonScalar(PyArrayObject, int, PyArrayObject)
+
+ object PyArray_ContiguousFromObject(object op, int type, int min_dim, int max_dim)
+ void*NA_OFFSETDATA(_numarray a)
+ int NA_nameToTypeNo(char*)
+ char*NA_typeNoToName(int typeno)
+ object NA_typeNoToTypeObject(int typeno)
+ int NA_ByteOrder()
+
+import numarray
+import_libnumarray()
+
+
+
+
--- /dev/null
+#!/usr/bin/env python
+""" cdecl.py - parse c declarations
+
+(c) 2002, 2003, 2004, 2005 Simon Burton <simon@arrowtheory.com>
+Released under GNU LGPL license.
+
+version 0.xx
+
+"""
+
+import sys
+
+
+class Symbols(object):
+ def __init__(self,parent=None,verbose=False):
+ self.verbose = verbose
+ self.parent=parent # are we a nested namespace?
+ self.lookup = {} # identifiers
+ self.tags = {} # struct, union, enum tags
+
+ def __str__(self):
+ return "Symbols(%s,%s)"%(self.lookup,self.tags)
+
+ def __getitem__(self,key):
+ try:
+ item = self.lookup[key]
+ except KeyError:
+ item = None
+ #if self.parent is not None:
+ #item = self.parent[item]
+ ## self[key] = item # cache
+ #if self.verbose: print "%s.get('%s')='%s'"%(self,key,item)
+ return item
+
+ def __setitem__(self,key,val):
+ #if self.verbose: print "%s.set('%s','%s')"%(self,key,val)
+ assert val is not None
+ self.lookup[key] = val
+
+ def set_tag(self,key,val):
+ #if self.verbose: print "%s.set_tag(%s,%s)"%(self,key,val)
+ assert len(key)
+ self.tags[key] = val
+
+ def deep_get_tag(self,key):
+ try:
+ item = self.tags[key]
+ except KeyError:
+ item = None
+ if self.parent is not None:
+ item = self.parent.deep_get_tag(key)
+ #if self.verbose: print "%s.get_tag(%s)=%s"%(self,key,item)
+ return item
+
+ def get_tag(self,key):
+ try:
+ item = self.tags[key]
+ except KeyError:
+ item = None
+ #if self.verbose: print "%s.get_tag(%s)=%s"%(self,key,item)
+ return item
+
+###################################################################
+#
+###################################################################
+#
+
+
+class ParseError(Exception):
+ def __init__(self,*e):
+ self.e = e
+
+ def __str__(self):
+ return "".join(map(str,self.e))
+
+
+class Parser(object):
+ def parse_error(self,lexer,reason="?",*blah):
+ sys.stderr.write( "%s.parse_error()\n"%self.deepstr() )
+ sys.stderr.write( "at line %s: %s\n"%(lexer.lno+1,reason) )
+ sys.stderr.write( lexer.err_string() )
+ raise ParseError(reason,*blah)
+
+ def expected_error(self,lexer,*l):
+ self.parse_error( lexer, "expected %s, got '%s'"\
+ %(" or ".join(map(repr,l)),lexer.tok))
+
+ def consume(self,lexer,tok):
+ if lexer.tok != tok:
+ self.expected_error(lexer, tok)
+ lexer.get_token()
+
+ def parse_enter(self,lexer):
+ #return
+ self.start_lno=lexer.lno
+ self.file=lexer.so_file
+
+ def parse_leave(self,lexer):
+ #return
+ self.lines = lexer.lines[self.start_lno:max(lexer.lno,self.start_lno+1)]
+
+###################################################################
+#
+###################################################################
+#
+
--- /dev/null
+# parse ('/home/users/simonb/local/petsc-dev/include/petsc.h', '/home/users/simonb/local/petsc-dev/include/petsc.h')
+# gcc -E tmp/728459750.h -I/home/users/simonb/local/petsc-dev/include -I/home/users/simonb/local/petsc-dev/bmake/linux-gnu -I/home/users/simonb/local/petsc-dev/externalpackages/mpich2-1.0.2/src/include > tmp/728459750.i
+write: output/petsc.pyx
+Array
+ ('Pointer', 'NoneType')
+ ('Pointer', 'str')
+ ('Array', 'NoneType')
+ ('Qualifier', 'Pointer', 'NoneType')
+ ('TypeSpecifiers', 'NoneType')
+ ('TypeSpecifiers', 'str')
+BasicType
+ ('str',)
+Pointer
+ ('Function',)
+ ('TypeSpecifiers',)
+ ('Pointer',)
+AbstractDeclarator
+ ('Identifier', 'Array')
+ ('Identifier', 'Ellipses', 'TypeSpecifiers')
+ ('Identifier', 'TypeSpecifiers')
+ ('Identifier', 'Pointer')
+Identifier
+ ('str', 'int')
+ ('str',)
+Typedef
+ ('Identifier', 'Function')
+ ('Identifier', 'TypeSpecifiers')
+ ('Identifier', 'Pointer')
+Function
+ ('AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'TypeSpecifiers')
+ ('AbstractDeclarator', 'AbstractDeclarator', 'Pointer')
+ ('AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'TypeSpecifiers')
+ ('AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'Pointer')
+ ('AbstractDeclarator', 'AbstractDeclarator', 'TypeSpecifiers')
+ ('AbstractDeclarator', 'Pointer')
+ ('AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'TypeSpecifiers')
+ ('AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'TypeSpecifiers')
+ ('AbstractDeclarator', 'TypeSpecifiers')
+ ('AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'TypeSpecifiers')
+ ('AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'TypeSpecifiers')
+ ('AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'TypeSpecifiers')
+ ('AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'TypeSpecifiers')
+ ('AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'AbstractDeclarator', 'TypeSpecifiers')
+StructDeclarator
+ ('Identifier', 'Array')
+ ('Identifier', 'TypeSpecifiers')
+ ('Identifier', 'Pointer')
+TypeAlias
+ ('str',)
+Ellipses
+ ('str',)
+Declarator
+ ('Identifier', 'Array')
+ ('Identifier', 'Function')
+ ('TypeSpecifiers',)
+ ('Identifier', 'TypeSpecifiers')
+ ('Identifier', 'Pointer')
+TypeSpecifiers
+ ('Qualifier', 'Qualifier')
+ ('Union',)
+ ('BasicType',)
+ ('TypeAlias', 'Qualifier', 'StorageClass')
+ ('Struct',)
+ ('Enum',)
+ ('BasicType', 'Qualifier')
+ ('Struct', 'StorageClass')
+ ('BasicType', 'Qualifier', 'StorageClass')
+ ('BasicType', 'Qualifier', 'Qualifier')
+ ('BasicType', 'Qualifier', 'Qualifier', 'Qualifier')
+ ('TypeAlias', 'StorageClass')
+ ()
+ ('TypeAlias', 'Qualifier')
+ ('TypeAlias',)
+ ('GCCBuiltin',)
+ ('BasicType', 'StorageClass')
+StorageClass
+ ('str',)
+GCCBuiltin
+ ('str',)
+Enum
+ ('Tag', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier')
+ ('Tag', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier')
+ ('Tag', 'Identifier', 'Identifier', 'Identifier', 'Identifier')
+ ('Tag', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier')
+ ('Tag', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier')
+ ('Tag', 'Identifier')
+ ('Tag', 'Identifier', 'Identifier', 'Identifier')
+ ('Tag', 'Identifier', 'Identifier', 'Identifier', 'Identifier', 'Identifier')
+ ('Tag', 'Identifier', 'Identifier')
+Struct
+ ('Tag', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator')
+ ('Tag', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator')
+ ('Tag', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator')
+ ('Tag', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator')
+ ('Tag', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator')
+ ('Tag', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator')
+ ('Tag', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator')
+ ('Tag', 'StructDeclarator')
+ ('Tag', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator')
+ ('Tag', 'StructDeclarator', 'StructDeclarator')
+ ('Tag', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator')
+ ('Tag', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator')
+ ('Tag', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator')
+ ('Tag',)
+ ('Tag', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator', 'StructDeclarator')
+Union
+ ('Tag', 'StructDeclarator', 'StructDeclarator')
+Qualifier
+ ('str',)
+Tag
+ ()
+ ('str',)
--- /dev/null
+#!/usr/bin/env python
+
+import adapt
+from adapt import *
+
+tps = [
+ CChar,
+ CSChar,
+ CUChar,
+ CShort,
+ CUShort,
+ CInt,
+ CUInt,
+ CLong,
+ CULong,
+ CLLong,
+ CULLong,
+ CFloat,
+ CDouble,
+ CLDouble,
+]
+
+
+a = CLLong(1234567890123456789)
+print a
+print repr(a)
+print int(a)
+print float(a)
+print
+
+a = CUInt(a)
+print a
+print repr(a)
+print int(a)
+print float(a)
+print
+
+a = CSChar(a)
+print a
+print repr(a)
+print int(a)
+print float(a)
+print
+
+a = CChar(ord('@'))
+print a
+print repr(a)
+print int(a)
+print float(a)
+print
+
+a = CFloat(123456)
+print a
+print repr(a)
+print int(a)
+print float(a)
+print
+
+print dir(adapt)
+mem = adapt._CObject()
+#print mem.malloced
+mem.incref = None
+
+mem = a.addr
+print mem
+print mem.incref
+b = mem.deref
+print b
+
+
+
--- /dev/null
+#!/usr/bin/env python
+
+"""
+
+(c) 2002, 2003, 2004, 2005 Simon Burton <simon@arrowtheory.com>
+Released under GNU LGPL license.
+
+version 0.xx
+
+"""
+
+
+import sys
+import os
+#import string
+#import types
+#import cPickle as pickle
+
+import cparse
+import ir
+
+#_self = None
+
+class WorkUnit(object):
+ def __init__( self, files, modname, filename,
+ std=False, strip=False, mark_cb=None,
+ extradefs="", use_header=None, CC="gcc", CPP="gcc -E", CPPFLAGS="" ):
+ self.files = tuple(files)
+ self.modname = modname
+ self.filename = filename
+ self.CPPFLAGS = CPPFLAGS
+ self.CPP = CPP
+ if CC == 'g++':
+ self.CPPFLAGS = self.CPPFLAGS + " -D__cplusplus"
+ self.std = std
+ self.strip = strip
+ self.mark_cb = mark_cb
+ self.node = None
+ self.extradefs = extradefs
+ self.CC = CC
+ self.use_header = use_header
+ def mkheader( self ):
+ if self.use_header:
+ return self.use_header
+ tmpname = str(abs(hash( (self.files,self.CPPFLAGS) )))
+ name = '.tmp/%s' % tmpname
+ ifile = open( name+'.h', "w" )
+ ifile.write( """
+#define __attribute__(...)
+#define __const const
+#define __restrict
+#define __extension__
+#define __asm__(...)
+#define __asm(...)
+#define __inline__
+#define __inline
+""" )
+ for filename in self.files:
+ if self.std:
+ line = '#include <%s>\n'%filename
+ else:
+ line = '#include "%s"\n'%filename
+ ifile.write( line )
+ print line,
+ ifile.close()
+ cmd = '%s %s %s > %s'%(self.CPP,name+'.h',self.CPPFLAGS,name+'.E')
+ sys.stderr.write( "# %s\n" % cmd )
+ status = os.system( cmd )
+ assert status == 0, "command failed: %s"%cmd
+ assert open(name+'.E').read().count('\n') > 10, "failed to run preprocessor"
+ cmd = '%s -dM %s %s > %s'%(self.CPP,name+'.h',self.CPPFLAGS,name+'.dM')
+ sys.stderr.write( "# %s\n" % cmd )
+ status = os.system( cmd )
+ assert status == 0, "command failed: %s"%cmd
+ assert open(name+'.dM').read().count('\n') > 10, "failed to run preprocessor with -dM"
+ return name
+ def parse(self, verbose=False):
+ sys.stderr.write( "# parse %s\n" % str(self.files) )
+ name = self.mkheader()
+ # read macros
+ f = open(name+'.dM')
+ macros = {}
+ for line in f.readlines():
+ if line:
+ macro = line.split()[1]
+ if macro.count('('):
+ macro = macro[:macro.index('(')]
+ macros[macro] = None
+# keys = macros.keys()
+# keys.sort()
+# for key in keys:
+# print key
+ self.macros = macros
+ # parse preprocessed code
+ f = open(name+'.E')
+ s = f.read() + self.extradefs
+ self.node = cparse.TransUnit(verbose = verbose)
+ sys.stderr.write( "# parsing %s lines\n" % s.count('\n') )
+ self.node.parse( s )
+ if self.strip:
+ self.node.strip(self.files)
+ def transform(self, verbose=False, test_parse=False, test_types=False):
+ sys.stderr.write( "# processing...\n" )
+ self.node = ir.TransUnit( self.node )
+ self.node.transform(verbose, test_parse, test_types)
+# self.node[0].psource()
+ if self.mark_cb is not None:
+ self.node.mark(self.mark_cb,verbose=False)
+ def output( self, defsonly = False, func_cb = None ):
+# if 'prof' in sys.argv:
+# sys.argv.remove('prof')
+# self.output_with_prof()
+# return
+ sys.stderr.write( "# pyxstr...\n" )
+ decls, wrappers, special = self.node.pyxstr(self.files, self.modname, macros = self.macros, defsonly = False, func_cb = func_cb )
+
+ name = self.filename
+ assert name.endswith(".pyx")
+
+ pxi = name[:-3]+'pxi'
+ file = open( pxi, "w" )
+ file.write(decls)
+ sys.stderr.write( "# wrote %s, %d lines\n" % (pxi,decls.count('\n')) )
+ file = open( name, "w" )
+ pxi = pxi.split('/')[-1]
+ file.write('include "%s"\n' % pxi)
+ file.write(wrappers)
+ sys.stderr.write( "# wrote %s, %d lines\n" % (name,wrappers.count('\n')) )
+ try:
+ open( "%s_special.pxi" % name[:-4] )
+ # file already exists
+ except IOError:
+ file = open( "%s_special.pxi" % name[:-4], "w" )
+ file.write(special)
+
+ def pprint(self):
+ for decl in self.node:
+ #decl.psource()
+# cstr = decl.cstr()
+# cstr = cstr.replace( '\n', '\n# ' )
+ print
+# print '#', cstr
+ print decl.deepstr()
+
+# def output_with_prof( self ):
+# global _self
+# from profile import run
+# import pstats
+# _self = self
+# run( "_self.output()", "prof.out" )
+# stats = pstats.Stats("prof.out")
+# stats.sort_stats('cumulative')
+# stats.print_stats(60)
+# print '-'*80,'\n\n\n'
+# stats.print_callers(60)
+# print '-'*80,'\n\n\n'
+# stats.print_callees(60)
+# print '-'*80,'\n\n\n'
+
+
+def file_exists(path):
+ try:
+ os.stat(path)
+ return True
+ except OSError:
+ return False
+
+if sys.platform.count('darwin'):
+ shared_ext = '.dylib'
+else:
+ shared_ext = '.so'
+
+def get_syms(libs, libdirs):
+ # XX write interface to objdump -t XX
+ libnames = []
+ for lib in libs:
+ for ext in shared_ext,'.a':
+ libname = 'lib'+lib+ext
+ for libdir in libdirs:
+ path = libdir+'/'+libname
+ if file_exists(path):
+ libnames.append(path)
+ break
+ else:
+ print "cannot find %s lib as %s in %s" % ( lib, libname, libdir )
+ print 'libnames:', libnames
+ syms = {}
+ accept = [ ' %s '%c for c in 'TVWBCDGRS' ]
+# f = open('syms.out','w')
+ for libname in libnames:
+ fin, fout = os.popen2( 'nm %s' % libname )
+ for line in fout.readlines():
+ for acc in accept:
+ if line.count(acc):
+ left, right = line.split(acc)
+ sym = right.strip()
+ if sys.platform.count('darwin'):
+ if sym[0] == '_':
+ sym = sym[1:] # remove underscore prefix
+ if sym.endswith('.eh'):
+ sym = sym[:-len('.eh')]
+ syms[sym] = None
+# f.write( '%s: %s %s\n' % (sym,line[:-1],libname) )
+ break
+ return syms
+
+
+
--- /dev/null
+#!/usr/bin/env python
+
+"""
+
+(c) 2002, 2003, 2004, 2005 Simon Burton <simon@arrowtheory.com>
+Released under GNU LGPL license.
+
+version 0.xx
+
+"""
+
+
+import sys
+import os
+
+from work_unit import WorkUnit, get_syms
+import ir
+
+
+tao_defs = """
+typedef struct _p_TAO_SOLVER* TAO_SOLVER;
+typedef struct _p_TAOAPPLICATION* TAO_APPLICATION;
+
+typedef const char* TaoMethod;
+
+
+extern int TAO_APP_COOKIE;
+
+
+typedef void TaoApplication;
+typedef void TaoIndexSet;
+typedef void TaoIndexSetPetsc;
+typedef void TaoLinearSolver;
+typedef void TaoLinearSolverPetsc;
+typedef void TaoMat;
+typedef void TaoMatPetsc;
+typedef void TaoVec;
+typedef void TaoVecPetsc;
+
+
+typedef enum {
+ TAOMAT_SYMMETRIC_PSD=1,
+ TAOMAT_SYMMETRIC=2,
+ TAOMAT_UNSYMMETRIC=3 } TaoMatStructure;
+
+typedef enum {
+ TaoRedistributeSubset=0,
+ TaoNoRedistributeSubset=2,
+ TaoSingleProcessor=5,
+ TaoMaskFullSpace=3} TaoPetscISType;
+
+
+typedef enum { TAO_FALSE,TAO_TRUE } TaoTruth;
+
+typedef double TaoScalar;
+
+/* Convergence flags.
+ Be sure to check that these match the flags in
+ $TAO_DIR/include/finclude/tao_solver.h
+*/
+typedef enum {/* converged */
+ TAO_CONVERGED_ATOL = 2, /* F < F_minabs */
+ TAO_CONVERGED_RTOL = 3, /* F < F_mintol*F_initial */
+ TAO_CONVERGED_TRTOL = 4, /* step size small */
+ TAO_CONVERGED_MINF = 5, /* grad F < grad F_min */
+ TAO_CONVERGED_USER = 6, /* User defined */
+ /* diverged */
+ TAO_DIVERGED_MAXITS = -2,
+ TAO_DIVERGED_NAN = -4,
+ TAO_DIVERGED_MAXFCN = -5,
+ TAO_DIVERGED_LS_FAILURE = -6,
+ TAO_DIVERGED_TR_REDUCTION = -7,
+ TAO_DIVERGED_USER = -8, /* User defined */
+ TAO_CONTINUE_ITERATING = 0} TaoTerminateReason;
+"""
+
+def mk_ctao(cflags="",**options):
+ PETSC_DIR = os.environ["PETSC_DIR"]
+ PETSC_ARCH = os.environ["PETSC_ARCH"]
+ TAO_DIR = os.environ["TAO_DIR"]
+ files = [ 'pytao/include/tao.h' ]
+
+ taolibdir = TAO_DIR+'/lib/'+PETSC_ARCH
+ taolibs = [ taolibdir+'/'+name for name in os.listdir(taolibdir) if name.endswith('.so') ]
+ print "taolibs", taolibs
+
+ syms_dict = get_syms( *taolibs )
+ syms = '\n'.join(syms_dict)
+ open('syms.txt','w').write(syms)
+ def cb(node):
+ name, file = node.name, node.file
+ return file and file.count('tao.h') and syms.count(name) and not name in syms_dict
+ modname = '_tao'
+ oname = 'pytao/%s.pyx'%modname # not used
+ unit = WorkUnit(files,modname,oname,cflags,False,mark_cb=cb,**options)
+
+ unit.parse( True )
+ unit.transform(verbose=False, test_parse=False, test_types=False)
+ node = unit.node
+ ctoks = []
+ htoks = []
+ htoks.append( """
+#ifdef __cplusplus
+#define CTAO_EXTERN_C extern "C"
+#else
+#define CTAO_EXTERN_C extern
+#endif
+""")
+ ctoks.append( '#include "tao.h"' )
+ ctoks.append( '#include "petscksp.h"' )
+ htoks.append( tao_defs )
+ names = {}
+ for node in unit.node:
+ if node.function and node.name not in names:
+ names[node.name] = None
+ node.deeprm( ir.StorageClass('extern') )
+ func = node.function
+ for i, arg in enumerate(func.args):
+ arg.name = "arg%s"%i
+ cnode = node.clone()
+ cnode.name = 'c' + node.name
+ ctoks.append( 'extern "C" ' + cnode.cstr() )
+ htoks.append( 'CTAO_EXTERN_C ' + cnode.cstr() + ';' )
+ ctoks.append( '{return %s(%s);}' % ( node.name, ','.join([arg.name for arg in func.args]) ) )
+# else:
+# toks.append( node.cstr() + ';' )
+ ofile = open('pytao/include/ctao.h','w')
+ ofile.write( '\n'.join(htoks) + '\n' )
+ ofile = open('pytao/include/ctao.c','w')
+ ofile.write( '\n'.join(ctoks) + '\n' )
+
+if __name__=="__main__":
+ options = {}
+ for i,arg in enumerate(sys.argv[1:]):
+ if arg.count('='):
+ key,val = arg.split('=')
+ options[key]=val
+
+ mk_ctao(**options)
+
+
+
+
--- /dev/null
+#!/usr/bin/env python
+
+"""
+
+(c) 2002, 2003, 2004, 2005 Simon Burton <simon@arrowtheory.com>
+Released under GNU LGPL license.
+
+version 0.xx
+
+"""
+
+
+import sys
+import os
+
+from work_unit import WorkUnit, get_syms
+
+def mk_mpi():
+ PETSC_DIR = os.environ["PETSC_DIR"]
+ PETSC_ARCH = os.environ["PETSC_ARCH"]
+ cflags = ""
+# for dirname in 'include', 'bmake/'+PETSC_ARCH, 'externalpackages/mpich2-1.0.2/src/include':
+# cflags = cflags + '-I%s/%s '%(PETSC_DIR,dirname)
+ cflags = " ".join( sys.argv[1:] )
+ files = [ 'mpi.h' ]
+
+ mpi = PETSC_DIR + "/externalpackages/mpich2-1.0.2p1/"
+ libmpi = PETSC_DIR + "/externalpackages/mpich2-1.0.2p1/lib/libmpich.a"
+
+ syms = get_syms( libmpi )
+
+ extradefs = """
+"""
+ def cb(node):
+ name, file = node.name, node.file
+# if file.count('mpi'): return False
+ if name and name.count('Fortran'): return False
+# if node.tagged and 'petsc' in node.file:
+# return True
+ return name and ( name in syms or extradefs.count(name) )
+ modname = '_mpi'
+ oname = '%s.pyx'%modname
+ unit = WorkUnit(files,modname,oname,cflags,False,mark_cb=cb,extradefs=extradefs)
+
+ unit.parse( True )
+ del unit.node[100:]
+ unit.transform(verbose=False, test_parse=False, test_types=False)
+ unit.output()
+
+def mk1( cstrs ):
+ names = {}
+ for cstr in cstrs:
+ name = 'tmp/cmdline.h'
+ f = open(name, 'w')
+ f.write( cstr + ';\n' )
+ f.close()
+ files = [ name ]
+ cflags = "-I. -Itmp"
+ unit = WorkUnit(files,cflags,False)
+ unit.parse( True )
+ unit.transform()
+ # unit.pprint()
+ # for node in unit.node:
+ # print
+ # print node.deepstr()
+ # print "# basetype:"
+ # print "# ctype:"
+ # print node.cbasetype().deepstr()
+ # print node.ctype().deepstr()
+ # print
+ # print node.cstr()
+ # print
+ # print node.pyxstr()
+ print '# '+unit.node.deepstr().replace('\n','\n# ')
+ print unit.node.pyxstr(names=names)
+
+if __name__=="__main__":
+ try:
+ import psyco
+ psyco.full()
+ except:
+ pass
+
+# if sys.argv[1:]:
+## cstr = " ".join( sys.argv[1:] )
+# mk1( sys.argv[1:] )
+# else:
+# mk_all()
+ mk_mpi()
+
+
+
+
+
+
--- /dev/null
+#!/usr/bin/env python
+
+"""
+
+(c) 2002, 2003, 2004, 2005 Simon Burton <simon@arrowtheory.com>
+Released under GNU LGPL license.
+
+version 0.xx
+
+"""
+
+import sys
+import os
+
+from work_unit import WorkUnit, get_syms
+
+import genpyx
+
+def mk_petsc(CPPFLAGS = "", CPP = None, modname = '_petsc', oname = None, **options):
+ if oname is None:
+ oname = modname+'.pyx'
+ PETSC_DIR = os.environ["PETSC_DIR"]
+ PETSC_ARCH = os.environ["PETSC_ARCH"]
+# files = [ 'petsc.h' ]
+ files = [ 'petscblaslapack.h', 'petscvec.h', 'petscmat.h', 'petscksp.h' ]
+ files += [ 'src/mat/matimpl.h', 'src/mat/impls/aij/seq/aij.h' ]
+
+ syms = get_syms( 'petsc petscvec petscmat petscksp'.split(), ['%s/lib/%s/'%(PETSC_DIR,PETSC_ARCH)] )
+
+# this may be a macro:
+# PetscErrorCode VecRestoreArray(Vec x,PetscScalar *a[]);
+
+# This function is defined (with body) in the header (!) as static inline:
+#PetscErrorCode VecSetValue(Vec v,int row,PetscScalar value, InsertMode mode);
+
+ extradefs = ''
+ if len(files)>1:
+ extradefs = """
+PetscErrorCode VecRestoreArray(Vec x,PetscScalar *a[]);
+void SETERRQ(PetscErrorCode errorcode,char *message);
+void CHKERRQ(PetscErrorCode errorcode);
+PetscErrorCode VecGetArray(Vec x,PetscScalar *a[]);
+PetscErrorCode MatSetValue(Mat m,PetscInt row,PetscInt col,PetscScalar value,InsertMode mode);
+void BLASgemm_(const char*,const char*,PetscBLASInt*,PetscBLASInt*,PetscBLASInt*,PetscScalar*,PetscScalar*,PetscBLASInt*,PetscScalar*,PetscBLASInt*,PetscScalar*,PetscScalar*,PetscBLASInt*);
+void LAPACKgesvd_(char *,char *,PetscBLASInt*,PetscBLASInt*,PetscScalar *,PetscBLASInt*,PetscReal*,PetscScalar*,PetscBLASInt*,PetscScalar*,PetscBLASInt*,PetscScalar*,PetscBLASInt*,PetscBLASInt*);
+void LAPACKsygv_(PetscBLASInt*,const char*,const char*,PetscBLASInt*,PetscScalar*,PetscBLASInt*,PetscScalar*,PetscBLASInt*,PetscScalar*,PetscScalar*,PetscBLASInt*,PetscBLASInt*);
+PetscErrorCode PetscMalloc(size_t m,void **result);
+PetscErrorCode VecsDestroy(Vecs x);
+PetscErrorCode VecsCreateSeqWithArray(MPI_Comm comm, PetscInt p, PetscInt m, PetscScalar a[], Vecs *x);
+PetscErrorCode VecsCreateSeq(MPI_Comm comm, PetscInt p, PetscInt m, Vecs *x);
+PetscErrorCode VecsDuplicate(Vecs x, Vecs *y);
+"""
+
+# files = ['petsc.h']
+# extradefs = ""
+
+ assert syms
+ def mark_cb(trans_unit, node, *args):
+ " mark the nodes we want to keep "
+ name, file = node.name, node.file
+ if node.compound:
+ # for now, we rip out all struct members
+ node.compound[1:] = [] # XX encapsulation
+ return True
+ if file.count('mpi'): return False
+ if name and name.count('Fortran'): return False
+ if name and name.count('PLAPACK'): return False # PetscPLAPACKInitializePackage etc.
+# if node.tagged and node.tagged.tag.name and node.tagged.tag.name.startswith('__'): return False
+ if node.tagged and 'petsc' in node.file:
+ return True # struct, union or enum
+ return name and ( name in syms or extradefs.count(name) )
+# def func_cb(ostream, node, *args):
+# " build adaptors for functions "
+# return genpyx.FunctionAdaptor(ostream,node)
+ unit = WorkUnit(files,modname,oname,False,mark_cb=mark_cb,extradefs=extradefs,
+ CPPFLAGS=CPPFLAGS, CPP=CPP, **options)
+
+ unit.parse( True )
+ unit.transform(verbose=False, test_parse=False, test_types=False)
+# unit.output(func_cb=func_cb)
+ unit.output()
+
+def main():
+ options = {}
+ for i,arg in enumerate(sys.argv[1:]):
+ if arg.count('='):
+ key,val = arg.split('=')
+ options[key]=val
+ mk_petsc(**options)
+
+if __name__=="__main__":
+ if 'prof' in sys.argv:
+ from profile import run
+ import pstats
+ run( "main()", "prof.out" )
+ stats = pstats.Stats("prof.out")
+ stats.sort_stats('cumulative')
+ stats.print_stats(60)
+ print '-'*80,'\n\n\n'
+ stats.print_callers(60)
+ print '-'*80,'\n\n\n'
+ stats.print_callees(60)
+ print '-'*80,'\n\n\n'
+ else:
+ main()
+
+
+
+
+
+
--- /dev/null
+#!/usr/bin/env python
+
+"""
+
+(c) 2002, 2003, 2004, 2005 Simon Burton <simon@arrowtheory.com>
+Released under GNU LGPL license.
+
+version 0.xx
+
+"""
+
+
+import sys
+import os
+
+from work_unit import WorkUnit, get_syms
+import ir
+
+
+def mk_tao(CPPFLAGS = "", CPP = None, modname = '_tao', oname = None, **options):
+ if oname is None:
+ oname = modname+'.pyx'
+ PETSC_DIR = os.environ["PETSC_DIR"]
+ PETSC_ARCH = os.environ["PETSC_ARCH"]
+ TAO_DIR = os.environ["TAO_DIR"]
+ files = [ 'ctao.h' ]
+
+ taolibdir = TAO_DIR+'/lib/'+PETSC_ARCH
+ syms = get_syms( ['ctao'], [taolibdir] )
+ def cb(trans_unit, node, *args):
+ name, file = node.name, node.file
+ return name in syms
+ extradefs = ""
+ unit = WorkUnit(files,modname,oname,False,mark_cb=cb,extradefs=extradefs,
+ CPPFLAGS=CPPFLAGS, CPP=CPP, **options)
+
+
+ unit.parse( True )
+ unit.transform(verbose=False, test_parse=False, test_types=False)
+ unit.output()
+
+def main():
+ options = {}
+ for i,arg in enumerate(sys.argv[1:]):
+ if arg.count('='):
+ key,val = arg.split('=')
+ options[key]=val
+ mk_tao(**options)
+
+if __name__=="__main__":
+ main()
+
+
+
+
--- /dev/null
+#!/usr/bin/env python
+
+"""
+
+(c) 2002, 2003, 2004, 2005 Simon Burton <simon@arrowtheory.com>
+Released under GNU LGPL license.
+
+version 0.xx
+
+"""
+
+
+import sys
+import os
+
+from work_unit import WorkUnit, get_syms
+
+def mk_all():
+ cflags = ""
+ cflags = " ".join( sys.argv[1:] )
+# syms = get_syms( *libs )
+
+# files = [ "/usr/X11R6/include/X11/Xlib.h" ]
+ files = [ "gdk/gdk.h" ]
+
+ extradefs = """
+"""
+ def cb(node):
+ name, file = node.name, node.file
+ return True
+ return name and ( name in syms or extradefs.count(name) )
+ modname = '_gdk'
+ oname = '%s.pyx'%modname
+ unit = WorkUnit(files,modname,oname,cflags,True,mark_cb=cb,extradefs=extradefs)
+
+ unit.parse( True )
+ unit.transform(verbose=False, test_parse=False, test_types=False)
+ unit.output()
+
+if __name__=="__main__":
+ if 'trace' in sys.argv:
+ sys.argv.remove('trace')
+# import dump
+# def cb( data, filename, lineno, name, lcl, gbl ):
+# print filename, name, lineno, lcl.keys()
+ class Self: pass
+ def trace( frame, event, arg ):
+ code = frame.f_code
+ # print "trace:", event, arg, frame.f_lineno, frame.f_locals
+ name, lineno, filename = code.co_name, frame.f_lineno, code.co_filename
+ lcl, gbl = frame.f_locals, frame.f_globals
+ if 'self' in lcl:
+ self = Self()
+ self.__dict__.update( lcl['self'].__dict__ )
+ depth = -1
+ while frame.f_back:
+ depth += 1
+ frame = frame.f_back
+ indent = " "*depth
+ if "cparse" in filename:
+ if 'self' in lcl:
+ name = lcl['self'].__class__.__name__ + '.' + name
+ msg = indent+"%s: %s"%(name, lineno)
+ print msg
+ return trace
+ elif name == 'get_token':
+ if event == 'return':
+ print indent + str( ( self.lno, self.tok, self.kind ) )
+ return trace
+# dump.Tracer( cb, None ) #, 'cparse.py' )
+ sys.settrace(trace)
+ mk_all()
+ else:
+ try:
+ import psyco
+ psyco.full()
+ except:
+ pass
+
+
+ mk_all()
+
+
+
+
+
+