8000 MAINT: Clean up more PY_VERSION_HEX by sethtroisi · Pull Request #15233 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: Clean up more PY_VERSION_HEX #15233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/release/upcoming_changes/15233.highlight.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* Code compatibility with Python versions < 3.5 (including Python 2) was
dropped from both the python and C code. The shims in numpy.compat will
remain to support third-party packages, but they may be deprecated in a
future release.
19 changes: 5 additions & 14 deletions numpy/core/include/numpy/ndarraytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,21 +353,12 @@ struct NpyAuxData_tag {

#define NPY_USE_PYMEM 1


#if NPY_USE_PYMEM == 1
/* numpy sometimes calls PyArray_malloc() with the GIL released. On Python
3.3 and older, it was safe to call PyMem_Malloc() with the GIL released.
On Python 3.4 and newer, it's better to use PyMem_RawMalloc() to be able
to use tracemalloc. On Python 3.6, calling PyMem_Malloc() with the GIL
released is now a fatal error in debug mode. */
# if PY_VERSION_HEX >= 0x03040000
# define PyArray_malloc PyMem_RawMalloc
# define PyArray_free PyMem_RawFree
# define PyArray_realloc PyMem_RawRealloc
# else
# define PyArray_malloc PyMem_Malloc
# define PyArray_free PyMem_Free
# define PyArray_realloc PyMem_Realloc
# endif
/* use the Raw versions which are safe to call with the GIL released */
#define PyArray_malloc PyMem_RawMalloc
#define PyArray_free PyMem_RawFree
#define PyArray_realloc PyMem_RawRealloc
#else
#define PyArray_malloc malloc
#define PyArray_free free
Expand Down
45 changes: 1 addition & 44 deletions numpy/core/include/numpy/npy_3kcompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,7 @@ static NPY_INLINE int PyInt_Check(PyObject *op) {
PySlice_GetIndicesEx((PySliceObject *)op, nop, start, end, step, slicelength)
#endif

/* <2.7.11 and <3.4.4 have the wrong argument type for Py_EnterRecursiveCall */
#if (PY_VERSION_HEX < 0x02070B00) || \
((0x03000000 <= PY_VERSION_HEX) && (PY_VERSION_HEX < 0x03040400))
#define Npy_EnterRecursiveCall(x) Py_EnterRecursiveCall((char *)(x))
#else
#define Npy_EnterRecursiveCall(x) Py_EnterRecursiveCall(x)
#endif
#define Npy_EnterRecursiveCall(x) Py_EnterRecursiveCall(x)

/* Py_SETREF was added in 3.5.2, and only if Py_LIMITED_API is absent */
#if PY_VERSION_HEX < 0x03050200
Expand Down Expand Up @@ -488,8 +482,6 @@ PyObject_Cmp(PyObject *i1, PyObject *i2, int *cmp)
* The main job here is to get rid of the improved error handling
* of PyCapsules. It's a shame...
*/
#if PY_VERSION_HEX >= 0x03000000

static NPY_INLINE PyObject *
NpyCapsule_FromVoidPtr(void *ptr, void (*dtor)(PyObject *))
{
Expand Down Expand Up @@ -534,41 +526,6 @@ NpyCapsule_Check(PyObject *ptr)
return PyCapsule_CheckExact(ptr);
}

#else

static NPY_INLINE PyObject *
NpyCapsule_FromVoidPtr(void *ptr, void (*dtor)(void *))
{
return PyCObject_FromVoidPtr(ptr, dtor);
}

static NPY_INLINE PyObject *
NpyCapsule_FromVoidPtrAndDesc(void *ptr, void* context,
void (*dtor)(void *, void *))
{
return PyCObject_FromVoidPtrAndDesc(ptr, context, dtor);
}

static NPY_INLINE void *
NpyCapsule_AsVoidPtr(PyObject *ptr)
{
return PyCObject_AsVoidPtr(ptr);
}

static NPY_INLINE void *
NpyCapsule_GetDesc(PyObject *obj)
{
return PyCObject_GetDesc(obj);
}

static NPY_INLINE int
NpyCapsule_Check(PyObject *ptr)
{
return PyCObject_Check(ptr);
}

#endif

#ifdef __cplusplus
}
#endif
Expand Down
10 changes: 0 additions & 10 deletions numpy/core/include/numpy/npy_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,18 +369,8 @@ typedef long npy_long;
typedef float npy_float;
typedef double npy_double;

/*
* Hash value compatibility.
* As of Python 3.2 hash values are of type Py_hash_t.
* Previous versions use C long.
*/
#if PY_VERSION_HEX < 0x03020000
typedef long npy_hash_t;
#define NPY_SIZEOF_HASH_T NPY_SIZEOF_LONG
#else
typedef Py_hash_t npy_hash_t;
#define NPY_SIZEOF_HASH_T NPY_SIZEOF_INTP
#endif

/*
* Disabling C99 complex usage: a lot of C code in numpy/scipy rely on being
Expand Down
8 changes: 0 additions & 8 deletions numpy/core/src/multiarray/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,7 @@ PyArray_DTypeFromObjectHelper(PyObject *obj, int maxdims,
goto fail;
}
#if defined(NPY_PY3K)
#if PY_VERSION_HEX >= 0x03030000
itemsize = PyUnicode_GetLength(temp);
#else
itemsize = PyUnicode_GET_SIZE(temp);
#endif
#else
itemsize = PyString_GET_SIZE(temp);
#endif
Expand Down Expand Up @@ -222,11 +218,7 @@ PyArray_DTypeFromObjectHelper(PyObject *obj, int maxdims,
goto fail;
}
#if defined(NPY_PY3K)
#if PY_VERSION_HEX >= 0x03030000
itemsize = PyUnicode_GetLength(temp);
#else
itemsize = PyUnicode_GET_SIZE(temp);
#endif
#else
itemsize = PyString_GET_SIZE(temp);
#endif
Expand Down
5 changes: 1 addition & 4 deletions numpy/core/src/multiarray/number.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ array_divmod(PyArrayObject *m1, PyObject *m2)
return PyArray_GenericBinaryFunction(m1, m2, n_ops.divmod);
}

#if PY_VERSION_HEX >= 0x03050000
/* Need this to be version dependent on account of the slot check */
static PyObject *
array_matrix_multiply(PyArrayObject *m1, PyObject *m2)
Expand All @@ -399,7 +398,6 @@ array_inplace_matrix_multiply(
"Use 'a = a @ b' instead of 'a @= b'.");
return NULL;
}
#endif

