~ubuntu-branches/ubuntu/trusty/pyrex/trusty-proposed

« back to all changes in this revision

Viewing changes to Pyrex/Compiler/PyrexTypes.py

  • Committer: Bazaar Package Importer
  • Author(s): Paul Brossier
  • Date: 2006-06-14 18:21:15 UTC
  • mto: (2.1.5 edgy) (4.1.1 lenny) (1.1.7 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20060614182115-54t2s7w2azy5oyof
Tags: upstream-0.9.4.1
ImportĀ upstreamĀ versionĀ 0.9.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
    #    of this type, given a code fragment for the entity.
36
36
    #    * If for_display, this is for reading by a human in an error
37
37
    #      message; otherwise it must be valid C code.
38
 
    #    * If dll_linkage is not None, it must be 'DL_IMPORT' or
39
 
    #      'DL_EXPORT', and will be added to the base type part of
 
38
    #    * If dll_linkage is not None, it must be 'DL_EXPORT' or
 
39
    #      'DL_IMPORT', and will be added to the base type part of
40
40
    #      the declaration.
41
41
    #    * If pyrex = 1, this is for use in a 'cdef extern'
42
42
    #      statement of a Pyrex include file.
111
111
        # A type is incomplete if it is an unsized array,
112
112
        # a struct whose attributes are not defined, etc.
113
113
        return 1
 
114
    
 
115
    def cast_code(self, expr_code):
 
116
        return "((%s)%s)" % (self.declaration_code(""), expr_code)
114
117
 
115
118
 
116
119
class CTypedefType:
289
292
    is_numeric = 1
290
293
    default_value = "0"
291
294
    
292
 
    parsetuple_formats = "chilLfd?" # rank -> format
 
295
    parsetuple_formats = ( # rank -> format
 
296
        "?HIkK???", # unsigned
 
297
        "chilLfd?", # signed
 
298
    )
293
299
    
294
 
    def __init__(self, rank, pymemberdef_typecode = None):
 
300
    def __init__(self, rank, signed = 1, pymemberdef_typecode = None):
295
301
        self.rank = rank
296
 
        ptf = self.parsetuple_formats[rank]
 
302
        self.signed = signed
 
303
        ptf = self.parsetuple_formats[signed][rank]
297
304
        if ptf == '?':
298
305
            ptf = None
299
306
        self.parsetuple_format = ptf
329
336
    from_py_function = "PyInt_AsLong"
330
337
 
331
338
    def __init__(self, rank, signed, pymemberdef_typecode = None, is_returncode = 0):
332
 
        CNumericType.__init__(self, rank, pymemberdef_typecode)
333
 
        self.signed = signed
 
339
        CNumericType.__init__(self, rank, signed, pymemberdef_typecode)
334
340
        self.is_returncode = is_returncode
335
341
 
336
342
 
 
343
class CUIntType(CIntType):
 
344
 
 
345
    to_py_function = "PyLong_FromUnsignedLong"
 
346
    from_py_function = "PyInt_AsUnsignedLongMask"
 
347
 
 
348
 
337
349
class CULongType(CIntType):
338
350
 
339
351
    to_py_function = "PyLong_FromUnsignedLong"
340
 
    from_py_function = "PyLong_AsUnsignedLong"
 
352
    from_py_function = "PyInt_AsUnsignedLongMask"
341
353
 
342
354
 
343
355
class CLongLongType(CIntType):
344
356
 
345
357
    to_py_function = "PyLong_FromLongLong"
346
 
    from_py_function = "PyLong_AsLongLong"
 
358
    from_py_function = "PyInt_AsUnsignedLongLongMask"
347
359
 
348
360
 
349
361
class CULongLongType(CIntType):
350
362
 
351
363
    to_py_function = "PyLong_FromUnsignedLongLong"
352
 
    from_py_function = "PyLong_AsUnsignedLongLong"
 
364
    from_py_function = "PyInt_AsUnsignedLongLongMask"
353
365
 
354
366
 
355
367
class CFloatType(CNumericType):
356
368
 
357
369
    is_float = 1
358
 
    signed = 1
359
370
    to_py_function = "PyFloat_FromDouble"
360
371
    from_py_function = "PyFloat_AsDouble"
361
 
 
 
372
    
 
373
    def __init__(self, rank, pymemberdef_typecode = None):
 
374
        CNumericType.__init__(self, rank, 1, pymemberdef_typecode)
 
375
    
362
376
 
363
377
class CArrayType(CType):
364
378
    #  base_type     CType              Element type
423
437
    
424
438
    def declaration_code(self, entity_code, 
425
439
            for_display = 0, dll_linkage = None, pyrex = 0):
 
440
        #print "CPtrType.declaration_code: pointer to", self.base_type ###
426
441
        return self.base_type.declaration_code(
427
442
            "(*%s)" % entity_code,
428
443
            for_display, dll_linkage, pyrex)
521
536
        if not arg_decl_code and not pyrex:
522
537
            arg_decl_code = "void"
523
538
        exc_clause = ""
524
 
        if for_display:
 
539
        if pyrex or for_display:
525
540
            if self.exception_value and self.exception_check:
526
541
                exc_clause = " except? %s" % self.exception_value
527
542
            elif self.exception_value:
683
698
c_short_type =    CIntType(1, 1, "T_SHORT")
684
699
c_int_type =      CIntType(2, 1, "T_INT")
685
700
c_long_type =     CIntType(3, 1, "T_LONG")
686
 
c_longlong_type = CLongLongType(4, 1)
 
701
c_longlong_type = CLongLongType(4, 1, "T_LONGLONG")
687
702
 
688
703
c_uchar_type =     CIntType(0, 0, "T_UBYTE")
689
704
c_ushort_type =    CIntType(1, 0, "T_USHORT")
690
 
c_uint_type =      CIntType(2, 0, "T_UINT")
 
705
c_uint_type =      CUIntType(2, 0, "T_UINT")
691
706
c_ulong_type =     CULongType(3, 0, "T_ULONG")
692
 
c_ulonglong_type = CULongLongType(4, 0)
 
707
c_ulonglong_type = CULongLongType(4, 0, "T_ULONGLONG")
693
708
 
694
709
c_float_type =      CFloatType(5, "T_FLOAT")
695
710
c_double_type =     CFloatType(6, "T_DOUBLE")
788
803
    else:
789
804
        return base
790
805
 
 
806
def same_type(type1, type2):
 
807
    return type1.same_as(type2)
 
808
    
 
809
def assignable_from(type1, type2):
 
810
    return type1.assignable_from(type2)
 
811
 
 
812
def typecast(to_type, from_type, expr_code):
 
813
    #  Return expr_code cast to a C type which can be
 
814
    #  assigned to to_type, assuming its existing C type
 
815
    #  is from_type.
 
816
    if to_type is from_type or \
 
817
        (not to_type.is_pyobject and assignable_from(to_type, from_type)):
 
818
            return expr_code
 
819
    else:
 
820
        #print "typecast: to", to_type, "from", from_type ###
 
821
        return to_type.cast_code(expr_code)