cython-devel

changeset 2654:76e7121cbdc7

Fix #441
author Dag Sverre Seljebotn <dagss@student.matnat.uio.no>
date Tue Nov 03 16:35:19 2009 +0100 (2 years ago)
parents 2e3dda4a7d23
children 1e53c34f7f74
files Cython/Compiler/Code.py Cython/Compiler/ModuleNode.py Cython/Compiler/PyrexTypes.py tests/run/complex_numbers_T305.pyx
line diff
1.1 --- a/Cython/Compiler/Code.py Tue Nov 03 01:01:54 2009 -0800 1.2 +++ b/Cython/Compiler/Code.py Tue Nov 03 16:35:19 2009 +0100 1.3 @@ -408,8 +408,10 @@ 1.4 code_layout = [ 1.5 'h_code', 1.6 'utility_code_proto_before_types', 1.7 - 'type_declarations', 1.8 - 'utility_code_proto', 1.9 + 'numeric_typedefs', # Let these detailed individual parts stay!, 1.10 + 'complex_type_declarations', # as the proper solution is to make a full DAG... 1.11 + 'type_declarations', # More coarse-grained blocks would simply hide 1.12 + 'utility_code_proto', # the ugliness, not fix it 1.13 'module_declarations', 1.14 'typeinfo', 1.15 'before_global_var',
2.1 --- a/Cython/Compiler/ModuleNode.py Tue Nov 03 01:01:54 2009 -0800 2.2 +++ b/Cython/Compiler/ModuleNode.py Tue Nov 03 16:35:19 2009 +0100 2.3 @@ -654,8 +654,12 @@ 2.4 2.5 def generate_typedef(self, entry, code): 2.6 base_type = entry.type.typedef_base_type 2.7 - code.putln("") 2.8 - code.putln("typedef %s;" % base_type.declaration_code(entry.cname)) 2.9 + if base_type.is_numeric: 2.10 + writer = code.globalstate['numeric_typedefs'] 2.11 + else: 2.12 + writer = code 2.13 + writer.putln("") 2.14 + writer.putln("typedef %s;" % base_type.declaration_code(entry.cname)) 2.15 2.16 def sue_header_footer(self, type, kind, name): 2.17 if type.typedef_flag:
3.1 --- a/Cython/Compiler/PyrexTypes.py Tue Nov 03 01:01:54 2009 -0800 3.2 +++ b/Cython/Compiler/PyrexTypes.py Tue Nov 03 16:35:19 2009 +0100 3.3 @@ -871,12 +871,21 @@ 3.4 scope = None 3.5 3.6 def __init__(self, real_type): 3.7 + while real_type.is_typedef and not real_type.typedef_is_external: 3.8 + real_type = real_type.typedef_base_type 3.9 + if real_type.is_typedef and real_type.typedef_is_external: 3.10 + # The below is not actually used: Coercions are currently disabled 3.11 + # so that complex types of external types can not be created 3.12 + self.funcsuffix = "_%s" % real_type.specalization_name() 3.13 + else: 3.14 + self.funcsuffix = real_type.math_h_modifier 3.15 + 3.16 self.real_type = real_type 3.17 CNumericType.__init__(self, real_type.rank + 0.5, real_type.signed) 3.18 self.binops = {} 3.19 self.from_parts = "%s_from_parts" % self.specalization_name() 3.20 self.default_value = "%s(0, 0)" % self.from_parts 3.21 - 3.22 + 3.23 def __eq__(self, other): 3.24 if isinstance(self, CComplexType) and isinstance(other, CComplexType): 3.25 return self.real_type == other.real_type 3.26 @@ -899,7 +908,7 @@ 3.27 3.28 def __hash__(self): 3.29 return ~hash(self.real_type) 3.30 - 3.31 + 3.32 def declaration_code(self, entity_code, 3.33 for_display = 0, dll_linkage = None, pyrex = 0): 3.34 if for_display: 3.35 @@ -915,8 +924,9 @@ 3.36 3.37 def assignable_from(self, src_type): 3.38 # Temporary hack/feature disabling, see #441 3.39 - if not src_type.is_complex and src_type.is_numeric and src_type.is_typedef: 3.40 - return False 3.41 + if (not src_type.is_complex and src_type.is_numeric and src_type.is_typedef 3.42 + and src_type.typedef_is_external): 3.43 + return False 3.44 else: 3.45 return super(CComplexType, self).assignable_from(src_type) 3.46 3.47 @@ -940,7 +950,7 @@ 3.48 CFuncType(self, [CFuncTypeArg("self", self, None)]), 3.49 pos=None, 3.50 defining=1, 3.51 - cname="__Pyx_c_conj%s" % self.real_type.math_h_modifier) 3.52 + cname="__Pyx_c_conj%s" % self.funcsuffix) 3.53 3.54 return True 3.55 3.56 @@ -956,7 +966,7 @@ 3.57 utility_code.specialize( 3.58 self, 3.59 real_type = self.real_type.declaration_code(''), 3.60 - m = self.real_type.math_h_modifier)) 3.61 + m = self.funcsuffix)) 3.62 return True 3.63 3.64 def create_to_py_utility_code(self, env): 3.65 @@ -973,7 +983,7 @@ 3.66 utility_code.specialize( 3.67 self, 3.68 real_type = self.real_type.declaration_code(''), 3.69 - m = self.real_type.math_h_modifier)) 3.70 + m = self.funcsuffix)) 3.71 self.from_py_function = "__Pyx_PyComplex_As_" + self.specalization_name() 3.72 return True 3.73 3.74 @@ -984,8 +994,7 @@ 3.75 pass 3.76 try: 3.77 op_name = complex_ops[nargs, op] 3.78 - modifier = self.real_type.math_h_modifier 3.79 - self.binops[nargs, op] = func_name = "__Pyx_c_%s%s" % (op_name, modifier) 3.80 + self.binops[nargs, op] = func_name = "__Pyx_c_%s%s" % (op_name, self.funcsuffix) 3.81 return func_name 3.82 except KeyError: 3.83 return None 3.84 @@ -1045,7 +1054,7 @@ 3.85 """) 3.86 3.87 complex_type_utility_code = UtilityCode( 3.88 -proto_block='utility_code_proto_before_types', 3.89 +proto_block='complex_type_declarations', 3.90 proto=""" 3.91 #if CYTHON_CCOMPLEX 3.92 #ifdef __cplusplus 3.93 @@ -1204,7 +1213,6 @@ 3.94 #endif 3.95 """) 3.96 3.97 - 3.98 class CArrayType(CType): 3.99 # base_type CType Element type 3.100 # size integer or None Number of elements
4.1 --- a/tests/run/complex_numbers_T305.pyx Tue Nov 03 01:01:54 2009 -0800 4.2 +++ b/tests/run/complex_numbers_T305.pyx Tue Nov 03 16:35:19 2009 +0100 4.3 @@ -123,15 +123,45 @@ 4.4 4.5 ctypedef double complex cdouble 4.6 def test_conjugate_typedef(cdouble z): 4.7 + """ 4.8 + >>> test_conjugate_typedef(2+3j) 4.9 + (2-3j) 4.10 + """ 4.11 return z.conjugate() 4.12 4.13 -#ctypedef double mydouble 4.14 -#def test_coerce_typedef_multiply(mydouble x, double complex z): 4.15 -# """ 4.16 -# >>> test_coerce_typedef_multiply(3, 1j) 4.17 -# (3j) 4.18 -# """ 4.19 -# return x * z 4.20 +## cdef extern from "complex_numbers_T305.h": 4.21 +## ctypedef double double_really_float "myfloat" 4.22 +## ctypedef float float_really_double "mydouble" 4.23 +## ctypedef float real_float "myfloat" 4.24 +## ctypedef double real_double "mydouble" 4.25 + 4.26 +## def test_conjugate_nosizeassumptions(double_really_float x, 4.27 +## float_really_double y, 4.28 +## real_float z, real_double w): 4.29 +## """ 4.30 +## >>> test_conjugate_nosizeassumptions(1, 1, 1, 1) 4.31 +## (-1j, -1j, -1j, -1j) 4.32 +## >>> ["%.2f" % x.imag for x in test_conjugate_nosizeassumptions(2e300, 2e300, 2e300, 2e300)] 4.33 +## ['-inf', '-2e+300', '-inf', '-2e+300'] 4.34 +## """ 4.35 +## cdef double complex I = 1j 4.36 +## return ((x*I).conjugate(), (y*I).conjugate(), (z*I).conjugate(), (w*I).conjugate()) 4.37 + 4.38 +ctypedef double mydouble 4.39 +def test_coerce_typedef_multiply(mydouble x, double complex z): 4.40 + """ 4.41 + >>> test_coerce_typedef_multiply(3, 1+1j) 4.42 + (3+3j) 4.43 + """ 4.44 + return x * z 4.45 + 4.46 +ctypedef int myint 4.47 +def test_coerce_typedef_multiply_int(myint x, double complex z): 4.48 + """ 4.49 + >>> test_coerce_typedef_multiply_int(3, 1+1j) 4.50 + (3+3j) 4.51 + """ 4.52 + return x * z 4.53 4.54 cpdef double complex complex_retval(): 4.55 """