Skip to content

MAINT: Cleaning up PY_MAJOR_VERSION/PY_VERSION_HEX #15232

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 1 commit into from
Jan 4, 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
2 changes: 0 additions & 2 deletions doc/Py3K.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,7 @@ The Py2/Py3 compatible structure definition looks like::
(binaryfunc)0, /*nb_true_divide*/
0, /*nb_inplace_floor_divide*/
0, /*nb_inplace_true_divide*/
#if PY_VERSION_HEX >= 0x02050000
(unaryfunc)NULL, /*nb_index*/
#endif
};


Expand Down
87 changes: 0 additions & 87 deletions doc/source/user/c-info.ufunc-tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ the module.


/* This initiates the module using the above definitions. */
#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"spam",
Expand All @@ -159,17 +158,6 @@ the module.
}
return m;
}
#else
PyMODINIT_FUNC initspam(void)
{
PyObject *m;

m = Py_InitModule("spam", SpamMethods);
if (m == NULL) {
return;
}
}
#endif

To use the setup.py file, place setup.py and spammodule.c in the same
folder. Then python setup.py build will build the module to import,
Expand Down Expand Up @@ -322,7 +310,6 @@ the primary thing that must be changed to create your own ufunc.

static void *data[1] = {NULL};

#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"npufunc",
Expand Down Expand Up @@ -357,30 +344,6 @@ the primary thing that must be changed to create your own ufunc.

return m;
}
#else
PyMODINIT_FUNC initnpufunc(void)
{
PyObject *m, *logit, *d;


m = Py_InitModule("npufunc", LogitMethods);
if (m == NULL) {
return;
}

import_array();
import_umath();

logit = PyUFunc_FromFuncAndData(funcs, data, types, 1, 1, 1,
PyUFunc_None, "logit",
"logit_docstring", 0);

d = PyModule_GetDict(m);

PyDict_SetItemString(d, "logit", logit);
Py_DECREF(logit);
}
#endif

This is a setup.py file for the above code. As before, the module
can be build via calling python setup.py build at the command prompt,
Expand Down Expand Up @@ -601,7 +564,6 @@ the primary thing that must be changed to create your own ufunc.
NPY_LONGDOUBLE, NPY_LONGDOUBLE};
static void *data[4] = {NULL, NULL, NULL, NULL};

#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"npufunc",
Expand Down Expand Up @@ -636,30 +598,6 @@ the primary thing that must be changed to create your own ufunc.

return m;
}
#else
PyMODINIT_FUNC initnpufunc(void)
{
PyObject *m, *logit, *d;


m = Py_InitModule("npufunc", LogitMethods);
if (m == NULL) {
return;
}

import_array();
import_umath();

logit = PyUFunc_FromFuncAndData(funcs, data, types, 4, 1, 1,
PyUFunc_None, "logit",
"logit_docstring", 0);

d = PyModule_GetDict(m);

PyDict_SetItemString(d, "logit", logit);
Py_DECREF(logit);
}
#endif

This is a setup.py file for the above code. As before, the module
can be build via calling python setup.py build at the command prompt,
Expand Down Expand Up @@ -824,7 +762,6 @@ as well as all other properties of a ufunc.

static void *data[1] = {NULL};

#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"npufunc",
Expand Down Expand Up @@ -859,30 +796,6 @@ as well as all other properties of a ufunc.

return m;
}
#else
PyMODINIT_FUNC initnpufunc(void)
{
PyObject *m, *logit, *d;


m = Py_InitModule("npufunc", LogitMethods);
if (m == NULL) {
return;
}

import_array();
import_umath();

logit = PyUFunc_FromFuncAndData(funcs, data, types, 1, 2, 2,
PyUFunc_None, "logit",
"logit_docstring", 0);

d = PyModule_GetDict(m);

PyDict_SetItemString(d, "logit", logit);
Py_DECREF(logit);
}
#endif


.. _`sec:NumPy-struct-dtype`:
Expand Down
9 changes: 0 additions & 9 deletions numpy/core/code_generators/generate_numpy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,12 @@
return -1;
}

#if PY_VERSION_HEX >= 0x03000000
if (!PyCapsule_CheckExact(c_api)) {
PyErr_SetString(PyExc_RuntimeError, "_ARRAY_API is not PyCapsule object");
Py_DECREF(c_api);
return -1;
}
PyArray_API = (void **)PyCapsule_GetPointer(c_api, NULL);
#else
if (!PyCObject_Check(c_api)) {
PyErr_SetString(PyExc_RuntimeError, "_ARRAY_API is not PyCObject object");
Py_DECREF(c_api);
return -1;
}
PyArray_API = (void **)PyCObject_AsVoidPtr(c_api);
#endif
Py_DECREF(c_api);
if (PyArray_API == NULL) {
PyErr_SetString(PyExc_RuntimeError, "_ARRAY_API is NULL pointer");
Expand Down
9 changes: 0 additions & 9 deletions numpy/core/code_generators/generate_ufunc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,12 @@
return -1;
}

