Skip to content

Commit

Permalink
ENH: core: Deprecate some bad namespace-polluting macros
Browse files Browse the repository at this point in the history
This one handles PyArray_DEFAULT -> NPY_DEFAULT_TYPE and
the NPY_* array flags -> NPY_ARRAY_*. The PyArray_DEFAULT vs NPY_DEFAULT
confusion was particularly egregious here.
  • Loading branch information
Mark Wiebe authored and charris committed Jul 6, 2011
1 parent d0160c6 commit b00beab
Show file tree
Hide file tree
Showing 44 changed files with 569 additions and 439 deletions.
65 changes: 34 additions & 31 deletions numpy/core/include/numpy/ndarrayobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ extern "C" CONFUSE_EMACS
#define PyArray_FROM_OT(m,type) PyArray_FromAny(m, \
PyArray_DescrFromType(type), 0, 0, 0, NULL);

#define PyArray_FROM_OTF(m, type, flags) \
PyArray_FromAny(m, PyArray_DescrFromType(type), 0, 0, \
(((flags) & NPY_ENSURECOPY) ? \
((flags) | NPY_DEFAULT) : (flags)), NULL)
#define PyArray_FROM_OTF(m, type, flags) \
PyArray_FromAny(m, PyArray_DescrFromType(type), 0, 0, \
(((flags) & NPY_ARRAY_ENSURECOPY) ? \
((flags) | NPY_ARRAY_DEFAULT) : (flags)), NULL)

#define PyArray_FROMANY(m, type, min, max, flags) \
PyArray_FromAny(m, PyArray_DescrFromType(type), min, max, \
(((flags) & NPY_ENSURECOPY) ? \
(flags) | NPY_DEFAULT : (flags)), NULL)
#define PyArray_FROMANY(m, type, min, max, flags) \
PyArray_FromAny(m, PyArray_DescrFromType(type), min, max, \
(((flags) & NPY_ARRAY_ENSURECOPY) ? \
(flags) | NPY_ARRAY_DEFAULT : (flags)), NULL)

#define PyArray_ZEROS(m, dims, type, fortran) \
PyArray_Zeros(m, dims, PyArray_DescrFromType(type), fortran)
Expand All @@ -113,7 +113,7 @@ extern "C" CONFUSE_EMACS

#define PyArray_ContiguousFromAny(op, type, min_depth, max_depth) \
PyArray_FromAny(op, PyArray_DescrFromType(type), min_depth, \
max_depth, NPY_DEFAULT, NULL)
max_depth, NPY_ARRAY_DEFAULT, NULL)

#define PyArray_EquivArrTypes(a1, a2) \
PyArray_EquivTypes(PyArray_DESCR(a1), PyArray_DESCR(a2))
Expand All @@ -126,7 +126,7 @@ extern "C" CONFUSE_EMACS

#define PyArray_SimpleNewFromData(nd, dims, typenum, data) \
PyArray_New(&PyArray_Type, nd, dims, typenum, NULL, \
data, 0, NPY_CARRAY, NULL)
data, 0, NPY_ARRAY_CARRAY, NULL)

#define PyArray_SimpleNewFromDescr(nd, dims, descr) \
PyArray_NewFromDescr(&PyArray_Type, descr, nd, dims, \
Expand Down Expand Up @@ -160,34 +160,37 @@ extern "C" CONFUSE_EMACS
(l)*PyArray_STRIDES(obj)[3]))

#define PyArray_XDECREF_ERR(obj) \
if (obj && (PyArray_FLAGS(obj) & NPY_UPDATEIFCOPY)) { \
PyArray_FLAGS(PyArray_BASE(obj)) |= NPY_WRITEABLE; \
PyArray_FLAGS(obj) &= ~NPY_UPDATEIFCOPY; \
} \
if (obj && (PyArray_FLAGS(obj) & NPY_ARRAY_UPDATEIFCOPY)) { \
PyArray_FLAGS(PyArray_BASE(obj)) |= NPY_ARRAY_WRITEABLE; \
PyArray_FLAGS(obj) &= ~NPY_ARRAY_UPDATEIFCOPY; \
} \
Py_XDECREF(obj)

#define PyArray_DESCR_REPLACE(descr) do { \
PyArray_Descr *_new_; \
_new_ = PyArray_DescrNew(descr); \
Py_XDECREF(descr); \
descr = _new_; \
#define PyArray_DESCR_REPLACE(descr) do { \
PyArray_Descr *_new_; \
_new_ = PyArray_DescrNew(descr); \
Py_XDECREF(descr); \
descr = _new_; \
} while(0)

