5 from Code import UtilityCode
10 class BaseType(object):
12 # Base class for all Pyrex types including pseudo-types.
14 def cast_code(self, expr_code):
15 return "((%s)%s)" % (self.declaration_code(""), expr_code)
17 def specalization_name(self):
18 return self.declaration_code("").replace(" ", "__")
20 def base_declaration_code(self, base_code, entity_code):
22 return "%s %s" % (base_code, entity_code)
26 class PyrexType(BaseType):
28 # Base class for all Pyrex types.
30 # is_pyobject boolean Is a Python object type
31 # is_extension_type boolean Is a Python extension type
32 # is_numeric boolean Is a C numeric type
33 # is_int boolean Is a C integer type
34 # is_float boolean Is a C floating point type
35 # is_complex boolean Is a C complex type
36 # is_void boolean Is the C void type
37 # is_array boolean Is a C array type
38 # is_ptr boolean Is a C pointer type
39 # is_null_ptr boolean Is the type of NULL
40 # is_cfunction boolean Is a C function type
41 # is_struct_or_union boolean Is a C struct or union type
42 # is_struct boolean Is a C struct type
43 # is_enum boolean Is a C enum type
44 # is_typedef boolean Is a typedef type
45 # is_string boolean Is a C char * type
46 # is_unicode boolean Is a UTF-8 encoded C char * type
47 # is_returncode boolean Is used only to signal exceptions
48 # is_error boolean Is the dummy error type
49 # is_buffer boolean Is buffer access type
50 # has_attributes boolean Has C dot-selectable attributes
51 # default_value string Initial value
52 # pymemberdef_typecode string Type code for PyMemberDef struct
54 # declaration_code(entity_code,
55 # for_display = 0, dll_linkage = None, pyrex = 0)
56 # Returns a code fragment for the declaration of an entity
57 # of this type, given a code fragment for the entity.
58 # * If for_display, this is for reading by a human in an error
59 # message; otherwise it must be valid C code.
60 # * If dll_linkage is not None, it must be 'DL_EXPORT' or
61 # 'DL_IMPORT', and will be added to the base type part of
63 # * If pyrex = 1, this is for use in a 'cdef extern'
64 # statement of a Pyrex include file.
66 # assignable_from(src_type)
67 # Tests whether a variable of this type can be
68 # assigned a value of type src_type.
71 # Tests whether this type represents the same type
75 # Coerces array type into pointer type for use as
76 # a formal argument type.
92 is_struct_or_union = 0
103 pymemberdef_typecode = None
106 # If a typedef, returns the base type.
109 def literal_code(self, value):
110 # Returns a C code fragment representing a literal
111 # value of this type.
115 return self.declaration_code("", for_display = 1).strip()
117 def same_as(self, other_type, **kwds):
118 return self.same_as_resolved_type(other_type.resolve(), **kwds)
120 def same_as_resolved_type(self, other_type):
121 return self == other_type or other_type is error_type
123 def subtype_of(self, other_type):
124 return self.subtype_of_resolved_type(other_type.resolve())
126 def subtype_of_resolved_type(self, other_type):
127 return self.same_as(other_type)
129 def assignable_from(self, src_type):
130 return self.assignable_from_resolved_type(src_type.resolve())
132 def assignable_from_resolved_type(self, src_type):
133 return self.same_as(src_type)
135 def as_argument_type(self):
138 def is_complete(self):
139 # A type is incomplete if it is an unsized array,
140 # a struct whose attributes are not defined, etc.
143 def is_simple_buffer_dtype(self):
144 return (self.is_int or self.is_float or self.is_complex or self.is_pyobject or
145 self.is_extension_type or self.is_ptr)
147 def struct_nesting_depth(self):
148 # Returns the number levels of nested structs. This is
149 # used for constructing a stack for walking the run-time
150 # type information of the struct.
154 def create_typedef_type(cname, base_type, is_external=0):
155 if base_type.is_complex:
157 raise ValueError("Complex external typedefs not supported")
160 return CTypedefType(cname, base_type, is_external)
162 class CTypedefType(BaseType):
164 # Pseudo-type defined with a ctypedef statement in a
165 # 'cdef extern from' block. Delegates most attribute
166 # lookups to the base type. ANYTHING NOT DEFINED
169 # qualified_name string
170 # typedef_cname string
171 # typedef_base_type PyrexType
172 # typedef_is_external bool
175 typedef_is_external = 0
177 to_py_utility_code = None
178 from_py_utility_code = None
181 def __init__(self, cname, base_type, is_external=0):
182 assert not base_type.is_complex
183 self.typedef_cname = cname
184 self.typedef_base_type = base_type
185 self.typedef_is_external = is_external
186 # Make typecodes in external typedefs use typesize-neutral macros
190 if base_type.signed == 0:
191 typecode = "__Pyx_T_UNSIGNED_INT"
193 typecode = "__Pyx_T_SIGNED_INT"
194 elif base_type.is_float and not rank_to_type_name[base_type.rank] == "long double":
195 typecode = "__Pyx_T_FLOATING"
197 self.pymemberdef_typecode = "%s(%s)" % (typecode, cname)
200 return self.typedef_base_type.resolve()
202 def declaration_code(self, entity_code,
203 for_display = 0, dll_linkage = None, pyrex = 0):
204 name = self.declaration_name(for_display, pyrex)
205 return self.base_declaration_code(name, entity_code)
207 def declaration_name(self, for_display = 0, pyrex = 0):
208 if pyrex or for_display:
209 return self.qualified_name
211 return self.typedef_cname
213 def as_argument_type(self):
216 def cast_code(self, expr_code):
217 # If self is really an array (rather than pointer), we can't cast.
218 # For example, the gmp mpz_t.
219 if self.typedef_base_type.is_ptr:
220 return self.typedef_base_type.cast_code(expr_code)
222 return BaseType.cast_code(self, expr_code)
225 return "<CTypedefType %s>" % self.typedef_cname
228 return self.declaration_name(for_display = 1)
230 def _create_utility_code(self, template_utility_code,
231 template_function_name):
232 type_name = self.typedef_cname.replace(" ","_")
233 utility_code = template_utility_code.specialize(
234 type = self.typedef_cname,
235 TypeName = type_name)
236 function_name = template_function_name % type_name
237 return utility_code, function_name
239 def create_to_py_utility_code(self, env):
240 if self.typedef_is_external:
241 if not self.to_py_utility_code:
242 base_type = self.typedef_base_type
244 self.to_py_utility_code, self.to_py_function = \
245 self._create_utility_code(c_typedef_int_to_py_function,
246 '__Pyx_PyInt_to_py_%s')
247 elif base_type.is_float:
248 pass # XXX implement!
249 elif base_type.is_complex:
250 pass # XXX implement!
252 if self.to_py_utility_code:
253 env.use_utility_code(self.to_py_utility_code)
256 return self.typedef_base_type.create_to_py_utility_code(env)
258 def create_from_py_utility_code(self, env):
259 if self.typedef_is_external:
260 if not self.from_py_utility_code:
261 base_type = self.typedef_base_type
263 self.from_py_utility_code, self.from_py_function = \
264 self._create_utility_code(c_typedef_int_from_py_function,
265 '__Pyx_PyInt_from_py_%s')
266 elif base_type.is_float:
267 pass # XXX implement!
268 elif base_type.is_complex:
269 pass # XXX implement!
270 if self.from_py_utility_code:
271 env.use_utility_code(self.from_py_utility_code)
274 return self.typedef_base_type.create_from_py_utility_code(env)
276 def error_condition(self, result_code):
277 if self.typedef_is_external:
278 if self.exception_value:
279 condition = "(%s == (%s)%s)" % (
280 result_code, self.typedef_cname, self.exception_value)
281 if self.exception_check:
282 condition += " && PyErr_Occurred()"
285 return self.typedef_base_type.error_condition(result_code)
287 def __getattr__(self, name):
288 return getattr(self.typedef_base_type, name)
290 class BufferType(BaseType):
292 # Delegates most attribute
293 # lookups to the base type. ANYTHING NOT DEFINED
299 # negative_indices bool
306 def __init__(self, base, dtype, ndim, mode, negative_indices, cast):
310 self.buffer_ptr_type = CPtrType(dtype)
312 self.negative_indices = negative_indices
315 def as_argument_type(self):
318 def __getattr__(self, name):
319 return getattr(self.base, name)
322 return "<BufferType %r>" % self.base
324 def public_decl(base, dll_linkage):
326 return "%s(%s)" % (dll_linkage, base)
330 class PyObjectType(PyrexType):
332 # Base class for all Python object types (reference-counted).
334 # buffer_defaults dict or None Default options for bu
339 pymemberdef_typecode = "T_OBJECT"
340 buffer_defaults = None
343 return "Python object"
346 return "<PyObjectType>"
348 def assignable_from(self, src_type):
349 # except for pointers, conversion will be attempted
350 return not src_type.is_ptr or src_type.is_string
352 def declaration_code(self, entity_code,
353 for_display = 0, dll_linkage = None, pyrex = 0):
354 if pyrex or for_display:
355 return self.base_declaration_code("object", entity_code)
357 return "%s *%s" % (public_decl("PyObject", dll_linkage), entity_code)
359 def as_pyobject(self, cname):
360 if (not self.is_complete()) or self.is_extension_type:
361 return "(PyObject *)" + cname
365 class BuiltinObjectType(PyObjectType):
370 module_name = '__builtin__'
372 alternative_name = None # used for str/bytes duality
374 def __init__(self, name, cname):
377 self.alternative_name = 'bytes'
378 elif name == 'bytes':
379 self.alternative_name = 'str'
381 self.typeptr_cname = "&" + cname
383 def set_scope(self, scope):
386 scope.parent_type = self
389 return "%s object" % self.name
392 return "<%s>"% self.cname
394 def assignable_from(self, src_type):
395 if isinstance(src_type, BuiltinObjectType):
396 return src_type.name == self.name or (
397 src_type.name == self.alternative_name and
398 src_type.name is not None)
400 return not src_type.is_extension_type
402 def typeobj_is_available(self):
405 def attributes_known(self):
408 def subtype_of(self, type):
409 return type.is_pyobject and self.assignable_from(type)
411 def type_test_code(self, arg, notnone=False):
412 type_name = self.name
413 if type_name == 'str':
414 type_check = 'PyString_CheckExact'
415 elif type_name == 'set':
416 type_check = 'PyAnySet_CheckExact'
417 elif type_name == 'frozenset':
418 type_check = 'PyFrozenSet_CheckExact'
419 elif type_name == 'bool':
420 type_check = 'PyBool_Check'
422 type_check = 'Py%s_CheckExact' % type_name.capitalize()
424 check = 'likely(%s(%s))' % (type_check, arg)
426 check = check + ('||((%s) == Py_None)' % arg)
427 error = '(PyErr_Format(PyExc_TypeError, "Expected %s, got %%.200s", Py_TYPE(%s)->tp_name), 0)' % (self.name, arg)
428 return check + '||' + error
430 def declaration_code(self, entity_code,
431 for_display = 0, dll_linkage = None, pyrex = 0):
432 if pyrex or for_display:
433 return self.base_declaration_code(self.name, entity_code)
435 return "%s *%s" % (public_decl("PyObject", dll_linkage), entity_code)
438 class PyExtensionType(PyObjectType):
440 # A Python extension type.
443 # scope CClassScope Attribute namespace
445 # typedef_flag boolean
446 # base_type PyExtensionType or None
447 # module_name string or None Qualified name of defining module
448 # objstruct_cname string Name of PyObject struct
449 # objtypedef_cname string Name of PyObject struct typedef
450 # typeobj_cname string or None C code fragment referring to type object
451 # typeptr_cname string or None Name of pointer to external type object
452 # vtabslot_cname string Name of C method table member
453 # vtabstruct_cname string Name of C method table struct
454 # vtabptr_cname string Name of pointer to C method table
455 # vtable_cname string Name of C method table definition
457 is_extension_type = 1
460 objtypedef_cname = None
462 def __init__(self, name, typedef_flag, base_type):
465 self.typedef_flag = typedef_flag
466 self.base_type = base_type
467 self.module_name = None
468 self.objstruct_cname = None
469 self.typeobj_cname = None
470 self.typeptr_cname = None
471 self.vtabslot_cname = None
472 self.vtabstruct_cname = None
473 self.vtabptr_cname = None
474 self.vtable_cname = None
476 def set_scope(self, scope):
479 scope.parent_type = self
481 def subtype_of_resolved_type(self, other_type):
482 if other_type.is_extension_type:
483 return self is other_type or (
484 self.base_type and self.base_type.subtype_of(other_type))
486 return other_type is py_object_type
488 def typeobj_is_available(self):
489 # Do we have a pointer to the type object?
490 return self.typeptr_cname
492 def typeobj_is_imported(self):
493 # If we don't know the C name of the type object but we do
494 # know which module it's defined in, it will be imported.
495 return self.typeobj_cname is None and self.module_name is not None
497 def declaration_code(self, entity_code,
498 for_display = 0, dll_linkage = None, pyrex = 0, deref = 0):
499 if pyrex or for_display:
500 return self.base_declaration_code(self.name, entity_code)
502 if self.typedef_flag:
505 base_format = "struct %s"
506 base = public_decl(base_format % self.objstruct_cname, dll_linkage)
508 return "%s %s" % (base, entity_code)
510 return "%s *%s" % (base, entity_code)
512 def type_test_code(self, py_arg, notnone=False):
514 none_check = "((%s) == Py_None)" % py_arg
515 type_check = "likely(__Pyx_TypeTest(%s, %s))" % (
516 py_arg, self.typeptr_cname)
520 return "likely(%s || %s)" % (none_check, type_check)
522 def attributes_known(self):
523 return self.scope is not None
529 return "<PyExtensionType %s%s>" % (self.scope.class_name,
530 ("", " typedef")[self.typedef_flag])
533 class CType(PyrexType):
535 # Base class for all C types (non-reference-counted).
537 # to_py_function string C function for converting to Python object
538 # from_py_function string C function for constructing from Python object
541 to_py_function = None
542 from_py_function = None
543 exception_value = None
546 def create_to_py_utility_code(self, env):
547 return self.to_py_function is not None
549 def create_from_py_utility_code(self, env):
550 return self.from_py_function is not None
552 def error_condition(self, result_code):
555 conds.append("(!%s)" % result_code)
556 elif self.exception_value is not None:
557 conds.append("(%s == (%s)%s)" % (result_code, self.sign_and_name(), self.exception_value))
558 if self.exception_check:
559 conds.append("PyErr_Occurred()")
561 return " && ".join(conds)
566 class CVoidType(CType):
572 def declaration_code(self, entity_code,
573 for_display = 0, dll_linkage = None, pyrex = 0):
574 base = public_decl("void", dll_linkage)
575 return self.base_declaration_code(base, entity_code)
577 def is_complete(self):
581 class CNumericType(CType):
583 # Base class for all C numeric types.
585 # rank integer Relative size
586 # signed integer 0 = unsigned, 1 = unspecified, 2 = explicitly signed
592 sign_words = ("unsigned ", "", "signed ")
594 def __init__(self, rank, signed = 1, pymemberdef_typecode = None):
597 self.pymemberdef_typecode = pymemberdef_typecode
599 def sign_and_name(self):
600 s = self.sign_words[self.signed]
601 n = rank_to_type_name[self.rank]
605 return "<CNumericType %s>" % self.sign_and_name()
607 def declaration_code(self, entity_code,
608 for_display = 0, dll_linkage = None, pyrex = 0):
609 base = public_decl(self.sign_and_name(), dll_linkage)
611 base = base.replace('PY_LONG_LONG', 'long long')
612 return self.base_declaration_code(base, entity_code)
615 type_conversion_predeclarations = ""
616 type_conversion_functions = ""
618 c_int_from_py_function = UtilityCode(
620 static INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject *);
623 static INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject* x) {
624 const %(type)s neg_one = (%(type)s)-1, const_zero = 0;
625 const int is_unsigned = neg_one > const_zero;
626 if (sizeof(%(type)s) < sizeof(long)) {
627 long val = __Pyx_PyInt_AsLong(x);
628 if (unlikely(val != (long)(%(type)s)val)) {
629 if (!unlikely(val == -1 && PyErr_Occurred())) {
630 PyErr_SetString(PyExc_OverflowError,
631 (is_unsigned && unlikely(val < 0)) ?
632 "can't convert negative value to %(type)s" :
633 "value too large to convert to %(type)s");
637 return (%(type)s)val;
639 return (%(type)s)__Pyx_PyInt_As%(SignWord)sLong(x);
643 c_long_from_py_function = UtilityCode(
645 static INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject *);
648 static INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject* x) {
649 const %(type)s neg_one = (%(type)s)-1, const_zero = 0;
650 const int is_unsigned = neg_one > const_zero;
651 #if PY_VERSION_HEX < 0x03000000
652 if (likely(PyInt_Check(x))) {
653 long val = PyInt_AS_LONG(x);
654 if (is_unsigned && unlikely(val < 0)) {
655 PyErr_SetString(PyExc_OverflowError,
656 "can't convert negative value to %(type)s");
659 return (%(type)s)val;
662 if (likely(PyLong_Check(x))) {
664 if (unlikely(Py_SIZE(x) < 0)) {
665 PyErr_SetString(PyExc_OverflowError,
666 "can't convert negative value to %(type)s");
669 return PyLong_AsUnsigned%(TypeName)s(x);
671 return PyLong_As%(TypeName)s(x);
675 PyObject *tmp = __Pyx_PyNumber_Int(x);
676 if (!tmp) return (%(type)s)-1;
677 val = __Pyx_PyInt_As%(SignWord)s%(TypeName)s(tmp);
684 c_typedef_int_from_py_function = UtilityCode(
686 static INLINE %(type)s __Pyx_PyInt_from_py_%(TypeName)s(PyObject *);
689 static INLINE %(type)s __Pyx_PyInt_from_py_%(TypeName)s(PyObject* x) {
690 const %(type)s neg_one = (%(type)s)-1, const_zero = 0;
691 const int is_unsigned = neg_one > const_zero;
692 if (sizeof(%(type)s) == sizeof(char)) {
694 return (%(type)s)__Pyx_PyInt_AsUnsignedChar(x);
696 return (%(type)s)__Pyx_PyInt_AsSignedChar(x);
697 } else if (sizeof(%(type)s) == sizeof(short)) {
699 return (%(type)s)__Pyx_PyInt_AsUnsignedShort(x);
701 return (%(type)s)__Pyx_PyInt_AsSignedShort(x);
702 } else if (sizeof(%(type)s) == sizeof(int)) {
704 return (%(type)s)__Pyx_PyInt_AsUnsignedInt(x);
706 return (%(type)s)__Pyx_PyInt_AsSignedInt(x);
707 } else if (sizeof(%(type)s) == sizeof(long)) {
709 return (%(type)s)__Pyx_PyInt_AsUnsignedLong(x);
711 return (%(type)s)__Pyx_PyInt_AsSignedLong(x);
712 } else if (sizeof(%(type)s) == sizeof(PY_LONG_LONG)) {
714 return (%(type)s)__Pyx_PyInt_AsUnsignedLongLong(x);
716 return (%(type)s)__Pyx_PyInt_AsSignedLongLong(x);
718 } else if (sizeof(%(type)s) > sizeof(short) &&
719 sizeof(%(type)s) < sizeof(int)) { /* __int32 ILP64 ? */
721 return (%(type)s)__Pyx_PyInt_AsUnsignedInt(x);
723 return (%(type)s)__Pyx_PyInt_AsSignedInt(x);
726 PyErr_SetString(PyExc_TypeError, "%(TypeName)s");
731 c_typedef_int_to_py_function = UtilityCode(
733 static INLINE PyObject *__Pyx_PyInt_to_py_%(TypeName)s(%(type)s);
736 static INLINE PyObject *__Pyx_PyInt_to_py_%(TypeName)s(%(type)s val) {
737 const %(type)s neg_one = (%(type)s)-1, const_zero = 0;
738 const int is_unsigned = neg_one > const_zero;
739 if (sizeof(%(type)s) < sizeof(long)) {
740 return PyInt_FromLong((long)val);
741 } else if (sizeof(%(type)s) == sizeof(long)) {
743 return PyLong_FromUnsignedLong((unsigned long)val);
745 return PyInt_FromLong((long)val);
746 } else { /* (sizeof(%(type)s) > sizeof(long)) */
748 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)val);
750 return PyLong_FromLongLong((PY_LONG_LONG)val);
755 class CIntType(CNumericType):
759 to_py_function = "PyInt_FromLong"
760 from_py_function = "__Pyx_PyInt_AsInt"
763 def __init__(self, rank, signed, pymemberdef_typecode = None, is_returncode = 0):
764 CNumericType.__init__(self, rank, signed, pymemberdef_typecode)
765 self.is_returncode = is_returncode
766 if self.from_py_function == "__Pyx_PyInt_AsInt":
767 self.from_py_function = self.get_type_conversion()
769 def get_type_conversion(self):
770 ctype = self.declaration_code('')
771 bits = ctype.split(" ", 1)
773 sign_word, type_name = "", bits[0]
775 sign_word, type_name = bits
776 type_name = type_name.replace("PY_LONG_LONG","long long")
777 SignWord = sign_word.title()
778 TypeName = type_name.title().replace(" ", "")
779 if "Long" in TypeName:
780 utility_code = c_long_from_py_function
782 utility_code = c_int_from_py_function
783 utility_code.specialize(self,
786 func_name = "__Pyx_PyInt_As%s%s" % (SignWord, TypeName)
789 def assignable_from_resolved_type(self, src_type):
790 return src_type.is_int or src_type.is_enum or src_type is error_type
793 class CBIntType(CIntType):
795 to_py_function = "__Pyx_PyBool_FromLong"
796 from_py_function = "__Pyx_PyObject_IsTrue"
800 class CAnonEnumType(CIntType):
804 def sign_and_name(self):
808 class CUIntType(CIntType):
810 to_py_function = "PyLong_FromUnsignedLong"
814 class CLongType(CIntType):
816 to_py_function = "PyInt_FromLong"
819 class CULongType(CUIntType):
821 to_py_function = "PyLong_FromUnsignedLong"
824 class CLongLongType(CIntType):
826 to_py_function = "PyLong_FromLongLong"
829 class CULongLongType(CUIntType):
831 to_py_function = "PyLong_FromUnsignedLongLong"
834 class CPySSizeTType(CIntType):
836 to_py_function = "PyInt_FromSsize_t"
837 from_py_function = "__Pyx_PyIndex_AsSsize_t"
839 def sign_and_name(self):
840 return rank_to_type_name[self.rank]
843 class CSizeTType(CUIntType):
845 to_py_function = "__Pyx_PyInt_FromSize_t"
846 from_py_function = "__Pyx_PyInt_AsSize_t"
848 def sign_and_name(self):
849 return rank_to_type_name[self.rank]
852 class CFloatType(CNumericType):
855 to_py_function = "PyFloat_FromDouble"
856 from_py_function = "__pyx_PyFloat_AsDouble"
858 def __init__(self, rank, pymemberdef_typecode = None, math_h_modifier = ''):
859 CNumericType.__init__(self, rank, 1, pymemberdef_typecode)
860 self.math_h_modifier = math_h_modifier
862 def assignable_from_resolved_type(self, src_type):
863 return (src_type.is_numeric and not src_type.is_complex) or src_type is error_type
866 class CComplexType(CNumericType):
869 to_py_function = "__pyx_PyComplex_FromComplex"
873 def __init__(self, real_type):
874 while real_type.is_typedef and not real_type.typedef_is_external:
875 real_type = real_type.typedef_base_type
876 if real_type.is_typedef and real_type.typedef_is_external:
877 # The below is not actually used: Coercions are currently disabled
878 # so that complex types of external types can not be created
879 self.funcsuffix = "_%s" % real_type.specalization_name()
880 elif hasattr(real_type, 'math_h_modifier'):
881 self.funcsuffix = real_type.math_h_modifier
883 self.funcsuffix = "_%s" % real_type.specalization_name()
885 self.real_type = real_type
886 CNumericType.__init__(self, real_type.rank + 0.5, real_type.signed)
888 self.from_parts = "%s_from_parts" % self.specalization_name()
889 self.default_value = "%s(0, 0)" % self.from_parts
891 def __eq__(self, other):
892 if isinstance(self, CComplexType) and isinstance(other, CComplexType):
893 return self.real_type == other.real_type
897 def __ne__(self, other):
898 if isinstance(self, CComplexType) and isinstance(other, CComplexType):
899 return self.real_type != other.real_type
903 def __lt__(self, other):
904 if isinstance(self, CComplexType) and isinstance(other, CComplexType):
905 return self.real_type < other.real_type
907 # this is arbitrary, but it makes sure we always have
908 # *some* kind of order
912 return ~hash(self.real_type)
914 def declaration_code(self, entity_code,
915 for_display = 0, dll_linkage = None, pyrex = 0):
917 base = public_decl(self.real_type.sign_and_name() + " complex", dll_linkage)
919 base = public_decl(self.sign_and_name(), dll_linkage)
920 return self.base_declaration_code(base, entity_code)
922 def sign_and_name(self):
923 real_type_name = self.real_type.specalization_name()
924 real_type_name = real_type_name.replace('long__double','long_double')
925 return Naming.type_prefix + real_type_name + "_complex"
927 def assignable_from(self, src_type):
928 # Temporary hack/feature disabling, see #441
929 if (not src_type.is_complex and src_type.is_numeric and src_type.is_typedef
930 and src_type.typedef_is_external):
933 return super(CComplexType, self).assignable_from(src_type)
935 def assignable_from_resolved_type(self, src_type):
936 return (src_type.is_complex and self.real_type.assignable_from_resolved_type(src_type.real_type)
937 or src_type.is_numeric and self.real_type.assignable_from_resolved_type(src_type)
938 or src_type is error_type)
940 def attributes_known(self):
941 if self.scope is None:
943 self.scope = scope = Symtab.CClassScope(
947 scope.parent_type = self
948 scope.declare_var("real", self.real_type, None, "real", is_cdef=True)
949 scope.declare_var("imag", self.real_type, None, "imag", is_cdef=True)
950 entry = scope.declare_cfunction(
952 CFuncType(self, [CFuncTypeArg("self", self, None)]),
955 cname="__Pyx_c_conj%s" % self.funcsuffix)
959 def create_declaration_utility_code(self, env):
960 # This must always be run, because a single CComplexType instance can be shared
961 # across multiple compilations (the one created in the module scope)
962 env.use_utility_code(complex_header_utility_code)
963 env.use_utility_code(complex_real_imag_utility_code)
964 for utility_code in (complex_type_utility_code,
965 complex_from_parts_utility_code,
966 complex_arithmatic_utility_code):
967 env.use_utility_code(
968 utility_code.specialize(
970 real_type = self.real_type.declaration_code(''),
971 m = self.funcsuffix))
974 def create_to_py_utility_code(self, env):
975 env.use_utility_code(complex_real_imag_utility_code)
976 env.use_utility_code(complex_to_py_utility_code)
979 def create_from_py_utility_code(self, env):
980 self.real_type.create_from_py_utility_code(env)
982 for utility_code in (complex_from_parts_utility_code,
983 complex_from_py_utility_code):
984 env.use_utility_code(
985 utility_code.specialize(
987 real_type = self.real_type.declaration_code(''),
988 m = self.funcsuffix))
989 self.from_py_function = "__Pyx_PyComplex_As_" + self.specalization_name()
992 def lookup_op(self, nargs, op):
994 return self.binops[nargs, op]
998 op_name = complex_ops[nargs, op]
999 self.binops[nargs, op] = func_name = "__Pyx_c_%s%s" % (op_name, self.funcsuffix)
1004 def unary_op(self, op):
1005 return self.lookup_op(1, op)
1007 def binary_op(self, op):
1008 return self.lookup_op(2, op)
1012 (1, 'zero'): 'is_zero',
1020 complex_header_utility_code = UtilityCode(
1021 proto_block='h_code',
1023 #if !defined(CYTHON_CCOMPLEX)
1024 #if defined(__cplusplus)
1025 #define CYTHON_CCOMPLEX 1
1026 #elif defined(_Complex_I)
1027 #define CYTHON_CCOMPLEX 1
1029 #define CYTHON_CCOMPLEX 0
1037 #include <complex.h>
1042 complex_real_imag_utility_code = UtilityCode(
1046 #define __Pyx_CREAL(z) ((z).real())
1047 #define __Pyx_CIMAG(z) ((z).imag())
1049 #define __Pyx_CREAL(z) (__real__(z))
1050 #define __Pyx_CIMAG(z) (__imag__(z))
1053 #define __Pyx_CREAL(z) ((z).real)
1054 #define __Pyx_CIMAG(z) ((z).imag)
1058 complex_type_utility_code = UtilityCode(
1059 proto_block='complex_type_declarations',
1063 typedef ::std::complex< %(real_type)s > %(type_name)s;
1065 typedef %(real_type)s _Complex %(type_name)s;
1068 typedef struct { %(real_type)s real, imag; } %(type_name)s;
1072 complex_from_parts_utility_code = UtilityCode(
1073 proto_block='utility_code_proto',
1077 static INLINE %(type)s %(type_name)s_from_parts(%(real_type)s, %(real_type)s);
1079 static INLINE %(type)s %(type_name)s_from_parts(%(real_type)s, %(real_type)s);
1082 static INLINE %(type)s %(type_name)s_from_parts(%(real_type)s, %(real_type)s);
1088 static INLINE %(type)s %(type_name)s_from_parts(%(real_type)s x, %(real_type)s y) {
1089 return ::std::complex< %(real_type)s >(x, y);
1092 static INLINE %(type)s %(type_name)s_from_parts(%(real_type)s x, %(real_type)s y) {
1093 return x + y*(%(type)s)_Complex_I;
1097 static INLINE %(type)s %(type_name)s_from_parts(%(real_type)s x, %(real_type)s y) {
1106 complex_to_py_utility_code = UtilityCode(
1108 #define __pyx_PyComplex_FromComplex(z) \\
1109 PyComplex_FromDoubles((double)__Pyx_CREAL(z), \\
1110 (double)__Pyx_CIMAG(z))
1113 complex_from_py_utility_code = UtilityCode(
1115 static %(type)s __Pyx_PyComplex_As_%(type_name)s(PyObject*);
1118 static %(type)s __Pyx_PyComplex_As_%(type_name)s(PyObject* o) {
1120 if (PyComplex_CheckExact(o))
1121 cval = ((PyComplexObject *)o)->cval;
1123 cval = PyComplex_AsCComplex(o);
1124 return %(type_name)s_from_parts(
1125 (%(real_type)s)cval.real,
1126 (%(real_type)s)cval.imag);
1130 complex_arithmatic_utility_code = UtilityCode(
1133 #define __Pyx_c_eq%(m)s(a, b) ((a)==(b))
1134 #define __Pyx_c_sum%(m)s(a, b) ((a)+(b))
1135 #define __Pyx_c_diff%(m)s(a, b) ((a)-(b))
1136 #define __Pyx_c_prod%(m)s(a, b) ((a)*(b))
1137 #define __Pyx_c_quot%(m)s(a, b) ((a)/(b))
1138 #define __Pyx_c_neg%(m)s(a) (-(a))
1140 #define __Pyx_c_is_zero%(m)s(z) ((z)==(%(real_type)s)0)
1141 #define __Pyx_c_conj%(m)s(z) (::std::conj(z))
1142 /*#define __Pyx_c_abs%(m)s(z) (::std::abs(z))*/
1144 #define __Pyx_c_is_zero%(m)s(z) ((z)==0)
1145 #define __Pyx_c_conj%(m)s(z) (conj%(m)s(z))
1146 /*#define __Pyx_c_abs%(m)s(z) (cabs%(m)s(z))*/
1149 static INLINE int __Pyx_c_eq%(m)s(%(type)s, %(type)s);
1150 static INLINE %(type)s __Pyx_c_sum%(m)s(%(type)s, %(type)s);
1151 static INLINE %(type)s __Pyx_c_diff%(m)s(%(type)s, %(type)s);
1152 static INLINE %(type)s __Pyx_c_prod%(m)s(%(type)s, %(type)s);
1153 static INLINE %(type)s __Pyx_c_quot%(m)s(%(type)s, %(type)s);
1154 static INLINE %(type)s __Pyx_c_neg%(m)s(%(type)s);
1155 static INLINE int __Pyx_c_is_zero%(m)s(%(type)s);
1156 static INLINE %(type)s __Pyx_c_conj%(m)s(%(type)s);
1157 /*static INLINE %(real_type)s __Pyx_c_abs%(m)s(%(type)s);*/
1163 static INLINE int __Pyx_c_eq%(m)s(%(type)s a, %(type)s b) {
1164 return (a.real == b.real) && (a.imag == b.imag);
1166 static INLINE %(type)s __Pyx_c_sum%(m)s(%(type)s a, %(type)s b) {
1168 z.real = a.real + b.real;
1169 z.imag = a.imag + b.imag;
1172 static INLINE %(type)s __Pyx_c_diff%(m)s(%(type)s a, %(type)s b) {
1174 z.real = a.real - b.real;
1175 z.imag = a.imag - b.imag;
1178 static INLINE %(type)s __Pyx_c_prod%(m)s(%(type)s a, %(type)s b) {
1180 z.real = a.real * b.real - a.imag * b.imag;
1181 z.imag = a.real * b.imag + a.imag * b.real;
1184 static INLINE %(type)s __Pyx_c_quot%(m)s(%(type)s a, %(type)s b) {
1186 %(real_type)s denom = b.real * b.real + b.imag * b.imag;
1187 z.real = (a.real * b.real + a.imag * b.imag) / denom;
1188 z.imag = (a.imag * b.real - a.real * b.imag) / denom;
1191 static INLINE %(type)s __Pyx_c_neg%(m)s(%(type)s a) {
1197 static INLINE int __Pyx_c_is_zero%(m)s(%(type)s a) {
1198 return (a.real == 0) && (a.imag == 0);
1200 static INLINE %(type)s __Pyx_c_conj%(m)s(%(type)s a) {
1207 static INLINE %(real_type)s __Pyx_c_abs%(m)s(%(type)s z) {
1209 return hypot%(m)s(z.real, z.imag);
1211 return sqrt%(m)s(z.real*z.real + z.imag*z.imag);
1218 class CArrayType(CType):
1219 # base_type CType Element type
1220 # size integer or None Number of elements
1224 def __init__(self, base_type, size):
1225 self.base_type = base_type
1227 if base_type is c_char_type:
1231 return "<CArrayType %s %s>" % (self.size, repr(self.base_type))
1233 def same_as_resolved_type(self, other_type):
1234 return ((other_type.is_array and
1235 self.base_type.same_as(other_type.base_type))
1236 or other_type is error_type)
1238 def assignable_from_resolved_type(self, src_type):
1239 # Can't assign to a variable of an array type
1242 def element_ptr_type(self):
1243 return c_ptr_type(self.base_type)
1245 def declaration_code(self, entity_code,
1246 for_display = 0, dll_linkage = None, pyrex = 0):
1247 if self.size is not None:
1248 dimension_code = self.size
1251 if entity_code.startswith("*"):
1252 entity_code = "(%s)" % entity_code
1253 return self.base_type.declaration_code(
1254 "%s[%s]" % (entity_code, dimension_code),
1255 for_display, dll_linkage, pyrex)
1257 def as_argument_type(self):
1258 return c_ptr_type(self.base_type)
1260 def is_complete(self):
1261 return self.size is not None
1264 class CPtrType(CType):
1265 # base_type CType Referenced type
1270 def __init__(self, base_type):
1271 self.base_type = base_type
1274 return "<CPtrType %s>" % repr(self.base_type)
1276 def same_as_resolved_type(self, other_type):
1277 return ((other_type.is_ptr and
1278 self.base_type.same_as(other_type.base_type))
1279 or other_type is error_type)
1281 def declaration_code(self, entity_code,
1282 for_display = 0, dll_linkage = None, pyrex = 0):
1283 #print "CPtrType.declaration_code: pointer to", self.base_type ###
1284 return self.base_type.declaration_code(
1285 "*%s" % entity_code,
1286 for_display, dll_linkage, pyrex)
1288 def assignable_from_resolved_type(self, other_type):
1289 if other_type is error_type:
1291 if other_type.is_null_ptr:
1293 if self.base_type.is_cfunction:
1294 if other_type.is_ptr:
1295 other_type = other_type.base_type.resolve()
1296 if other_type.is_cfunction:
1297 return self.base_type.pointer_assignable_from_resolved_type(other_type)
1300 if other_type.is_array or other_type.is_ptr:
1301 return self.base_type.is_void or self.base_type.same_as(other_type.base_type)
1305 class CNullPtrType(CPtrType):
1310 class CFuncType(CType):
1312 # args [CFuncTypeArg]
1313 # has_varargs boolean
1314 # exception_value string
1315 # exception_check boolean True if PyErr_Occurred check needed
1316 # calling_convention string Function calling convention
1317 # nogil boolean Can be called without gil
1318 # with_gil boolean Acquire gil around function body
1323 def __init__(self, return_type, args, has_varargs = 0,
1324 exception_value = None, exception_check = 0, calling_convention = "",
1325 nogil = 0, with_gil = 0, is_overridable = 0, optional_arg_count = 0):
1326 self.return_type = return_type
1328 self.has_varargs = has_varargs
1329 self.optional_arg_count = optional_arg_count
1330 self.exception_value = exception_value
1331 self.exception_check = exception_check
1332 self.calling_convention = calling_convention
1334 self.with_gil = with_gil
1335 self.is_overridable = is_overridable
1338 arg_reprs = map(repr, self.args)
1339 if self.has_varargs:
1340 arg_reprs.append("...")
1341 if self.exception_value:
1342 except_clause = " %r" % self.exception_value
1345 if self.exception_check:
1346 except_clause += "?"
1347 return "<CFuncType %s %s[%s]%s>" % (
1348 repr(self.return_type),
1349 self.calling_convention_prefix(),
1350 ",".join(arg_reprs),
1353 def calling_convention_prefix(self):
1354 cc = self.calling_convention
1360 def same_c_signature_as(self, other_type, as_cmethod = 0):
1361 return self.same_c_signature_as_resolved_type(
1362 other_type.resolve(), as_cmethod)
1364 def same_c_signature_as_resolved_type(self, other_type, as_cmethod = 0):
1365 #print "CFuncType.same_c_signature_as_resolved_type:", \
1366 # self, other_type, "as_cmethod =", as_cmethod ###
1367 if other_type is error_type:
1369 if not other_type.is_cfunction:
1371 if self.is_overridable != other_type.is_overridable:
1373 nargs = len(self.args)
1374 if nargs != len(other_type.args):
1376 # When comparing C method signatures, the first argument
1377 # is exempt from compatibility checking (the proper check
1378 # is performed elsewhere).
1379 for i in range(as_cmethod, nargs):
1380 if not self.args[i].type.same_as(
1381 other_type.args[i].type):
1383 if self.has_varargs != other_type.has_varargs:
1385 if self.optional_arg_count != other_type.optional_arg_count:
1387 if not self.return_type.same_as(other_type.return_type):
1389 if not self.same_calling_convention_as(other_type):
1393 def compatible_signature_with(self, other_type, as_cmethod = 0):
1394 return self.compatible_signature_with_resolved_type(other_type.resolve(), as_cmethod)
1396 def compatible_signature_with_resolved_type(self, other_type, as_cmethod):
1397 #print "CFuncType.same_c_signature_as_resolved_type:", \
1398 # self, other_type, "as_cmethod =", as_cmethod ###
1399 if other_type is error_type:
1401 if not other_type.is_cfunction:
1403 if not self.is_overridable and other_type.is_overridable:
1405 nargs = len(self.args)
1406 if nargs - self.optional_arg_count != len(other_type.args) - other_type.optional_arg_count:
1408 if self.optional_arg_count < other_type.optional_arg_count:
1410 # When comparing C method signatures, the first argument
1411 # is exempt from compatibility checking (the proper check
1412 # is performed elsewhere).
1413 for i in range(as_cmethod, len(other_type.args)):
1414 if not self.args[i].type.same_as(
1415 other_type.args[i].type):
1417 if self.has_varargs != other_type.has_varargs:
1419 if not self.return_type.subtype_of_resolved_type(other_type.return_type):
1421 if not self.same_calling_convention_as(other_type):
1423 if self.nogil != other_type.nogil:
1425 self.original_sig = other_type.original_sig or other_type
1427 self.args[0] = other_type.args[0]
1431 def narrower_c_signature_than(self, other_type, as_cmethod = 0):
1432 return self.narrower_c_signature_than_resolved_type(other_type.resolve(), as_cmethod)
1434 def narrower_c_signature_than_resolved_type(self, other_type, as_cmethod):
1435 if other_type is error_type:
1437 if not other_type.is_cfunction:
1439 nargs = len(self.args)
1440 if nargs != len(other_type.args):
1442 for i in range(as_cmethod, nargs):
1443 if not self.args[i].type.subtype_of_resolved_type(other_type.args[i].type):
1446 self.args[i].needs_type_test = other_type.args[i].needs_type_test \
1447 or not self.args[i].type.same_as(other_type.args[i].type)
1448 if self.has_varargs != other_type.has_varargs:
1450 if self.optional_arg_count != other_type.optional_arg_count:
1452 if not self.return_type.subtype_of_resolved_type(other_type.return_type):
1456 def same_calling_convention_as(self, other):
1457 ## XXX Under discussion ...
1458 ## callspec_words = ("__stdcall", "__cdecl", "__fastcall")
1459 ## cs1 = self.calling_convention
1460 ## cs2 = other.calling_convention
1461 ## if (cs1 in callspec_words or
1462 ## cs2 in callspec_words):
1463 ## return cs1 == cs2
1466 sc1 = self.calling_convention == '__stdcall'
1467 sc2 = other.calling_convention == '__stdcall'
1470 def same_exception_signature_as(self, other_type):
1471 return self.same_exception_signature_as_resolved_type(
1472 other_type.resolve())
1474 def same_exception_signature_as_resolved_type(self, other_type):
1475 return self.exception_value == other_type.exception_value \
1476 and self.exception_check == other_type.exception_check
1478 def same_as_resolved_type(self, other_type, as_cmethod = 0):
1479 return self.same_c_signature_as_resolved_type(other_type, as_cmethod) \
1480 and self.same_exception_signature_as_resolved_type(other_type) \
1481 and self.nogil == other_type.nogil
1483 def pointer_assignable_from_resolved_type(self, other_type):
1484 return self.same_c_signature_as_resolved_type(other_type) \
1485 and self.same_exception_signature_as_resolved_type(other_type) \
1486 and not (self.nogil and not other_type.nogil)
1488 def declaration_code(self, entity_code,
1489 for_display = 0, dll_linkage = None, pyrex = 0,
1490 with_calling_convention = 1):
1492 for arg in self.args[:len(self.args)-self.optional_arg_count]:
1493 arg_decl_list.append(
1494 arg.type.declaration_code("", for_display, pyrex = pyrex))
1495 if self.is_overridable:
1496 arg_decl_list.append("int %s" % Naming.skip_dispatch_cname)
1497 if self.optional_arg_count:
1498 arg_decl_list.append(self.op_arg_struct.declaration_code(Naming.optional_args_cname))
1499 if self.has_varargs:
1500 arg_decl_list.append("...")
1501 arg_decl_code = ", ".join(arg_decl_list)
1502 if not arg_decl_code and not pyrex:
1503 arg_decl_code = "void"
1505 if (pyrex or for_display) and not self.return_type.is_pyobject:
1506 if self.exception_value and self.exception_check:
1507 trailer = " except? %s" % self.exception_value
1508 elif self.exception_value:
1509 trailer = " except %s" % self.exception_value
1510 elif self.exception_check == '+':
1511 trailer = " except +"
1513 " except *" # ignored
1516 if not with_calling_convention:
1519 cc = self.calling_convention_prefix()
1520 if (not entity_code and cc) or entity_code.startswith("*"):
1521 entity_code = "(%s%s)" % (cc, entity_code)
1523 return self.return_type.declaration_code(
1524 "%s%s(%s)%s" % (cc, entity_code, arg_decl_code, trailer),
1525 for_display, dll_linkage, pyrex)
1527 def function_header_code(self, func_name, arg_code):
1528 return "%s%s(%s)" % (self.calling_convention_prefix(),
1529 func_name, arg_code)
1531 def signature_string(self):
1532 s = self.declaration_code("")
1535 def signature_cast_string(self):
1536 s = self.declaration_code("(*)", with_calling_convention=False)
1539 def opt_arg_cname(self, arg_name):
1540 return self.op_arg_struct.base_type.scope.lookup(arg_name).cname
1543 class CFuncTypeArg(object):
1547 # pos source file position
1549 def __init__(self, name, type, pos, cname=None):
1551 if cname is not None:
1554 self.cname = Naming.var_prefix + name
1557 self.not_none = False
1558 self.needs_type_test = False # TODO: should these defaults be set in analyse_types()?
1561 return "%s:%s" % (self.name, repr(self.type))
1563 def declaration_code(self, for_display = 0):
1564 return self.type.declaration_code(self.cname, for_display)
1566 class StructUtilityCode(object):
1567 def __init__(self, type, forward_decl):
1569 self.header = "static PyObject* %s(%s)" % (type.to_py_function, type.declaration_code('s'))
1570 self.forward_decl = forward_decl
1572 def __eq__(self, other):
1573 return isinstance(other, StructUtilityCode) and self.header == other.header
1575 return hash(self.header)
1577 def put_code(self, output):
1578 code = output['utility_code_def']
1579 proto = output['utility_code_proto']
1581 code.putln("%s {" % self.header)
1582 code.putln("PyObject* res;")
1583 code.putln("PyObject* member;")
1584 code.putln("res = PyDict_New(); if (res == NULL) return NULL;")
1585 for member in self.type.scope.var_entries:
1586 nameconst_cname = code.get_py_string_const(member.name, identifier=True)
1587 code.putln("member = %s(s.%s); if (member == NULL) goto bad;" % (
1588 member.type.to_py_function, member.cname))
1589 code.putln("if (PyDict_SetItem(res, %s, member) < 0) goto bad;" % nameconst_cname)
1590 code.putln("Py_DECREF(member);")
1591 code.putln("return res;")
1593 code.putln("Py_XDECREF(member);")
1594 code.putln("Py_DECREF(res);")
1595 code.putln("return NULL;")
1598 # This is a bit of a hack, we need a forward declaration
1599 # due to the way things are ordered in the module...
1600 if self.forward_decl:
1601 proto.putln(self.type.declaration_code('') + ';')
1602 proto.putln(self.header + ";")
1605 class CStructOrUnionType(CType):
1608 # kind string "struct" or "union"
1609 # scope StructOrUnionScope, or None if incomplete
1610 # typedef_flag boolean
1615 is_struct_or_union = 1
1618 def __init__(self, name, kind, scope, typedef_flag, cname, packed=False):
1623 self.typedef_flag = typedef_flag
1624 self.is_struct = kind == 'struct'
1626 self.to_py_function = "%s_to_py_%s" % (Naming.convert_func_prefix, self.cname)
1627 self.exception_check = True
1628 self._convert_code = None
1629 self.packed = packed
1631 def create_to_py_utility_code(self, env):
1632 if env.outer_scope is None:
1635 if self._convert_code is False: return # tri-state-ish
1637 if self._convert_code is None:
1638 for member in self.scope.var_entries:
1639 if not member.type.to_py_function or not member.type.create_to_py_utility_code(env):
1640 self.to_py_function = None
1641 self._convert_code = False
1643 forward_decl = (self.entry.visibility != 'extern')
1644 self._convert_code = StructUtilityCode(self, forward_decl)
1646 env.use_utility_code(self._convert_code)
1650 return "<CStructOrUnionType %s %s%s>" % (self.name, self.cname,
1651 ("", " typedef")[self.typedef_flag])
1653 def declaration_code(self, entity_code,
1654 for_display = 0, dll_linkage = None, pyrex = 0):
1656 return self.base_declaration_code(self.name, entity_code)
1660 elif self.typedef_flag:
1663 base = "%s %s" % (self.kind, self.cname)
1664 return self.base_declaration_code(public_decl(base, dll_linkage), entity_code)
1666 def __eq__(self, other):
1668 return (isinstance(other, CStructOrUnionType) and
1669 self.name == other.name)
1670 except AttributeError:
1673 def __lt__(self, other):
1675 return self.name < other.name
1676 except AttributeError:
1677 # this is arbitrary, but it makes sure we always have
1678 # *some* kind of order
1682 return hash(self.cname) ^ hash(self.kind)
1684 def is_complete(self):
1685 return self.scope is not None
1687 def attributes_known(self):
1688 return self.is_complete()
1690 def can_be_complex(self):
1691 # Does the struct consist of exactly two identical floats?
1692 fields = self.scope.var_entries
1693 if len(fields) != 2: return False
1695 return (a.type.is_float and b.type.is_float and
1696 a.type.declaration_code("") ==
1697 b.type.declaration_code(""))
1699 def struct_nesting_depth(self):
1700 child_depths = [x.type.struct_nesting_depth()
1701 for x in self.scope.var_entries]
1702 return max(child_depths) + 1
1704 class CEnumType(CType):
1706 # cname string or None
1707 # typedef_flag boolean
1711 rank = -1 # Ranks below any integer type
1712 to_py_function = "PyInt_FromLong"
1713 from_py_function = "PyInt_AsLong"
1715 def __init__(self, name, cname, typedef_flag):
1719 self.typedef_flag = typedef_flag
1725 return "<CEnumType %s %s%s>" % (self.name, self.cname,
1726 ("", " typedef")[self.typedef_flag])
1728 def declaration_code(self, entity_code,
1729 for_display = 0, dll_linkage = None, pyrex = 0):
1731 return self.base_declaration_code(self.cname, entity_code)
1733 if self.typedef_flag:
1736 base = "enum %s" % self.cname
1737 return self.base_declaration_code(public_decl(base, dll_linkage), entity_code)
1740 class CStringType(object):
1741 # Mixin class for C string types.
1746 to_py_function = "__Pyx_PyBytes_FromString"
1747 from_py_function = "__Pyx_PyBytes_AsString"
1748 exception_value = "NULL"
1750 def literal_code(self, value):
1751 assert isinstance(value, str)
1752 return '"%s"' % StringEncoding.escape_byte_string(value)
1755 class CUTF8CharArrayType(CStringType, CArrayType):
1758 pymemberdef_typecode = "T_STRING_INPLACE"
1761 to_py_function = "PyUnicode_DecodeUTF8"
1762 exception_value = "NULL"
1764 def __init__(self, size):
1765 CArrayType.__init__(self, c_char_type, size)
1767 class CCharArrayType(CStringType, CArrayType):
1770 pymemberdef_typecode = "T_STRING_INPLACE"
1772 def __init__(self, size):
1773 CArrayType.__init__(self, c_char_type, size)
1776 class CCharPtrType(CStringType, CPtrType):
1779 pymemberdef_typecode = "T_STRING"
1782 CPtrType.__init__(self, c_char_type)
1785 class CUCharPtrType(CStringType, CPtrType):
1786 # C 'unsigned char *' type.
1788 pymemberdef_typecode = "T_STRING"
1790 to_py_function = "__Pyx_PyBytes_FromUString"
1791 from_py_function = "__Pyx_PyBytes_AsUString"
1794 CPtrType.__init__(self, c_uchar_type)
1797 class UnspecifiedType(PyrexType):
1798 # Used as a placeholder until the type can be determined.
1802 def declaration_code(self, entity_code,
1803 for_display = 0, dll_linkage = None, pyrex = 0):
1804 return "<unspecified>"
1806 def same_as_resolved_type(self, other_type):
1810 class ErrorType(PyrexType):
1811 # Used to prevent propagation of error messages.
1814 exception_value = "0"
1816 to_py_function = "dummy"
1817 from_py_function = "dummy"
1819 def create_to_py_utility_code(self, env):
1822 def create_from_py_utility_code(self, env):
1825 def declaration_code(self, entity_code,
1826 for_display = 0, dll_linkage = None, pyrex = 0):
1829 def same_as_resolved_type(self, other_type):
1832 def error_condition(self, result_code):
1836 rank_to_type_name = (
1849 py_object_type = PyObjectType()
1851 c_void_type = CVoidType()
1852 c_void_ptr_type = CPtrType(c_void_type)
1853 c_void_ptr_ptr_type = CPtrType(c_void_ptr_type)
1855 c_uchar_type = CIntType(0, 0, "T_UBYTE")
1856 c_ushort_type = CIntType(1, 0, "T_USHORT")
1857 c_uint_type = CUIntType(2, 0, "T_UINT")
1858 c_ulong_type = CULongType(3, 0, "T_ULONG")
1859 c_ulonglong_type = CULongLongType(6, 0, "T_ULONGLONG")
1861 c_char_type = CIntType(0, 1, "T_CHAR")
1862 c_short_type = CIntType(1, 1, "T_SHORT")
1863 c_int_type = CIntType(2, 1, "T_INT")
1864 c_long_type = CLongType(3, 1, "T_LONG")
1865 c_longlong_type = CLongLongType(6, 1, "T_LONGLONG")
1866 c_bint_type = CBIntType(2, 1, "T_INT")
1868 c_schar_type = CIntType(0, 2, "T_CHAR")
1869 c_sshort_type = CIntType(1, 2, "T_SHORT")
1870 c_sint_type = CIntType(2, 2, "T_INT")
1871 c_slong_type = CLongType(3, 2, "T_LONG")
1872 c_slonglong_type = CLongLongType(6, 2, "T_LONGLONG")
1874 c_py_ssize_t_type = CPySSizeTType(4, 2, "T_PYSSIZET")
1875 c_size_t_type = CSizeTType(5, 0, "T_SIZET")
1877 c_float_type = CFloatType(7, "T_FLOAT", math_h_modifier='f')
1878 c_double_type = CFloatType(8, "T_DOUBLE")
1879 c_longdouble_type = CFloatType(9, math_h_modifier='l')
1881 c_double_complex_type = CComplexType(c_double_type)
1883 c_null_ptr_type = CNullPtrType(c_void_type)
1884 c_char_array_type = CCharArrayType(None)
1885 c_char_ptr_type = CCharPtrType()
1886 c_uchar_ptr_type = CUCharPtrType()
1887 c_utf8_char_array_type = CUTF8CharArrayType(None)
1888 c_char_ptr_ptr_type = CPtrType(c_char_ptr_type)
1889 c_int_ptr_type = CPtrType(c_int_type)
1890 c_py_ssize_t_ptr_type = CPtrType(c_py_ssize_t_type)
1891 c_size_t_ptr_type = CPtrType(c_size_t_type)
1893 c_returncode_type = CIntType(2, 1, "T_INT", is_returncode = 1)
1895 c_anon_enum_type = CAnonEnumType(-1, 1)
1897 # the Py_buffer type is defined in Builtin.py
1898 c_py_buffer_type = CStructOrUnionType("Py_buffer", "struct", None, 1, "Py_buffer")
1899 c_py_buffer_ptr_type = CPtrType(c_py_buffer_type)
1901 error_type = ErrorType()
1902 unspecified_type = UnspecifiedType()
1904 sign_and_rank_to_type = {
1906 (0, 0): c_uchar_type,
1907 (0, 1): c_ushort_type,
1908 (0, 2): c_uint_type,
1909 (0, 3): c_ulong_type,
1910 (0, 6): c_ulonglong_type,
1912 (1, 0): c_char_type,
1913 (1, 1): c_short_type,
1915 (1, 3): c_long_type,
1916 (1, 6): c_longlong_type,
1918 (2, 0): c_schar_type,
1919 (2, 1): c_sshort_type,
1920 (2, 2): c_sint_type,
1921 (2, 3): c_slong_type,
1922 (2, 6): c_slonglong_type,
1924 (0, 4): c_py_ssize_t_type,
1925 (1, 4): c_py_ssize_t_type,
1926 (2, 4): c_py_ssize_t_type,
1927 (0, 5): c_size_t_type,
1928 (1, 5): c_size_t_type,
1929 (2, 5): c_size_t_type,
1931 (1, 7): c_float_type,
1932 (1, 8): c_double_type,
1933 (1, 9): c_longdouble_type,
1934 # In case we're mixing unsigned ints and floats...
1935 (0, 7): c_float_type,
1936 (0, 8): c_double_type,
1937 (0, 9): c_longdouble_type,
1940 modifiers_and_name_to_type = {
1941 #(signed, longness, name)
1942 (0, 0, "char"): c_uchar_type,
1943 (0, -1, "int"): c_ushort_type,
1944 (0, 0, "int"): c_uint_type,
1945 (0, 1, "int"): c_ulong_type,
1946 (0, 2, "int"): c_ulonglong_type,
1947 (1, 0, "void"): c_void_type,
1948 (1, 0, "char"): c_char_type,
1949 (1, -1, "int"): c_short_type,
1950 (1, 0, "int"): c_int_type,
1951 (1, 1, "int"): c_long_type,
1952 (1, 2, "int"): c_longlong_type,
1953 (1, 0, "float"): c_float_type,
1954 (1, 0, "double"): c_double_type,
1955 (1, 1, "double"): c_longdouble_type,
1956 (1, 0, "object"): py_object_type,
1957 (1, 0, "bint"): c_bint_type,
1958 (2, 0, "char"): c_schar_type,
1959 (2, -1, "int"): c_sshort_type,
1960 (2, 0, "int"): c_sint_type,
1961 (2, 1, "int"): c_slong_type,
1962 (2, 2, "int"): c_slonglong_type,
1964 (2, 0, "Py_ssize_t"): c_py_ssize_t_type,
1965 (0, 0, "size_t") : c_size_t_type,
1967 (1, 0, "long"): c_long_type,
1968 (1, 0, "short"): c_short_type,
1969 (1, 0, "longlong"): c_longlong_type,
1970 (1, 0, "bint"): c_bint_type,
1973 def widest_numeric_type(type1, type2):
1974 # Given two numeric types, return the narrowest type
1975 # encompassing both of them.
1978 if type1.is_complex:
1979 if type2.is_complex:
1980 return CComplexType(widest_numeric_type(type1.real_type, type2.real_type))
1982 return CComplexType(widest_numeric_type(type1.real_type, type2))
1983 elif type2.is_complex:
1984 return CComplexType(widest_numeric_type(type1, type2.real_type))
1985 if type1.is_enum and type2.is_enum:
1987 elif type1 is type2:
1989 elif (type1.signed and type2.signed) or (not type1.signed and not type2.signed):
1990 if type2.rank > type1.rank:
1995 return sign_and_rank_to_type[min(type1.signed, type2.signed), max(type1.rank, type2.rank)]
1998 def spanning_type(type1, type2):
1999 # Return a type assignable from both type1 and type2.
2000 if type1 is py_object_type or type2 is py_object_type:
2001 return py_object_type
2002 elif type1 == type2:
2004 elif type1.is_numeric and type2.is_numeric:
2005 return widest_numeric_type(type1, type2)
2006 elif type1.is_pyobject ^ type2.is_pyobject:
2007 return py_object_type
2008 elif type1.assignable_from(type2):
2010 elif type2.assignable_from(type1):
2013 return py_object_type
2015 def simple_c_type(signed, longness, name):
2016 # Find type descriptor for simple type given name and modifiers.
2017 # Returns None if arguments don't make sense.
2018 return modifiers_and_name_to_type.get((signed, longness, name))
2020 def parse_basic_type(name):
2022 if name.startswith('p_'):
2023 base = parse_basic_type(name[2:])
2024 elif name.startswith('p'):
2025 base = parse_basic_type(name[1:])
2026 elif name.endswith('*'):
2027 base = parse_basic_type(name[:-1])
2029 return CPtrType(base)
2030 elif name.startswith('u'):
2031 return simple_c_type(0, 0, name[1:])
2033 return simple_c_type(1, 0, name)
2035 def c_array_type(base_type, size):
2036 # Construct a C array type.
2037 if base_type is c_char_type:
2038 return CCharArrayType(size)
2039 elif base_type is error_type:
2042 return CArrayType(base_type, size)
2044 def c_ptr_type(base_type):
2045 # Construct a C pointer type.
2046 if base_type is c_char_type:
2047 return c_char_ptr_type
2048 elif base_type is c_uchar_type:
2049 return c_uchar_ptr_type
2050 elif base_type is error_type:
2053 return CPtrType(base_type)
2055 def same_type(type1, type2):
2056 return type1.same_as(type2)
2058 def assignable_from(type1, type2):
2059 return type1.assignable_from(type2)
2061 def typecast(to_type, from_type, expr_code):
2062 # Return expr_code cast to a C type which can be
2063 # assigned to to_type, assuming its existing C type
2065 if to_type is from_type or \
2066 (not to_type.is_pyobject and assignable_from(to_type, from_type)):
2069 #print "typecast: to", to_type, "from", from_type ###
2070 return to_type.cast_code(expr_code)
2073 type_conversion_predeclarations = """
2074 /* Type Conversion Predeclarations */
2076 #if PY_MAJOR_VERSION < 3
2077 #define __Pyx_PyBytes_FromString PyString_FromString
2078 #define __Pyx_PyBytes_FromStringAndSize PyString_FromStringAndSize
2079 #define __Pyx_PyBytes_AsString PyString_AsString
2081 #define __Pyx_PyBytes_FromString PyBytes_FromString
2082 #define __Pyx_PyBytes_FromStringAndSize PyBytes_FromStringAndSize
2083 #define __Pyx_PyBytes_AsString PyBytes_AsString
2086 #define __Pyx_PyBytes_FromUString(s) __Pyx_PyBytes_FromString((char*)s)
2087 #define __Pyx_PyBytes_AsUString(s) ((unsigned char*) __Pyx_PyBytes_AsString(s))
2089 #define __Pyx_PyBool_FromLong(b) ((b) ? (Py_INCREF(Py_True), Py_True) : (Py_INCREF(Py_False), Py_False))
2090 static INLINE int __Pyx_PyObject_IsTrue(PyObject*);
2091 static INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x);
2093 #if !defined(T_PYSSIZET)
2094 #if PY_VERSION_HEX < 0x02050000
2095 #define T_PYSSIZET T_INT
2096 #elif !defined(T_LONGLONG)
2097 #define T_PYSSIZET \\
2098 ((sizeof(Py_ssize_t) == sizeof(int)) ? T_INT : \\
2099 ((sizeof(Py_ssize_t) == sizeof(long)) ? T_LONG : -1))
2101 #define T_PYSSIZET \\
2102 ((sizeof(Py_ssize_t) == sizeof(int)) ? T_INT : \\
2103 ((sizeof(Py_ssize_t) == sizeof(long)) ? T_LONG : \\
2104 ((sizeof(Py_ssize_t) == sizeof(PY_LONG_LONG)) ? T_LONGLONG : -1)))
2109 #if !defined(T_ULONGLONG)
2110 #define __Pyx_T_UNSIGNED_INT(x) \\
2111 ((sizeof(x) == sizeof(unsigned char)) ? T_UBYTE : \\
2112 ((sizeof(x) == sizeof(unsigned short)) ? T_USHORT : \\
2113 ((sizeof(x) == sizeof(unsigned int)) ? T_UINT : \\
2114 ((sizeof(x) == sizeof(unsigned long)) ? T_ULONG : -1))))
2116 #define __Pyx_T_UNSIGNED_INT(x) \\
2117 ((sizeof(x) == sizeof(unsigned char)) ? T_UBYTE : \\
2118 ((sizeof(x) == sizeof(unsigned short)) ? T_USHORT : \\
2119 ((sizeof(x) == sizeof(unsigned int)) ? T_UINT : \\
2120 ((sizeof(x) == sizeof(unsigned long)) ? T_ULONG : \\
2121 ((sizeof(x) == sizeof(unsigned PY_LONG_LONG)) ? T_ULONGLONG : -1)))))
2123 #if !defined(T_LONGLONG)
2124 #define __Pyx_T_SIGNED_INT(x) \\
2125 ((sizeof(x) == sizeof(char)) ? T_BYTE : \\
2126 ((sizeof(x) == sizeof(short)) ? T_SHORT : \\
2127 ((sizeof(x) == sizeof(int)) ? T_INT : \\
2128 ((sizeof(x) == sizeof(long)) ? T_LONG : -1))))
2130 #define __Pyx_T_SIGNED_INT(x) \\
2131 ((sizeof(x) == sizeof(char)) ? T_BYTE : \\
2132 ((sizeof(x) == sizeof(short)) ? T_SHORT : \\
2133 ((sizeof(x) == sizeof(int)) ? T_INT : \\
2134 ((sizeof(x) == sizeof(long)) ? T_LONG : \\
2135 ((sizeof(x) == sizeof(PY_LONG_LONG)) ? T_LONGLONG : -1)))))
2138 #define __Pyx_T_FLOATING(x) \\
2139 ((sizeof(x) == sizeof(float)) ? T_FLOAT : \\
2140 ((sizeof(x) == sizeof(double)) ? T_DOUBLE : -1))
2142 #if !defined(T_SIZET)
2143 #if !defined(T_ULONGLONG)
2145 ((sizeof(size_t) == sizeof(unsigned int)) ? T_UINT : \\
2146 ((sizeof(size_t) == sizeof(unsigned long)) ? T_ULONG : -1))
2149 ((sizeof(size_t) == sizeof(unsigned int)) ? T_UINT : \\
2150 ((sizeof(size_t) == sizeof(unsigned long)) ? T_ULONG : \\
2151 ((sizeof(size_t) == sizeof(unsigned PY_LONG_LONG)) ? T_ULONGLONG : -1)))
2155 static INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*);
2156 static INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t);
2157 static INLINE size_t __Pyx_PyInt_AsSize_t(PyObject*);
2159 #define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x))
2161 """ + type_conversion_predeclarations
2163 type_conversion_functions = """
2164 /* Type Conversion Functions */
2166 static INLINE int __Pyx_PyObject_IsTrue(PyObject* x) {
2167 if (x == Py_True) return 1;
2168 else if ((x == Py_False) | (x == Py_None)) return 0;
2169 else return PyObject_IsTrue(x);
2172 static INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x) {
2174 const char *name = NULL;
2175 PyObject *res = NULL;
2176 #if PY_VERSION_HEX < 0x03000000
2177 if (PyInt_Check(x) || PyLong_Check(x))
2179 if (PyLong_Check(x))
2181 return Py_INCREF(x), x;
2182 m = Py_TYPE(x)->tp_as_number;
2183 #if PY_VERSION_HEX < 0x03000000
2184 if (m && m->nb_int) {
2186 res = PyNumber_Int(x);
2188 else if (m && m->nb_long) {
2190 res = PyNumber_Long(x);
2193 if (m && m->nb_int) {
2195 res = PyNumber_Long(x);
2199 #if PY_VERSION_HEX < 0x03000000
2200 if (!PyInt_Check(res) && !PyLong_Check(res)) {
2202 if (!PyLong_Check(res)) {
2204 PyErr_Format(PyExc_TypeError,
2205 "__%s__ returned non-%s (type %.200s)",
2206 name, name, Py_TYPE(res)->tp_name);
2211 else if (!PyErr_Occurred()) {
2212 PyErr_SetString(PyExc_TypeError,
2213 "an integer is required");
2218 static INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) {
2220 PyObject* x = PyNumber_Index(b);
2222 ival = PyInt_AsSsize_t(x);
2227 static INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) {
2228 #if PY_VERSION_HEX < 0x02050000
2229 if (ival <= LONG_MAX)
2230 return PyInt_FromLong((long)ival);
2232 unsigned char *bytes = (unsigned char *) &ival;
2233 int one = 1; int little = (int)*(unsigned char*)&one;
2234 return _PyLong_FromByteArray(bytes, sizeof(size_t), little, 0);
2237 return PyInt_FromSize_t(ival);
2241 static INLINE size_t __Pyx_PyInt_AsSize_t(PyObject* x) {
2242 unsigned PY_LONG_LONG val = __Pyx_PyInt_AsUnsignedLongLong(x);
2243 if (unlikely(val == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())) {
2245 } else if (unlikely(val != (unsigned PY_LONG_LONG)(size_t)val)) {
2246 PyErr_SetString(PyExc_OverflowError,
2247 "value too large to convert to size_t");
2253 """ + type_conversion_functions