diff options
| author | Sergey Fedoseev <fedoseev.sergey@gmail.com> | 2019-02-25 21:59:12 +0500 |
|---|---|---|
| committer | Victor Stinner <vstinner@redhat.com> | 2019-02-25 17:59:12 +0100 |
| commit | 234531b4462b20d668762bd78406fd2ebab129c9 (patch) | |
| tree | f0a93cbdf6ebf42055498ea9533891cec0680bcf /Objects | |
| parent | 55e335d7d59be44819c6b672d258e2d5feb1e633 (diff) | |
| download | cpython-git-234531b4462b20d668762bd78406fd2ebab129c9.tar.gz | |
bpo-36030: Add _PyTuple_FromArray() function (GH-11954)
Diffstat (limited to 'Objects')
| -rw-r--r-- | Objects/call.c | 30 | ||||
| -rw-r--r-- | Objects/listobject.c | 18 | ||||
| -rw-r--r-- | Objects/structseq.c | 10 | ||||
| -rw-r--r-- | Objects/tupleobject.c | 33 |
4 files changed, 24 insertions, 67 deletions
diff --git a/Objects/call.c b/Objects/call.c index be8e90d03d..3250f8a10d 100644 --- a/Objects/call.c +++ b/Objects/call.c @@ -1276,20 +1276,7 @@ PyObject_CallFunctionObjArgs(PyObject *callable, ...) _Py_NO_INLINE PyObject * _PyStack_AsTuple(PyObject *const *stack, Py_ssize_t nargs) { - PyObject *args; - Py_ssize_t i; - - args = PyTuple_New(nargs); - if (args == NULL) { - return NULL; - } - - for (i=0; i < nargs; i++) { - PyObject *item = stack[i]; - Py_INCREF(item); - PyTuple_SET_ITEM(args, i, item); - } - return args; + return _PyTuple_FromArray(stack, nargs); } @@ -1297,24 +1284,11 @@ PyObject* _PyStack_AsTupleSlice(PyObject *const *stack, Py_ssize_t nargs, Py_ssize_t start, Py_ssize_t end) { - PyObject *args; - Py_ssize_t i; - assert(0 <= start); assert(end <= nargs); assert(start <= end); - args = PyTuple_New(end - start); - if (args == NULL) { - return NULL; - } - - for (i=start; i < end; i++) { - PyObject *item = stack[i]; - Py_INCREF(item); - PyTuple_SET_ITEM(args, i - start, item); - } - return args; + return _PyTuple_FromArray(stack + start, end - start); } diff --git a/Objects/listobject.c b/Objects/listobject.c index cbd6e81ea4..b6524e8bd7 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -3,6 +3,7 @@ #include "Python.h" #include "pycore_object.h" #include "pycore_pystate.h" +#include "pycore_tupleobject.h" #include "pycore_accu.h" #ifdef STDC_HEADERS @@ -2501,26 +2502,11 @@ PyList_Reverse(PyObject *v) PyObject * PyList_AsTuple(PyObject *v) { - PyObject *w; - PyObject **p, **q; - Py_ssize_t n; if (v == NULL || !PyList_Check(v)) { PyErr_BadInternalCall(); return NULL; } - n = Py_SIZE(v); - w = PyTuple_New(n); - if (w == NULL) - return NULL; - p = ((PyTupleObject *)w)->ob_item; - q = ((PyListObject *)v)->ob_item; - while (--n >= 0) { - Py_INCREF(*q); - *p = *q; - p++; - q++; - } - return w; + return _PyTuple_FromArray(((PyListObject *)v)->ob_item, Py_SIZE(v)); } /*[clinic input] diff --git a/Objects/structseq.c b/Objects/structseq.c index cf94155f18..56b06c707f 100644 --- a/Objects/structseq.c +++ b/Objects/structseq.c @@ -2,6 +2,7 @@ and posixmodule for example uses. */ #include "Python.h" +#include "pycore_tupleobject.h" #include "structmember.h" static const char visible_length_key[] = "n_sequence_fields"; @@ -250,7 +251,7 @@ structseq_reduce(PyStructSequence* self, PyObject *Py_UNUSED(ignored)) n_fields = REAL_SIZE(self); n_visible_fields = VISIBLE_SIZE(self); n_unnamed_fields = UNNAMED_FIELDS(self); - tup = PyTuple_New(n_visible_fields); + tup = _PyTuple_FromArray(self->ob_item, n_visible_fields); if (!tup) goto error; @@ -258,12 +259,7 @@ structseq_reduce(PyStructSequence* self, PyObject *Py_UNUSED(ignored)) if (!dict) goto error; - for (i = 0; i < n_visible_fields; i++) { - Py_INCREF(self->ob_item[i]); - PyTuple_SET_ITEM(tup, i, self->ob_item[i]); - } - - for (; i < n_fields; i++) { + for (i = n_visible_fields; i < n_fields; i++) { const char *n = Py_TYPE(self)->tp_members[i-n_unnamed_fields].name; if (PyDict_SetItemString(dict, n, self->ob_item[i]) < 0) goto error; diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 9cf3f3dd66..75d2bf95e6 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -419,14 +419,26 @@ tupleitem(PyTupleObject *a, Py_ssize_t i) return a->ob_item[i]; } +PyObject * +_PyTuple_FromArray(PyObject *const *src, Py_ssize_t n) +{ + PyTupleObject *tuple = (PyTupleObject *)PyTuple_New(n); + if (tuple == NULL) { + return NULL; + } + PyObject **dst = tuple->ob_item; + for (Py_ssize_t i = 0; i < n; i++) { + PyObject *item = src[i]; + Py_INCREF(item); + dst[i] = item; + } + return (PyObject *)tuple; +} + static PyObject * tupleslice(PyTupleObject *a, Py_ssize_t ilow, Py_ssize_t ihigh) { - PyTupleObject *np; - PyObject **src, **dest; - Py_ssize_t i; - Py_ssize_t len; if (ilow < 0) ilow = 0; if (ihigh > Py_SIZE(a)) @@ -437,18 +449,7 @@ tupleslice(PyTupleObject *a, Py_ssize_t ilow, Py_INCREF(a); return (PyObject *)a; } - len = ihigh - ilow; - np = (PyTupleObject *)PyTuple_New(len); - if (np == NULL) - return NULL; - src = a->ob_item + ilow; - dest = np->ob_item; - for (i = 0; i < len; i++) { - PyObject *v = src[i]; - Py_INCREF(v); - dest[i] = v; - } - return (PyObject *)np; + return _PyTuple_FromArray(a->ob_item + ilow, ihigh - ilow); } PyObject * |