/* Copy should always return contiguous array */
#define PyArray_Copy(obj) PyArray_NewCopy(obj, NPY_CORDER)

#define PyArray_FromObject(op, type, min_depth, max_depth) \
PyArray_FromAny(op, PyArray_DescrFromType(type), min_depth, \
max_depth, NPY_BEHAVED | NPY_ENSUREARRAY, NULL)

#define PyArray_ContiguousFromObject(op, type, min_depth, max_depth) \
PyArray_FromAny(op, PyArray_DescrFromType(type), min_depth, \
max_depth, NPY_DEFAULT | NPY_ENSUREARRAY, NULL)

#define PyArray_CopyFromObject(op, type, min_depth, max_depth) \
PyArray_FromAny(op, PyArray_DescrFromType(type), min_depth, \
max_depth, NPY_ENSURECOPY | NPY_DEFAULT | \
NPY_ENSUREARRAY, NULL)
#define PyArray_FromObject(op, type, min_depth, max_depth) \
PyArray_FromAny(op, PyArray_DescrFromType(type), min_depth, \
max_depth, NPY_ARRAY_BEHAVED | \
NPY_ARRAY_ENSUREARRAY, NULL)

#define PyArray_ContiguousFromObject(op, type, min_depth, max_depth) \
PyArray_FromAny(op, PyArray_DescrFromType(type), min_depth, \
max_depth, NPY_ARRAY_DEFAULT | \
NPY_ARRAY_ENSUREARRAY, NULL)

#define PyArray_CopyFromObject(op, type, min_depth, max_depth) \
PyArray_FromAny(op, PyArray_DescrFromType(type), min_depth, \
max_depth, NPY_ARRAY_ENSURECOPY | \
NPY_ARRAY_DEFAULT | \
NPY_ARRAY_ENSUREARRAY, NULL)

#define PyArray_Cast(mp, type_num) \
PyArray_CastToType(mp, PyArray_DescrFromType(type_num), 0)
Expand Down
113 changes: 47 additions & 66 deletions numpy/core/include/numpy/ndarraytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -654,20 +654,6 @@ typedef struct {
npy_int32 sec, us, ps, as;
} npy_timedeltastruct;

/* TO BE REMOVED - NOT 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

typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *);

/*
Expand All @@ -677,15 +663,15 @@ typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *);
#define NPY_ARRAY_C_CONTIGUOUS 0x0001

/*
* set if array is a contiguous Fortran array: the first index varies
* Set if array is a contiguous Fortran array: the first index varies
* the fastest in memory (strides array is reverse of C-contiguous
* array)
*/
#define NPY_ARRAY_F_CONTIGUOUS 0x0002

/*
* Note: all 0-d arrays are CONTIGUOUS and FORTRAN contiguous. If a
* 1-d array is CONTIGUOUS it is also FORTRAN contiguous
* Note: all 0-d arrays are C_CONTIGUOUS and F_CONTIGUOUS. If a
* 1-d array is C_CONTIGUOUS it is also F_CONTIGUOUS
*/

/*
Expand Down Expand Up @@ -738,41 +724,37 @@ typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *);
*/
#define NPY_ARRAY_UPDATEIFCOPY 0x1000