/*
* Determine if object is a scalar and if so, convert the object
Expand Down Expand Up @@ -1062,8 +1060,7 @@ NPY_NO_EXPORT PyNumberMethods array_as_number = {
(binaryfunc)array_inplace_floor_divide, /*nb_inplace_floor_divide*/
(binaryfunc)array_inplace_true_divide, /*nb_inplace_true_divide*/
(unaryfunc)array_index, /*nb_index */
#if PY_VERSION_HEX >= 0x03050000

(binaryfunc)array_matrix_multiply, /*nb_matrix_multiply*/
(binaryfunc)array_inplace_matrix_multiply, /*nb_inplace_matrix_multiply*/
#endif
};
75 changes: 0 additions & 75 deletions numpy/core/src/multiarray/scalarapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,6 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base)
itemsize = (((itemsize - 1) >> 2) + 1) << 2;
}
}
#if PY_VERSION_HEX >= 0x03030000
if (type_num == NPY_UNICODE) {
PyObject *u, *args;
int byteorder;
Expand Down Expand Up @@ -684,7 +683,6 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base)
Py_DECREF(args);
return obj;
}
#endif
if (type->tp_itemsize != 0) {
/* String type */
obj = type->tp_alloc(type, itemsize);
Expand Down Expand Up @@ -716,79 +714,6 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base)
memcpy(destptr, data, itemsize);
return obj;
}
#if PY_VERSION_HEX < 0x03030000
else if (type_num == NPY_UNICODE) {
/* tp_alloc inherited from Python PyBaseObject_Type */
PyUnicodeObject *uni = (PyUnicodeObject*)obj;
size_t length = itemsize >> 2;
Py_UNICODE *dst;
#ifndef Py_UNICODE_WIDE
char *buffer;
Py_UNICODE *tmp;
int alloc = 0;

length *= 2;
#endif
/* Set uni->str so that object can be deallocated on failure */
uni->str = NULL;
uni->defenc = NULL;
uni->hash = -1;
dst = PyObject_MALLOC(sizeof(Py_UNICODE) * (length + 1));
if (dst == NULL) {
Py_DECREF(obj);
PyErr_NoMemory();
return NULL;
}
#ifdef Py_UNICODE_WIDE
memcpy(dst, data, itemsize);
if (swap) {
byte_swap_vector(dst, length, 4);
}
uni->str = dst;
uni->str[length] = 0;
uni->length = length;
#else
/* need aligned data buffer */
if ((swap) || ((((npy_intp)data) % descr->alignment) != 0)) {
buffer = malloc(itemsize);
if (buffer == NULL) {
PyObject_FREE(dst);
Py_DECREF(obj);
PyErr_NoMemory();
}
alloc = 1;
memcpy(buffer, data, itemsize);
if (swap) {
byte_swap_vector(buffer, itemsize >> 2, 4);
}
}
else {
buffer = data;
}

/*
* Allocated enough for 2-characters per itemsize.
* Now convert from the data-buffer
*/
length = PyUCS2Buffer_FromUCS4(dst,
(npy_ucs4 *)buffer, itemsize >> 2);
if (alloc) {
free(buffer);
}
/* Resize the unicode result */
tmp = PyObject_REALLOC(dst, sizeof(Py_UNICODE)*(length + 1));
if (tmp == NULL) {
PyObject_FREE(dst);
Py_DECREF(obj);
return NULL;
}
uni->str = tmp;
uni->str[length] = 0;
uni->length = length;
#endif
return obj;
}
#endif /* PY_VERSION_HEX < 0x03030000 */
else {
PyVoidScalarObject *vobj = (PyVoidScalarObject *)obj;
vobj->base = NULL;
Expand Down
6 changes: 0 additions & 6 deletions numpy/core/src/multiarray/scalartypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -1152,10 +1152,8 @@ static PyNumberMethods gentype_as_number = {
0, /*nb_inplace_floor_divide*/
0, /*nb_inplace_true_divide*/
(unaryfunc)NULL, /*nb_index*/
#if PY_VERSION_HEX >= 0x03050000
0, /*np_matmul*/
0, /*np_inplace_matmul*/
#endif
};


Expand Down Expand Up @@ -2877,11 +2875,7 @@ finish:
*((npy_@name@ *)dest) = *((npy_@name@ *)src);
#elif @default@ == 1 /* unicode and strings */
if (itemsize == 0) { /* unicode */
#if PY_VERSION_HEX >= 0x03030000
itemsize = PyUnicode_GetLength(robj) * PyUnicode_KIND(robj);
#else
itemsize = ((PyUnicodeObject *)robj)->length * sizeof(Py_UNICODE);
#endif
}
memcpy(dest, src, itemsize);
/* @default@ == 2 won't get here */
Expand Down
6 changes: 2 additions & 4 deletions numpy/core/src/multiarray/typeinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,8 @@ PyArray_typeinforanged(
return entry;
}

/* Python version only needed for backport to 2.7 */
#if (PY_VERSION_HEX < 0x03040000) \
|| (defined(PYPY_VERSION_NUM) && (PYPY_VERSION_NUM < 0x07020000))

/* Python version needed for older PyPy */
#if (defined(PYPY_VERSION_NUM) && (PYPY_VERSION_NUM < 0x07020000))
static int
PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc) {
PyStructSequence_InitType(type, desc);
Expand Down
4 changes: 1 addition & 3 deletions numpy/core/src/umath/funcs.inc.src
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ npy_ObjectGCD(PyObject *i1, PyObject *i2)
{
PyObject *gcd = NULL;

/* use math.gcd if available, and valid on the provided types */
#if PY_VERSION_HEX >= 0x03050000
/* use math.gcd if valid on the provided types */
{
static PyObject *math_gcd_func = NULL;

Expand All @@ -213,7 +212,6 @@ npy_ObjectGCD(PyObject *i1, PyObject *i2)
/* silence errors, and fall back on pure-python gcd */
PyErr_Clear();
}
#endif

/* otherwise, use our internal one, written in python */
{
Expand Down
25 changes: 0 additions & 25 deletions numpy/f2py/src/fortranobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1017,8 +1017,6 @@ int copy_ND_array(const PyArrayObject *arr, PyArrayObject *out)
/* Compatibility functions for Python >= 3.0 */
/*********************************************/

#if PY_VERSION_HEX >= 0x03000000

PyObject *
F2PyCapsule_FromVoidPtr(void *ptr, void (*dtor)(PyObject *))
{
Expand All @@ -1045,29 +1043,6 @@ F2PyCapsule_Check(PyObject *ptr)
return PyCapsule_CheckExact(ptr);
}

#else

PyObject *
F2PyCapsule_FromVoidPtr(void *ptr, void (*dtor)(void *))
{
return PyCObject_FromVoidPtr(ptr, dtor);
}

void *
F2PyCapsule_AsVoidPtr(PyObject *ptr)
{
return PyCObject_AsVoidPtr(ptr);
}

int
F2PyCapsule_Check(PyObject *ptr)
{
return PyCObject_Check(ptr);
}

#endif


#ifdef __cplusplus
}
#endif
Expand Down
0