diff --git a/numpy/__init__.cython-30.pxd b/numpy/__init__.cython-30.pxd index 2453cd27a249..b55116c654c3 100644 --- a/numpy/__init__.cython-30.pxd +++ b/numpy/__init__.cython-30.pxd @@ -82,6 +82,7 @@ cdef extern from "numpy/arrayobject.h": NPY_COMPLEX512 NPY_INTP + NPY_DEFAULT_INT # Not a compile time constant (normally)! ctypedef enum NPY_ORDER: NPY_ANYORDER @@ -743,12 +744,7 @@ ctypedef npy_float64 float64_t ctypedef float complex complex64_t ctypedef double complex complex128_t -# The int types are mapped a bit surprising -- -# numpy.int corresponds to 'l' and numpy.long to 'q' -ctypedef npy_long int_t ctypedef npy_longlong longlong_t - -ctypedef npy_ulong uint_t ctypedef npy_ulonglong ulonglong_t ctypedef npy_intp intp_t diff --git a/numpy/__init__.pxd b/numpy/__init__.pxd index da74eaaa9803..e3cf6bd4b885 100644 --- a/numpy/__init__.pxd +++ b/numpy/__init__.pxd @@ -79,6 +79,7 @@ cdef extern from "numpy/arrayobject.h": NPY_COMPLEX512 NPY_INTP + NPY_DEFAULT_INT ctypedef enum NPY_ORDER: NPY_ANYORDER @@ -701,12 +702,7 @@ ctypedef npy_float64 float64_t ctypedef float complex complex64_t ctypedef double complex complex128_t -# The int types are mapped a bit surprising -- -# numpy.int corresponds to 'l' and numpy.long to 'q' -ctypedef npy_long int_t ctypedef npy_longlong longlong_t - -ctypedef npy_ulong uint_t ctypedef npy_ulonglong ulonglong_t ctypedef npy_intp intp_t diff --git a/numpy/__init__.py b/numpy/__init__.py index efdeb6f36e9a..37493211535d 100644 --- a/numpy/__init__.py +++ b/numpy/__init__.py @@ -211,7 +211,7 @@ # probably wait for NumPy 1.26 or 2.0. # When defined, these should possibly not be added to `__all__` to avoid # import with `from numpy import *`. - __future_scalars__ = {"bool", "long", "ulong", "str", "bytes", "object"} + __future_scalars__ = {"bool", "str", "bytes", "object"} __deprecated_attrs__.update({ n: (alias, _msg.format(n=n, an=an)) for n, alias, an in _type_info}) @@ -239,11 +239,9 @@ # both `numpy.core.issubdtype` and `numpy.lib.issubdtype`. __all__.remove('issubdtype') - # These are exported by np.core, but are replaced by the builtins below - # remove them to ensure that we don't end up with `np.long == np.int_`, - # which would be a breaking change. - del long, unicode - __all__.remove('long') + # This is exported by np.core, but is not exported as it used to be the + # builtin unicode version. + del unicode __all__.remove('unicode') # Remove things that are in the numpy.lib but not in the numpy namespace diff --git a/numpy/core/_add_newdocs_scalars.py b/numpy/core/_add_newdocs_scalars.py index b505e9b268c9..42a5ca36ee9c 100644 --- a/numpy/core/_add_newdocs_scalars.py +++ b/numpy/core/_add_newdocs_scalars.py @@ -120,9 +120,12 @@ def add_newdoc_for_scalar_type(obj, fixed_aliases, doc): Signed integer type, compatible with C ``int``. """) +# TODO: These docs probably need an if to highlight the default rather than +# the C-types (and be correct). add_newdoc_for_scalar_type('int_', [], """ - Signed integer type, compatible with Python `int` and C ``long``. + Default signed integer type, 64bit on 64bit systems and 32bit on 32bit + systems. """) add_newdoc_for_scalar_type('longlong', [], @@ -147,7 +150,8 @@ def add_newdoc_for_scalar_type(obj, fixed_aliases, doc): add_newdoc_for_scalar_type('uint', [], """ - Unsigned integer type, compatible with C ``unsigned long``. + Unsigned signed integer type, 64bit on 64bit systems and 32bit on 32bit + systems. """) add_newdoc_for_scalar_type('ulonglong', [], diff --git a/numpy/core/_type_aliases.py b/numpy/core/_type_aliases.py index cef5b1d81562..83420f0195d6 100644 --- a/numpy/core/_type_aliases.py +++ b/numpy/core/_type_aliases.py @@ -155,8 +155,11 @@ def _set_up_aliases(): ('float_', 'double'), ('intc', 'int'), ('uintc', 'uint'), - ('int_', 'long'), - ('uint', 'ulong'), + ('long', 'long'), + ('ulong', 'ulong'), + # Default integer is a misnomer + ('int_', 'intp'), + ('uint', 'uintp'), ('cfloat', 'cdouble'), ('longfloat', 'longdouble'), ('clongfloat', 'clongdouble'), @@ -182,14 +185,6 @@ def _set_up_aliases(): except KeyError: pass - # Additional aliases in sctypeDict that should not be exposed as attributes - attrs_to_remove = ['ulong'] - - for t in attrs_to_remove: - try: - del allTypes[t] - except KeyError: - pass _set_up_aliases() diff --git a/numpy/core/code_generators/generate_numpy_api.py b/numpy/core/code_generators/generate_numpy_api.py index b3af9536b61c..e3446382ea92 100644 --- a/numpy/core/code_generators/generate_numpy_api.py +++ b/numpy/core/code_generators/generate_numpy_api.py @@ -26,16 +26,22 @@ #else #if defined(PY_ARRAY_UNIQUE_SYMBOL) -#define PyArray_API PY_ARRAY_UNIQUE_SYMBOL + #define PyArray_API PY_ARRAY_UNIQUE_SYMBOL + #define _NPY_VERSION_CONCAT_HELPER2(x, y) x ## y + #define _NPY_VERSION_CONCAT_HELPER(arg) _NPY_VERSION_CONCAT_HELPER2(arg, PyArray_RUNTIME_VERSION) + #define PyArray_RUNTIME_VERSION _NPY_VERSION_CONCAT_HELPER(PY_ARRAY_UNIQUE_SYMBOL) #endif #if defined(NO_IMPORT) || defined(NO_IMPORT_ARRAY) extern void **PyArray_API; +extern int PyArray_RUNTIME_VERSION; #else #if defined(PY_ARRAY_UNIQUE_SYMBOL) void **PyArray_API; +int PyArray_RUNTIME_VERSION; #else -static void **PyArray_API=NULL; +static void **PyArray_API = NULL; +static int PyArray_RUNTIME_VERSION = 0; #endif #endif @@ -82,7 +88,8 @@ (int) NPY_VERSION, (int) PyArray_GetNDArrayCVersion()); return -1; } - if (NPY_FEATURE_VERSION > PyArray_GetNDArrayCFeatureVersion()) { + PyArray_RUNTIME_VERSION = (int)PyArray_GetNDArrayCFeatureVersion(); + if (NPY_FEATURE_VERSION > PyArray_RUNTIME_VERSION) { PyErr_Format(PyExc_RuntimeError, "module compiled against "\ "API version 0x%%x but this version of numpy is 0x%%x . "\ "Check the section C-API incompatibility at the "\ @@ -90,7 +97,7 @@ "https://numpy.org/devdocs/user/troubleshooting-importerror.html"\ "#c-api-incompatibility "\ "for indications on how to solve this problem .", \ - (int) NPY_FEATURE_VERSION, (int) PyArray_GetNDArrayCFeatureVersion()); + (int)NPY_FEATURE_VERSION, PyArray_RUNTIME_VERSION); return -1; } diff --git a/numpy/core/code_generators/generate_umath.py b/numpy/core/code_generators/generate_umath.py index a170f83be622..9b0ef61e17f3 100644 --- a/numpy/core/code_generators/generate_umath.py +++ b/numpy/core/code_generators/generate_umath.py @@ -94,7 +94,8 @@ def finish_signature(self, nin, nout): def _check_order(types1, types2): - dtype_order = allP + "O" + # Insert kK (int64) after all other ints (assumes long long isn't larger) + dtype_order = bints + 'kK' + times + flts + cmplxP + "O" for t1, t2 in zip(types1, types2): # We have no opinion on object or time ordering for now: if t1 in "OP" or t2 in "OP": @@ -275,6 +276,10 @@ def english_upper(s): 'I': 'uint', 'l': 'long', 'L': 'ulong', + # We sometimes need int64, but we have no obvious char for it, use k and + # define it as `int64` below. + 'k': 'int64', + 'K': 'uint64', 'q': 'longlong', 'Q': 'ulonglong', 'e': 'half', @@ -330,13 +335,8 @@ def english_upper(s): no_bool_times_obj = ints + inexact # Find which code corresponds to int64. -int64 = '' -uint64 = '' -for code in 'bhilq': - if struct.calcsize(code) == 8: - int64 = code - uint64 = english_upper(code) - break +int64 = 'k' +uint64 = 'K' # This dictionary describes all the ufunc implementations, generating # all the function names and their corresponding ufunc signatures. TD is @@ -384,8 +384,8 @@ def english_upper(s): ('loops_arithm_fp', 'fdFD'), ('loops_autovec', ints), ]), - [TypeDescription('m', FullTypeDescr, 'mq', 'm'), - TypeDescription('m', FullTypeDescr, 'qm', 'm'), + [TypeDescription('m', FullTypeDescr, 'mk', 'm'), + TypeDescription('m', FullTypeDescr, 'km', 'm'), TypeDescription('m', FullTypeDescr, 'md', 'm'), TypeDescription('m', FullTypeDescr, 'dm', 'm'), ], @@ -400,9 +400,9 @@ def english_upper(s): TD(ints, cfunc_alias='divide', dispatch=[('loops_arithmetic', 'bBhHiIlLqQ')]), TD(flts), - [TypeDescription('m', FullTypeDescr, 'mq', 'm'), + [TypeDescription('m', FullTypeDescr, 'mk', 'm'), TypeDescription('m', FullTypeDescr, 'md', 'm'), - TypeDescription('m', FullTypeDescr, 'mm', 'q'), + TypeDescription('m', FullTypeDescr, 'mm', 'k'), ], TD(O, f='PyNumber_FloorDivide'), indexed=flts + ints @@ -412,7 +412,7 @@ def english_upper(s): docstrings.get('numpy.core.umath.divide'), 'PyUFunc_TrueDivisionTypeResolver', TD(flts+cmplx, cfunc_alias='divide', dispatch=[('loops_arithm_fp', 'fd')]), - [TypeDescription('m', FullTypeDescr, 'mq', 'm', cfunc_alias='divide'), + [TypeDescription('m', FullTypeDescr, 'mk', 'm', cfunc_alias='divide'), TypeDescription('m', FullTypeDescr, 'md', 'm', cfunc_alias='divide'), TypeDescription('m', FullTypeDescr, 'mm', 'd', cfunc_alias='divide'), ], @@ -529,8 +529,8 @@ def english_upper(s): docstrings.get('numpy.core.umath.greater'), 'PyUFunc_SimpleBinaryComparisonTypeResolver', TD(bints, out='?'), - [TypeDescription('q', FullTypeDescr, 'qQ', '?'), - TypeDescription('q', FullTypeDescr, 'Qq', '?')], + [TypeDescription(int64, FullTypeDescr, int64+uint64, '?'), + TypeDescription(int64, FullTypeDescr, uint64+int64, '?')], TD(inexact+times, out='?', dispatch=[('loops_comparison', bints+'fd')]), TD('O', out='?'), [TypeDescription('O', FullTypeDescr, 'OO', 'O')], @@ -540,8 +540,8 @@ def english_upper(s): docstrings.get('numpy.core.umath.greater_equal'), 'PyUFunc_SimpleBinaryComparisonTypeResolver', TD(bints, out='?'), - [TypeDescription('q', FullTypeDescr, 'qQ', '?'), - TypeDescription('q', FullTypeDescr, 'Qq', '?')], + [TypeDescription(int64, FullTypeDescr, int64+uint64, '?'), + TypeDescription(int64, FullTypeDescr, uint64+int64, '?')], TD(inexact+times, out='?', dispatch=[('loops_comparison', bints+'fd')]), TD('O', out='?'), [TypeDescription('O', FullTypeDescr, 'OO', 'O')], @@ -551,8 +551,8 @@ def english_upper(s): docstrings.get('numpy.core.umath.less'), 'PyUFunc_SimpleBinaryComparisonTypeResolver', TD(bints, out='?'), - [TypeDescription('q', FullTypeDescr, 'qQ', '?'), - TypeDescription('q', FullTypeDescr, 'Qq', '?')], + [TypeDescription(int64, FullTypeDescr, int64+uint64, '?'), + TypeDescription(int64, FullTypeDescr, uint64+int64, '?')], TD(inexact+times, out='?', dispatch=[('loops_comparison', bints+'fd')]), TD('O', out='?'), [TypeDescription('O', FullTypeDescr, 'OO', 'O')], @@ -562,8 +562,8 @@ def english_upper(s): docstrings.get('numpy.core.umath.less_equal'), 'PyUFunc_SimpleBinaryComparisonTypeResolver', TD(bints, out='?'), - [TypeDescription('q', FullTypeDescr, 'qQ', '?'), - TypeDescription('q', FullTypeDescr, 'Qq', '?')], + [TypeDescription(int64, FullTypeDescr, int64+uint64, '?'), + TypeDescription(int64, FullTypeDescr, uint64+int64, '?')], TD(inexact+times, out='?', dispatch=[('loops_comparison', bints+'fd')]), TD('O', out='?'), [TypeDescription('O', FullTypeDescr, 'OO', 'O')], @@ -573,8 +573,8 @@ def english_upper(s): docstrings.get('numpy.core.umath.equal'), 'PyUFunc_SimpleBinaryComparisonTypeResolver', TD(bints, out='?'), - [TypeDescription('q', FullTypeDescr, 'qQ', '?'), - TypeDescription('q', FullTypeDescr, 'Qq', '?')], + [TypeDescription(int64, FullTypeDescr, int64+uint64, '?'), + TypeDescription(int64, FullTypeDescr, uint64+int64, '?')], TD(inexact+times, out='?', dispatch=[('loops_comparison', bints+'fd')]), TD('O', out='?'), [TypeDescription('O', FullTypeDescr, 'OO', 'O')], @@ -584,8 +584,8 @@ def english_upper(s): docstrings.get('numpy.core.umath.not_equal'), 'PyUFunc_SimpleBinaryComparisonTypeResolver', TD(bints, out='?'), - [TypeDescription('q', FullTypeDescr, 'qQ', '?'), - TypeDescription('q', FullTypeDescr, 'Qq', '?')], + [TypeDescription(int64, FullTypeDescr, int64+uint64, '?'), + TypeDescription(int64, FullTypeDescr, uint64+int64, '?')], TD(inexact+times, out='?', dispatch=[('loops_comparison', bints+'fd')]), TD('O', out='?'), [TypeDescription('O', FullTypeDescr, 'OO', 'O')], @@ -1011,7 +1011,7 @@ def english_upper(s): 'PyUFunc_DivmodTypeResolver', TD(ints, dispatch=[('loops_modulo', ints)]), TD(flts), - [TypeDescription('m', FullTypeDescr, 'mm', 'qm')], + [TypeDescription('m', FullTypeDescr, 'mm', 'km')], # TD(O, f='PyNumber_Divmod'), # gh-9730 ), 'hypot': @@ -1090,12 +1090,12 @@ def english_upper(s): None, [TypeDescription('e', None, 'ei', 'e'), TypeDescription('f', None, 'fi', 'f', dispatch='loops_exponent_log'), - TypeDescription('e', FuncNameSuffix('long'), 'el', 'e'), - TypeDescription('f', FuncNameSuffix('long'), 'fl', 'f'), + TypeDescription('e', FuncNameSuffix('int64'), 'e'+int64, 'e'), + TypeDescription('f', FuncNameSuffix('int64'), 'f'+int64, 'f'), TypeDescription('d', None, 'di', 'd', dispatch='loops_exponent_log'), - TypeDescription('d', FuncNameSuffix('long'), 'dl', 'd'), + TypeDescription('d', FuncNameSuffix('int64'), 'd'+int64, 'd'), TypeDescription('g', None, 'gi', 'g'), - TypeDescription('g', FuncNameSuffix('long'), 'gl', 'g'), + TypeDescription('g', FuncNameSuffix('int64'), 'g'+int64, 'g'), ], ), 'frexp' : diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index 27b18e263b26..1160cc6f21a4 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -1458,6 +1458,13 @@ PyArrayNeighborhoodIter_Next2D(PyArrayNeighborhoodIterObject* iter); /* The default array type */ #define NPY_DEFAULT_TYPE NPY_DOUBLE +/* default integer type */ +#if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION + #define NPY_DEFAULT_INT NPY_INTP +#else + #define NPY_DEFAULT_INT \ + (PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION ? NPY_INTP : NPY_LONG) +#endif /* * All sorts of useful ways to look into a PyArrayObject. It is recommended diff --git a/numpy/core/include/numpy/npy_common.h b/numpy/core/include/numpy/npy_common.h index fb976aa6ae09..74c2101d9010 100644 --- a/numpy/core/include/numpy/npy_common.h +++ b/numpy/core/include/numpy/npy_common.h @@ -225,16 +225,7 @@ typedef Py_uintptr_t npy_uintp; * functions use different formatting codes that are portably specified * according to the Python documentation. See issue gh-2388. */ -#if NPY_SIZEOF_PY_INTPTR_T == NPY_SIZEOF_INT - #define NPY_INTP NPY_INT - #define NPY_UINTP NPY_UINT - #define PyIntpArrType_Type PyIntArrType_Type - #define PyUIntpArrType_Type PyUIntArrType_Type - #define NPY_MAX_INTP NPY_MAX_INT - #define NPY_MIN_INTP NPY_MIN_INT - #define NPY_MAX_UINTP NPY_MAX_UINT - #define NPY_INTP_FMT "d" -#elif NPY_SIZEOF_PY_INTPTR_T == NPY_SIZEOF_LONG +#if NPY_SIZEOF_PY_INTPTR_T == NPY_SIZEOF_LONG #define NPY_INTP NPY_LONG #define NPY_UINTP NPY_ULONG #define PyIntpArrType_Type PyLongArrType_Type @@ -243,6 +234,16 @@ typedef Py_uintptr_t npy_uintp; #define NPY_MIN_INTP NPY_MIN_LONG #define NPY_MAX_UINTP NPY_MAX_ULONG #define NPY_INTP_FMT "ld" +#elif NPY_SIZEOF_PY_INTPTR_T == NPY_SIZEOF_INT + /* NumPy always prefers long over int if the size is identical! */ + #define NPY_INTP NPY_INT + #define NPY_UINTP NPY_UINT + #define PyIntpArrType_Type PyIntArrType_Type + #define PyUIntpArrType_Type PyUIntArrType_Type + #define NPY_MAX_INTP NPY_MAX_INT + #define NPY_MIN_INTP NPY_MIN_INT + #define NPY_MAX_UINTP NPY_MAX_UINT + #define NPY_INTP_FMT "d" #elif defined(PY_LONG_LONG) && (NPY_SIZEOF_PY_INTPTR_T == NPY_SIZEOF_LONGLONG) #define NPY_INTP NPY_LONGLONG #define NPY_UINTP NPY_ULONGLONG diff --git a/numpy/core/src/multiarray/abstractdtypes.c b/numpy/core/src/multiarray/abstractdtypes.c index da44fd01f11f..5a707ac82bb9 100644 --- a/numpy/core/src/multiarray/abstractdtypes.c +++ b/numpy/core/src/multiarray/abstractdtypes.c @@ -16,7 +16,7 @@ static inline PyArray_Descr * int_default_descriptor(PyArray_DTypeMeta* NPY_UNUSED(cls)) { - return PyArray_DescrFromType(NPY_LONG); + return PyArray_DescrFromType(NPY_INTP); } static PyArray_Descr * @@ -33,8 +33,8 @@ discover_descriptor_from_pyint( PyErr_Clear(); } else { - if (NPY_MIN_LONG <= value && value <= NPY_MAX_LONG) { - return PyArray_DescrFromType(NPY_LONG); + if (NPY_MIN_INTP <= value && value <= NPY_MAX_INTP) { + return PyArray_DescrFromType(NPY_INTP); } return PyArray_DescrFromType(NPY_LONGLONG); } @@ -153,7 +153,7 @@ int_common_dtype(PyArray_DTypeMeta *NPY_UNUSED(cls), PyArray_DTypeMeta *other) if (NPY_DT_is_legacy(other) && other->type_num < NPY_NTYPES) { if (other->type_num == NPY_BOOL) { /* Use the default integer for bools: */ - return PyArray_DTypeFromTypeNum(NPY_LONG); + return PyArray_DTypeFromTypeNum(NPY_INTP); } else if (PyTypeNum_ISNUMBER(other->type_num) || other->type_num == NPY_TIMEDELTA) { @@ -190,7 +190,7 @@ int_common_dtype(PyArray_DTypeMeta *NPY_UNUSED(cls), PyArray_DTypeMeta *other) return res; } /* And finally, we will try the default integer, just for sports... */ - PyArray_DTypeMeta *default_int = PyArray_DTypeFromTypeNum(NPY_LONG); + PyArray_DTypeMeta *default_int = PyArray_DTypeFromTypeNum(NPY_INTP); res = NPY_DT_CALL_common_dtype(other, default_int); Py_DECREF(default_int); if (res == NULL) { diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src index 10cdc1394e7a..be3244453bbd 100644 --- a/numpy/core/src/multiarray/arraytypes.c.src +++ b/numpy/core/src/multiarray/arraytypes.c.src @@ -4608,8 +4608,8 @@ set_typeinfo(PyObject *dict) /**begin repeat * * #NAME = BOOL, - * BYTE, UBYTE, SHORT, USHORT, INT, UINT, - * LONG, ULONG, LONGLONG, ULONGLONG, + * INT8, UINT8, INT16, UINT16, + * INT32, UINT32, INT64, UINT64, * HALF, FLOAT, DOUBLE, LONGDOUBLE, * CFLOAT, CDOUBLE, CLONGDOUBLE, * OBJECT, STRING, UNICODE, VOID, @@ -4617,16 +4617,19 @@ set_typeinfo(PyObject *dict) */ if (dtypemeta_wrap_legacy_descriptor( _builtin_descrs[NPY_@NAME@], - "numpy.dtypes." NPY_@NAME@_Name "DType", -#ifdef NPY_@NAME@_alias - "numpy.dtypes." NPY_@NAME@_Alias "DType" -#else - NULL -#endif - ) < 0) { + "numpy.dtypes." NPY_@NAME@_Name "DType") < 0) { + return -1; + } + /**end repeat**/ + /**begin repeat + * #NAME = BYTE, UBYTE, SHORT, USHORT, INT, UINT, + * LONG, ULONG, LONGLONG, ULONGLONG# + */ + if (make_dtypemeta_integer_alias( + _builtin_descrs[NPY_@NAME@], + NPY_@NAME@, NPY_@NAME@_BASE, NPY_@NAME@_Alias) < 0) { return -1; } - /**end repeat**/ /* diff --git a/numpy/core/src/multiarray/arraytypes.h.src b/numpy/core/src/multiarray/arraytypes.h.src index 90a075dad4f7..3cba0ed72143 100644 --- a/numpy/core/src/multiarray/arraytypes.h.src +++ b/numpy/core/src/multiarray/arraytypes.h.src @@ -73,16 +73,19 @@ NPY_CPU_DISPATCH_DECLARE(NPY_NO_EXPORT int BOOL_argmax, /**begin repeat * #NAME = BOOL, + * INT8, UINT8, INT16, UINT16, INT32, UINT32, INT64, UINT64, * HALF, FLOAT, DOUBLE, LONGDOUBLE, * CFLOAT, CDOUBLE, CLONGDOUBLE, * STRING, UNICODE, VOID, OBJECT, * DATETIME, TIMEDELTA# * #name = bool_, + * int8, uint8, int16, uint16, int32, uint32, int64, uint64, * float16, float32, float64, longdouble, * complex64, complex128, clongdouble, * bytes_, str_, void, object_, * datetime64, timedelta64# * #Name = Bool, + * Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, * Float16, Float32, Float64, LongDouble, * Complex64, Complex128, CLongDouble, * Bytes, Str, Void, Object, @@ -123,38 +126,50 @@ NPY_CPU_DISPATCH_DECLARE(NPY_NO_EXPORT int BOOL_argmax, * #NAME = BYTE, SHORT, INT, LONG, LONGLONG, * UBYTE, USHORT, UINT, ULONG, ULONGLONG# * #CNAME = (BYTE, SHORT, INT, LONG, LONGLONG)*2# - * #cname = byte, short, intc, int_, longlong, - * ubyte, ushort, uintc, uint, ulonglong# + * #cname = byte, short, intc, long, longlong, + * ubyte, ushort, uintc, ulong, ulonglong# * #CName = Byte, Short, Int, Long, LongLong, * UByte, UShort, UInt, ULong, ULongLong# * #bitname = int*5, uint*5# * #BitName = Int*5, UInt*5# + * #BITNAME = INT*5, UINT*5# */ + +/* The bitsof macro includes math, so cannot be stringified */ +#if NPY_BITSOF_@CNAME@ == 8 + #define NPY_@NAME@_BASE NPY_@BITNAME@8 + #define NPY_@NAME@_name "@bitname@8" + #define NPY_@NAME@_Name "@BitName@8" +#elif NPY_BITSOF_@CNAME@ == 16 + #define NPY_@NAME@_BASE NPY_@BITNAME@16 + #define NPY_@NAME@_name "@bitname@16" + #define NPY_@NAME@_Name "@BitName@16" +#elif NPY_BITSOF_@CNAME@ == 32 + #define NPY_@NAME@_BASE NPY_@BITNAME@32 + #define NPY_@NAME@_name "@bitname@32" + #define NPY_@NAME@_Name "@BitName@32" +#elif NPY_BITSOF_@CNAME@ == 64 + #define NPY_@NAME@_BASE NPY_@BITNAME@64 + #define NPY_@NAME@_name "@bitname@64" + #define NPY_@NAME@_Name "@BitName@64" +#else + #error "need to fix integer bit-length name code" +#endif + + #ifdef @CNAME@_not_size_named + /* not the main bit-sized integer, so name is also the C name alias */ + #undef NPY_@NAME@_name + #undef NPY_@NAME@_Name #define NPY_@NAME@_name "@cname@" #define NPY_@NAME@_Name "@CName@" + #define NPY_@NAME@_alias "@cname@" + #define NPY_@NAME@_Alias "@CName@" #else /* The C-name is considered just an alias for these: */ #define NPY_@NAME@_alias "@cname@" #define NPY_@NAME@_Alias "@CName@" - - /* The bitsof macro includes math, so cannot be stringified */ - #if NPY_BITSOF_@CNAME@ == 8 - #define NPY_@NAME@_name "@bitname@8" - #define NPY_@NAME@_Name "@BitName@8" - #elif NPY_BITSOF_@CNAME@ == 16 - #define NPY_@NAME@_name "@bitname@16" - #define NPY_@NAME@_Name "@BitName@16" - #elif NPY_BITSOF_@CNAME@ == 32 - #define NPY_@NAME@_name "@bitname@32" - #define NPY_@NAME@_Name "@BitName@32" - #elif NPY_BITSOF_@CNAME@ == 64 - #define NPY_@NAME@_name "@bitname@64" - #define NPY_@NAME@_Name "@BitName@64" - #else - #error "need to fix integer bit-length name code" - #endif #endif /**end repeat**/ diff --git a/numpy/core/src/multiarray/convert_datatype.c b/numpy/core/src/multiarray/convert_datatype.c index 4311139f2b43..b012256c0eee 100644 --- a/numpy/core/src/multiarray/convert_datatype.c +++ b/numpy/core/src/multiarray/convert_datatype.c @@ -2733,19 +2733,30 @@ PyArray_InitializeNumericCasts(void) continue; } PyArray_DTypeMeta *from_dt = PyArray_DTypeFromTypeNum(from); + if (from_dt->type_num != from) { + Py_DECREF(from_dt); /* This is just an integer alias */ + continue; + } for (int to = 0; to < NPY_NTYPES; to++) { if (!PyTypeNum_ISNUMBER(to) && to != NPY_BOOL) { continue; } PyArray_DTypeMeta *to_dt = PyArray_DTypeFromTypeNum(to); + if (to_dt->type_num != to) { + Py_DECREF(to_dt); /* This is just an integer alias */ + continue; + } + int res = add_numeric_cast(from_dt, to_dt); Py_DECREF(to_dt); if (res < 0) { + printf("failed for %d, %d\n", from, to); Py_DECREF(from_dt); return -1; } } + Py_DECREF(from_dt); } return 0; } @@ -3004,6 +3015,10 @@ PyArray_InitializeStringCasts(void) continue; } other_dt = PyArray_DTypeFromTypeNum(other); + if (other_dt->type_num != other) { + Py_CLEAR(other_dt); + continue; + } /* The functions skip string == other_dt or unicode == other_dt */ if (add_other_to_and_from_string_cast(string, other_dt) < 0) { diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 29b5f9891382..219a617a092e 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -3209,7 +3209,7 @@ PyArray_ArangeObj(PyObject *start, PyObject *stop, PyObject *step, PyArray_Descr if (!dtype) { /* intentionally made to be at least NPY_LONG */ - dtype = PyArray_DescrFromType(NPY_LONG); + dtype = PyArray_DescrFromType(NPY_INTP); Py_SETREF(dtype, PyArray_DescrFromObject(start, dtype)); if (dtype == NULL) { goto fail; diff --git a/numpy/core/src/multiarray/datetime.c b/numpy/core/src/multiarray/datetime.c index 9cd67c0971e0..cc389fa577c1 100644 --- a/numpy/core/src/multiarray/datetime.c +++ b/numpy/core/src/multiarray/datetime.c @@ -4166,6 +4166,9 @@ PyArray_InitializeDatetimeCasts() } Py_XSETREF(tmp, PyArray_DTypeFromTypeNum(num)); + if (tmp->type_num != num) { + continue; /* integer type alias */ + } if (PyArray_AddLegacyWrapping_CastingImpl( tmp, datetime, NPY_UNSAFE_CASTING) < 0) { diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c index 36a95f8138b8..18f6c2023b72 100644 --- a/numpy/core/src/multiarray/descriptor.c +++ b/numpy/core/src/multiarray/descriptor.c @@ -1537,7 +1537,7 @@ _convert_from_type(PyObject *obj) { return PyArray_DescrFromTypeObject(obj); } else if (typ == &PyLong_Type) { - return PyArray_DescrFromType(NPY_LONG); + return PyArray_DescrFromType(NPY_INTP); } else if (typ == &PyFloat_Type) { return PyArray_DescrFromType(NPY_DOUBLE); diff --git a/numpy/core/src/multiarray/dtypemeta.c b/numpy/core/src/multiarray/dtypemeta.c index eef8d2f996e6..1147541a08ad 100644 --- a/numpy/core/src/multiarray/dtypemeta.c +++ b/numpy/core/src/multiarray/dtypemeta.c @@ -738,8 +738,7 @@ object_common_dtype( * @returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -dtypemeta_wrap_legacy_descriptor(PyArray_Descr *descr, - const char *name, const char *alias) +dtypemeta_wrap_legacy_descriptor(PyArray_Descr *descr, const char *name) { int has_type_set = Py_TYPE(descr) == &PyArrayDescr_Type; @@ -913,7 +912,7 @@ dtypemeta_wrap_legacy_descriptor(PyArray_Descr *descr, if (PyObject_CallFunction( add_dtype_helper, - "Os", (PyObject *)dtype_class, alias) == NULL) { + "Os", (PyObject *)dtype_class, NULL) == NULL) { return -1; } } @@ -921,6 +920,29 @@ dtypemeta_wrap_legacy_descriptor(PyArray_Descr *descr, return 0; } +NPY_NO_EXPORT int +make_dtypemeta_integer_alias( + PyArray_Descr *descr, int dtype, int base, char *name) +{ + PyArray_DTypeMeta *dtype_class = PyArray_DTypeFromTypeNum(base); + + Py_SET_TYPE(descr, (PyTypeObject *)dtype_class); + /* And it to the types submodule if it is a builtin dtype */ + static PyObject *add_dtype_helper = NULL; + npy_cache_import("numpy.dtypes", "_add_dtype_helper", &add_dtype_helper); + if (add_dtype_helper == NULL) { + return -1; + } + + /* Sets the name multiple times, but that shouldn't matter. */ + if (PyObject_CallFunction( + add_dtype_helper, + "Os", (PyObject *)dtype_class, name) == NULL) { + return -1; + } + return 0; +} + static PyObject * dtypemeta_get_abstract(PyArray_DTypeMeta *self) { diff --git a/numpy/core/src/multiarray/dtypemeta.h b/numpy/core/src/multiarray/dtypemeta.h index 6c641a3bd138..fe6774d1c59c 100644 --- a/numpy/core/src/multiarray/dtypemeta.h +++ b/numpy/core/src/multiarray/dtypemeta.h @@ -140,7 +140,11 @@ python_builtins_are_known_scalar_types( NPY_NO_EXPORT int dtypemeta_wrap_legacy_descriptor( - PyArray_Descr *dtypem, const char *name, const char *alias); + PyArray_Descr *dtypem, const char *name); + +NPY_NO_EXPORT int +make_dtypemeta_integer_alias( + PyArray_Descr *descr, int dtype, int base, char *name); #ifdef __cplusplus } diff --git a/numpy/core/src/multiarray/usertypes.c b/numpy/core/src/multiarray/usertypes.c index 5e36c914b220..32bce0b0c1be 100644 --- a/numpy/core/src/multiarray/usertypes.c +++ b/numpy/core/src/multiarray/usertypes.c @@ -294,7 +294,7 @@ PyArray_RegisterDataType(PyArray_Descr *descr) userdescrs[NPY_NUMUSERTYPES++] = descr; descr->type_num = typenum; - if (dtypemeta_wrap_legacy_descriptor(descr, name, NULL) < 0) { + if (dtypemeta_wrap_legacy_descriptor(descr, name) < 0) { descr->type_num = -1; NPY_NUMUSERTYPES--; PyMem_Free(name); /* free the name only on failure */ diff --git a/numpy/core/src/umath/_operand_flag_tests.c b/numpy/core/src/umath/_operand_flag_tests.c index c59e13baf6b1..a674dfa560b7 100644 --- a/numpy/core/src/umath/_operand_flag_tests.c +++ b/numpy/core/src/umath/_operand_flag_tests.c @@ -25,7 +25,7 @@ inplace_add(char **args, npy_intp const *dimensions, npy_intp const *steps, void npy_intp in2_step = steps[1]; for (i = 0; i < n; i++) { - (*(long *)in1) = *(long*)in1 + *(long*)in2; + (*(npy_intp *)in1) = *(npy_intp*)in1 + *(npy_intp*)in2; in1 += in1_step; in2 += in2_step; } @@ -36,7 +36,7 @@ inplace_add(char **args, npy_intp const *dimensions, npy_intp const *steps, void PyUFuncGenericFunction funcs[1] = {&inplace_add}; /* These are the input and return dtypes of logit.*/ -static char types[2] = {NPY_LONG, NPY_LONG}; +static char types[2] = {NPY_INTP, NPY_INTP}; static void *data[1] = {NULL}; diff --git a/numpy/core/src/umath/_umath_tests.c.src b/numpy/core/src/umath/_umath_tests.c.src index b427991e5463..f3d180f524ad 100644 --- a/numpy/core/src/umath/_umath_tests.c.src +++ b/numpy/core/src/umath/_umath_tests.c.src @@ -84,8 +84,8 @@ char *inner1d_signature = "(i),(i)->()"; /**begin repeat - #TYPE=LONG,DOUBLE# - #typ=npy_long,npy_double# + #TYPE=INTP,DOUBLE# + #typ=npy_intp,npy_double# */ /* @@ -118,8 +118,8 @@ char *innerwt_signature = "(i),(i),(i)->()"; /**begin repeat - #TYPE=LONG,DOUBLE# - #typ=npy_long,npy_double# + #TYPE=INTP,DOUBLE# + #typ=npy_intp,npy_double# */ @@ -156,8 +156,8 @@ char *matmul_signature = "(m?,n),(n,p?)->(m?,p?)"; /**begin repeat - #TYPE=FLOAT,DOUBLE,LONG# - #typ=npy_float,npy_double,npy_long# + #TYPE=FLOAT,DOUBLE,INTP# + #typ=npy_float,npy_double,npy_intp# */ /* @@ -224,8 +224,8 @@ char *cross1d_signature = "(3),(3)->(3)"; /**begin repeat - #TYPE=LONG,DOUBLE# - #typ=npy_long, npy_double# + #TYPE=INTP,DOUBLE# + #typ=npy_intp, npy_double# */ /* @@ -326,8 +326,8 @@ char *cumsum_signature = "(i)->(i)"; /**begin repeat - #TYPE=LONG,DOUBLE# - #typ=npy_long,npy_double# + #TYPE=INTP,DOUBLE# + #typ=npy_intp,npy_double# */ static void @@ -417,27 +417,27 @@ defdict = { static PyUFuncGenericFunction always_error_functions[] = { always_error_loop }; static void *always_error_data[] = { (void *)NULL }; static char always_error_signatures[] = { NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE }; -static PyUFuncGenericFunction inner1d_functions[] = { LONG_inner1d, DOUBLE_inner1d }; +static PyUFuncGenericFunction inner1d_functions[] = { INTP_inner1d, DOUBLE_inner1d }; static void *inner1d_data[] = { (void *)NULL, (void *)NULL }; -static char inner1d_signatures[] = { NPY_LONG, NPY_LONG, NPY_LONG, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE }; -static PyUFuncGenericFunction innerwt_functions[] = { LONG_innerwt, DOUBLE_innerwt }; +static char inner1d_signatures[] = { NPY_INTP, NPY_INTP, NPY_INTP, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE }; +static PyUFuncGenericFunction innerwt_functions[] = { INTP_innerwt, DOUBLE_innerwt }; static void *innerwt_data[] = { (void *)NULL, (void *)NULL }; -static char innerwt_signatures[] = { NPY_LONG, NPY_LONG, NPY_LONG, NPY_LONG, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE }; -static PyUFuncGenericFunction matrix_multiply_functions[] = { LONG_matrix_multiply, FLOAT_matrix_multiply, DOUBLE_matrix_multiply }; +static char innerwt_signatures[] = { NPY_INTP, NPY_INTP, NPY_INTP, NPY_INTP, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE }; +static PyUFuncGenericFunction matrix_multiply_functions[] = { INTP_matrix_multiply, FLOAT_matrix_multiply, DOUBLE_matrix_multiply }; static void *matrix_multiply_data[] = { (void *)NULL, (void *)NULL, (void *)NULL }; -static char matrix_multiply_signatures[] = { NPY_LONG, NPY_LONG, NPY_LONG, NPY_FLOAT, NPY_FLOAT, NPY_FLOAT, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE }; -static PyUFuncGenericFunction cross1d_functions[] = { LONG_cross1d, DOUBLE_cross1d }; +static char matrix_multiply_signatures[] = { NPY_INTP, NPY_INTP, NPY_INTP, NPY_FLOAT, NPY_FLOAT, NPY_FLOAT, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE }; +static PyUFuncGenericFunction cross1d_functions[] = { INTP_cross1d, DOUBLE_cross1d }; static void *cross1d_data[] = { (void *)NULL, (void *)NULL }; -static char cross1d_signatures[] = { NPY_LONG, NPY_LONG, NPY_LONG, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE }; +static char cross1d_signatures[] = { NPY_INTP, NPY_INTP, NPY_INTP, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE }; static PyUFuncGenericFunction euclidean_pdist_functions[] = { FLOAT_euclidean_pdist, DOUBLE_euclidean_pdist }; static void *eucldiean_pdist_data[] = { (void *)NULL, (void *)NULL }; static char euclidean_pdist_signatures[] = { NPY_FLOAT, NPY_FLOAT, NPY_DOUBLE, NPY_DOUBLE }; -static PyUFuncGenericFunction cumsum_functions[] = { LONG_cumsum, DOUBLE_cumsum }; +static PyUFuncGenericFunction cumsum_functions[] = { INTP_cumsum, DOUBLE_cumsum }; static void *cumsum_data[] = { (void *)NULL, (void *)NULL }; -static char cumsum_signatures[] = { NPY_LONG, NPY_LONG, NPY_DOUBLE, NPY_DOUBLE }; +static char cumsum_signatures[] = { NPY_INTP, NPY_INTP, NPY_DOUBLE, NPY_DOUBLE }; static int diff --git a/numpy/core/src/umath/loops.c.src b/numpy/core/src/umath/loops.c.src index 97a74b4257aa..dcdec4cdb1d4 100644 --- a/numpy/core/src/umath/loops.c.src +++ b/numpy/core/src/umath/loops.c.src @@ -556,7 +556,7 @@ NPY_NO_EXPORT void * #OP = ==, !=, <, <=, >, >=# */ NPY_NO_EXPORT void -LONGLONG_Qq_bool_@kind@(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) +INT64_Kk_bool_@kind@(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { BINARY_LOOP { const npy_ulonglong in1 = *(npy_ulonglong *)ip1; @@ -571,7 +571,7 @@ LONGLONG_Qq_bool_@kind@(char **args, npy_intp const *dimensions, npy_intp const } NPY_NO_EXPORT void -LONGLONG_qQ_bool_@kind@(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) +INT64_kK_bool_@kind@(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { BINARY_LOOP { const npy_longlong in1 = *(npy_longlong *)ip1; @@ -841,7 +841,7 @@ TIMEDELTA_mm_m_subtract(char **args, npy_intp const *dimensions, npy_intp const /* Note: Assuming 'q' == NPY_LONGLONG */ NPY_NO_EXPORT void -TIMEDELTA_mq_m_multiply(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) +TIMEDELTA_mk_m_multiply(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { BINARY_LOOP { const npy_timedelta in1 = *(npy_timedelta *)ip1; @@ -857,7 +857,7 @@ TIMEDELTA_mq_m_multiply(char **args, npy_intp const *dimensions, npy_intp const /* Note: Assuming 'q' == NPY_LONGLONG */ NPY_NO_EXPORT void -TIMEDELTA_qm_m_multiply(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) +TIMEDELTA_km_m_multiply(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { BINARY_LOOP { const npy_int64 in1 = *(npy_int64 *)ip1; @@ -915,7 +915,7 @@ TIMEDELTA_dm_m_multiply(char **args, npy_intp const *dimensions, npy_intp const /* Note: Assuming 'q' == NPY_LONGLONG */ NPY_NO_EXPORT void -TIMEDELTA_mq_m_divide(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) +TIMEDELTA_mk_m_divide(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { /* NOTE: This code is similar to array floor divide */ BINARY_DEFS @@ -1028,7 +1028,7 @@ TIMEDELTA_mm_m_remainder(char **args, npy_intp const *dimensions, npy_intp const } NPY_NO_EXPORT void -TIMEDELTA_mm_q_floor_divide(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) +TIMEDELTA_mm_k_floor_divide(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { /* NOTE: This code is similar to array floor divide */ BINARY_DEFS @@ -1099,7 +1099,7 @@ TIMEDELTA_mm_q_floor_divide(char **args, npy_intp const *dimensions, npy_intp co } NPY_NO_EXPORT void -TIMEDELTA_mm_qm_divmod(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) +TIMEDELTA_mm_km_divmod(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { BINARY_LOOP_TWO_OUT { const npy_timedelta in1 = *(npy_timedelta *)ip1; @@ -1321,16 +1321,16 @@ NPY_NO_EXPORT void } NPY_NO_EXPORT void -@TYPE@_ldexp_long(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) +@TYPE@_ldexp_int64(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { /* * Additional loop to handle npy_long integer inputs (cf. #866, #1633). * npy_long != npy_int on many 64-bit platforms, so we need this second loop - * to handle the default integer type. + * to handle the default (and larger) integer types. */ BINARY_LOOP { const @type@ in1 = *(@type@ *)ip1; - const long in2 = *(long *)ip2; + const npy_int64 in2 = *(npy_int64 *)ip2; if (((int)in2) == in2) { /* Range OK */ *((@type@ *)op1) = npy_ldexp@c@(in1, ((int)in2)); @@ -1853,7 +1853,7 @@ HALF_ldexp(char **args, npy_intp const *dimensions, npy_intp const *steps, void } NPY_NO_EXPORT void -HALF_ldexp_long(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) +HALF_ldexp_int64(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { /* * Additional loop to handle npy_long integer inputs (cf. #866, #1633). @@ -1862,7 +1862,7 @@ HALF_ldexp_long(char **args, npy_intp const *dimensions, npy_intp const *steps, */ BINARY_LOOP { const float in1 = npy_half_to_float(*(npy_half *)ip1); - const long in2 = *(long *)ip2; + const npy_int64 in2 = *(npy_int64 *)ip2; if (((int)in2) == in2) { /* Range OK */ *((npy_half *)op1) = npy_float_to_half(npy_ldexpf(in1, ((int)in2))); diff --git a/numpy/core/src/umath/loops.h.src b/numpy/core/src/umath/loops.h.src index cce73aff8504..08ce179487f3 100644 --- a/numpy/core/src/umath/loops.h.src +++ b/numpy/core/src/umath/loops.h.src @@ -216,9 +216,9 @@ NPY_NO_EXPORT void * #OP = ==, !=, <, <=, >, >=# */ NPY_NO_EXPORT void -LONGLONG_Qq_bool_@kind@(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +INT64_Kk_bool_@kind@(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); NPY_NO_EXPORT void -LONGLONG_qQ_bool_@kind@(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +INT64_kK_bool_@kind@(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); /**end repeat**/ @@ -525,7 +525,7 @@ NPY_NO_EXPORT void @TYPE@_ldexp(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); NPY_NO_EXPORT void -@TYPE@_ldexp_long(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +@TYPE@_ldexp_int64(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); /**end repeat**/ /**begin repeat @@ -759,10 +759,10 @@ NPY_NO_EXPORT void TIMEDELTA_mm_m_subtract(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); NPY_NO_EXPORT void -TIMEDELTA_mq_m_multiply(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +TIMEDELTA_mk_m_multiply(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); NPY_NO_EXPORT void -TIMEDELTA_qm_m_multiply(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +TIMEDELTA_km_m_multiply(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); NPY_NO_EXPORT void TIMEDELTA_md_m_multiply(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); @@ -771,7 +771,7 @@ NPY_NO_EXPORT void TIMEDELTA_dm_m_multiply(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); NPY_NO_EXPORT void -TIMEDELTA_mq_m_divide(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +TIMEDELTA_mk_m_divide(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); NPY_NO_EXPORT void TIMEDELTA_md_m_divide(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); @@ -780,16 +780,16 @@ NPY_NO_EXPORT void TIMEDELTA_mm_d_divide(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); NPY_NO_EXPORT void -TIMEDELTA_mm_q_floor_divide(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +TIMEDELTA_mm_k_floor_divide(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); NPY_NO_EXPORT void TIMEDELTA_mm_m_remainder(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); NPY_NO_EXPORT void -TIMEDELTA_mm_qm_divmod(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +TIMEDELTA_mm_km_divmod(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); /* Special case equivalents to above functions */ -#define TIMEDELTA_mq_m_floor_divide TIMEDELTA_mq_m_divide +#define TIMEDELTA_mk_m_floor_divide TIMEDELTA_mk_m_divide #define TIMEDELTA_md_m_floor_divide TIMEDELTA_md_m_divide /* #define TIMEDELTA_mm_d_floor_divide TIMEDELTA_mm_d_divide */ diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 5b75b3bfcdfa..d234c6449dd0 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -2738,14 +2738,14 @@ reducelike_promote_and_resolve(PyUFuncObject *ufunc, && ((strcmp(ufunc->name, "add") == 0) || (strcmp(ufunc->name, "multiply") == 0))) { if (PyTypeNum_ISBOOL(typenum)) { - typenum = NPY_LONG; + typenum = NPY_INTP; } - else if ((size_t)PyArray_DESCR(arr)->elsize < sizeof(long)) { + else if ((size_t)PyArray_DESCR(arr)->elsize < sizeof(npy_intp)) { if (PyTypeNum_ISUNSIGNED(typenum)) { - typenum = NPY_ULONG; + typenum = NPY_UINTP; } else { - typenum = NPY_LONG; + typenum = NPY_INTP; } } signature[0] = PyArray_DTypeFromTypeNum(typenum); @@ -6512,7 +6512,7 @@ py_resolve_dtypes_generic(PyUFuncObject *ufunc, npy_bool return_context, } /* Explicitly allow int, float, and complex for the "weak" types. */ else if (descr_obj == (PyObject *)&PyLong_Type) { - descr = PyArray_DescrFromType(NPY_LONG); + descr = PyArray_DescrFromType(NPY_INTP); dummy_arrays[i] = (PyArrayObject *)PyArray_Empty(0, NULL, descr, 0); if (dummy_arrays[i] == NULL) { goto finish; diff --git a/numpy/core/src/umath/ufunc_type_resolution.c b/numpy/core/src/umath/ufunc_type_resolution.c index 4eea566842db..11640b5d826e 100644 --- a/numpy/core/src/umath/ufunc_type_resolution.c +++ b/numpy/core/src/umath/ufunc_type_resolution.c @@ -1441,7 +1441,7 @@ find_userloop(PyUFuncObject *ufunc, break; } - type_num = dtypes[i]->type_num; + type_num = NPY_DTYPE(dtypes[i])->type_num; if (type_num != last_userdef && (PyTypeNum_ISUSERDEF(type_num) || type_num == NPY_VOID)) { PyObject *key, *obj; @@ -1468,7 +1468,7 @@ find_userloop(PyUFuncObject *ufunc, int *types = funcdata->arg_types; for (j = 0; j < nargs; ++j) { - if (types[j] != dtypes[j]->type_num) { + if (types[j] != NPY_DTYPE(dtypes[j])->type_num) { break; } } @@ -1518,7 +1518,7 @@ PyUFunc_DefaultLegacyInnerLoopSelector(PyUFuncObject *ufunc, for (i = 0; i < ufunc->ntypes; ++i) { /* Copy the types into an int array for matching */ for (j = 0; j < nargs; ++j) { - if (types[j] != dtypes[j]->type_num) { + if (types[j] != NPY_DTYPE(dtypes[j])->type_num) { break; } } diff --git a/numpy/core/tests/examples/cython/checks.pyx b/numpy/core/tests/examples/cython/checks.pyx index ba32ef21320a..d4d464ddcbaf 100644 --- a/numpy/core/tests/examples/cython/checks.pyx +++ b/numpy/core/tests/examples/cython/checks.pyx @@ -77,3 +77,11 @@ def make_iso_8601_datetime(dt: "datetime"): cnp.NPY_NO_CASTING, ) return result + + +def get_default_integer(): + if cnp.NPY_DEFAULT_INT == cnp.NPY_LONG: + return cnp.dtype("long") + if cnp.NPY_DEFAULT_INT == cnp.NPY_INTP: + return cnp.dtype("intp") + return None \ No newline at end of file diff --git a/numpy/core/tests/test_cython.py b/numpy/core/tests/test_cython.py index 73e1b7a7bc36..fa6d70ce2213 100644 --- a/numpy/core/tests/test_cython.py +++ b/numpy/core/tests/test_cython.py @@ -138,6 +138,10 @@ def test_abstract_scalars(install_temp): assert checks.is_integer(np.int8(1)) assert checks.is_integer(np.uint64(1)) +def test_default_int(install_temp): + import checks + + assert checks.get_default_integer() is np.dtype(int) def test_convert_datetime64_to_datetimestruct(install_temp): # GH#21199 diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index 3f1d3e3c1ca5..521eee6acf44 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -710,7 +710,7 @@ def test_type_aliases(self): @pytest.mark.parametrize("name", - ["bool", "long", "ulong", "str", "bytes", "object"]) + ["bool", "str", "bytes", "object"]) def test_future_scalar_attributes(name): # FutureWarning added 2022-11-17, NumPy 1.24, assert name not in dir(np) # we may want to not add them diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py index 2db170d09c4e..49982e76eed6 100644 --- a/numpy/core/tests/test_dtype.py +++ b/numpy/core/tests/test_dtype.py @@ -1524,11 +1524,6 @@ def test_keyword_argument(): assert np.dtype(dtype=np.float64) == np.dtype(np.float64) -def test_ulong_dtype(): - # test for gh-21063 - assert np.dtype("ulong") == np.dtype(np.uint) - - class TestFromDTypeAttribute: def test_simple(self): class dt: diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index a7c89aa4380d..bbc54e7ab526 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -312,7 +312,8 @@ def test_attributes(self): def test_dtypeattr(self): assert_equal(self.one.dtype, np.dtype(np.int_)) assert_equal(self.three.dtype, np.dtype(np.float_)) - assert_equal(self.one.dtype.char, 'l') + assert_equal(self.one.dtype.char, np.dtype(int).char) + assert self.one.dtype.char in "lq" assert_equal(self.three.dtype.char, 'd') assert_(self.three.dtype.str[0] in '<>') assert_equal(self.one.dtype.str[1], 'i') diff --git a/numpy/core/tests/test_numerictypes.py b/numpy/core/tests/test_numerictypes.py index bab5bf246664..e98ba451bcea 100644 --- a/numpy/core/tests/test_numerictypes.py +++ b/numpy/core/tests/test_numerictypes.py @@ -444,13 +444,9 @@ def test_longdouble(self): assert_(np.sctypeDict['c16'] is not np.clongdouble) def test_ulong(self): - # Test that 'ulong' behaves like 'long'. np.sctypeDict['long'] is an - # alias for np.int_, but np.long is not supported for historical - # reasons (gh-21063) - assert_(np.sctypeDict['ulong'] is np.uint) - with pytest.warns(FutureWarning): - # We will probably allow this in the future: - assert not hasattr(np, 'ulong') + assert np.sctypeDict['ulong'] is np.ulong + assert np.dtype(np.ulong) is np.dtype("ulong") + assert np.dtype(np.ulong).itemsize == np.dtype(np.long).itemsize class TestBitName: def test_abstract(self): @@ -490,7 +486,8 @@ class Test_sctype2char: def test_scalar_type(self): assert_equal(np.sctype2char(np.double), 'd') - assert_equal(np.sctype2char(np.int_), 'l') + assert_equal(np.sctype2char(np.long), 'l') + assert_equal(np.sctype2char(np.int_), np.array(0).dtype.char) assert_equal(np.sctype2char(np.str_), 'U') assert_equal(np.sctype2char(np.bytes_), 'S') @@ -545,19 +542,19 @@ class TestScalarTypeNames: # gh-9799 numeric_types = [ - np.byte, np.short, np.intc, np.int_, np.longlong, - np.ubyte, np.ushort, np.uintc, np.uint, np.ulonglong, + np.byte, np.short, np.intc, np.int_, np.long, np.longlong, + np.ubyte, np.ushort, np.uintc, np.uint, np.ulong, np.ulonglong, np.half, np.single, np.double, np.longdouble, np.csingle, np.cdouble, np.clongdouble, ] def test_names_are_unique(self): - # none of the above may be aliases for each other - assert len(set(self.numeric_types)) == len(self.numeric_types) + # exactly np.int and np.uint will cause a duplicate in the above + assert len(set(self.numeric_types)) == len(self.numeric_types) - 2 - # names must be unique + # names must be unique except those two names = [t.__name__ for t in self.numeric_types] - assert len(set(names)) == len(names) + assert len(set(names)) == len(names) - 2 @pytest.mark.parametrize('t', numeric_types) def test_names_reflect_attributes(self, t): diff --git a/numpy/core/tests/test_scalarbuffer.py b/numpy/core/tests/test_scalarbuffer.py index 31b0494cf00b..c7fb1308df7e 100644 --- a/numpy/core/tests/test_scalarbuffer.py +++ b/numpy/core/tests/test_scalarbuffer.py @@ -14,12 +14,12 @@ (np.byte, 'b'), (np.short, 'h'), (np.intc, 'i'), - (np.int_, 'l'), + (np.long, 'l'), (np.longlong, 'q'), (np.ubyte, 'B'), (np.ushort, 'H'), (np.uintc, 'I'), - (np.uint, 'L'), + (np.ulong, 'L'), (np.ulonglong, 'Q'), (np.half, 'e'), (np.single, 'f'), diff --git a/numpy/core/tests/test_ufunc.py b/numpy/core/tests/test_ufunc.py index 99390e48c3db..fe85d6cfb9a2 100644 --- a/numpy/core/tests/test_ufunc.py +++ b/numpy/core/tests/test_ufunc.py @@ -1887,11 +1887,11 @@ def test_ufunc_custom_out(self): _rational_tests.test_add(a, np.uint16(2)) def test_operand_flags(self): - a = np.arange(16, dtype='l').reshape(4, 4) - b = np.arange(9, dtype='l').reshape(3, 3) + a = np.arange(16, dtype=int).reshape(4, 4) + b = np.arange(9, dtype=int).reshape(3, 3) opflag_tests.inplace_add(a[:-1, :-1], b) assert_equal(a, np.array([[0, 2, 4, 3], [7, 9, 11, 7], - [14, 16, 18, 11], [12, 13, 14, 15]], dtype='l')) + [14, 16, 18, 11], [12, 13, 14, 15]])) a = np.array(0) opflag_tests.inplace_add(a, 3) @@ -2890,8 +2890,16 @@ def test_weird_dtypes(self): np.equal.resolve_dtypes((dts, dts, None)) def test_resolve_dtypes_reduction(self): + i2 = np.dtype("i2") + default_int_ = np.dtype(np.int_) + # Check special addition resolution: + res = np.add.resolve_dtypes((None, i2, None), reduction=True) + assert res == (default_int_, default_int_, default_int_) + + def test_resolve_dtypes_reduction_no_output(self): i4 = np.dtype("i4") - with pytest.raises(NotImplementedError): + with pytest.raises(TypeError): + # May be allowable at some point? np.add.resolve_dtypes((i4, i4, i4), reduction=True) @pytest.mark.parametrize("dtypes", [ @@ -2903,13 +2911,6 @@ def test_resolve_dtypes_errors(self, dtypes): with pytest.raises(TypeError): np.add.resolve_dtypes(dtypes) - def test_resolve_dtypes_reduction(self): - i2 = np.dtype("i2") - long_ = np.dtype("long") - # Check special addition resolution: - res = np.add.resolve_dtypes((None, i2, None), reduction=True) - assert res == (long_, long_, long_) - def test_resolve_dtypes_reduction_errors(self): i2 = np.dtype("i2") diff --git a/numpy/random/mtrand.pyx b/numpy/random/mtrand.pyx index 752d9beaefd2..ebced51a5caa 100644 --- a/numpy/random/mtrand.pyx +++ b/numpy/random/mtrand.pyx @@ -26,6 +26,9 @@ from ._common cimport (POISSON_LAM_MAX, CONS_POSITIVE, CONS_NONE, validate_output_shape ) +cdef extern from "limits.h": + cdef long LONG_MAX # NumPy has it, maybe `__init__.pyd` should expose it + cdef extern from "numpy/random/distributions.h": struct s_binomial_t: int has_binomial @@ -622,8 +625,13 @@ cdef class RandomState: tomaxint(size=None) Return a sample of uniformly distributed random integers in the interval - [0, ``np.iinfo(np.int_).max``]. The `np.int_` type translates to the C long - integer type and its precision is platform dependent. + [0, ``np.iinfo("long").max``]. + + .. warning:: + This function users the C-long dtype, which is 32bit on windows + and otherwise 64bit on 64bit platforms (and 32bit on 32bit ones). + Since NumPy 2.0, NumPy's default integer is 32bit on 32bit platforms + and 64bit on 64bit platforms. Parameters ---------- @@ -675,9 +683,9 @@ cdef class RandomState: randoms_data[i] = random_positive_int(&self._bitgen) return randoms - def randint(self, low, high=None, size=None, dtype=int): + def randint(self, low, high=None, size=None, dtype=np.long): """ - randint(low, high=None, size=None, dtype=int) + randint(low, high=None, size=None, dtype=np.long) Return random integers from `low` (inclusive) to `high` (exclusive). @@ -706,10 +714,16 @@ cdef class RandomState: single value is returned. dtype : dtype, optional Desired dtype of the result. Byteorder must be native. - The default value is int. + The default value is long. .. versionadded:: 1.11.0 + .. warning:: + This function defaults to the C-long dtype, which is 32bit on windows + and otherwise 64bit on 64bit platforms (and 32bit on 32bit ones). + Since NumPy 2.0, NumPy's default integer is 32bit on 32bit platforms + and 64bit on 64bit platforms. Which corresponds to `np.intp`. + Returns ------- out : int or ndarray of ints @@ -851,6 +865,13 @@ cdef class RandomState: method of a `~numpy.random.Generator` instance instead; please see the :ref:`random-quick-start`. + .. warning:: + This function uses the C-long dtype, which is 32bit on windows + and otherwise 64bit on 64bit platforms (and 32bit on 32bit ones). + Since NumPy 2.0, NumPy's default integer is 32bit on 32bit platforms + and 64bit on 64bit platforms. + + Parameters ---------- a : 1-D array-like or int @@ -992,7 +1013,7 @@ cdef class RandomState: idx = cdf.searchsorted(uniform_samples, side='right') # searchsorted returns a scalar # force cast to int for LLP64 - idx = np.array(idx, copy=False).astype(int, casting='unsafe') + idx = np.array(idx, copy=False).astype(np.long, casting='unsafe') else: idx = self.randint(0, pop_size, size=shape) else: @@ -1007,7 +1028,7 @@ cdef class RandomState: raise ValueError("Fewer non-zero entries in p than size") n_uniq = 0 p = p.copy() - found = np.zeros(shape, dtype=int) + found = np.zeros(shape, dtype=np.long) flat_found = found.ravel() while n_uniq < size: x = self.rand(size - n_uniq) @@ -3443,7 +3464,7 @@ cdef class RandomState: # Uses a custom implementation since self._binomial is required cdef double _dp = 0 - cdef long _in = 0 + cdef np.npy_intp _in = 0 cdef bint is_scalar = True cdef np.npy_intp i, cnt cdef np.ndarray randoms @@ -3452,17 +3473,17 @@ cdef class RandomState: p_arr = np.PyArray_FROM_OTF(p, np.NPY_DOUBLE, np.NPY_ALIGNED) is_scalar = is_scalar and np.PyArray_NDIM(p_arr) == 0 - n_arr = np.PyArray_FROM_OTF(n, np.NPY_LONG, np.NPY_ALIGNED) + n_arr = np.PyArray_FROM_OTF(n, np.NPY_INTP, np.NPY_ALIGNED) is_scalar = is_scalar and np.PyArray_NDIM(n_arr) == 0 if not is_scalar: check_array_constraint(p_arr, 'p', CONS_BOUNDED_0_1) check_array_constraint(n_arr, 'n', CONS_NON_NEGATIVE) if size is not None: - randoms = np.empty(size, int) + randoms = np.empty(size, np.long) else: it = np.PyArray_MultiIterNew2(p_arr, n_arr) - randoms = np.empty(it.shape, int) + randoms = np.empty(it.shape, np.long) cnt = np.PyArray_SIZE(randoms) @@ -3471,7 +3492,11 @@ cdef class RandomState: with self.lock, nogil: for i in range(cnt): _dp = (np.PyArray_MultiIter_DATA(it, 1))[0] - _in = (np.PyArray_MultiIter_DATA(it, 2))[0] + _in = (np.PyArray_MultiIter_DATA(it, 2))[0] + if _in > LONG_MAX: + raise ValueError( + "`n` is out of bounds for long, consider using " + "the new generator API for 64bit integers.") (np.PyArray_MultiIter_DATA(it, 0))[0] = \ legacy_random_binomial(&self._bitgen, _dp, _in, &self._binomial) @@ -3481,16 +3506,20 @@ cdef class RandomState: return randoms _dp = PyFloat_AsDouble(p) - _in = n + _in = int(n) check_constraint(_dp, 'p', CONS_BOUNDED_0_1) check_constraint(_in, 'n', CONS_NON_NEGATIVE) + if _in > LONG_MAX: + raise ValueError( + "`n` is out of bounds for long, consider using " + "the new generator API for 64bit integers.") if size is None: with self.lock: return legacy_random_binomial(&self._bitgen, _dp, _in, &self._binomial) - randoms = np.empty(size, int) + randoms = np.empty(size, np.long) cnt = np.PyArray_SIZE(randoms) randoms_data = np.PyArray_DATA(randoms) @@ -3536,6 +3565,12 @@ cdef class RandomState: where each sample is equal to N, the number of failures that occurred before a total of n successes was reached. + .. warning:: + This function returns the C-long dtype, which is 32bit on windows + and otherwise 64bit on 64bit platforms (and 32bit on 32bit ones). + Since NumPy 2.0, NumPy's default integer is 32bit on 32bit platforms + and 64bit on 64bit platforms. + See Also -------- random.Generator.negative_binomial: which should be used for new code. @@ -3933,15 +3968,18 @@ cdef class RandomState: cdef int64_t lngood, lnbad, lnsample # This cast to long is required to ensure that the values are inbounds - ongood = np.PyArray_FROM_OTF(ngood, np.NPY_LONG, np.NPY_ALIGNED) - onbad = np.PyArray_FROM_OTF(nbad, np.NPY_LONG, np.NPY_ALIGNED) - onsample = np.PyArray_FROM_OTF(nsample, np.NPY_LONG, np.NPY_ALIGNED) + ongood = np.PyArray_FROM_OTF(ngood, np.NPY_INT64, np.NPY_ALIGNED) + onbad = np.PyArray_FROM_OTF(nbad, np.NPY_INT64, np.NPY_ALIGNED) + onsample = np.PyArray_FROM_OTF(nsample, np.NPY_INT64, np.NPY_ALIGNED) if np.PyArray_NDIM(ongood) == np.PyArray_NDIM(onbad) == np.PyArray_NDIM(onsample) == 0: - lngood = ngood lnbad = nbad lnsample = nsample + if lnsample > LONG_MAX: + raise ValueError( + "`nsample` is out of bounds for long, consider using " + "the new generator API for 64bit integers.") if lngood + lnbad < lnsample: raise ValueError("ngood + nbad < nsample") @@ -3954,10 +3992,11 @@ cdef class RandomState: if np.any(np.less(np.add(ongood, onbad), onsample)): raise ValueError("ngood + nbad < nsample") - # Convert to int64, if necessary, to use int64 infrastructure - ongood = ongood.astype(np.int64) - onbad = onbad.astype(np.int64) - onsample = onsample.astype(np.int64) + if np.any(onsample > LONG_MAX): + raise ValueError( + "`nsample` is out of bounds for long, consider using " + "the new generator API for 64bit integers.") + out = discrete_broadcast_iii(&legacy_random_hypergeometric,&self._bitgen, size, self.lock, ongood, 'ngood', CONS_NON_NEGATIVE, onbad, 'nbad', CONS_NON_NEGATIVE, @@ -4272,6 +4311,13 @@ cdef class RandomState: method of a `~numpy.random.Generator` instance instead; please see the :ref:`random-quick-start`. + .. warning:: + This function defaults to the C-long dtype, which is 32bit on windows + and otherwise 64bit on 64bit platforms (and 32bit on 32bit ones). + Since NumPy 2.0, NumPy's default integer is 32bit on 32bit platforms + and 64bit on 64bit platforms. + + Parameters ---------- n : int @@ -4374,7 +4420,7 @@ cdef class RandomState: shape = (operator.index(size), d) except: shape = tuple(size) + (d,) - multin = np.zeros(shape, dtype=int) + multin = np.zeros(shape, dtype=np.long) mnarr = multin mnix = np.PyArray_DATA(mnarr) sz = np.PyArray_SIZE(mnarr) @@ -4712,7 +4758,8 @@ cdef class RandomState: """ if isinstance(x, (int, np.integer)): - arr = np.arange(x) + # keep using long as the default here (main numpy switched to intp) + arr = np.arange(x, dtype=np.result_type(x, np.long)) self.shuffle(arr) return arr diff --git a/numpy/random/tests/test_randomstate.py b/numpy/random/tests/test_randomstate.py index 3099853d2a8e..04de539e94dc 100644 --- a/numpy/random/tests/test_randomstate.py +++ b/numpy/random/tests/test_randomstate.py @@ -24,7 +24,7 @@ 'zipf': (2,), } -if np.iinfo(int).max < 2**32: +if np.iinfo(np.long).max < 2**32: # Windows and some 32-bit platforms, e.g., ARM INT_FUNC_HASHES = {'binomial': '2fbead005fc63942decb5326d36a1f32fe2c9d32c904ee61e46866b88447c263', 'logseries': '23ead5dcde35d4cfd4ef2c105e4c3d43304b45dc1b1444b7823b9ee4fa144ebb', @@ -495,7 +495,7 @@ def test_tomaxint(self): random.seed(self.seed) rs = random.RandomState(self.seed) actual = rs.tomaxint(size=(3, 2)) - if np.iinfo(int).max == 2147483647: + if np.iinfo(np.long).max == 2147483647: desired = np.array([[1328851649, 731237375], [1270502067, 320041495], [1908433478, 499156889]], dtype=np.int64) diff --git a/numpy/random/tests/test_randomstate_regression.py b/numpy/random/tests/test_randomstate_regression.py index 7ad19ab5562b..6e55cf5f1f29 100644 --- a/numpy/random/tests/test_randomstate_regression.py +++ b/numpy/random/tests/test_randomstate_regression.py @@ -165,15 +165,15 @@ def test_named_argument_initialization(self): assert rs1.randint(0, 100) == rs2.randint(0, 100) def test_choice_retun_dtype(self): - # GH 9867 + # GH 9867, now long since the NumPy default changed. c = np.random.choice(10, p=[.1]*10, size=2) - assert c.dtype == np.dtype(int) + assert c.dtype == np.dtype(np.long) c = np.random.choice(10, p=[.1]*10, replace=False, size=2) - assert c.dtype == np.dtype(int) + assert c.dtype == np.dtype(np.long) c = np.random.choice(10, size=2) - assert c.dtype == np.dtype(int) + assert c.dtype == np.dtype(np.long) c = np.random.choice(10, replace=False, size=2) - assert c.dtype == np.dtype(int) + assert c.dtype == np.dtype(np.long) @pytest.mark.skipif(np.iinfo('l').max < 2**32, reason='Cannot test with 32-bit C long')