/*
* These versions of the above flags are not yet deprecated,
* but the intention is to do so in a future release.
*/
#define NPY_C_CONTIGUOUS NPY_ARRAY_C_CONTIGUOUS
#define NPY_F_CONTIGUOUS NPY_ARRAY_F_CONTIGUOUS
#define NPY_OWNDATA NPY_ARRAY_OWNDATA
#define NPY_FORCECAST NPY_ARRAY_FORCECAST
#define NPY_ENSURECOPY NPY_ARRAY_ENSURECOPY
#define NPY_ENSUREARRAY NPY_ARRAY_ENSUREARRAY
#define NPY_ELEMENTSTRIDES NPY_ARRAY_ELEMENTSTRIDES
#define NPY_ALIGNED NPY_ARRAY_ALIGNED
#define NPY_NOTSWAPPED NPY_ARRAY_NOTSWAPPED
#define NPY_WRITEABLE NPY_ARRAY_WRITEABLE
#define NPY_UPDATEIFCOPY NPY_ARRAY_UPDATEIFCOPY
#define NPY_ARRAY_BEHAVED (NPY_ARRAY_ALIGNED | \
NPY_ARRAY_WRITEABLE)
#define NPY_ARRAY_BEHAVED_NS (NPY_ARRAY_ALIGNED | \
NPY_ARRAY_WRITEABLE | \
NPY_ARRAY_NOTSWAPPED)
#define NPY_ARRAY_CARRAY (NPY_ARRAY_C_CONTIGUOUS | \
NPY_ARRAY_BEHAVED)
#define NPY_ARRAY_CARRAY_RO (NPY_ARRAY_C_CONTIGUOUS | \
NPY_ARRAY_ALIGNED)
#define NPY_ARRAY_FARRAY (NPY_ARRAY_F_CONTIGUOUS | \
NPY_ARRAY_BEHAVED)
#define NPY_ARRAY_FARRAY_RO (NPY_ARRAY_F_CONTIGUOUS | \
NPY_ARRAY_ALIGNED)
#define NPY_ARRAY_DEFAULT (NPY_ARRAY_CARRAY)
#define NPY_ARRAY_IN_ARRAY (NPY_ARRAY_CARRAY_RO)
#define NPY_ARRAY_OUT_ARRAY (NPY_ARRAY_CARRAY)
#define NPY_ARRAY_INOUT_ARRAY (NPY_ARRAY_CARRAY | \
NPY_ARRAY_UPDATEIFCOPY)
#define NPY_ARRAY_IN_FARRAY (NPY_ARRAY_FARRAY_RO)
#define NPY_ARRAY_OUT_FARRAY (NPY_ARRAY_FARRAY)
#define NPY_ARRAY_INOUT_FARRAY (NPY_ARRAY_FARRAY | \
NPY_ARRAY_UPDATEIFCOPY)

#define NPY_ARRAY_UPDATE_ALL (NPY_ARRAY_C_CONTIGUOUS | \
NPY_ARRAY_F_CONTIGUOUS | \
NPY_ARRAY_ALIGNED)

/* This flag is for the array interface */
#define NPY_ARR_HAS_DESCR 0x0800


#define NPY_BEHAVED (NPY_ALIGNED | NPY_WRITEABLE)
#define NPY_BEHAVED_NS (NPY_ALIGNED | NPY_WRITEABLE | NPY_NOTSWAPPED)
#define NPY_CARRAY (NPY_CONTIGUOUS | NPY_BEHAVED)
#define NPY_CARRAY_RO (NPY_CONTIGUOUS | NPY_ALIGNED)
#define NPY_FARRAY (NPY_FORTRAN | NPY_BEHAVED)
#define NPY_FARRAY_RO (NPY_FORTRAN | NPY_ALIGNED)
#define NPY_DEFAULT NPY_CARRAY
#define NPY_IN_ARRAY NPY_CARRAY_RO
#define NPY_OUT_ARRAY NPY_CARRAY
#define NPY_INOUT_ARRAY (NPY_CARRAY | NPY_UPDATEIFCOPY)
#define NPY_IN_FARRAY NPY_FARRAY_RO
#define NPY_OUT_FARRAY NPY_FARRAY
#define NPY_INOUT_FARRAY (NPY_FARRAY | NPY_UPDATEIFCOPY)

#define NPY_UPDATE_ALL (NPY_CONTIGUOUS | NPY_FORTRAN | NPY_ALIGNED)


