35
35
# of this type, given a code fragment for the entity.
36
36
# * If for_display, this is for reading by a human in an error
37
37
# message; otherwise it must be valid C code.
38
# * If dll_linkage is not None, it must be 'DL_IMPORT' or
39
# 'DL_EXPORT', and will be added to the base type part of
38
# * If dll_linkage is not None, it must be 'DL_EXPORT' or
39
# 'DL_IMPORT', and will be added to the base type part of
41
41
# * If pyrex = 1, this is for use in a 'cdef extern'
42
42
# statement of a Pyrex include file.
111
111
# A type is incomplete if it is an unsized array,
112
112
# a struct whose attributes are not defined, etc.
115
def cast_code(self, expr_code):
116
return "((%s)%s)" % (self.declaration_code(""), expr_code)
116
119
class CTypedefType:
290
293
default_value = "0"
292
parsetuple_formats = "chilLfd?" # rank -> format
295
parsetuple_formats = ( # rank -> format
296
"?HIkK???", # unsigned
294
def __init__(self, rank, pymemberdef_typecode = None):
300
def __init__(self, rank, signed = 1, pymemberdef_typecode = None):
296
ptf = self.parsetuple_formats[rank]
303
ptf = self.parsetuple_formats[signed][rank]
299
306
self.parsetuple_format = ptf
329
336
from_py_function = "PyInt_AsLong"
331
338
def __init__(self, rank, signed, pymemberdef_typecode = None, is_returncode = 0):
332
CNumericType.__init__(self, rank, pymemberdef_typecode)
339
CNumericType.__init__(self, rank, signed, pymemberdef_typecode)
334
340
self.is_returncode = is_returncode
343
class CUIntType(CIntType):
345
to_py_function = "PyLong_FromUnsignedLong"
346
from_py_function = "PyInt_AsUnsignedLongMask"
337
349
class CULongType(CIntType):
339
351
to_py_function = "PyLong_FromUnsignedLong"
340
from_py_function = "PyLong_AsUnsignedLong"
352
from_py_function = "PyInt_AsUnsignedLongMask"
343
355
class CLongLongType(CIntType):
345
357
to_py_function = "PyLong_FromLongLong"
346
from_py_function = "PyLong_AsLongLong"
358
from_py_function = "PyInt_AsUnsignedLongLongMask"
349
361
class CULongLongType(CIntType):
351
363
to_py_function = "PyLong_FromUnsignedLongLong"
352
from_py_function = "PyLong_AsUnsignedLongLong"
364
from_py_function = "PyInt_AsUnsignedLongLongMask"
355
367
class CFloatType(CNumericType):
359
370
to_py_function = "PyFloat_FromDouble"
360
371
from_py_function = "PyFloat_AsDouble"
373
def __init__(self, rank, pymemberdef_typecode = None):
374
CNumericType.__init__(self, rank, 1, pymemberdef_typecode)
363
377
class CArrayType(CType):
364
378
# base_type CType Element type
424
438
def declaration_code(self, entity_code,
425
439
for_display = 0, dll_linkage = None, pyrex = 0):
440
#print "CPtrType.declaration_code: pointer to", self.base_type ###
426
441
return self.base_type.declaration_code(
427
442
"(*%s)" % entity_code,
428
443
for_display, dll_linkage, pyrex)
683
698
c_short_type = CIntType(1, 1, "T_SHORT")
684
699
c_int_type = CIntType(2, 1, "T_INT")
685
700
c_long_type = CIntType(3, 1, "T_LONG")
686
c_longlong_type = CLongLongType(4, 1)
701
c_longlong_type = CLongLongType(4, 1, "T_LONGLONG")
688
703
c_uchar_type = CIntType(0, 0, "T_UBYTE")
689
704
c_ushort_type = CIntType(1, 0, "T_USHORT")
690
c_uint_type = CIntType(2, 0, "T_UINT")
705
c_uint_type = CUIntType(2, 0, "T_UINT")
691
706
c_ulong_type = CULongType(3, 0, "T_ULONG")
692
c_ulonglong_type = CULongLongType(4, 0)
707
c_ulonglong_type = CULongLongType(4, 0, "T_ULONGLONG")
694
709
c_float_type = CFloatType(5, "T_FLOAT")
695
710
c_double_type = CFloatType(6, "T_DOUBLE")
806
def same_type(type1, type2):
807
return type1.same_as(type2)
809
def assignable_from(type1, type2):
810
return type1.assignable_from(type2)
812
def typecast(to_type, from_type, expr_code):
813
# Return expr_code cast to a C type which can be
814
# assigned to to_type, assuming its existing C type
816
if to_type is from_type or \
817
(not to_type.is_pyobject and assignable_from(to_type, from_type)):
820
#print "typecast: to", to_type, "from", from_type ###
821
return to_type.cast_code(expr_code)