#if PY_VERSION_HEX >= 0x03000000
if (!PyCapsule_CheckExact(c_api)) {
PyErr_SetString(PyExc_RuntimeError, "_UFUNC_API is not PyCapsule object");
Py_DECREF(c_api);
return -1;
}
PyUFunc_API = (void **)PyCapsule_GetPointer(c_api, NULL);
#else
if (!PyCObject_Check(c_api)) {
PyErr_SetString(PyExc_RuntimeError, "_UFUNC_API is not PyCObject object");
Py_DECREF(c_api);
return -1;
}
PyUFunc_API = (void **)PyCObject_AsVoidPtr(c_api);
#endif
Py_DECREF(c_api);
if (PyUFunc_API == NULL) {
PyErr_SetString(PyExc_RuntimeError, "_UFUNC_API is NULL pointer");
Expand Down
17 changes: 0 additions & 17 deletions numpy/core/include/numpy/ndarrayobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ extern "C" {

#define PyArray_CheckScalar(m) (PyArray_IsScalar(m, Generic) || \
PyArray_IsZeroDim(m))
#if PY_MAJOR_VERSION >= 3
#define PyArray_IsPythonNumber(obj) \
(PyFloat_Check(obj) || PyComplex_Check(obj) || \
PyLong_Check(obj) || PyBool_Check(obj))
Expand All @@ -54,17 +53,6 @@ extern "C" {
#define PyArray_IsPythonScalar(obj) \
(PyArray_IsPythonNumber(obj) || PyBytes_Check(obj) || \
PyUnicode_Check(obj))
#else
#define PyArray_IsPythonNumber(obj) \
(PyInt_Check(obj) || PyFloat_Check(obj) || PyComplex_Check(obj) || \
PyLong_Check(obj) || PyBool_Check(obj))
#define PyArray_IsIntegerScalar(obj) (PyInt_Check(obj) \
|| PyLong_Check(obj) \
|| PyArray_IsScalar((obj), Integer))
#define PyArray_IsPythonScalar(obj) \
(PyArray_IsPythonNumber(obj) || PyString_Check(obj) || \
PyUnicode_Check(obj))
#endif

#define PyArray_IsAnyScalar(obj) \
(PyArray_IsScalar(obj, Generic) || PyArray_IsPythonScalar(obj))
Expand Down Expand Up @@ -248,11 +236,6 @@ NPY_TITLE_KEY_check(PyObject *key, PyObject *value)
if (PyUnicode_Check(title) && PyUnicode_Check(key)) {
return PyUnicode_Compare(title, key) == 0 ? 1 : 0;
}
#if PY_VERSION_HEX < 0x03000000
if (PyString_Check(title) && PyString_Check(key)) {
return PyObject_Compare(title, key) == 0 ? 1 : 0;
}
#endif
#endif
return 0;
}
Expand Down
7 changes: 0 additions & 7 deletions numpy/core/include/numpy/npy_1_7_deprecated_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,11 @@
#define PyArray_DEFAULT NPY_DEFAULT_TYPE

/* These DATETIME bits aren't used internally */
#if PY_VERSION_HEX >= 0x03000000
#define PyDataType_GetDatetimeMetaData(descr) \
((descr->metadata == NULL) ? NULL : \
((PyArray_DatetimeMetaData *)(PyCapsule_GetPointer( \
PyDict_GetItemString( \
descr->metadata, NPY_METADATA_DTSTR), NULL))))
#else
#define PyDataType_GetDatetimeMetaData(descr) \
((descr->metadata == NULL) ? NULL : \
((PyArray_DatetimeMetaData *)(PyCObject_AsVoidPtr( \
PyDict_GetItemString(descr->metadata, NPY_METADATA_DTSTR)))))
#endif