/*
Expand Down Expand Up @@ -807,12 +789,12 @@ typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *);
#define PyArray_CHKFLAGS(m, FLAGS) \
((((PyArrayObject *)(m))->flags & (FLAGS)) == (FLAGS))

#define PyArray_ISCONTIGUOUS(m) PyArray_CHKFLAGS(m, NPY_CONTIGUOUS)
#define PyArray_ISWRITEABLE(m) PyArray_CHKFLAGS(m, NPY_WRITEABLE)
#define PyArray_ISALIGNED(m) PyArray_CHKFLAGS(m, NPY_ALIGNED)
#define PyArray_ISCONTIGUOUS(m) PyArray_CHKFLAGS(m, NPY_ARRAY_C_CONTIGUOUS)
#define PyArray_ISWRITEABLE(m) PyArray_CHKFLAGS(m, NPY_ARRAY_WRITEABLE)
#define PyArray_ISALIGNED(m) PyArray_CHKFLAGS(m, NPY_ARRAY_ALIGNED)

#define PyArray_IS_C_CONTIGUOUS(m) PyArray_CHKFLAGS(m, NPY_C_CONTIGUOUS)
#define PyArray_IS_F_CONTIGUOUS(m) PyArray_CHKFLAGS(m, NPY_F_CONTIGUOUS)
#define PyArray_IS_C_CONTIGUOUS(m) PyArray_CHKFLAGS(m, NPY_ARRAY_C_CONTIGUOUS)
#define PyArray_IS_F_CONTIGUOUS(m) PyArray_CHKFLAGS(m, NPY_ARRAY_F_CONTIGUOUS)

#if NPY_ALLOW_THREADS
#define NPY_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
Expand Down Expand Up @@ -1249,7 +1231,6 @@ PyArrayNeighborhoodIter_Next2D(PyArrayNeighborhoodIterObject* iter);

/* The default array type */
#define NPY_DEFAULT_TYPE NPY_DOUBLE
#define PyArray_DEFAULT NPY_DEFAULT_TYPE

/*
* All sorts of useful ways to look into a PyArrayObject. These are
Expand All @@ -1258,15 +1239,15 @@ PyArrayNeighborhoodIter_Next2D(PyArrayNeighborhoodIterObject* iter);
*/

#define PyArray_NDIM(obj) (((PyArrayObject *)(obj))->nd)
#define PyArray_ISONESEGMENT(m) (PyArray_NDIM(m) == 0 || \
PyArray_CHKFLAGS(m, NPY_CONTIGUOUS) || \
PyArray_CHKFLAGS(m, NPY_FORTRAN))
#define PyArray_ISONESEGMENT(m) (PyArray_NDIM(m) == 0 || \
PyArray_CHKFLAGS(m, NPY_ARRAY_C_CONTIGUOUS) || \
PyArray_CHKFLAGS(m, NPY_ARRAY_F_CONTIGUOUS))

#define PyArray_ISFORTRAN(m) (PyArray_CHKFLAGS(m, NPY_FORTRAN) && \
#define PyArray_ISFORTRAN(m) (PyArray_CHKFLAGS(m, NPY_ARRAY_F_CONTIGUOUS) && \
(PyArray_NDIM(m) > 1))

#define PyArray_FORTRAN_IF(m) ((PyArray_CHKFLAGS(m, NPY_FORTRAN) ? \
NPY_FORTRAN : 0))
#define PyArray_FORTRAN_IF(m) ((PyArray_CHKFLAGS(m, NPY_ARRAY_F_CONTIGUOUS) ? \
NPY_ARRAY_F_CONTIGUOUS : 0))

#define FORTRAN_IF PyArray_FORTRAN_IF
#define PyArray_DATA(obj) ((void *)(((PyArrayObject *)(obj))->data))
Expand Down Expand Up @@ -1408,12 +1389,12 @@ PyArrayNeighborhoodIter_Next2D(PyArrayNeighborhoodIterObject* iter);
#define PyArray_FLAGSWAP(m, flags) (PyArray_CHKFLAGS(m, flags) && \
PyArray_ISNOTSWAPPED(m))

#define PyArray_ISCARRAY(m) PyArray_FLAGSWAP(m, NPY_CARRAY)
#define PyArray_ISCARRAY_RO(m) PyArray_FLAGSWAP(m, NPY_CARRAY_RO)
#define PyArray_ISFARRAY(m) PyArray_FLAGSWAP(m, NPY_FARRAY)
#define PyArray_ISFARRAY_RO(m) PyArray_FLAGSWAP(m, NPY_FARRAY_RO)
#define PyArray_ISBEHAVED(m) PyArray_FLAGSWAP(m, NPY_BEHAVED)
#define PyArray_ISBEHAVED_RO(m) PyArray_FLAGSWAP(m, NPY_ALIGNED)
#define PyArray_ISCARRAY(m) PyArray_FLAGSWAP(m, NPY_ARRAY_CARRAY)
#define PyArray_ISCARRAY_RO(m) PyArray_FLAGSWAP(m, NPY_ARRAY_CARRAY_RO)
#define PyArray_ISFARRAY(m) PyArray_FLAGSWAP(m, NPY_ARRAY_FARRAY)
#define PyArray_ISFARRAY_RO(m) PyArray_FLAGSWAP(m, NPY_ARRAY_FARRAY_RO)
#define PyArray_ISBEHAVED(m) PyArray_FLAGSWAP(m, NPY_ARRAY_BEHAVED)
#define PyArray_ISBEHAVED_RO(m) PyArray_FLAGSWAP(m, NPY_ARRAY_ALIGNED)


#define PyDataType_ISNOTSWAPPED(d) PyArray_ISNBO(((PyArray_Descr *)(d))->byteorder)
Expand Down Expand Up @@ -1443,7 +1424,7 @@ typedef struct {

int flags; /*
* how should be data interpreted. Valid
* flags are CONTIGUOUS (1), FORTRAN (2),
* flags are CONTIGUOUS (1), F_CONTIGUOUS (2),
* ALIGNED (0x100), NOTSWAPPED (0x200), and
* WRITEABLE (0x400). ARR_HAS_DESCR (0x800)
* states that arrdescr field is present in
Expand Down
62 changes: 62 additions & 0 deletions numpy/core/include/numpy/npy_deprecated_api.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#ifndef _NPY_DEPRECATED_API_H
#define _NPY_DEPRECATED_API_H

#if defined(_WIN32)
#define _WARN___STR2__(x) #x
#define _WARN___STR1__(x) _WARN___STR2__(x)
#define _WARN___LOC__ __FILE__ "("_WARN___STR1__(__LINE__)") : Warning Msg: "
#pragma message(_WARN___LOC__"Using deprecated NumPy API, disable it by " \
"#defining NPY_NO_DEPRECATED_API")
#elif defined(__GNUC__)
#warning "Using deprecated NumPy API, disable it by #defining NPY_NO_DEPRECATED_API"
#endif
/* TODO: How to do this warning message for other compilers? */

/*
* This header exists to collect all dangerous/deprecated NumPy API.
*
Expand All @@ -16,4 +27,55 @@
#define NPY_CONTIGUOUS NPY_ARRAY_C_CONTIGUOUS
#define NPY_FORTRAN NPY_ARRAY_F_CONTIGUOUS

/*
* The consistent NPY_ARRAY_* names which don't pollute the NPY_*
* namespace were added in NumPy 1.7.
*
* These versions of the carray flags are deprecated, but
* probably should only be removed after two releases instead of one.
*/
#define NPY_C_CONTIGUOUS NPY_ARRAY_C_CONTIGUOUS
#define NPY_F_CONTIGUOUS NPY_ARRAY_F_CONTIGUOUS
#define NPY_OWNDATA NPY_ARRAY_OWNDATA
#define NPY_FORCECAST NPY_ARRAY_FORCECAST
#define NPY_ENSURECOPY NPY_ARRAY_ENSURECOPY
#define NPY_ENSUREARRAY NPY_ARRAY_ENSUREARRAY
#define NPY_ELEMENTSTRIDES NPY_ARRAY_ELEMENTSTRIDES
#define NPY_ALIGNED NPY_ARRAY_ALIGNED
#define NPY_NOTSWAPPED NPY_ARRAY_NOTSWAPPED
#define NPY_WRITEABLE NPY_ARRAY_WRITEABLE
#define NPY_UPDATEIFCOPY NPY_ARRAY_UPDATEIFCOPY
#define NPY_BEHAVED NPY_ARRAY_BEHAVED
#define NPY_BEHAVED_NS NPY_ARRAY_BEHAVED_NS
#define NPY_CARRAY NPY_ARRAY_CARRAY
#define NPY_CARRAY_RO NPY_ARRAY_CARRAY_RO
#define NPY_FARRAY NPY_ARRAY_FARRAY
#define NPY_FARRAY_RO NPY_ARRAY_FARRAY_RO
#define NPY_DEFAULT NPY_ARRAY_DEFAULT
#define NPY_IN_ARRAY NPY_ARRAY_IN_ARRAY
#define NPY_OUT_ARRAY NPY_ARRAY_OUT_ARRAY
#define NPY_INOUT_ARRAY NPY_ARRAY_INOUT_ARRAY
#define NPY_IN_FARRAY NPY_ARRAY_IN_FARRAY
#define NPY_OUT_FARRAY NPY_ARRAY_OUT_FARRAY
#define NPY_INOUT_FARRAY NPY_ARRAY_INOUT_FARRAY
#define NPY_UPDATE_ALL NPY_ARRAY_UPDATE_ALL

/* This way of accessing the default type is deprecated as of NumPy 1.7 */
#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


#endif

0 comments on commit b00beab

Please sign in to comment.