Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactor memoryview dtype validation

  • Loading branch information...
commit 98d0d6062c69312c55fca56db54a543246a2f3cb 1 parent 1df5077
Mark Florisson authored
3  Cython/Compiler/ExprNodes.py
@@ -7554,8 +7554,6 @@ def analyse_types(self, env):
7554 7554 array_dtype = self.base_type_node.base_type_node.analyse(env)
7555 7555 axes = self.base_type_node.axes
7556 7556
7557   - MemoryView.validate_memslice_dtype(self.pos, array_dtype)
7558   -
7559 7557 self.type = error_type
7560 7558 self.shapes = []
7561 7559 ndim = len(axes)
@@ -7635,6 +7633,7 @@ def analyse_types(self, env):
7635 7633 axes[-1] = ('direct', 'contig')
7636 7634
7637 7635 self.coercion_type = PyrexTypes.MemoryViewSliceType(array_dtype, axes)
  7636 + self.coercion_type.validate_memslice_dtype(self.pos)
7638 7637 self.type = self.get_cython_array_type(env)
7639 7638 MemoryView.use_cython_array_utility_code(env)
7640 7639 env.use_utility_code(MemoryView.typeinfo_to_format_code)
2  Cython/Compiler/FusedNode.py
@@ -184,7 +184,7 @@ def _specialize_function_args(self, args, fused_to_specific):
184 184 if arg.type.is_fused:
185 185 arg.type = arg.type.specialize(fused_to_specific)
186 186 if arg.type.is_memoryviewslice:
187   - MemoryView.validate_memslice_dtype(arg.pos, arg.type.dtype)
  187 + arg.type.validate_memslice_dtype(arg.pos)
188 188
189 189 def create_new_local_scope(self, node, env, f2s):
190 190 """
40 Cython/Compiler/MemoryView.py
@@ -170,35 +170,6 @@ def src_conforms_to_dst(src, dst, broadcast=False):
170 170
171 171 return True
172 172
173   -def valid_memslice_dtype(dtype, i=0):
174   - """
175   - Return whether type dtype can be used as the base type of a
176   - memoryview slice.
177   -
178   - We support structs, numeric types and objects
179   - """
180   - if dtype.is_complex and dtype.real_type.is_int:
181   - return False
182   -
183   - if dtype.is_struct and dtype.kind == 'struct':
184   - for member in dtype.scope.var_entries:
185   - if not valid_memslice_dtype(member.type):
186   - return False
187   -
188   - return True
189   -
190   - return (
191   - dtype.is_error or
192   - # Pointers are not valid (yet)
193   - # (dtype.is_ptr and valid_memslice_dtype(dtype.base_type)) or
194   - (dtype.is_array and i < 8 and
195   - valid_memslice_dtype(dtype.base_type, i + 1)) or
196   - dtype.is_numeric or
197   - dtype.is_pyobject or
198   - dtype.is_fused or # accept this as it will be replaced by specializations later
199   - (dtype.is_typedef and valid_memslice_dtype(dtype.typedef_base_type))
200   - )
201   -
202 173 def validate_memslice_dtype(pos, dtype):
203 174 if not valid_memslice_dtype(dtype):
204 175 error(pos, "Invalid base type for memoryview slice: %s" % dtype)
@@ -436,18 +407,13 @@ def get_is_contig_utility(c_contig, ndim):
436 407 def copy_src_to_dst_cname():
437 408 return "__pyx_memoryview_copy_contents"
438 409
439   -def verify_direct_dimensions(node):
440   - for access, packing in node.type.axes:
441   - if access != 'direct':
442   - error(self.pos, "All dimensions must be direct")
443   -
444 410 def copy_broadcast_memview_src_to_dst(src, dst, code):
445 411 """
446 412 Copy the contents of slice src to slice dst. Does not support indirect
447 413 slices.
448 414 """
449   - verify_direct_dimensions(src)
450   - verify_direct_dimensions(dst)
  415 + src.type.assert_direct_dims(src.pos)
  416 + dst.type.assert_direct_dims(dst.pos)
451 417
452 418 code.putln(code.error_goto_if_neg(
453 419 "%s(%s, %s, %d, %d, %d)" % (copy_src_to_dst_cname(),
@@ -471,7 +437,7 @@ def assign_scalar(dst, scalar, code):
471 437 Assign a scalar to a slice. dst must be a temp, scalar will be assigned
472 438 to a correct type and not just something assignable.
473 439 """
474   - verify_direct_dimensions(dst)
  440 + dst.type.assert_direct_dims(dst.pos)
475 441 dtype = dst.type.dtype
476 442 type_decl = dtype.declaration_code("")
477 443 slice_decl = dst.type.declaration_code("")
2  Cython/Compiler/Nodes.py
@@ -886,8 +886,8 @@ def analyse(self, env, could_be_name = False):
886 886 if not MemoryView.validate_axes(self.pos, axes_specs):
887 887 self.type = error_type
888 888 else:
889   - MemoryView.validate_memslice_dtype(self.pos, base_type)
890 889 self.type = PyrexTypes.MemoryViewSliceType(base_type, axes_specs)
  890 + self.type.validate_memslice_dtype(self.pos)
891 891 self.use_memview_utilities(env)
892 892
893 893 return self.type
39 Cython/Compiler/PyrexTypes.py
@@ -604,6 +604,45 @@ def declare_attribute(self, attribute, env, pos):
604 604
605 605 return True
606 606
  607 + def valid_dtype(self, dtype, i=0):
  608 + """
  609 + Return whether type dtype can be used as the base type of a
  610 + memoryview slice.
  611 +
  612 + We support structs, numeric types and objects
  613 + """
  614 + if dtype.is_complex and dtype.real_type.is_int:
  615 + return False
  616 +
  617 + if dtype.is_struct and dtype.kind == 'struct':
  618 + for member in dtype.scope.var_entries:
  619 + if not self.valid_dtype(member.type):
  620 + return False
  621 +
  622 + return True
  623 +
  624 + return (
  625 + dtype.is_error or
  626 + # Pointers are not valid (yet)
  627 + # (dtype.is_ptr and valid_memslice_dtype(dtype.base_type)) or
  628 + (dtype.is_array and i < 8 and
  629 + self.valid_dtype(dtype.base_type, i + 1)) or
  630 + dtype.is_numeric or
  631 + dtype.is_pyobject or
  632 + dtype.is_fused or # accept this as it will be replaced by specializations later
  633 + (dtype.is_typedef and self.valid_dtype(dtype.typedef_base_type))
  634 + )
  635 +
  636 + def validate_memslice_dtype(self, pos):
  637 + if not self.valid_dtype(self.dtype):
  638 + error(pos, "Invalid base type for memoryview slice: %s" % self.dtype)
  639 +
  640 + def assert_direct_dims(self, pos):
  641 + for access, packing in self.axes:
  642 + if access != 'direct':
  643 + error(pos, "All dimensions must be direct")
  644 + break
  645 +
607 646 def specialization_suffix(self):
608 647 return "%s_%s" % (self.axes_to_name(), self.dtype_name)
609 648

0 comments on commit 98d0d60

Please sign in to comment.
Something went wrong with that request. Please try again.