Permalink
Browse files

Choose typedefed type in widest_numeric_type & support tyepdef Cython…

… types in vector expressions
  • Loading branch information...
1 parent 1217a97 commit 3fdcc52b5cfb11cd539d82589b9cdc6232803ed4 @markflorisson committed May 29, 2012
Showing with 35 additions and 8 deletions.
  1. +10 −1 Cython/Compiler/PyrexTypes.py
  2. +15 −6 Cython/Compiler/Vector.py
  3. +1 −1 Cython/minivect
  4. +9 −0 tests/run/elementwise.pyx
@@ -1378,6 +1378,10 @@ def __lt__(self, other):
# Prefer numeric types over others
return True
+ def __eq__(self, other):
+ return (isinstance(other, CNumericType) and
+ self.rank == other.rank and self.signed == other.signed)
+
def py_type_name(self):
if self.rank <= 4:
return "(int, long)"
@@ -3699,7 +3703,10 @@ def widest_numeric_type(type1, type2):
# Given two numeric types, return the narrowest type
# encompassing both of them.
if type1 == type2:
- widest_type = type1
+ if type2.is_typedef:
+ widest_type = type2
+ else:
+ widest_type = type1
elif type1.is_complex or type2.is_complex:
def real_type(ntype):
if ntype.is_complex:
@@ -3717,6 +3724,8 @@ def real_type(ntype):
widest_type = type1
elif type1.signed < type2.signed:
widest_type = type1
+ elif type1.is_typedef:
+ widest_type = type1
else:
widest_type = type2
return widest_type
View
@@ -16,9 +16,18 @@ def getpos(self, node):
def getchildren(self, node):
return node.child_attrs
+ def declare_type(self, type):
+ if type.is_typewrapper:
+ return type.opaque_type.declaration_code("")
+
+ return super(Context, self).declare_type(type)
+
+
class TypeMapper(minitypes.TypeMapper):
def map_type(self, type):
- if type.is_memoryviewslice:
+ if type.is_typedef:
+ return minitypes.TypeWrapper(type)
+ elif type.is_memoryviewslice:
return minitypes.ArrayType(self.map_type(type.dtype),
len(type.axes),
is_c_contig=type.is_c_contig,
@@ -114,8 +123,7 @@ def map_type(self, node):
except minierror.UnmappableTypeError, e:
error(node.pos, "Unsupported type in elementwise "
"operation: %s" % (node.type,))
- self.error = True
- return minitypes.error_type
+ raise
def visit_ExprNode(self, node):
assert not node.is_elemental
@@ -157,8 +165,9 @@ def visit_elemental(self, node):
astmapper = ElementalMapper(self.minicontext, self.current_env())
shapevar = b.variable(minitypes.Py_ssize_t.pointer(),
'__pyx_shape')
- body = astmapper.visit(node)
- if astmapper.error:
+ try:
+ body = astmapper.visit(node)
+ except minierror.UnmappableTypeError:
return None
name = '__pyx_array_expression%d' % self.funccount
@@ -188,4 +197,4 @@ def visit_SingleAssignmentNode(self, node):
return self.visit_elemental(node)
self.visitchildren(node)
- return node
+ return node
@@ -1,6 +1,7 @@
# tag: numpy
# mode: run
+cimport numpy as np
import numpy as np
def test_simple_binop_assign(int[:] m):
@@ -56,3 +57,11 @@ def test_simple_binop_assign_contig_2d(int[:, :] m):
"""
m[:] = m + m + m
return m
+
+def test_typedef(np.int32_t[:] m):
+ """
+ >>> np.asarray(test_typedef(np.arange(10, dtype=np.int32)))
+ array([ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27], dtype=int32)
+ """
+ m[:] = m + m + m
+ return m

0 comments on commit 3fdcc52

Please sign in to comment.