diff options
author | Sergey Fedoseev <fedoseev.sergey@gmail.com> | 2019-02-25 16:59:12 (GMT) |
---|---|---|
committer | Victor Stinner <vstinner@redhat.com> | 2019-02-25 16:59:12 (GMT) |
commit | 234531b4462b20d668762bd78406fd2ebab129c9 (patch) | |
tree | f0a93cbdf6ebf42055498ea9533891cec0680bcf /Objects/tupleobject.c | |
parent | 55e335d7d59be44819c6b672d258e2d5feb1e633 (diff) | |
download | cpython-234531b4462b20d668762bd78406fd2ebab129c9.zip cpython-234531b4462b20d668762bd78406fd2ebab129c9.tar.gz cpython-234531b4462b20d668762bd78406fd2ebab129c9.tar.bz2 |
bpo-36030: Add _PyTuple_FromArray() function (GH-11954)
Diffstat (limited to 'Objects/tupleobject.c')
-rw-r--r-- | Objects/tupleobject.c | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 9cf3f3d..75d2bf9 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 * |