/*
* Deprecated as of NumPy 1.7, this kind of shortcut doesn't
Expand Down
2 changes: 0 additions & 2 deletions numpy/core/include/numpy/npy_3kcompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
#include <Python.h>
#include <stdio.h>

#if PY_VERSION_HEX >= 0x03000000
#ifndef NPY_PY3K
#define NPY_PY3K 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removing the internal use of this is one of the next steps.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm also working on cleaning that up (in another CL)

#endif
#endif

#include "numpy/npy_common.h"
#include "numpy/ndarrayobject.h"
Expand Down
12 changes: 0 additions & 12 deletions numpy/core/src/multiarray/scalartypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,6 @@ gentype_sizeof(PyObject *self)
return PyLong_FromSsize_t(nbytes);
}

#if PY_VERSION_HEX >= 0x03000000
NPY_NO_EXPORT void
gentype_struct_free(PyObject *ptr)
{
Expand All @@ -1307,17 +1306,6 @@ gentype_struct_free(PyObject *ptr)
PyArray_free(arrif->shape);
PyArray_free(arrif);
}
#else
NPY_NO_EXPORT void
gentype_struct_free(void *ptr, void *arg)
{
PyArrayInterface *arrif = (PyArrayInterface *)ptr;
Py_DECREF((PyObject *)arg);
Py_XDECREF(arrif->descr);
PyArray_free(arrif->shape);
PyArray_free(arrif);
}
#endif

static PyObject *
gentype_struct_get(PyObject *self)
Expand Down
5 changes: 0 additions & 5 deletions numpy/core/src/multiarray/scalartypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@ initialize_casting_tables(void);
NPY_NO_EXPORT void
initialize_numeric_types(void);

#if PY_VERSION_HEX >= 0x03000000
NPY_NO_EXPORT void
gentype_struct_free(PyObject *ptr);
#else
NPY_NO_EXPORT void
gentype_struct_free(void *ptr, void *arg);
#endif

NPY_NO_EXPORT int
is_anyscalar_exact(PyObject *obj);
Expand Down
17 changes: 0 additions & 17 deletions numpy/core/src/umath/_rational_tests.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,6 @@ static PyNumberMethods pyrational_as_number = {
pyrational_add, /* nb_add */
pyrational_subtract, /* nb_subtract */
pyrational_multiply, /* nb_multiply */
#if PY_MAJOR_VERSION < 3
pyrational_divide, /* nb_divide */
#endif
pyrational_remainder, /* nb_remainder */
0, /* nb_divmod */
0, /* nb_power */
Expand All @@ -625,27 +622,13 @@ static PyNumberMethods pyrational_as_number = {
0, /* nb_and */
0, /* nb_xor */
0, /* nb_or */
#if PY_MAJOR_VERSION < 3
0, /* nb_coerce */
#endif
pyrational_int, /* nb_int */
#if PY_MAJOR_VERSION < 3
pyrational_int, /* nb_long */
#else
0, /* reserved */
#endif
pyrational_float, /* nb_float */
#if PY_MAJOR_VERSION < 3
0, /* nb_oct */
0, /* nb_hex */
#endif

0, /* nb_inplace_add */
0, /* nb_inplace_subtract */
0, /* nb_inplace_multiply */
#if PY_MAJOR_VERSION < 3
0, /* nb_inplace_divide */
#endif
0, /* nb_inplace_remainder */
0, /* nb_inplace_power */
0, /* nb_inplace_lshift */
Expand Down
22 changes: 0 additions & 22 deletions numpy/core/src/umath/ufunc_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -929,22 +929,9 @@ parse_ufunc_keywords(PyUFuncObject *ufunc, PyObject *kwds, PyObject **kwnames, .
}
}
else {
#if PY_VERSION_HEX >= 0x03000000
PyErr_Format(PyExc_TypeError,
"'%S' is an invalid keyword to ufunc '%s'",
key, ufunc_get_name_cstr(ufunc));
#else
char *str = PyString_AsString(key);
if (str == NULL) {
PyErr_Clear();
PyErr_SetString(PyExc_TypeError, "invalid keyword argument");
}
else {
PyErr_Format(PyExc_TypeError,
"'%s' is an invalid keyword to ufunc '%s'",
str, ufunc_get_name_cstr(ufunc));
}
#endif
return -1;
}
}
Expand Down Expand Up @@ -5068,21 +5055,12 @@ _free_loop1d_list(PyUFunc_Loop1d *data)
}
}

#if PY_VERSION_HEX >= 0x03000000
static void
_loop1d_list_free(PyObject *ptr)
{
PyUFunc_Loop1d *data = (PyUFunc_Loop1d *)PyCapsule_GetPointer(ptr, NULL);
_free_loop1d_list(data);
}
#else
static void
_loop1d_list_free(void *ptr)
{
PyUFunc_Loop1d *data = (PyUFunc_Loop1d *)ptr;
_free_loop1d_list(data);
}
#endif


/*
Expand Down
Loading