def can_cast(space, w_from, w_totype, casting='safe'): try: target = as_dtype(space, w_totype, allow_None=False) except TypeError: raise oefmt(space.w_TypeError, "did not understand one of the types; 'None' not accepted") if isinstance(w_from, W_NDimArray): return space.wrap(can_cast_array(space, w_from, target, casting)) elif is_scalar_w(space, w_from): w_scalar = as_scalar(space, w_from) w_arr = W_NDimArray.from_scalar(space, w_scalar) return space.wrap(can_cast_array(space, w_arr, target, casting)) try: origin = as_dtype(space, w_from, allow_None=False) except TypeError: raise oefmt(space.w_TypeError, "did not understand one of the types; 'None' not accepted") return space.wrap(can_cast_type(space, origin, target, casting))
def result_type(space, __args__): args_w, kw_w = __args__.unpack() if kw_w: raise oefmt(space.w_TypeError, "result_type() takes no keyword arguments") if not args_w: raise oefmt(space.w_ValueError, "at least one array or dtype is required") arrays_w = [] dtypes_w = [] for w_arg in args_w: if isinstance(w_arg, W_NDimArray): arrays_w.append(w_arg) elif is_scalar_w(space, w_arg): w_scalar = as_scalar(space, w_arg) w_arr = W_NDimArray.from_scalar(space, w_scalar) arrays_w.append(w_arr) else: dtype = as_dtype(space, w_arg) dtypes_w.append(dtype) return find_result_type(space, arrays_w, dtypes_w)
def can_cast(space, w_from, w_totype, casting='safe'): try: target = as_dtype(space, w_totype, allow_None=False) except TypeError: raise oefmt( space.w_TypeError, "did not understand one of the types; 'None' not accepted") if isinstance(w_from, W_NDimArray): return space.wrap(can_cast_array(space, w_from, target, casting)) elif is_scalar_w(space, w_from): w_scalar = as_scalar(space, w_from) w_arr = W_NDimArray.from_scalar(space, w_scalar) return space.wrap(can_cast_array(space, w_arr, target, casting)) try: origin = as_dtype(space, w_from, allow_None=False) except TypeError: raise oefmt( space.w_TypeError, "did not understand one of the types; 'None' not accepted") return space.wrap(can_cast_type(space, origin, target, casting))