summaryrefslogtreecommitdiff
path: root/numpy/core
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/core')
-rw-r--r--numpy/core/src/multiarray/methods.c254
-rw-r--r--numpy/core/src/multiarray/multiarraymodule.c167
2 files changed, 238 insertions, 183 deletions
diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c
index 04ce53ed7..ff5a5d8bc 100644
--- a/numpy/core/src/multiarray/methods.c
+++ b/numpy/core/src/multiarray/methods.c
@@ -10,6 +10,7 @@
#include "numpy/arrayscalars.h"
#include "arrayfunction_override.h"
+#include "npy_argparse.h"
#include "npy_config.h"
#include "npy_pycompat.h"
#include "npy_import.h"
@@ -103,20 +104,23 @@ forward_ndarray_method(PyArrayObject *self, PyObject *args, PyObject *kwds,
static PyObject *
-array_take(PyArrayObject *self, PyObject *args, PyObject *kwds)
+array_take(PyArrayObject *self,
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
int dimension = NPY_MAXDIMS;
PyObject *indices;
PyArrayObject *out = NULL;
NPY_CLIPMODE mode = NPY_RAISE;
- static char *kwlist[] = {"indices", "axis", "out", "mode", NULL};
+ NPY_PREPARE_ARGPARSER;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O&O&O&:take", kwlist,
- &indices,
- PyArray_AxisConverter, &dimension,
- PyArray_OutputConverter, &out,
- PyArray_ClipmodeConverter, &mode))
+ if (npy_parse_arguments("take", args, len_args, kwnames,
+ "indices", NULL, &indices,
+ "|axis", &PyArray_AxisConverter, &dimension,
+ "|out", &PyArray_OutputConverter, &out,
+ "|mode", &PyArray_ClipmodeConverter, &mode,
+ NULL, NULL, NULL) < 0) {
return NULL;
+ }
PyObject *ret = PyArray_TakeFrom(self, indices, dimension, out, mode);
@@ -199,14 +203,16 @@ array_reshape(PyArrayObject *self, PyObject *args, PyObject *kwds)
}
static PyObject *
-array_squeeze(PyArrayObject *self, PyObject *args, PyObject *kwds)
+array_squeeze(PyArrayObject *self,
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
PyObject *axis_in = NULL;
npy_bool axis_flags[NPY_MAXDIMS];
+ NPY_PREPARE_ARGPARSER;
- static char *kwlist[] = {"axis", NULL};
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:squeeze", kwlist,
- &axis_in)) {
+ if (npy_parse_arguments("squeeze", args, len_args, kwnames,
+ "|axis", NULL, &axis_in,
+ NULL, NULL, NULL) < 0) {
return NULL;
}
@@ -224,16 +230,18 @@ array_squeeze(PyArrayObject *self, PyObject *args, PyObject *kwds)
}
static PyObject *
-array_view(PyArrayObject *self, PyObject *args, PyObject *kwds)
+array_view(PyArrayObject *self,
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
PyObject *out_dtype = NULL;
PyObject *out_type = NULL;
PyArray_Descr *dtype = NULL;
+ NPY_PREPARE_ARGPARSER;
- static char *kwlist[] = {"dtype", "type", NULL};
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:view", kwlist,
- &out_dtype,
- &out_type)) {
+ if (npy_parse_arguments("view", args, len_args, kwnames,
+ "|dtype", NULL, &out_dtype,
+ "|type", NULL, &out_type,
+ NULL, NULL, NULL) < 0) {
return NULL;
}
@@ -271,16 +279,19 @@ array_view(PyArrayObject *self, PyObject *args, PyObject *kwds)
}
static PyObject *
-array_argmax(PyArrayObject *self, PyObject *args, PyObject *kwds)
+array_argmax(PyArrayObject *self,
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
int axis = NPY_MAXDIMS;
PyArrayObject *out = NULL;
- static char *kwlist[] = {"axis", "out", NULL};
+ NPY_PREPARE_ARGPARSER;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&:argmax", kwlist,
- PyArray_AxisConverter, &axis,
- PyArray_OutputConverter, &out))
+ if (npy_parse_arguments("argmax", args, len_args, kwnames,
+ "|axis", &PyArray_AxisConverter, &axis,
+ "|out", &PyArray_OutputConverter, &out,
+ NULL, NULL, NULL) < 0) {
return NULL;
+ }
PyObject *ret = PyArray_ArgMax(self, axis, out);
@@ -294,16 +305,19 @@ array_argmax(PyArrayObject *self, PyObject *args, PyObject *kwds)
}
static PyObject *
-array_argmin(PyArrayObject *self, PyObject *args, PyObject *kwds)
+array_argmin(PyArrayObject *self,
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
int axis = NPY_MAXDIMS;
PyArrayObject *out = NULL;
- static char *kwlist[] = {"axis", "out", NULL};
+ NPY_PREPARE_ARGPARSER;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&:argmin", kwlist,
- PyArray_AxisConverter, &axis,
- PyArray_OutputConverter, &out))
+ if (npy_parse_arguments("argmin", args, len_args, kwnames,
+ "|axis", &PyArray_AxisConverter, &axis,
+ "|out", &PyArray_OutputConverter, &out,
+ NULL, NULL, NULL) < 0) {
return NULL;
+ }
PyObject *ret = PyArray_ArgMin(self, axis, out);
@@ -804,10 +818,9 @@ array_setscalar(PyArrayObject *self, PyObject *args)
static PyObject *
-array_astype(PyArrayObject *self, PyObject *args, PyObject *kwds)
+array_astype(PyArrayObject *self,
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
- static char *kwlist[] = {"dtype", "order", "casting",
- "subok", "copy", NULL};
PyArray_Descr *dtype = NULL;
/*
* TODO: UNSAFE default for compatibility, I think
@@ -816,13 +829,15 @@ array_astype(PyArrayObject *self, PyObject *args, PyObject *kwds)
NPY_CASTING casting = NPY_UNSAFE_CASTING;
NPY_ORDER order = NPY_KEEPORDER;
int forcecopy = 1, subok = 1;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|O&O&ii:astype", kwlist,
- PyArray_DescrConverter, &dtype,
- PyArray_OrderConverter, &order,
- PyArray_CastingConverter, &casting,
- &subok,
- &forcecopy)) {
+ NPY_PREPARE_ARGPARSER;
+
+ if (npy_parse_arguments("astype", args, len_args, kwnames,
+ "dtype", &PyArray_DescrConverter, &dtype,
+ "|order", &PyArray_OrderConverter, &order,
+ "|casting", &PyArray_CastingConverter, &casting,
+ "|subok", &PyArray_PythonPyIntFromInt, &subok,
+ "|copy", &PyArray_PythonPyIntFromInt, &forcecopy,
+ NULL, NULL, NULL) < 0) {
Py_XDECREF(dtype);
return NULL;
}
@@ -1143,13 +1158,15 @@ array_function(PyArrayObject *NPY_UNUSED(self), PyObject *c_args, PyObject *c_kw
}
static PyObject *
-array_copy(PyArrayObject *self, PyObject *args, PyObject *kwds)
+array_copy(PyArrayObject *self,
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
NPY_ORDER order = NPY_CORDER;
- static char *kwlist[] = {"order", NULL};
+ NPY_PREPARE_ARGPARSER;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&:copy", kwlist,
- PyArray_OrderConverter, &order)) {
+ if (npy_parse_arguments("copy", args, len_args, kwnames,
+ "|order", PyArray_OrderConverter, &order,
+ NULL, NULL, NULL) < 0) {
return NULL;
}
@@ -1257,7 +1274,8 @@ array_choose(PyArrayObject *self, PyObject *args, PyObject *kwds)
}
static PyObject *
-array_sort(PyArrayObject *self, PyObject *args, PyObject *kwds)
+array_sort(PyArrayObject *self,
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
int axis=-1;
int val;
@@ -1265,12 +1283,13 @@ array_sort(PyArrayObject *self, PyObject *args, PyObject *kwds)
PyObject *order = NULL;
PyArray_Descr *saved = NULL;
PyArray_Descr *newd;
- static char *kwlist[] = {"axis", "kind", "order", NULL};
+ NPY_PREPARE_ARGPARSER;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO&O:sort", kwlist,
- &axis,
- PyArray_SortkindConverter, &sortkind,
- &order)) {
+ if (npy_parse_arguments("sort", args, len_args, kwnames,
+ "|axis", &PyArray_PythonPyIntFromInt, &axis,
+ "|kind", &PyArray_SortkindConverter, &sortkind,
+ "|order", NULL, &order,
+ NULL, NULL, NULL) < 0) {
return NULL;
}
if (order == Py_None) {
@@ -1313,7 +1332,8 @@ array_sort(PyArrayObject *self, PyObject *args, PyObject *kwds)
}
static PyObject *
-array_partition(PyArrayObject *self, PyObject *args, PyObject *kwds)
+array_partition(PyArrayObject *self,
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
int axis=-1;
int val;
@@ -1321,16 +1341,16 @@ array_partition(PyArrayObject *self, PyObject *args, PyObject *kwds)
PyObject *order = NULL;
PyArray_Descr *saved = NULL;
PyArray_Descr *newd;
- static char *kwlist[] = {"kth", "axis", "kind", "order", NULL};
PyArrayObject * ktharray;
PyObject * kthobj;
+ NPY_PREPARE_ARGPARSER;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|iO&O:partition", kwlist,
- &kthobj,
- &axis,
- PyArray_SelectkindConverter, &sortkind,
- &order)) {
+ if (npy_parse_arguments("partition", args, len_args, kwnames,
+ "kth", NULL, &kthobj,
+ "|axis", &PyArray_PythonPyIntFromInt, &axis,
+ "|kind", &PyArray_SelectkindConverter, &sortkind,
+ "|order", NULL, &order,
+ NULL, NULL, NULL) < 0) {
return NULL;
}
@@ -1381,18 +1401,20 @@ array_partition(PyArrayObject *self, PyObject *args, PyObject *kwds)
}
static PyObject *
-array_argsort(PyArrayObject *self, PyObject *args, PyObject *kwds)
+array_argsort(PyArrayObject *self,
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
int axis = -1;
NPY_SORTKIND sortkind = NPY_QUICKSORT;
PyObject *order = NULL, *res;
PyArray_Descr *newd, *saved=NULL;
- static char *kwlist[] = {"axis", "kind", "order", NULL};
+ NPY_PREPARE_ARGPARSER;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O:argsort", kwlist,
- PyArray_AxisConverter, &axis,
- PyArray_SortkindConverter, &sortkind,
- &order)) {
+ if (npy_parse_arguments("argsort", args, len_args, kwnames,
+ "|axis", &PyArray_AxisConverter, &axis,
+ "|kind", &PyArray_SortkindConverter, &sortkind,
+ "|order", NULL, &order,
+ NULL, NULL, NULL) < 0) {
return NULL;
}
if (order == Py_None) {
@@ -1433,21 +1455,23 @@ array_argsort(PyArrayObject *self, PyObject *args, PyObject *kwds)
static PyObject *
-array_argpartition(PyArrayObject *self, PyObject *args, PyObject *kwds)
+array_argpartition(PyArrayObject *self,
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
int axis = -1;
NPY_SELECTKIND sortkind = NPY_INTROSELECT;
PyObject *order = NULL, *res;
PyArray_Descr *newd, *saved=NULL;
- static char *kwlist[] = {"kth", "axis", "kind", "order", NULL};
PyObject * kthobj;
PyArrayObject * ktharray;
+ NPY_PREPARE_ARGPARSER;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O&O&O:argpartition", kwlist,
- &kthobj,
- PyArray_AxisConverter, &axis,
- PyArray_SelectkindConverter, &sortkind,
- &order)) {
+ if (npy_parse_arguments("argpartition", args, len_args, kwnames,
+ "kth", NULL, &kthobj,
+ "|axis", &PyArray_AxisConverter, &axis,
+ "|kind", &PyArray_SelectkindConverter, &sortkind,
+ "|order", NULL, &order,
+ NULL, NULL, NULL) < 0) {
return NULL;
}
if (order == Py_None) {
@@ -1494,17 +1518,20 @@ array_argpartition(PyArrayObject *self, PyObject *args, PyObject *kwds)
}
static PyObject *
-array_searchsorted(PyArrayObject *self, PyObject *args, PyObject *kwds)
+array_searchsorted(PyArrayObject *self,
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
- static char *kwlist[] = {"v", "side", "sorter", NULL};
PyObject *keys;
PyObject *sorter;
NPY_SEARCHSIDE side = NPY_SEARCHLEFT;
+ NPY_PREPARE_ARGPARSER;
sorter = NULL;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O&O:searchsorted",
- kwlist, &keys,
- PyArray_SearchsideConverter, &side, &sorter)) {
+ if (npy_parse_arguments("searchsorted", args, len_args, kwnames,
+ "v", NULL, &keys,
+ "|side", &PyArray_SearchsideConverter, &side,
+ "|sorter", NULL, &sorter,
+ NULL, NULL, NULL) < 0) {
return NULL;
}
if (sorter == Py_None) {
@@ -2285,14 +2312,17 @@ array_cumprod(PyArrayObject *self, PyObject *args, PyObject *kwds)
static PyObject *
-array_dot(PyArrayObject *self, PyObject *args, PyObject *kwds)
+array_dot(PyArrayObject *self,
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
PyObject *a = (PyObject *)self, *b, *o = NULL;
PyArrayObject *ret;
- static char* kwlist[] = {"b", "out", NULL};
+ NPY_PREPARE_ARGPARSER;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:dot", kwlist, &b, &o)) {
+ if (npy_parse_arguments("dot", args, len_args, kwnames,
+ "b", NULL, &b,
+ "|out", NULL, &o,
+ NULL, NULL, NULL) < 0) {
return NULL;
}
@@ -2374,20 +2404,22 @@ array_nonzero(PyArrayObject *self, PyObject *args)
static PyObject *
-array_trace(PyArrayObject *self, PyObject *args, PyObject *kwds)
+array_trace(PyArrayObject *self,
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
int axis1 = 0, axis2 = 1, offset = 0;
PyArray_Descr *dtype = NULL;
PyArrayObject *out = NULL;
int rtype;
- static char *kwlist[] = {"offset", "axis1", "axis2", "dtype", "out", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iiiO&O&:trace", kwlist,
- &offset,
- &axis1,
- &axis2,
- PyArray_DescrConverter2, &dtype,
- PyArray_OutputConverter, &out)) {
+ NPY_PREPARE_ARGPARSER;
+
+ if (npy_parse_arguments("trace", args, len_args, kwnames,
+ "|offset", &PyArray_PythonPyIntFromInt, &offset,
+ "|axis1", &PyArray_PythonPyIntFromInt, &axis1,
+ "|axis2", &PyArray_PythonPyIntFromInt, &axis2,
+ "|dtype", &PyArray_DescrConverter2, &dtype,
+ "|out", &PyArray_OutputConverter, &out,
+ NULL, NULL, NULL) < 0) {
Py_XDECREF(dtype);
return NULL;
}
@@ -2448,13 +2480,15 @@ array_diagonal(PyArrayObject *self, PyObject *args, PyObject *kwds)
static PyObject *
-array_flatten(PyArrayObject *self, PyObject *args, PyObject *kwds)
+array_flatten(PyArrayObject *self,
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
NPY_ORDER order = NPY_CORDER;
- static char *kwlist[] = {"order", NULL};
+ NPY_PREPARE_ARGPARSER;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&:flatten", kwlist,
- PyArray_OrderConverter, &order)) {
+ if (npy_parse_arguments("flatten", args, len_args, kwnames,
+ "|order", PyArray_OrderConverter, &order,
+ NULL, NULL, NULL) < 0) {
return NULL;
}
return PyArray_Flatten(self, order);
@@ -2462,13 +2496,15 @@ array_flatten(PyArrayObject *self, PyObject *args, PyObject *kwds)
static PyObject *
-array_ravel(PyArrayObject *self, PyObject *args, PyObject *kwds)
+array_ravel(PyArrayObject *self,
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
NPY_ORDER order = NPY_CORDER;
- static char *kwlist[] = {"order", NULL};
+ NPY_PREPARE_ARGPARSER;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&:ravel", kwlist,
- PyArray_OrderConverter, &order)) {
+ if (npy_parse_arguments("ravel", args, len_args, kwnames,
+ "|order", PyArray_OrderConverter, &order,
+ NULL, NULL, NULL) < 0) {
return NULL;
}
return PyArray_Ravel(self, order);
@@ -2724,19 +2760,19 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = {
METH_VARARGS | METH_KEYWORDS, NULL},
{"argmax",
(PyCFunction)array_argmax,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"argmin",
(PyCFunction)array_argmin,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"argpartition",
(PyCFunction)array_argpartition,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"argsort",
(PyCFunction)array_argsort,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"astype",
(PyCFunction)array_astype,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"byteswap",
(PyCFunction)array_byteswap,
METH_VARARGS | METH_KEYWORDS, NULL},
@@ -2757,7 +2793,7 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = {
METH_VARARGS, NULL},
{"copy",
(PyCFunction)array_copy,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"cumprod",
(PyCFunction)array_cumprod,
METH_VARARGS | METH_KEYWORDS, NULL},
@@ -2769,13 +2805,13 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = {
METH_VARARGS | METH_KEYWORDS, NULL},
{"dot",
(PyCFunction)array_dot,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"fill",
(PyCFunction)array_fill,
METH_VARARGS, NULL},
{"flatten",
(PyCFunction)array_flatten,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"getfield",
(PyCFunction)array_getfield,
METH_VARARGS | METH_KEYWORDS, NULL},
@@ -2802,7 +2838,7 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = {
METH_VARARGS, NULL},
{"partition",
(PyCFunction)array_partition,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"prod",
(PyCFunction)array_prod,
METH_VARARGS | METH_KEYWORDS, NULL},
@@ -2814,7 +2850,7 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = {
METH_VARARGS | METH_KEYWORDS, NULL},
{"ravel",
(PyCFunction)array_ravel,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"repeat",
(PyCFunction)array_repeat,
METH_VARARGS | METH_KEYWORDS, NULL},
@@ -2829,7 +2865,7 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = {
METH_VARARGS | METH_KEYWORDS, NULL},
{"searchsorted",
(PyCFunction)array_searchsorted,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"setfield",
(PyCFunction)array_setfield,
METH_VARARGS | METH_KEYWORDS, NULL},
@@ -2838,10 +2874,10 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = {
METH_VARARGS | METH_KEYWORDS, NULL},
{"sort",
(PyCFunction)array_sort,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"squeeze",
(PyCFunction)array_squeeze,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"std",
(PyCFunction)array_stddev,
METH_VARARGS | METH_KEYWORDS, NULL},
@@ -2853,7 +2889,7 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = {
METH_VARARGS, NULL},
{"take",
(PyCFunction)array_take,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"tobytes",
(PyCFunction)array_tobytes,
METH_VARARGS | METH_KEYWORDS, NULL},
@@ -2868,7 +2904,7 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = {
METH_VARARGS | METH_KEYWORDS, NULL},
{"trace",
(PyCFunction)array_trace,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"transpose",
(PyCFunction)array_transpose,
METH_VARARGS, NULL},
@@ -2877,6 +2913,6 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = {
METH_VARARGS | METH_KEYWORDS, NULL},
{"view",
(PyCFunction)array_view,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{NULL, NULL, 0, NULL} /* sentinel */
};
diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c
index 7915c75be..a0f7afeb5 100644
--- a/numpy/core/src/multiarray/multiarraymodule.c
+++ b/numpy/core/src/multiarray/multiarraymodule.c
@@ -26,7 +26,7 @@
#include "numpy/arrayscalars.h"
#include "numpy/npy_math.h"
-
+#include "npy_argparse.h"
#include "npy_config.h"
#include "npy_pycompat.h"
#include "npy_import.h"
@@ -2829,28 +2829,36 @@ array_fastCopyAndTranspose(PyObject *NPY_UNUSED(dummy), PyObject *args)
}
static PyObject *
-array_correlate(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds)
+array_correlate(PyObject *NPY_UNUSED(dummy),
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
PyObject *shape, *a0;
int mode = 0;
- static char *kwlist[] = {"a", "v", "mode", NULL};
+ NPY_PREPARE_ARGPARSER;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|i:correlate", kwlist,
- &a0, &shape, &mode)) {
+ if (npy_parse_arguments("correlate", args, len_args, kwnames,
+ "a", NULL, &a0,
+ "v", NULL, &shape,
+ "|mode", &PyArray_PythonPyIntFromInt, &mode,
+ NULL, NULL, NULL) < 0) {
return NULL;
}
return PyArray_Correlate(a0, shape, mode);
}
static PyObject*
-array_correlate2(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds)
+array_correlate2(PyObject *NPY_UNUSED(dummy),
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
PyObject *shape, *a0;
int mode = 0;
- static char *kwlist[] = {"a", "v", "mode", NULL};
+ NPY_PREPARE_ARGPARSER;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|i:correlate2", kwlist,
- &a0, &shape, &mode)) {
+ if (npy_parse_arguments("correlate2", args, len_args, kwnames,
+ "a", NULL, &a0,
+ "v", NULL, &shape,
+ "|mode", &PyArray_PythonPyIntFromInt, &mode,
+ NULL, NULL, NULL) < 0) {
return NULL;
}
return PyArray_Correlate2(a0, shape, mode);
@@ -3426,6 +3434,42 @@ array_datetime_data(PyObject *NPY_UNUSED(dummy), PyObject *args)
return res;
}
+
+static int
+trimmode_converter(PyObject *obj, TrimMode *trim)
+{
+ if (!PyUnicode_Check(obj) || PyUnicode_GetLength(obj) != 1) {
+ goto error;
+ }
+ const char *trimstr = PyUnicode_AsUTF8AndSize(obj, NULL);
+
+ if (trimstr != NULL) {
+ if (trimstr[0] == 'k') {
+ *trim = TrimMode_None;
+ }
+ else if (trimstr[0] == '.') {
+ *trim = TrimMode_Zeros;
+ }
+ else if (trimstr[0] == '0') {
+ *trim = TrimMode_LeaveOneZero;
+ }
+ else if (trimstr[0] == '-') {
+ *trim = TrimMode_DptZeros;
+ }
+ else {
+ goto error;
+ }
+ }
+ return NPY_SUCCEED;
+
+error:
+ PyErr_Format(PyExc_TypeError,
+ "if supplied, trim must be 'k', '.', '0' or '-' found `%100S`",
+ obj);
+ return NPY_FAIL;
+}
+
+
/*
* Prints floating-point scalars using the Dragon4 algorithm, scientific mode.
* See docstring of `np.format_float_scientific` for description of arguments.
@@ -3433,43 +3477,28 @@ array_datetime_data(PyObject *NPY_UNUSED(dummy), PyObject *args)
* precision, which is equivalent to `None`.
*/
static PyObject *
-dragon4_scientific(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds)
+dragon4_scientific(PyObject *NPY_UNUSED(dummy),
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
PyObject *obj;
- static char *kwlist[] = {"x", "precision", "unique", "sign", "trim",
- "pad_left", "exp_digits", NULL};
int precision=-1, pad_left=-1, exp_digits=-1;
- char *trimstr=NULL;
DigitMode digit_mode;
TrimMode trim = TrimMode_None;
int sign=0, unique=1;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|iiisii:dragon4_scientific",
- kwlist, &obj, &precision, &unique, &sign, &trimstr, &pad_left,
- &exp_digits)) {
+ NPY_PREPARE_ARGPARSER;
+
+ if (npy_parse_arguments("dragon4_scientific", args, len_args, kwnames,
+ "x", NULL , &obj,
+ "|precision", &PyArray_PythonPyIntFromInt, &precision,
+ "|unique", &PyArray_PythonPyIntFromInt, &unique,
+ "|sign", &PyArray_PythonPyIntFromInt, &sign,
+ "|trim", &trimmode_converter, &trim,
+ "|pad_left", &PyArray_PythonPyIntFromInt, &pad_left,
+ "|exp_digits", &PyArray_PythonPyIntFromInt, &exp_digits,
+ NULL, NULL, NULL) < 0) {
return NULL;
}
- if (trimstr != NULL) {
- if (strcmp(trimstr, "k") == 0) {
- trim = TrimMode_None;
- }
- else if (strcmp(trimstr, ".") == 0) {
- trim = TrimMode_Zeros;
- }
- else if (strcmp(trimstr, "0") == 0) {
- trim = TrimMode_LeaveOneZero;
- }
- else if (strcmp(trimstr, "-") == 0) {
- trim = TrimMode_DptZeros;
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "if supplied, trim must be 'k', '.', '0' or '-'");
- return NULL;
- }
- }
-
digit_mode = unique ? DigitMode_Unique : DigitMode_Exact;
if (unique == 0 && precision < 0) {
@@ -3489,44 +3518,30 @@ dragon4_scientific(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds)
* precision, which is equivalent to `None`.
*/
static PyObject *
-dragon4_positional(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds)
+dragon4_positional(PyObject *NPY_UNUSED(dummy),
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
PyObject *obj;
- static char *kwlist[] = {"x", "precision", "unique", "fractional",
- "sign", "trim", "pad_left", "pad_right", NULL};
int precision=-1, pad_left=-1, pad_right=-1;
- char *trimstr=NULL;
CutoffMode cutoff_mode;
DigitMode digit_mode;
TrimMode trim = TrimMode_None;
int sign=0, unique=1, fractional=0;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|iiiisii:dragon4_positional",
- kwlist, &obj, &precision, &unique, &fractional, &sign, &trimstr,
- &pad_left, &pad_right)) {
+ NPY_PREPARE_ARGPARSER;
+
+ if (npy_parse_arguments("dragon4_positional", args, len_args, kwnames,
+ "x", NULL , &obj,
+ "|precision", &PyArray_PythonPyIntFromInt, &precision,
+ "|unique", &PyArray_PythonPyIntFromInt, &unique,
+ "|fractional", &PyArray_PythonPyIntFromInt, &fractional,
+ "|sign", &PyArray_PythonPyIntFromInt, &sign,
+ "|trim", &trimmode_converter, &trim,
+ "|pad_left", &PyArray_PythonPyIntFromInt, &pad_left,
+ "|pad_right", &PyArray_PythonPyIntFromInt, &pad_right,
+ NULL, NULL, NULL) < 0) {
return NULL;
}
- if (trimstr != NULL) {
- if (strcmp(trimstr, "k") == 0) {
- trim = TrimMode_None;
- }
- else if (strcmp(trimstr, ".") == 0) {
- trim = TrimMode_Zeros;
- }
- else if (strcmp(trimstr, "0") == 0) {
- trim = TrimMode_LeaveOneZero;
- }
- else if (strcmp(trimstr, "-") == 0) {
- trim = TrimMode_DptZeros;
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "if supplied, trim must be 'k', '.', '0' or '-'");
- return NULL;
- }
- }
-
digit_mode = unique ? DigitMode_Unique : DigitMode_Exact;
cutoff_mode = fractional ? CutoffMode_FractionLength :
CutoffMode_TotalLength;
@@ -4052,15 +4067,19 @@ array_may_share_memory(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *
}
static PyObject *
-normalize_axis_index(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)
+normalize_axis_index(PyObject *NPY_UNUSED(self),
+ PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
- static char *kwlist[] = {"axis", "ndim", "msg_prefix", NULL};
int axis;
int ndim;
PyObject *msg_prefix = Py_None;
+ NPY_PREPARE_ARGPARSER;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "ii|O:normalize_axis_index",
- kwlist, &axis, &ndim, &msg_prefix)) {
+ if (npy_parse_arguments("normalize_axis_index", args, len_args, kwnames,
+ "axis", &PyArray_PythonPyIntFromInt, &axis,
+ "ndim", &PyArray_PythonPyIntFromInt, &ndim,
+ "|msg_prefix", NULL, &msg_prefix,
+ NULL, NULL, NULL) < 0) {
return NULL;
}
if (check_and_adjust_axis_msg(&axis, ndim, msg_prefix) < 0) {
@@ -4191,10 +4210,10 @@ static struct PyMethodDef array_module_methods[] = {
METH_VARARGS, NULL},
{"correlate",
(PyCFunction)array_correlate,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"correlate2",
(PyCFunction)array_correlate2,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"frombuffer",
(PyCFunction)array_frombuffer,
METH_VARARGS | METH_KEYWORDS, NULL},
@@ -4241,10 +4260,10 @@ static struct PyMethodDef array_module_methods[] = {
METH_VARARGS | METH_KEYWORDS, NULL},
{"dragon4_positional",
(PyCFunction)dragon4_positional,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"dragon4_scientific",
(PyCFunction)dragon4_scientific,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"compare_chararrays",
(PyCFunction)compare_chararrays,
METH_VARARGS | METH_KEYWORDS, NULL},
@@ -4277,7 +4296,7 @@ static struct PyMethodDef array_module_methods[] = {
{"unpackbits", (PyCFunction)io_unpack,
METH_VARARGS | METH_KEYWORDS, NULL},
{"normalize_axis_index", (PyCFunction)normalize_axis_index,
- METH_VARARGS | METH_KEYWORDS, NULL},
+ METH_FASTCALL | METH_KEYWORDS, NULL},
{"set_legacy_print_mode", (PyCFunction)set_legacy_print_mode,
METH_VARARGS, NULL},
{"_discover_array_parameters", (PyCFunction)_discover_array_parameters,