Author: Ronan Lamy <[email protected]>
Branch:
Changeset: r78661:07616c948f75
Date: 2015-07-25 17:37 +0100
http://bitbucket.org/pypy/pypy/changeset/07616c948f75/
Log: Create out_converter() to factor out some code
diff --git a/pypy/module/micronumpy/converters.py
b/pypy/module/micronumpy/converters.py
--- a/pypy/module/micronumpy/converters.py
+++ b/pypy/module/micronumpy/converters.py
@@ -113,3 +113,12 @@
shape.append(space.int_w(w_item))
shape += dtype.shape
return shape[:]
+
+def out_converter(space, w_out):
+ from .ndarray import W_NDimArray
+ if space.is_none(w_out):
+ return None
+ elif not isinstance(w_out, W_NDimArray):
+ raise oefmt(space.w_TypeError, 'output must be an array')
+ else:
+ return w_out
diff --git a/pypy/module/micronumpy/ndarray.py
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -15,8 +15,9 @@
from pypy.module.micronumpy.base import W_NDimArray, convert_to_array, \
ArrayArgumentException, wrap_impl
from pypy.module.micronumpy.concrete import BaseConcreteArray
-from pypy.module.micronumpy.converters import multi_axis_converter, \
- order_converter, shape_converter, searchside_converter
+from pypy.module.micronumpy.converters import (
+ multi_axis_converter, order_converter, shape_converter,
+ searchside_converter, out_converter)
from pypy.module.micronumpy.flagsobj import W_FlagsObject
from pypy.module.micronumpy.strides import (
get_shape_from_iterable, shape_agreement, shape_agreement_multiple,
@@ -1090,12 +1091,7 @@
def descr_dot(self, space, w_other, w_out=None):
from .casting import find_result_type
- if space.is_none(w_out):
- out = None
- elif not isinstance(w_out, W_NDimArray):
- raise oefmt(space.w_TypeError, 'output must be an array')
- else:
- out = w_out
+ out = out_converter(space, w_out)
other = convert_to_array(space, w_other)
if other.is_scalar():
#Note: w_out is not modified, this is numpy compliant.
@@ -1152,12 +1148,7 @@
def _reduce_ufunc_impl(ufunc_name, cumulative=False, bool_result=False):
@unwrap_spec(keepdims=bool)
def impl(self, space, w_axis=None, w_dtype=None, w_out=None,
keepdims=False):
- if space.is_none(w_out):
- out = None
- elif not isinstance(w_out, W_NDimArray):
- raise oefmt(space.w_TypeError, 'output must be an array')
- else:
- out = w_out
+ out = out_converter(space, w_out)
if bool_result:
w_dtype = descriptor.get_dtype_cache(space).w_booldtype
return getattr(ufuncs.get(space), ufunc_name).reduce(
diff --git a/pypy/module/micronumpy/ufuncs.py b/pypy/module/micronumpy/ufuncs.py
--- a/pypy/module/micronumpy/ufuncs.py
+++ b/pypy/module/micronumpy/ufuncs.py
@@ -20,6 +20,7 @@
from pypy.module.micronumpy.strides import shape_agreement
from pypy.module.micronumpy.support import (_parse_signature, product,
get_storage_as_int, is_rhs_priority_higher)
+from .converters import out_converter
from .casting import (
can_cast_type, can_cast_array, can_cast_to,
find_result_type, promote_types)
@@ -124,10 +125,7 @@
args_w, kwds_w = __args__.unpack()
# sig, extobj are used in generic ufuncs
w_subok, w_out, sig, w_casting, extobj = self.parse_kwargs(space,
kwds_w)
- if space.is_w(w_out, space.w_None):
- out = None
- else:
- out = w_out
+ out = out_converter(space, w_out)
if (w_subok is not None and space.is_true(w_subok)):
raise oefmt(space.w_NotImplementedError, "parameter subok
unsupported")
if kwds_w:
@@ -149,9 +147,6 @@
out = args_w[-1]
else:
args_w = args_w + [out]
- if out is not None and not isinstance(out, W_NDimArray):
- raise OperationError(space.w_TypeError, space.wrap(
- 'output must be an array'))
if w_casting is None:
casting = 'unsafe'
else:
@@ -163,13 +158,7 @@
def descr_accumulate(self, space, w_obj, w_axis=None, w_dtype=None,
w_out=None):
if space.is_none(w_axis):
w_axis = space.wrap(0)
- if space.is_none(w_out):
- out = None
- elif not isinstance(w_out, W_NDimArray):
- raise OperationError(space.w_TypeError, space.wrap(
- 'output must be an array'))
- else:
- out = w_out
+ out = out_converter(space, w_out)
return self.reduce(space, w_obj, w_axis, True, #keepdims must be true
out, w_dtype, cumulative=True)
@@ -231,13 +220,7 @@
from pypy.module.micronumpy.ndarray import W_NDimArray
if w_axis is None:
w_axis = space.wrap(0)
- if space.is_none(w_out):
- out = None
- elif not isinstance(w_out, W_NDimArray):
- raise OperationError(space.w_TypeError, space.wrap(
- 'output must be an array'))
- else:
- out = w_out
+ out = out_converter(space, w_out)
return self.reduce(space, w_obj, w_axis, keepdims, out, w_dtype)
def reduce(self, space, w_obj, w_axis, keepdims=False, out=None,
dtype=None,
@@ -460,11 +443,7 @@
w_obj = args_w[0]
out = None
if len(args_w) > 1:
- out = args_w[1]
- if space.is_w(out, space.w_None):
- out = None
- elif out is not None and not isinstance(out, W_NDimArray):
- raise oefmt(space.w_TypeError, 'output must be an array')
+ out = out_converter(space, args_w[1])
w_obj = numpify(space, w_obj)
dtype = w_obj.get_dtype(space)
calc_dtype, dt_out, func = self.find_specialization(space, dtype, out,
casting)
@@ -562,10 +541,7 @@
def call(self, space, args_w, sig, casting, extobj):
if len(args_w) > 2:
[w_lhs, w_rhs, out] = args_w
- if space.is_none(out):
- out = None
- elif not isinstance(out, W_NDimArray):
- raise oefmt(space.w_TypeError, 'output must be an array')
+ out = out_converter(space, out)
else:
[w_lhs, w_rhs] = args_w
out = None
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit