Cython has moved to github.
cython-devel
view Cython/Compiler/PyrexTypes.py @ 1235:5de00fce9b73
Buffers: NumPy record array support, format string parsing improvements
| author | Dag Sverre Seljebotn <dagss@student.matnat.uio.no> |
|---|---|
| date | Sat Oct 11 18:48:15 2008 +0200 (3 years ago) |
| parents | ea91c6d40b4d |
| children | b7860eda3d84 |
line source
1 #
2 # Pyrex - Types
3 #
5 import StringEncoding
6 import Naming
7 import copy
9 class BaseType:
10 #
11 # Base class for all Pyrex types including pseudo-types.
13 def cast_code(self, expr_code):
14 return "((%s)%s)" % (self.declaration_code(""), expr_code)
16 def base_declaration_code(self, base_code, entity_code):
17 if entity_code:
18 return "%s %s" % (base_code, entity_code)
19 else:
20 return base_code
23 class PyrexType(BaseType):
24 #
25 # Base class for all Pyrex types.
26 #
27 # is_pyobject boolean Is a Python object type
28 # is_extension_type boolean Is a Python extension type
29 # is_numeric boolean Is a C numeric type
30 # is_int boolean Is a C integer type
31 # is_longlong boolean Is a long long or unsigned long long.
32 # is_float boolean Is a C floating point type
33 # is_void boolean Is the C void type
34 # is_array boolean Is a C array type
35 # is_ptr boolean Is a C pointer type
36 # is_null_ptr boolean Is the type of NULL
37 # is_cfunction boolean Is a C function type
38 # is_struct_or_union boolean Is a C struct or union type
39 # is_struct boolean Is a C struct type
40 # is_enum boolean Is a C enum type
41 # is_typedef boolean Is a typedef type
42 # is_string boolean Is a C char * type
43 # is_unicode boolean Is a UTF-8 encoded C char * type
44 # is_returncode boolean Is used only to signal exceptions
45 # is_error boolean Is the dummy error type
46 # is_buffer boolean Is buffer access type
47 # has_attributes boolean Has C dot-selectable attributes
48 # default_value string Initial value
49 # parsetuple_format string Format char for PyArg_ParseTuple
50 # pymemberdef_typecode string Type code for PyMemberDef struct
51 # typestring string String char defining the type (see Python struct module)
52 #
53 # declaration_code(entity_code,
54 # for_display = 0, dll_linkage = None, pyrex = 0)
55 # Returns a code fragment for the declaration of an entity
56 # of this type, given a code fragment for the entity.
57 # * If for_display, this is for reading by a human in an error
58 # message; otherwise it must be valid C code.
59 # * If dll_linkage is not None, it must be 'DL_EXPORT' or
60 # 'DL_IMPORT', and will be added to the base type part of
61 # the declaration.
62 # * If pyrex = 1, this is for use in a 'cdef extern'
63 # statement of a Pyrex include file.
64 #
65 # assignable_from(src_type)
66 # Tests whether a variable of this type can be
67 # assigned a value of type src_type.
68 #
69 # same_as(other_type)
70 # Tests whether this type represents the same type
71 # as other_type.
72 #
73 # as_argument_type():
74 # Coerces array type into pointer type for use as
75 # a formal argument type.
76 #
78 is_pyobject = 0
79 is_extension_type = 0
80 is_builtin_type = 0
81 is_numeric = 0
82 is_int = 0
83 is_longlong = 0
84 is_float = 0
85 is_void = 0
86 is_array = 0
87 is_ptr = 0
88 is_null_ptr = 0
89 is_cfunction = 0
90 is_struct_or_union = 0
91 is_struct = 0
92 is_enum = 0
93 is_typedef = 0
94 is_string = 0
95 is_unicode = 0
96 is_returncode = 0
97 is_error = 0
98 is_buffer = 0
99 has_attributes = 0
100 default_value = ""
101 parsetuple_format = ""
102 pymemberdef_typecode = None
103 typestring = None
105 def resolve(self):
106 # If a typedef, returns the base type.
107 return self
109 def literal_code(self, value):
110 # Returns a C code fragment representing a literal
111 # value of this type.
112 return str(value)
114 def __str__(self):
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):
136 return 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.
141 return 1
143 def is_simple_buffer_dtype(self):
144 return (self.is_int or self.is_float or self.is_pyobject or
145 self.is_extension_type or self.is_ptr)
147 class CTypedefType(BaseType):
148 #
149 # Pseudo-type defined with a ctypedef statement in a
150 # 'cdef extern from' block. Delegates most attribute
151 # lookups to the base type. ANYTHING NOT DEFINED
152 # HERE IS DELEGATED!
153 #
154 # qualified_name string
155 # typedef_cname string
156 # typedef_base_type PyrexType
158 is_typedef = 1
159 typestring = None # Because typedefs are not known exactly
161 def __init__(self, cname, base_type):
162 self.typedef_cname = cname
163 self.typedef_base_type = base_type
165 def resolve(self):
166 return self.typedef_base_type.resolve()
168 def declaration_code(self, entity_code,
169 for_display = 0, dll_linkage = None, pyrex = 0):
170 name = self.declaration_name(for_display, pyrex)
171 return self.base_declaration_code(name, entity_code)
173 def declaration_name(self, for_display = 0, pyrex = 0):
174 if pyrex or for_display:
175 return self.qualified_name
176 else:
177 return self.typedef_cname
179 def as_argument_type(self):
180 return self
182 def cast_code(self, expr_code):
183 # If self is really an array (rather than pointer), we can't cast.
184 # For example, the gmp mpz_t.
185 if self.typedef_base_type.is_ptr:
186 return self.typedef_base_type.cast_code(expr_code)
187 else:
188 return BaseType.cast_code(self, expr_code)
190 def __repr__(self):
191 return "<CTypedefType %s>" % self.typedef_cname
193 def __str__(self):
194 return self.declaration_name(for_display = 1)
196 def __getattr__(self, name):
197 return getattr(self.typedef_base_type, name)
199 class BufferType(BaseType):
200 #
201 # Delegates most attribute
202 # lookups to the base type. ANYTHING NOT DEFINED
203 # HERE IS DELEGATED!
205 # dtype PyrexType
206 # ndim int
207 # mode str
208 # negative_indices bool
209 # cast bool
210 # is_buffer bool
211 # writable bool
213 is_buffer = 1
214 writable = True
215 def __init__(self, base, dtype, ndim, mode, negative_indices, cast):
216 self.base = base
217 self.dtype = dtype
218 self.ndim = ndim
219 self.buffer_ptr_type = CPtrType(dtype)
220 self.mode = mode
221 self.negative_indices = negative_indices
222 self.cast = cast
224 def as_argument_type(self):
225 return self
227 def __getattr__(self, name):
228 return getattr(self.base, name)
230 def __repr__(self):
231 return "<BufferType %r>" % self.base
234 class PyObjectType(PyrexType):
235 #
236 # Base class for all Python object types (reference-counted).
237 #
238 # buffer_defaults dict or None Default options for bu
240 is_pyobject = 1
241 default_value = "0"
242 parsetuple_format = "O"
243 pymemberdef_typecode = "T_OBJECT"
244 buffer_defaults = None
245 typestring = "O"
247 def __str__(self):
248 return "Python object"
250 def __repr__(self):
251 return "<PyObjectType>"
253 def assignable_from(self, src_type):
254 return 1 # Conversion will be attempted
256 def declaration_code(self, entity_code,
257 for_display = 0, dll_linkage = None, pyrex = 0):
258 if pyrex or for_display:
259 return self.base_declaration_code("object", entity_code)
260 else:
261 return "%s *%s" % (public_decl("PyObject", dll_linkage), entity_code)
264 class BuiltinObjectType(PyObjectType):
266 is_builtin_type = 1
267 has_attributes = 1
268 base_type = None
269 module_name = '__builtin__'
271 def __init__(self, name, cname):
272 self.name = name
273 self.cname = cname
274 self.typeptr_cname = "&" + cname
276 def set_scope(self, scope):
277 self.scope = scope
278 if scope:
279 scope.parent_type = self
281 def __str__(self):
282 return "%s object" % self.name
284 def __repr__(self):
285 return "<%s>"% self.cname
287 def assignable_from(self, src_type):
288 if isinstance(src_type, BuiltinObjectType):
289 return src_type.name == self.name
290 else:
291 return not src_type.is_extension_type
293 def typeobj_is_available(self):
294 return True
296 def attributes_known(self):
297 return True
299 def subtype_of(self, type):
300 return type.is_pyobject and self.assignable_from(type)
302 def type_test_code(self, arg):
303 type = self.name.capitalize()
304 if type == 'Set':
305 type = 'AnySet'
306 elif type == 'Frozenset':
307 type = 'FrozenSet'
308 return 'likely(Py%s_CheckExact(%s)) || (%s) == Py_None || (PyErr_Format(PyExc_TypeError, "Expected %s, got %%s", Py_TYPE(%s)->tp_name), 0)' % (type, arg, arg, self.name, arg)
310 def declaration_code(self, entity_code,
311 for_display = 0, dll_linkage = None, pyrex = 0):
312 if pyrex or for_display:
313 return self.base_declaration_code(self.name, entity_code)
314 else:
315 return "%s *%s" % (public_decl("PyObject", dll_linkage), entity_code)
318 class PyExtensionType(PyObjectType):
319 #
320 # A Python extension type.
321 #
322 # name string
323 # scope CClassScope Attribute namespace
324 # visibility string
325 # typedef_flag boolean
326 # base_type PyExtensionType or None
327 # module_name string or None Qualified name of defining module
328 # objstruct_cname string Name of PyObject struct
329 # typeobj_cname string or None C code fragment referring to type object
330 # typeptr_cname string or None Name of pointer to external type object
331 # vtabslot_cname string Name of C method table member
332 # vtabstruct_cname string Name of C method table struct
333 # vtabptr_cname string Name of pointer to C method table
334 # vtable_cname string Name of C method table definition
336 is_extension_type = 1
337 has_attributes = 1
339 def __init__(self, name, typedef_flag, base_type):
340 self.name = name
341 self.scope = None
342 self.typedef_flag = typedef_flag
343 self.base_type = base_type
344 self.module_name = None
345 self.objstruct_cname = None
346 self.typeobj_cname = None
347 self.typeptr_cname = None
348 self.vtabslot_cname = None
349 self.vtabstruct_cname = None
350 self.vtabptr_cname = None
351 self.vtable_cname = None
353 def set_scope(self, scope):
354 self.scope = scope
355 if scope:
356 scope.parent_type = self
358 def subtype_of_resolved_type(self, other_type):
359 if other_type.is_extension_type:
360 return self is other_type or (
361 self.base_type and self.base_type.subtype_of(other_type))
362 else:
363 return other_type is py_object_type
365 def typeobj_is_available(self):
366 # Do we have a pointer to the type object?
367 return self.typeptr_cname
369 def typeobj_is_imported(self):
370 # If we don't know the C name of the type object but we do
371 # know which module it's defined in, it will be imported.
372 return self.typeobj_cname is None and self.module_name is not None
374 def declaration_code(self, entity_code,
375 for_display = 0, dll_linkage = None, pyrex = 0, deref = 0):
376 if pyrex or for_display:
377 return self.base_declaration_code(self.name, entity_code)
378 else:
379 if self.typedef_flag:
380 base_format = "%s"
381 else:
382 base_format = "struct %s"
383 base = public_decl(base_format % self.objstruct_cname, dll_linkage)
384 if deref:
385 return "%s %s" % (base, entity_code)
386 else:
387 return "%s *%s" % (base, entity_code)
389 def type_test_code(self, py_arg):
390 return "__Pyx_TypeTest(%s, %s)" % (py_arg, self.typeptr_cname)
392 def attributes_known(self):
393 return self.scope is not None
395 def __str__(self):
396 return self.name
398 def __repr__(self):
399 return "<PyExtensionType %s%s>" % (self.scope.class_name,
400 ("", " typedef")[self.typedef_flag])
403 class CType(PyrexType):
404 #
405 # Base class for all C types (non-reference-counted).
406 #
407 # to_py_function string C function for converting to Python object
408 # from_py_function string C function for constructing from Python object
409 #
411 to_py_function = None
412 from_py_function = None
413 exception_value = None
414 exception_check = 1
416 def create_convert_utility_code(self, env):
417 return True
419 def error_condition(self, result_code):
420 conds = []
421 if self.is_string:
422 conds.append("(!%s)" % result_code)
423 elif self.exception_value is not None:
424 conds.append("(%s == (%s)%s)" % (result_code, self.sign_and_name(), self.exception_value))
425 if self.exception_check:
426 conds.append("PyErr_Occurred()")
427 if len(conds) > 0:
428 return " && ".join(conds)
429 else:
430 return 0
433 class CVoidType(CType):
434 is_void = 1
436 def __repr__(self):
437 return "<CVoidType>"
439 def declaration_code(self, entity_code,
440 for_display = 0, dll_linkage = None, pyrex = 0):
441 base = public_decl("void", dll_linkage)
442 return self.base_declaration_code(base, entity_code)
444 def is_complete(self):
445 return 0
448 class CNumericType(CType):
449 #
450 # Base class for all C numeric types.
451 #
452 # rank integer Relative size
453 # signed integer 0 = unsigned, 1 = unspecified, 2 = explicitly signed
454 #
456 is_numeric = 1
457 default_value = "0"
459 parsetuple_formats = ( # rank -> format
460 "BHIkK????", # unsigned
461 "bhilL?fd?", # assumed signed
462 "bhilL?fd?", # explicitly signed
463 )
465 sign_words = ("unsigned ", "", "signed ")
467 def __init__(self, rank, signed = 1, pymemberdef_typecode = None, typestring = None):
468 self.rank = rank
469 self.signed = signed
470 self.typestring = typestring
471 ptf = self.parsetuple_formats[signed][rank]
472 if ptf == '?':
473 ptf = None
474 self.parsetuple_format = ptf
475 self.pymemberdef_typecode = pymemberdef_typecode
477 def sign_and_name(self):
478 s = self.sign_words[self.signed]
479 n = rank_to_type_name[self.rank]
480 return s + n
482 def __repr__(self):
483 return "<CNumericType %s>" % self.sign_and_name()
485 def declaration_code(self, entity_code,
486 for_display = 0, dll_linkage = None, pyrex = 0):
487 base = public_decl(self.sign_and_name(), dll_linkage)
488 if for_display and self.is_longlong:
489 base = base.replace('PY_LONG_LONG', 'long long')
490 return self.base_declaration_code(base, entity_code)
493 int_conversion_list = {}
494 type_conversion_functions = ""
495 type_conversion_predeclarations = ""
497 class CIntType(CNumericType):
499 is_int = 1
500 typedef_flag = 0
501 to_py_function = "PyInt_FromLong"
502 from_py_function = "__pyx_PyInt_AsLong"
503 exception_value = -1
505 def __init__(self, rank, signed, pymemberdef_typecode = None, is_returncode = 0,
506 typestring=None):
507 CNumericType.__init__(self, rank, signed, pymemberdef_typecode, typestring=typestring)
508 self.is_returncode = is_returncode
509 if self.from_py_function == '__pyx_PyInt_AsLong':
510 self.from_py_function = self.get_type_conversion()
512 def get_type_conversion(self):
513 # error on overflow
514 c_type = self.sign_and_name()
515 c_name = c_type.replace(' ', '_');
516 func_name = "__pyx_PyInt_%s" % c_name;
517 if not int_conversion_list.has_key(func_name):
518 # no env to add utility code to
519 global type_conversion_predeclarations, type_conversion_functions
520 if self.signed:
521 neg_test = ""
522 else:
523 neg_test = " || (long_val < 0)"
524 type_conversion_predeclarations += """
525 static INLINE %(c_type)s %(func_name)s(PyObject* x);""" % {'c_type': c_type, 'c_name': c_name, 'func_name': func_name }
526 type_conversion_functions += """
527 static INLINE %(c_type)s %(func_name)s(PyObject* x) {
528 if (sizeof(%(c_type)s) < sizeof(long)) {
529 long long_val = __pyx_PyInt_AsLong(x);
530 %(c_type)s val = (%(c_type)s)long_val;
531 if (unlikely((val != long_val) %(neg_test)s)) {
532 PyErr_SetString(PyExc_OverflowError, "value too large to convert to %(c_type)s");
533 return (%(c_type)s)-1;
534 }
535 return val;
536 }
537 else {
538 return __pyx_PyInt_AsLong(x);
539 }
540 }
541 """ % {'c_type': c_type, 'c_name': c_name, 'func_name': func_name, 'neg_test': neg_test }
542 int_conversion_list[func_name] = True
543 return func_name
545 def assignable_from_resolved_type(self, src_type):
546 return src_type.is_int or src_type.is_enum or src_type is error_type
549 class CBIntType(CIntType):
551 to_py_function = "__Pyx_PyBool_FromLong"
552 from_py_function = "__Pyx_PyObject_IsTrue"
553 exception_check = 0
556 class CAnonEnumType(CIntType):
558 is_enum = 1
561 class CUIntType(CIntType):
563 to_py_function = "PyLong_FromUnsignedLong"
564 from_py_function = "PyInt_AsUnsignedLongMask"
565 exception_value = -1
568 class CULongType(CUIntType):
570 to_py_function = "PyLong_FromUnsignedLong"
571 from_py_function = "PyInt_AsUnsignedLongMask"
574 class CLongLongType(CIntType):
576 is_longlong = 1
577 to_py_function = "PyLong_FromLongLong"
578 from_py_function = "__pyx_PyInt_AsLongLong"
581 class CULongLongType(CUIntType):
583 is_longlong = 1
584 to_py_function = "PyLong_FromUnsignedLongLong"
585 from_py_function = "__pyx_PyInt_AsUnsignedLongLong"
588 class CPySSizeTType(CIntType):
590 to_py_function = "PyInt_FromSsize_t"
591 from_py_function = "__pyx_PyIndex_AsSsize_t"
594 class CFloatType(CNumericType):
596 is_float = 1
597 to_py_function = "PyFloat_FromDouble"
598 from_py_function = "__pyx_PyFloat_AsDouble"
600 def __init__(self, rank, pymemberdef_typecode = None, typestring=None):
601 CNumericType.__init__(self, rank, 1, pymemberdef_typecode, typestring = typestring)
603 def assignable_from_resolved_type(self, src_type):
604 return src_type.is_numeric or src_type is error_type
607 class CArrayType(CType):
608 # base_type CType Element type
609 # size integer or None Number of elements
611 is_array = 1
613 def __init__(self, base_type, size):
614 self.base_type = base_type
615 self.size = size
616 if base_type is c_char_type:
617 self.is_string = 1
619 def __repr__(self):
620 return "<CArrayType %s %s>" % (self.size, repr(self.base_type))
622 def same_as_resolved_type(self, other_type):
623 return ((other_type.is_array and
624 self.base_type.same_as(other_type.base_type))
625 or other_type is error_type)
627 def assignable_from_resolved_type(self, src_type):
628 # Can't assign to a variable of an array type
629 return 0
631 def element_ptr_type(self):
632 return c_ptr_type(self.base_type)
634 def declaration_code(self, entity_code,
635 for_display = 0, dll_linkage = None, pyrex = 0):
636 if self.size is not None:
637 dimension_code = self.size
638 else:
639 dimension_code = ""
640 if entity_code.startswith("*"):
641 entity_code = "(%s)" % entity_code
642 return self.base_type.declaration_code(
643 "%s[%s]" % (entity_code, dimension_code),
644 for_display, dll_linkage, pyrex)
646 def as_argument_type(self):
647 return c_ptr_type(self.base_type)
649 def is_complete(self):
650 return self.size is not None
653 class CPtrType(CType):
654 # base_type CType Referenced type
656 is_ptr = 1
657 default_value = "0"
659 def __init__(self, base_type):
660 self.base_type = base_type
662 def __repr__(self):
663 return "<CPtrType %s>" % repr(self.base_type)
665 def same_as_resolved_type(self, other_type):
666 return ((other_type.is_ptr and
667 self.base_type.same_as(other_type.base_type))
668 or other_type is error_type)
670 def declaration_code(self, entity_code,
671 for_display = 0, dll_linkage = None, pyrex = 0):
672 #print "CPtrType.declaration_code: pointer to", self.base_type ###
673 return self.base_type.declaration_code(
674 "*%s" % entity_code,
675 for_display, dll_linkage, pyrex)
677 def assignable_from_resolved_type(self, other_type):
678 if other_type is error_type:
679 return 1
680 if other_type.is_null_ptr:
681 return 1
682 if self.base_type.is_cfunction:
683 if other_type.is_ptr:
684 other_type = other_type.base_type.resolve()
685 if other_type.is_cfunction:
686 return self.base_type.pointer_assignable_from_resolved_type(other_type)
687 else:
688 return 0
689 if other_type.is_array or other_type.is_ptr:
690 return self.base_type.is_void or self.base_type.same_as(other_type.base_type)
691 return 0
694 class CNullPtrType(CPtrType):
696 is_null_ptr = 1
699 class CFuncType(CType):
700 # return_type CType
701 # args [CFuncTypeArg]
702 # has_varargs boolean
703 # exception_value string
704 # exception_check boolean True if PyErr_Occurred check needed
705 # calling_convention string Function calling convention
706 # nogil boolean Can be called without gil
707 # with_gil boolean Acquire gil around function body
709 is_cfunction = 1
710 original_sig = None
712 def __init__(self, return_type, args, has_varargs = 0,
713 exception_value = None, exception_check = 0, calling_convention = "",
714 nogil = 0, with_gil = 0, is_overridable = 0, optional_arg_count = 0):
715 self.return_type = return_type
716 self.args = args
717 self.has_varargs = has_varargs
718 self.optional_arg_count = optional_arg_count
719 self.exception_value = exception_value
720 self.exception_check = exception_check
721 self.calling_convention = calling_convention
722 self.nogil = nogil
723 self.with_gil = with_gil
724 self.is_overridable = is_overridable
726 def __repr__(self):
727 arg_reprs = map(repr, self.args)
728 if self.has_varargs:
729 arg_reprs.append("...")
730 return "<CFuncType %s %s[%s]>" % (
731 repr(self.return_type),
732 self.calling_convention_prefix(),
733 ",".join(arg_reprs))
735 def calling_convention_prefix(self):
736 cc = self.calling_convention
737 if cc:
738 return cc + " "
739 else:
740 return ""
742 def same_c_signature_as(self, other_type, as_cmethod = 0):
743 return self.same_c_signature_as_resolved_type(
744 other_type.resolve(), as_cmethod)
746 def same_c_signature_as_resolved_type(self, other_type, as_cmethod = 0):
747 #print "CFuncType.same_c_signature_as_resolved_type:", \
748 # self, other_type, "as_cmethod =", as_cmethod ###
749 if other_type is error_type:
750 return 1
751 if not other_type.is_cfunction:
752 return 0
753 if self.is_overridable != other_type.is_overridable:
754 return 0
755 nargs = len(self.args)
756 if nargs != len(other_type.args):
757 return 0
758 # When comparing C method signatures, the first argument
759 # is exempt from compatibility checking (the proper check
760 # is performed elsewhere).
761 for i in range(as_cmethod, nargs):
762 if not self.args[i].type.same_as(
763 other_type.args[i].type):
764 return 0
765 if self.has_varargs != other_type.has_varargs:
766 return 0
767 if self.optional_arg_count != other_type.optional_arg_count:
768 return 0
769 if not self.return_type.same_as(other_type.return_type):
770 return 0
771 if not self.same_calling_convention_as(other_type):
772 return 0
773 return 1
775 def compatible_signature_with(self, other_type, as_cmethod = 0):
776 return self.compatible_signature_with_resolved_type(other_type.resolve(), as_cmethod)
778 def compatible_signature_with_resolved_type(self, other_type, as_cmethod):
779 #print "CFuncType.same_c_signature_as_resolved_type:", \
780 # self, other_type, "as_cmethod =", as_cmethod ###
781 if other_type is error_type:
782 return 1
783 if not other_type.is_cfunction:
784 return 0
785 if not self.is_overridable and other_type.is_overridable:
786 return 0
787 nargs = len(self.args)
788 if nargs - self.optional_arg_count != len(other_type.args) - other_type.optional_arg_count:
789 return 0
790 if self.optional_arg_count < other_type.optional_arg_count:
791 return 0
792 # When comparing C method signatures, the first argument
793 # is exempt from compatibility checking (the proper check
794 # is performed elsewhere).
795 for i in range(as_cmethod, len(other_type.args)):
796 if not self.args[i].type.same_as(
797 other_type.args[i].type):
798 return 0
799 if self.has_varargs != other_type.has_varargs:
800 return 0
801 if not self.return_type.subtype_of_resolved_type(other_type.return_type):
802 return 0
803 if not self.same_calling_convention_as(other_type):
804 return 0
805 if self.nogil != other_type.nogil:
806 return 0
807 self.original_sig = other_type.original_sig or other_type
808 if as_cmethod:
809 self.args[0] = other_type.args[0]
810 return 1
813 def narrower_c_signature_than(self, other_type, as_cmethod = 0):
814 return self.narrower_c_signature_than_resolved_type(other_type.resolve(), as_cmethod)
816 def narrower_c_signature_than_resolved_type(self, other_type, as_cmethod):
817 if other_type is error_type:
818 return 1
819 if not other_type.is_cfunction:
820 return 0
821 nargs = len(self.args)
822 if nargs != len(other_type.args):
823 return 0
824 for i in range(as_cmethod, nargs):
825 if not self.args[i].type.subtype_of_resolved_type(other_type.args[i].type):
826 return 0
827 else:
828 self.args[i].needs_type_test = other_type.args[i].needs_type_test \
829 or not self.args[i].type.same_as(other_type.args[i].type)
830 if self.has_varargs != other_type.has_varargs:
831 return 0
832 if self.optional_arg_count != other_type.optional_arg_count:
833 return 0
834 if not self.return_type.subtype_of_resolved_type(other_type.return_type):
835 return 0
836 return 1
838 def same_calling_convention_as(self, other):
839 sc1 = self.calling_convention == '__stdcall'
840 sc2 = other.calling_convention == '__stdcall'
841 return sc1 == sc2
843 def same_exception_signature_as(self, other_type):
844 return self.same_exception_signature_as_resolved_type(
845 other_type.resolve())
847 def same_exception_signature_as_resolved_type(self, other_type):
848 return self.exception_value == other_type.exception_value \
849 and self.exception_check == other_type.exception_check
851 def same_as_resolved_type(self, other_type, as_cmethod = 0):
852 return self.same_c_signature_as_resolved_type(other_type, as_cmethod) \
853 and self.same_exception_signature_as_resolved_type(other_type) \
854 and self.nogil == other_type.nogil
856 def pointer_assignable_from_resolved_type(self, other_type):
857 return self.same_c_signature_as_resolved_type(other_type) \
858 and self.same_exception_signature_as_resolved_type(other_type) \
859 and not (self.nogil and not other_type.nogil)
861 def declaration_code(self, entity_code,
862 for_display = 0, dll_linkage = None, pyrex = 0):
863 arg_decl_list = []
864 for arg in self.args[:len(self.args)-self.optional_arg_count]:
865 arg_decl_list.append(
866 arg.type.declaration_code("", for_display, pyrex = pyrex))
867 if self.is_overridable:
868 arg_decl_list.append("int %s" % Naming.skip_dispatch_cname)
869 if self.optional_arg_count:
870 arg_decl_list.append(self.op_arg_struct.declaration_code(Naming.optional_args_cname))
871 if self.has_varargs:
872 arg_decl_list.append("...")
873 arg_decl_code = ", ".join(arg_decl_list)
874 if not arg_decl_code and not pyrex:
875 arg_decl_code = "void"
876 trailer = ""
877 if (pyrex or for_display) and not self.return_type.is_pyobject:
878 if self.exception_value and self.exception_check:
879 trailer = " except? %s" % self.exception_value
880 elif self.exception_value:
881 trailer = " except %s" % self.exception_value
882 elif self.exception_check == '+':
883 trailer = " except +"
884 else:
885 " except *" # ignored
886 if self.nogil:
887 trailer += " nogil"
888 cc = self.calling_convention_prefix()
889 if (not entity_code and cc) or entity_code.startswith("*"):
890 entity_code = "(%s%s)" % (cc, entity_code)
891 cc = ""
892 return self.return_type.declaration_code(
893 "%s%s(%s)%s" % (cc, entity_code, arg_decl_code, trailer),
894 for_display, dll_linkage, pyrex)
896 def function_header_code(self, func_name, arg_code):
897 return "%s%s(%s)" % (self.calling_convention_prefix(),
898 func_name, arg_code)
900 def signature_string(self):
901 s = self.declaration_code("")
902 return s
905 class CFuncTypeArg:
906 # name string
907 # cname string
908 # type PyrexType
909 # pos source file position
911 def __init__(self, name, type, pos, cname=None):
912 self.name = name
913 if cname is not None:
914 self.cname = cname
915 else:
916 self.cname = Naming.var_prefix + name
917 self.type = type
918 self.pos = pos
919 self.not_none = False
920 self.needs_type_test = False # TODO: should these defaults be set in analyse_types()?
922 def __repr__(self):
923 return "%s:%s" % (self.name, repr(self.type))
925 def declaration_code(self, for_display = 0):
926 return self.type.declaration_code(self.cname, for_display)
929 class CStructOrUnionType(CType):
930 # name string
931 # cname string
932 # kind string "struct" or "union"
933 # scope StructOrUnionScope, or None if incomplete
934 # typedef_flag boolean
936 is_struct_or_union = 1
937 has_attributes = 1
939 def __init__(self, name, kind, scope, typedef_flag, cname):
940 self.name = name
941 self.cname = cname
942 self.kind = kind
943 self.scope = scope
944 self.typedef_flag = typedef_flag
945 self.is_struct = kind == 'struct'
946 if self.is_struct:
947 self.to_py_function = "%s_to_py_%s" % (Naming.convert_func_prefix, self.cname)
948 self.exception_check = True
949 self._convert_code = None
951 def create_convert_utility_code(self, env):
952 if env.outer_scope is None:
953 return False
954 if self._convert_code is None:
955 import Code
956 code = Code.CCodeWriter()
957 header = "static PyObject* %s(%s)" % (self.to_py_function, self.declaration_code('s'))
958 code.putln("%s {" % header)
959 code.putln("PyObject* res;")
960 code.putln("PyObject* member;")
961 code.putln("res = PyDict_New(); if (res == NULL) return NULL;")
962 for member in self.scope.var_entries:
963 if member.type.to_py_function and member.type.create_convert_utility_code(env):
964 interned_name = env.get_string_const(member.name, identifier=True)
965 env.add_py_string(interned_name)
966 code.putln("member = %s(s.%s); if (member == NULL) goto bad;" % (
967 member.type.to_py_function, member.cname))
968 code.putln("if (PyDict_SetItem(res, %s, member) < 0) goto bad;" % interned_name.pystring_cname)
969 code.putln("Py_DECREF(member);")
970 else:
971 self.to_py_function = None
972 return False
973 code.putln("return res;")
974 code.putln("bad:")
975 code.putln("Py_XDECREF(member);")
976 code.putln("Py_DECREF(res);")
977 code.putln("return NULL;")
978 code.putln("}")
979 proto = header + ";"
980 # This is a bit of a hack, we need a forward declaration
981 # due to the way things are ordered in the module...
982 entry = env.lookup(self.name)
983 if entry.visibility != 'extern':
984 proto = self.declaration_code('') + ';\n' + proto
985 self._convert_code = proto, code.buffer.getvalue()
987 env.use_utility_code(self._convert_code)
988 return True
990 def __repr__(self):
991 return "<CStructOrUnionType %s %s%s>" % (self.name, self.cname,
992 ("", " typedef")[self.typedef_flag])
994 def declaration_code(self, entity_code,
995 for_display = 0, dll_linkage = None, pyrex = 0):
996 if pyrex:
997 return self.base_declaration_code(self.name, entity_code)
998 else:
999 if for_display:
1000 base = self.name
1001 elif self.typedef_flag:
1002 base = self.cname
1003 else:
1004 base = "%s %s" % (self.kind, self.cname)
1005 return self.base_declaration_code(public_decl(base, dll_linkage), entity_code)
1007 def __cmp__(self, other):
1008 try:
1009 if self.name == other.name:
1010 return 0
1011 else:
1012 return 1
1013 except AttributeError:
1014 return 1
1016 def is_complete(self):
1017 return self.scope is not None
1019 def attributes_known(self):
1020 return self.is_complete()
1022 def can_be_complex(self):
1023 # Does the struct consist of exactly two floats?
1024 fields = self.scope.var_entries
1025 return len(fields) == 2 and fields[0].type.is_float and fields[1].type.is_float
1028 class CEnumType(CType):
1029 # name string
1030 # cname string or None
1031 # typedef_flag boolean
1033 is_enum = 1
1034 signed = 1
1035 rank = -1 # Ranks below any integer type
1036 to_py_function = "PyInt_FromLong"
1037 from_py_function = "PyInt_AsLong"
1039 def __init__(self, name, cname, typedef_flag):
1040 self.name = name
1041 self.cname = cname
1042 self.values = []
1043 self.typedef_flag = typedef_flag
1045 def __str__(self):
1046 return self.name
1048 def __repr__(self):
1049 return "<CEnumType %s %s%s>" % (self.name, self.cname,
1050 ("", " typedef")[self.typedef_flag])
1052 def declaration_code(self, entity_code,
1053 for_display = 0, dll_linkage = None, pyrex = 0):
1054 if pyrex:
1055 return self.base_declaration_code(self.cname, entity_code)
1056 else:
1057 if self.typedef_flag:
1058 base = self.cname
1059 else:
1060 base = "enum %s" % self.cname
1061 return self.base_declaration_code(public_decl(base, dll_linkage), entity_code)
1064 class CStringType:
1065 # Mixin class for C string types.
1067 is_string = 1
1068 is_unicode = 0
1070 to_py_function = "__Pyx_PyBytes_FromString"
1071 from_py_function = "__Pyx_PyBytes_AsString"
1072 exception_value = "NULL"
1074 def literal_code(self, value):
1075 assert isinstance(value, str)
1076 return '"%s"' % StringEncoding.escape_byte_string(value)
1079 class CUTF8CharArrayType(CStringType, CArrayType):
1080 # C 'char []' type.
1082 parsetuple_format = "s"
1083 pymemberdef_typecode = "T_STRING_INPLACE"
1084 is_unicode = 1
1086 to_py_function = "PyUnicode_DecodeUTF8"
1087 exception_value = "NULL"
1089 def __init__(self, size):
1090 CArrayType.__init__(self, c_char_type, size)
1092 class CCharArrayType(CStringType, CArrayType):
1093 # C 'char []' type.
1095 parsetuple_format = "s"
1096 pymemberdef_typecode = "T_STRING_INPLACE"
1098 def __init__(self, size):
1099 CArrayType.__init__(self, c_char_type, size)
1102 class CCharPtrType(CStringType, CPtrType):
1103 # C 'char *' type.
1105 parsetuple_format = "s"
1106 pymemberdef_typecode = "T_STRING"
1108 def __init__(self):
1109 CPtrType.__init__(self, c_char_type)
1112 class UnspecifiedType(PyrexType):
1113 # Used as a placeholder until the type can be determined.
1115 def declaration_code(self, entity_code,
1116 for_display = 0, dll_linkage = None, pyrex = 0):
1117 return "<unspecified>"
1119 def same_as_resolved_type(self, other_type):
1120 return False
1123 class ErrorType(PyrexType):
1124 # Used to prevent propagation of error messages.
1126 is_error = 1
1127 exception_value = "0"
1128 exception_check = 0
1129 to_py_function = "dummy"
1130 from_py_function = "dummy"
1131 typestring = None
1133 def create_convert_utility_code(self, env):
1134 return True
1136 def declaration_code(self, entity_code,
1137 for_display = 0, dll_linkage = None, pyrex = 0):
1138 return "<error>"
1140 def same_as_resolved_type(self, other_type):
1141 return 1
1143 def error_condition(self, result_code):
1144 return "dummy"
1147 rank_to_type_name = (
1148 "char", # 0
1149 "short", # 1
1150 "int", # 2
1151 "long", # 3
1152 "PY_LONG_LONG", # 4
1153 "Py_ssize_t", # 5
1154 "float", # 6
1155 "double", # 7
1156 "long double", # 8
1157 )
1159 py_object_type = PyObjectType()
1161 c_void_type = CVoidType()
1162 c_void_ptr_type = CPtrType(c_void_type)
1163 c_void_ptr_ptr_type = CPtrType(c_void_ptr_type)
1165 c_uchar_type = CIntType(0, 0, "T_UBYTE", typestring="B")
1166 c_ushort_type = CIntType(1, 0, "T_USHORT", typestring="H")
1167 c_uint_type = CUIntType(2, 0, "T_UINT", typestring="I")
1168 c_ulong_type = CULongType(3, 0, "T_ULONG", typestring="L")
1169 c_ulonglong_type = CULongLongType(4, 0, "T_ULONGLONG", typestring="Q")
1171 c_char_type = CIntType(0, 1, "T_CHAR", typestring="b")
1172 c_short_type = CIntType(1, 1, "T_SHORT", typestring="h")
1173 c_int_type = CIntType(2, 1, "T_INT", typestring="i")
1174 c_long_type = CIntType(3, 1, "T_LONG", typestring="l")
1175 c_longlong_type = CLongLongType(4, 1, "T_LONGLONG", typestring="q")
1176 c_py_ssize_t_type = CPySSizeTType(5, 1)
1177 c_bint_type = CBIntType(2, 1, "T_INT", typestring="i")
1179 c_schar_type = CIntType(0, 2, "T_CHAR", typestring="b")
1180 c_sshort_type = CIntType(1, 2, "T_SHORT", typestring="h")
1181 c_sint_type = CIntType(2, 2, "T_INT", typestring="i")
1182 c_slong_type = CIntType(3, 2, "T_LONG", typestring="l")
1183 c_slonglong_type = CLongLongType(4, 2, "T_LONGLONG", typestring="q")
1185 c_float_type = CFloatType(6, "T_FLOAT", typestring="f")
1186 c_double_type = CFloatType(7, "T_DOUBLE", typestring="d")
1187 c_longdouble_type = CFloatType(8, typestring="g")
1189 c_null_ptr_type = CNullPtrType(c_void_type)
1190 c_char_array_type = CCharArrayType(None)
1191 c_char_ptr_type = CCharPtrType()
1192 c_utf8_char_array_type = CUTF8CharArrayType(None)
1193 c_char_ptr_ptr_type = CPtrType(c_char_ptr_type)
1194 c_py_ssize_t_ptr_type = CPtrType(c_py_ssize_t_type)
1195 c_int_ptr_type = CPtrType(c_int_type)
1197 c_returncode_type = CIntType(2, 1, "T_INT", is_returncode = 1)
1199 c_anon_enum_type = CAnonEnumType(-1, 1)
1201 # the Py_buffer type is defined in Builtin.py
1202 c_py_buffer_type = CStructOrUnionType("Py_buffer", "struct", None, 1, "Py_buffer")
1203 c_py_buffer_ptr_type = CPtrType(c_py_buffer_type)
1205 error_type = ErrorType()
1206 unspecified_type = UnspecifiedType()
1208 lowest_float_rank = 6
1210 sign_and_rank_to_type = {
1211 #(signed, rank)
1212 (0, 0, ): c_uchar_type,
1213 (0, 1): c_ushort_type,
1214 (0, 2): c_uint_type,
1215 (0, 3): c_ulong_type,
1216 (0, 4): c_ulonglong_type,
1217 (0, 5): c_ulonglong_type, # I'm not sure about this. this should be for size_t Py_ssize_t
1218 (1, 0): c_char_type,
1219 (1, 1): c_short_type,
1220 (1, 2): c_int_type,
1221 (1, 3): c_long_type,
1222 (1, 4): c_longlong_type,
1223 (1, 5): c_py_ssize_t_type,
1224 (2, 0): c_schar_type,
1225 (2, 1): c_sshort_type,
1226 (2, 2): c_sint_type,
1227 (2, 3): c_slong_type,
1228 (2, 4): c_slonglong_type,
1229 (2, 5): c_py_ssize_t_type,
1230 (1, 6): c_float_type,
1231 (1, 7): c_double_type,
1232 (1, 8): c_longdouble_type,
1233 }
1235 modifiers_and_name_to_type = {
1236 #(signed, longness, name)
1237 (0, 0, "char"): c_uchar_type,
1238 (0, -1, "int"): c_ushort_type,
1239 (0, 0, "int"): c_uint_type,
1240 (0, 1, "int"): c_ulong_type,
1241 (0, 2, "int"): c_ulonglong_type,
1242 (1, 0, "void"): c_void_type,
1243 (1, 0, "char"): c_char_type,
1244 (1, -1, "int"): c_short_type,
1245 (1, 0, "int"): c_int_type,
1246 (1, 1, "int"): c_long_type,
1247 (1, 2, "int"): c_longlong_type,
1248 (1, 0, "Py_ssize_t"): c_py_ssize_t_type,
1249 (1, 0, "float"): c_float_type,
1250 (1, 0, "double"): c_double_type,
1251 (1, 1, "double"): c_longdouble_type,
1252 (1, 0, "object"): py_object_type,
1253 (1, 0, "bint"): c_bint_type,
1254 (2, 0, "char"): c_schar_type,
1255 (2, -1, "int"): c_sshort_type,
1256 (2, 0, "int"): c_sint_type,
1257 (2, 1, "int"): c_slong_type,
1258 (2, 2, "int"): c_slonglong_type,
1259 (2, 0, "Py_ssize_t"): c_py_ssize_t_type,
1261 (1, 0, "long"): c_long_type,
1262 (1, 0, "short"): c_short_type,
1263 (1, 0, "longlong"): c_longlong_type,
1264 (1, 0, "bint"): c_bint_type,
1265 }
1267 def widest_numeric_type(type1, type2):
1268 # Given two numeric types, return the narrowest type
1269 # encompassing both of them.
1270 if type1.is_enum and type2.is_enum:
1271 return c_int_type
1272 elif type1 is type2:
1273 return type1
1274 elif (type1.signed and type2.signed) or (not type1.signed and not type2.signed):
1275 if type2.rank > type1.rank:
1276 return type2
1277 else:
1278 return type1
1279 else:
1280 return sign_and_rank_to_type[min(type1.signed, type2.signed), max(type1.rank, type2.rank)]
1281 return widest_type
1283 def simple_c_type(signed, longness, name):
1284 # Find type descriptor for simple type given name and modifiers.
1285 # Returns None if arguments don't make sense.
1286 return modifiers_and_name_to_type.get((signed, longness, name))
1288 def parse_basic_type(name):
1289 base = None
1290 if name.startswith('p_'):
1291 base = parse_basic_type(name[2:])
1292 elif name.startswith('p'):
1293 base = parse_basic_type(name[1:])
1294 elif name.endswith('*'):
1295 base = parse_basic_type(name[:-1])
1296 if base:
1297 return CPtrType(base)
1298 elif name.startswith('u'):
1299 return simple_c_type(0, 0, name[1:])
1300 else:
1301 return simple_c_type(1, 0, name)
1303 def c_array_type(base_type, size):
1304 # Construct a C array type.
1305 if base_type is c_char_type:
1306 return CCharArrayType(size)
1307 else:
1308 return CArrayType(base_type, size)
1310 def c_ptr_type(base_type):
1311 # Construct a C pointer type.
1312 if base_type is c_char_type:
1313 return c_char_ptr_type
1314 else:
1315 return CPtrType(base_type)
1317 def Node_to_type(node, env):
1318 from ExprNodes import NameNode, AttributeNode, StringNode, error
1319 if isinstance(node, StringNode):
1320 node = NameNode(node.pos, name=node.value)
1321 if isinstance(node, NameNode) and node.name in rank_to_type_name:
1322 return simple_c_type(1, 0, node.name)
1323 elif isinstance(node, (AttributeNode, NameNode)):
1324 node.analyze_types(env)
1325 if not node.entry.is_type:
1326 pass
1327 else:
1328 error(node.pos, "Bad type")
1330 def public_decl(base, dll_linkage):
1331 if dll_linkage:
1332 return "%s(%s)" % (dll_linkage, base)
1333 else:
1334 return base
1336 def same_type(type1, type2):
1337 return type1.same_as(type2)
1339 def assignable_from(type1, type2):
1340 return type1.assignable_from(type2)
1342 def typecast(to_type, from_type, expr_code):
1343 # Return expr_code cast to a C type which can be
1344 # assigned to to_type, assuming its existing C type
1345 # is from_type.
1346 if to_type is from_type or \
1347 (not to_type.is_pyobject and assignable_from(to_type, from_type)):
1348 return expr_code
1349 else:
1350 #print "typecast: to", to_type, "from", from_type ###
1351 return to_type.cast_code(expr_code)
1354 type_conversion_predeclarations = """
1355 /* Type Conversion Predeclarations */
1357 #if PY_MAJOR_VERSION < 3
1358 #define __Pyx_PyBytes_FromString PyString_FromString
1359 #define __Pyx_PyBytes_AsString PyString_AsString
1360 #else
1361 #define __Pyx_PyBytes_FromString PyBytes_FromString
1362 #define __Pyx_PyBytes_AsString PyBytes_AsString
1363 #endif
1365 #define __Pyx_PyBool_FromLong(b) ((b) ? (Py_INCREF(Py_True), Py_True) : (Py_INCREF(Py_False), Py_False))
1366 static INLINE int __Pyx_PyObject_IsTrue(PyObject* x);
1367 static INLINE PY_LONG_LONG __pyx_PyInt_AsLongLong(PyObject* x);
1368 static INLINE unsigned PY_LONG_LONG __pyx_PyInt_AsUnsignedLongLong(PyObject* x);
1369 static INLINE Py_ssize_t __pyx_PyIndex_AsSsize_t(PyObject* b);
1371 #define __pyx_PyInt_AsLong(x) (PyInt_CheckExact(x) ? PyInt_AS_LONG(x) : PyInt_AsLong(x))
1372 #define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x))
1373 """ + type_conversion_predeclarations
1375 type_conversion_functions = """
1376 /* Type Conversion Functions */
1378 static INLINE Py_ssize_t __pyx_PyIndex_AsSsize_t(PyObject* b) {
1379 Py_ssize_t ival;
1380 PyObject* x = PyNumber_Index(b);
1381 if (!x) return -1;
1382 ival = PyInt_AsSsize_t(x);
1383 Py_DECREF(x);
1384 return ival;
1385 }
1387 static INLINE int __Pyx_PyObject_IsTrue(PyObject* x) {
1388 if (x == Py_True) return 1;
1389 else if (x == Py_False) return 0;
1390 else return PyObject_IsTrue(x);
1391 }
1393 static INLINE PY_LONG_LONG __pyx_PyInt_AsLongLong(PyObject* x) {
1394 if (PyInt_CheckExact(x)) {
1395 return PyInt_AS_LONG(x);
1396 }
1397 else if (PyLong_CheckExact(x)) {
1398 return PyLong_AsLongLong(x);
1399 }
1400 else {
1401 PY_LONG_LONG val;
1402 PyObject* tmp = PyNumber_Int(x); if (!tmp) return (PY_LONG_LONG)-1;
1403 val = __pyx_PyInt_AsLongLong(tmp);
1404 Py_DECREF(tmp);
1405 return val;
1406 }
1407 }
1409 static INLINE unsigned PY_LONG_LONG __pyx_PyInt_AsUnsignedLongLong(PyObject* x) {
1410 if (PyInt_CheckExact(x)) {
1411 long val = PyInt_AS_LONG(x);
1412 if (unlikely(val < 0)) {
1413 PyErr_SetString(PyExc_TypeError, "Negative assignment to unsigned type.");
1414 return (unsigned PY_LONG_LONG)-1;
1415 }
1416 return val;
1417 }
1418 else if (PyLong_CheckExact(x)) {
1419 return PyLong_AsUnsignedLongLong(x);
1420 }
1421 else {
1422 PY_LONG_LONG val;
1423 PyObject* tmp = PyNumber_Int(x); if (!tmp) return (PY_LONG_LONG)-1;
1424 val = __pyx_PyInt_AsUnsignedLongLong(tmp);
1425 Py_DECREF(tmp);
1426 return val;
1427 }
1428 }
1430 """ + type_conversion_functions
