Author: Ronan Lamy <ronan.l...@gmail.com> Branch: fix-result-types Changeset: r77470:9efeedc33356 Date: 2015-05-22 03:13 +0100 http://bitbucket.org/pypy/pypy/changeset/9efeedc33356/
Log: Create scalar2dtype() to simplify find_dtype_for_scalar() diff --git a/pypy/module/micronumpy/casting.py b/pypy/module/micronumpy/casting.py --- a/pypy/module/micronumpy/casting.py +++ b/pypy/module/micronumpy/casting.py @@ -201,7 +201,7 @@ return can_cast_type(space, dtype, target, casting) def as_scalar(space, w_obj): - dtype = find_dtype_for_scalar(space, w_obj) + dtype = scalar2dtype(space, w_obj) return dtype.coerce(space, w_obj) def min_scalar_type(space, w_a): @@ -289,8 +289,7 @@ su = su1 and (su2 or not dt2.is_signed()) return _promote_types(space, dt1, dt2), su - -def find_dtype_for_scalar(space, w_obj, current_guess=None): +def scalar2dtype(space, w_obj): from .boxes import W_GenericBox bool_dtype = get_dtype_cache(space).w_booldtype long_dtype = get_dtype_cache(space).w_longdtype @@ -300,34 +299,30 @@ float_dtype = get_dtype_cache(space).w_float64dtype object_dtype = get_dtype_cache(space).w_objectdtype if isinstance(w_obj, W_GenericBox): - dtype = w_obj.get_dtype(space) - return find_binop_result_dtype(space, dtype, current_guess) + return w_obj.get_dtype(space) if space.isinstance_w(w_obj, space.w_bool): - return find_binop_result_dtype(space, bool_dtype, current_guess) + return bool_dtype elif space.isinstance_w(w_obj, space.w_int): - return find_binop_result_dtype(space, long_dtype, current_guess) + return long_dtype elif space.isinstance_w(w_obj, space.w_long): try: space.int_w(w_obj) except OperationError, e: if e.match(space, space.w_OverflowError): if space.is_true(space.le(w_obj, space.wrap(0))): - return find_binop_result_dtype(space, int64_dtype, - current_guess) - return find_binop_result_dtype(space, uint64_dtype, - current_guess) + return int64_dtype + return uint64_dtype raise - return find_binop_result_dtype(space, int64_dtype, current_guess) + return int64_dtype elif space.isinstance_w(w_obj, space.w_float): - return find_binop_result_dtype(space, float_dtype, current_guess) + return float_dtype elif space.isinstance_w(w_obj, space.w_complex): return complex_dtype elif space.isinstance_w(w_obj, space.w_str): - if current_guess is None: - return variable_dtype(space, 'S%d' % space.len_w(w_obj)) - elif current_guess.num == NPY.STRING: - if current_guess.elsize < space.len_w(w_obj): - return variable_dtype(space, 'S%d' % space.len_w(w_obj)) - return current_guess + return variable_dtype(space, 'S%d' % space.len_w(w_obj)) return object_dtype + +def find_dtype_for_scalar(space, w_obj, current_guess=None): + dtype = scalar2dtype(space, w_obj) + return find_binop_result_dtype(space, dtype, current_guess) diff --git a/pypy/module/micronumpy/descriptor.py b/pypy/module/micronumpy/descriptor.py --- a/pypy/module/micronumpy/descriptor.py +++ b/pypy/module/micronumpy/descriptor.py @@ -1021,15 +1021,14 @@ return get_dtype_cache(space).dtypes_by_num[num] def as_dtype(space, w_arg, allow_None=True): - from pypy.module.micronumpy.casting import find_dtype_for_scalar + from pypy.module.micronumpy.casting import scalar2dtype # roughly equivalent to CNumPy's PyArray_DescrConverter2 if not allow_None and space.is_none(w_arg): raise TypeError("Cannot create dtype from None here") if isinstance(w_arg, W_NDimArray): return w_arg.get_dtype() elif is_scalar_w(space, w_arg): - result = find_dtype_for_scalar(space, w_arg) - assert result is not None # XXX: not guaranteed + result = scalar2dtype(space, w_arg) return result else: return space.interp_w(W_Dtype, _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit