summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorMark Wiebe <mwwiebe@gmail.com>2011-08-19 17:02:39 -0700
committerCharles Harris <charlesr.harris@gmail.com>2011-08-27 07:26:57 -0600
commit840323e81c41f02926a0d29b62b202a74b1fc2fa (patch)
tree7124ca119c17268cab6ff4236e1a3b8139219655 /numpy
parentc3fafa497c016303ef5f861d70ff3947111d2da8 (diff)
downloadnumpy-840323e81c41f02926a0d29b62b202a74b1fc2fa.tar.gz
STY: core: Move some misc converters into conversion_utils.c to clean things up
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/multiarray/conversion_utils.c217
-rw-r--r--numpy/core/src/multiarray/multiarraymodule.c158
-rw-r--r--numpy/core/src/multiarray/nditer_pywrap.c65
3 files changed, 220 insertions, 220 deletions
diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c
index 254b5389d..91b77ac9d 100644
--- a/numpy/core/src/multiarray/conversion_utils.c
+++ b/numpy/core/src/multiarray/conversion_utils.c
@@ -435,6 +435,223 @@ PyArray_SearchsideConverter(PyObject *obj, void *addr)
return PY_SUCCEED;
}
+/*NUMPY_API
+ * Convert an object to FORTRAN / C / ANY / KEEP
+ */
+NPY_NO_EXPORT int
+PyArray_OrderConverter(PyObject *object, NPY_ORDER *val)
+{
+ char *str;
+ /* Leave the desired default from the caller for NULL/Py_None */
+ if (object == NULL || object == Py_None) {
+ return PY_SUCCEED;
+ }
+ else if (PyUnicode_Check(object)) {
+ PyObject *tmp;
+ int ret;
+ tmp = PyUnicode_AsASCIIString(object);
+ ret = PyArray_OrderConverter(tmp, val);
+ Py_DECREF(tmp);
+ return ret;
+ }
+ else if (!PyBytes_Check(object) || PyBytes_GET_SIZE(object) < 1) {
+ if (PyObject_IsTrue(object)) {
+ *val = NPY_FORTRANORDER;
+ }
+ else {
+ *val = NPY_CORDER;
+ }
+ if (PyErr_Occurred()) {
+ return PY_FAIL;
+ }
+ return PY_SUCCEED;
+ }
+ else {
+ str = PyBytes_AS_STRING(object);
+ if (str[0] == 'C' || str[0] == 'c') {
+ *val = NPY_CORDER;
+ }
+ else if (str[0] == 'F' || str[0] == 'f') {
+ *val = NPY_FORTRANORDER;
+ }
+ else if (str[0] == 'A' || str[0] == 'a') {
+ *val = NPY_ANYORDER;
+ }
+ else if (str[0] == 'K' || str[0] == 'k') {
+ *val = NPY_KEEPORDER;
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "order not understood");
+ return PY_FAIL;
+ }
+ }
+ return PY_SUCCEED;
+}
+
+/*NUMPY_API
+ * Convert an object to NPY_RAISE / NPY_CLIP / NPY_WRAP
+ */
+NPY_NO_EXPORT int
+PyArray_ClipmodeConverter(PyObject *object, NPY_CLIPMODE *val)
+{
+ if (object == NULL || object == Py_None) {
+ *val = NPY_RAISE;
+ }
+ else if (PyBytes_Check(object)) {
+ char *str;
+ str = PyBytes_AS_STRING(object);
+ if (str[0] == 'C' || str[0] == 'c') {
+ *val = NPY_CLIP;
+ }
+ else if (str[0] == 'W' || str[0] == 'w') {
+ *val = NPY_WRAP;
+ }
+ else if (str[0] == 'R' || str[0] == 'r') {
+ *val = NPY_RAISE;
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "clipmode not understood");
+ return PY_FAIL;
+ }
+ }
+ else if (PyUnicode_Check(object)) {
+ PyObject *tmp;
+ int ret;
+ tmp = PyUnicode_AsASCIIString(object);
+ ret = PyArray_ClipmodeConverter(tmp, val);
+ Py_DECREF(tmp);
+ return ret;
+ }
+ else {
+ int number = PyInt_AsLong(object);
+ if (number == -1 && PyErr_Occurred()) {
+ goto fail;
+ }
+ if (number <= (int) NPY_RAISE
+ && number >= (int) NPY_CLIP) {
+ *val = (NPY_CLIPMODE) number;
+ }
+ else {
+ goto fail;
+ }
+ }
+ return PY_SUCCEED;
+
+ fail:
+ PyErr_SetString(PyExc_TypeError,
+ "clipmode not understood");
+ return PY_FAIL;
+}
+
+/*NUMPY_API
+ * Convert an object to an array of n NPY_CLIPMODE values.
+ * This is intended to be used in functions where a different mode
+ * could be applied to each axis, like in ravel_multi_index.
+ */
+NPY_NO_EXPORT int
+PyArray_ConvertClipmodeSequence(PyObject *object, NPY_CLIPMODE *modes, int n)
+{
+ int i;
+ /* Get the clip mode(s) */
+ if (object && (PyTuple_Check(object) || PyList_Check(object))) {
+ if (PySequence_Size(object) != n) {
+ PyErr_Format(PyExc_ValueError,
+ "list of clipmodes has wrong length (%d instead of %d)",
+ (int)PySequence_Size(object), n);
+ return PY_FAIL;
+ }
+
+ for (i = 0; i < n; ++i) {
+ PyObject *item = PySequence_GetItem(object, i);
+ if(item == NULL) {
+ return PY_FAIL;
+ }
+
+ if(PyArray_ClipmodeConverter(item, &modes[i]) != PY_SUCCEED) {
+ Py_DECREF(item);
+ return PY_FAIL;
+ }
+
+ Py_DECREF(item);
+ }
+ }
+ else if (PyArray_ClipmodeConverter(object, &modes[0]) == PY_SUCCEED) {
+ for (i = 1; i < n; ++i) {
+ modes[i] = modes[0];
+ }
+ }
+ else {
+ return PY_FAIL;
+ }
+ return PY_SUCCEED;
+}
+
+/*NUMPY_API
+ * Convert any Python object, *obj*, to an NPY_CASTING enum.
+ */
+NPY_NO_EXPORT int
+PyArray_CastingConverter(PyObject *obj, NPY_CASTING *casting)
+{
+ char *str = NULL;
+ Py_ssize_t length = 0;
+
+ if (PyUnicode_Check(obj)) {
+ PyObject *str_obj;
+ int ret;
+ str_obj = PyUnicode_AsASCIIString(obj);
+ if (str_obj == NULL) {
+ return 0;
+ }
+ ret = PyArray_CastingConverter(str_obj, casting);
+ Py_DECREF(str_obj);
+ return ret;
+ }
+
+ if (PyBytes_AsStringAndSize(obj, &str, &length) == -1) {
+ return 0;
+ }
+
+ if (length >= 2) switch (str[2]) {
+ case 0:
+ if (strcmp(str, "no") == 0) {
+ *casting = NPY_NO_CASTING;
+ return 1;
+ }
+ break;
+ case 'u':
+ if (strcmp(str, "equiv") == 0) {
+ *casting = NPY_EQUIV_CASTING;
+ return 1;
+ }
+ break;
+ case 'f':
+ if (strcmp(str, "safe") == 0) {
+ *casting = NPY_SAFE_CASTING;
+ return 1;
+ }
+ break;
+ case 'm':
+ if (strcmp(str, "same_kind") == 0) {
+ *casting = NPY_SAME_KIND_CASTING;
+ return 1;
+ }
+ break;
+ case 's':
+ if (strcmp(str, "unsafe") == 0) {
+ *casting = NPY_UNSAFE_CASTING;
+ return 1;
+ }
+ break;
+ }
+
+ PyErr_SetString(PyExc_ValueError,
+ "casting must be one of 'no', 'equiv', 'safe', "
+ "'same_kind', or 'unsafe'");
+ return 0;
+}
+
/*****************************
* Other conversion functions
*****************************/
diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c
index 26709d290..566036146 100644
--- a/numpy/core/src/multiarray/multiarraymodule.c
+++ b/numpy/core/src/multiarray/multiarraymodule.c
@@ -470,7 +470,7 @@ PyArray_ConcatenateArrays(int narrays, PyArrayObject **arrays, int axis)
}
/*
- * Concatenates a list of ndarrays, flattening each in C-order.
+ * Concatenates a list of ndarrays, flattening each in the specified order.
*/
NPY_NO_EXPORT PyArrayObject *
PyArray_ConcatenateFlattenedArrays(int narrays, PyArrayObject **arrays,
@@ -1123,7 +1123,8 @@ PyArray_CopyAndTranspose(PyObject *op)
}
/*
- * Implementation which is common between PyArray_Correlate and PyArray_Correlate2
+ * Implementation which is common between PyArray_Correlate
+ * and PyArray_Correlate2.
*
* inverted is set to 1 if computed correlate(ap2, ap1), 0 otherwise
*/
@@ -1411,159 +1412,6 @@ array_putmask(PyObject *NPY_UNUSED(module), PyObject *args, PyObject *kwds)
return PyArray_PutMask((PyArrayObject *)array, values, mask);
}
-/*NUMPY_API
- * Convert an object to FORTRAN / C / ANY / KEEP
- */
-NPY_NO_EXPORT int
-PyArray_OrderConverter(PyObject *object, NPY_ORDER *val)
-{
- char *str;
- /* Leave the desired default from the caller for NULL/Py_None */
- if (object == NULL || object == Py_None) {
- return PY_SUCCEED;
- }
- else if (PyUnicode_Check(object)) {
- PyObject *tmp;
- int ret;
- tmp = PyUnicode_AsASCIIString(object);
- ret = PyArray_OrderConverter(tmp, val);
- Py_DECREF(tmp);
- return ret;
- }
- else if (!PyBytes_Check(object) || PyBytes_GET_SIZE(object) < 1) {
- if (PyObject_IsTrue(object)) {
- *val = NPY_FORTRANORDER;
- }
- else {
- *val = NPY_CORDER;
- }
- if (PyErr_Occurred()) {
- return PY_FAIL;
- }
- return PY_SUCCEED;
- }
- else {
- str = PyBytes_AS_STRING(object);
- if (str[0] == 'C' || str[0] == 'c') {
- *val = NPY_CORDER;
- }
- else if (str[0] == 'F' || str[0] == 'f') {
- *val = NPY_FORTRANORDER;
- }
- else if (str[0] == 'A' || str[0] == 'a') {
- *val = NPY_ANYORDER;
- }
- else if (str[0] == 'K' || str[0] == 'k') {
- *val = NPY_KEEPORDER;
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "order not understood");
- return PY_FAIL;
- }
- }
- return PY_SUCCEED;
-}
-
-/*NUMPY_API
- * Convert an object to NPY_RAISE / NPY_CLIP / NPY_WRAP
- */
-NPY_NO_EXPORT int
-PyArray_ClipmodeConverter(PyObject *object, NPY_CLIPMODE *val)
-{
- if (object == NULL || object == Py_None) {
- *val = NPY_RAISE;
- }
- else if (PyBytes_Check(object)) {
- char *str;
- str = PyBytes_AS_STRING(object);
- if (str[0] == 'C' || str[0] == 'c') {
- *val = NPY_CLIP;
- }
- else if (str[0] == 'W' || str[0] == 'w') {
- *val = NPY_WRAP;
- }
- else if (str[0] == 'R' || str[0] == 'r') {
- *val = NPY_RAISE;
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "clipmode not understood");
- return PY_FAIL;
- }
- }
- else if (PyUnicode_Check(object)) {
- PyObject *tmp;
- int ret;
- tmp = PyUnicode_AsASCIIString(object);
- ret = PyArray_ClipmodeConverter(tmp, val);
- Py_DECREF(tmp);
- return ret;
- }
- else {
- int number = PyInt_AsLong(object);
- if (number == -1 && PyErr_Occurred()) {
- goto fail;
- }
- if (number <= (int) NPY_RAISE
- && number >= (int) NPY_CLIP) {
- *val = (NPY_CLIPMODE) number;
- }
- else {
- goto fail;
- }
- }
- return PY_SUCCEED;
-
- fail:
- PyErr_SetString(PyExc_TypeError,
- "clipmode not understood");
- return PY_FAIL;
-}
-
-/*NUMPY_API
- * Convert an object to an array of n NPY_CLIPMODE values.
- * This is intended to be used in functions where a different mode
- * could be applied to each axis, like in ravel_multi_index.
- */
-NPY_NO_EXPORT int
-PyArray_ConvertClipmodeSequence(PyObject *object, NPY_CLIPMODE *modes, int n)
-{
- int i;
- /* Get the clip mode(s) */
- if (object && (PyTuple_Check(object) || PyList_Check(object))) {
- if (PySequence_Size(object) != n) {
- PyErr_Format(PyExc_ValueError,
- "list of clipmodes has wrong length (%d instead of %d)",
- (int)PySequence_Size(object), n);
- return PY_FAIL;
- }
-
- for (i = 0; i < n; ++i) {
- PyObject *item = PySequence_GetItem(object, i);
- if(item == NULL) {
- return PY_FAIL;
- }
-
- if(PyArray_ClipmodeConverter(item, &modes[i]) != PY_SUCCEED) {
- Py_DECREF(item);
- return PY_FAIL;
- }
-
- Py_DECREF(item);
- }
- }
- else if (PyArray_ClipmodeConverter(object, &modes[0]) == PY_SUCCEED) {
- for (i = 1; i < n; ++i) {
- modes[i] = modes[0];
- }
- }
- else {
- return PY_FAIL;
- }
- return PY_SUCCEED;
-}
-
/*
* Compare the field dictionaries for two types.
*
diff --git a/numpy/core/src/multiarray/nditer_pywrap.c b/numpy/core/src/multiarray/nditer_pywrap.c
index b2ce78c11..2929ef2fc 100644
--- a/numpy/core/src/multiarray/nditer_pywrap.c
+++ b/numpy/core/src/multiarray/nditer_pywrap.c
@@ -256,71 +256,6 @@ npyiter_order_converter(PyObject *order_in, NPY_ORDER *order)
return 0;
}
-/*NUMPY_API
- * Convert any Python object, *obj*, to an NPY_CASTING enum.
- * TODO: Move elsewhere
- */
-NPY_NO_EXPORT int
-PyArray_CastingConverter(PyObject *obj, NPY_CASTING *casting)
-{
- char *str = NULL;
- Py_ssize_t length = 0;
-
- if (PyUnicode_Check(obj)) {
- PyObject *str_obj;
- int ret;
- str_obj = PyUnicode_AsASCIIString(obj);
- if (str_obj == NULL) {
- return 0;
- }
- ret = PyArray_CastingConverter(str_obj, casting);
- Py_DECREF(str_obj);
- return ret;
- }
-
- if (PyBytes_AsStringAndSize(obj, &str, &length) == -1) {
- return 0;
- }
-
- if (length >= 2) switch (str[2]) {
- case 0:
- if (strcmp(str, "no") == 0) {
- *casting = NPY_NO_CASTING;
- return 1;
- }
- break;
- case 'u':
- if (strcmp(str, "equiv") == 0) {
- *casting = NPY_EQUIV_CASTING;
- return 1;
- }
- break;
- case 'f':
- if (strcmp(str, "safe") == 0) {
- *casting = NPY_SAFE_CASTING;
- return 1;
- }
- break;
- case 'm':
- if (strcmp(str, "same_kind") == 0) {
- *casting = NPY_SAME_KIND_CASTING;
- return 1;
- }
- break;
- case 's':
- if (strcmp(str, "unsafe") == 0) {
- *casting = NPY_UNSAFE_CASTING;
- return 1;
- }
- break;
- }
-
- PyErr_SetString(PyExc_ValueError,
- "casting must be one of 'no', 'equiv', 'safe', "
- "'same_kind', or 'unsafe'");
- return 0;
-}
-
static int
NpyIter_OpFlagsConverter(PyObject *op_flags_in,
npy_uint32 *op_flags)