diff options
author | orivej <orivej@yandex-team.ru> | 2022-02-10 16:45:01 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:01 +0300 |
commit | 2d37894b1b037cf24231090eda8589bbb44fb6fc (patch) | |
tree | be835aa92c6248212e705f25388ebafcf84bc7a1 /contrib/tools/python3/src/Objects/stringlib/join.h | |
parent | 718c552901d703c502ccbefdfc3c9028d608b947 (diff) | |
download | ydb-2d37894b1b037cf24231090eda8589bbb44fb6fc.tar.gz |
Restoring authorship annotation for <orivej@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/tools/python3/src/Objects/stringlib/join.h')
-rw-r--r-- | contrib/tools/python3/src/Objects/stringlib/join.h | 242 |
1 files changed, 121 insertions, 121 deletions
diff --git a/contrib/tools/python3/src/Objects/stringlib/join.h b/contrib/tools/python3/src/Objects/stringlib/join.h index acb39b497f..53bcbdea7a 100644 --- a/contrib/tools/python3/src/Objects/stringlib/join.h +++ b/contrib/tools/python3/src/Objects/stringlib/join.h @@ -1,73 +1,73 @@ -/* stringlib: bytes joining implementation */ - -#if STRINGLIB_IS_UNICODE -#error join.h only compatible with byte-wise strings -#endif - -Py_LOCAL_INLINE(PyObject *) -STRINGLIB(bytes_join)(PyObject *sep, PyObject *iterable) -{ +/* stringlib: bytes joining implementation */ + +#if STRINGLIB_IS_UNICODE +#error join.h only compatible with byte-wise strings +#endif + +Py_LOCAL_INLINE(PyObject *) +STRINGLIB(bytes_join)(PyObject *sep, PyObject *iterable) +{ const char *sepstr = STRINGLIB_STR(sep); Py_ssize_t seplen = STRINGLIB_LEN(sep); - PyObject *res = NULL; - char *p; - Py_ssize_t seqlen = 0; - Py_ssize_t sz = 0; - Py_ssize_t i, nbufs; - PyObject *seq, *item; - Py_buffer *buffers = NULL; -#define NB_STATIC_BUFFERS 10 - Py_buffer static_buffers[NB_STATIC_BUFFERS]; + PyObject *res = NULL; + char *p; + Py_ssize_t seqlen = 0; + Py_ssize_t sz = 0; + Py_ssize_t i, nbufs; + PyObject *seq, *item; + Py_buffer *buffers = NULL; +#define NB_STATIC_BUFFERS 10 + Py_buffer static_buffers[NB_STATIC_BUFFERS]; #define GIL_THRESHOLD 1048576 int drop_gil = 1; PyThreadState *save = NULL; - - seq = PySequence_Fast(iterable, "can only join an iterable"); - if (seq == NULL) { - return NULL; - } - - seqlen = PySequence_Fast_GET_SIZE(seq); - if (seqlen == 0) { - Py_DECREF(seq); - return STRINGLIB_NEW(NULL, 0); - } -#ifndef STRINGLIB_MUTABLE - if (seqlen == 1) { - item = PySequence_Fast_GET_ITEM(seq, 0); - if (STRINGLIB_CHECK_EXACT(item)) { - Py_INCREF(item); - Py_DECREF(seq); - return item; - } - } -#endif - if (seqlen > NB_STATIC_BUFFERS) { - buffers = PyMem_NEW(Py_buffer, seqlen); - if (buffers == NULL) { - Py_DECREF(seq); - PyErr_NoMemory(); - return NULL; - } - } - else { - buffers = static_buffers; - } - - /* Here is the general case. Do a pre-pass to figure out the total - * amount of space we'll need (sz), and see whether all arguments are - * bytes-like. - */ - for (i = 0, nbufs = 0; i < seqlen; i++) { - Py_ssize_t itemlen; - item = PySequence_Fast_GET_ITEM(seq, i); - if (PyBytes_CheckExact(item)) { - /* Fast path. */ - Py_INCREF(item); - buffers[i].obj = item; - buffers[i].buf = PyBytes_AS_STRING(item); - buffers[i].len = PyBytes_GET_SIZE(item); - } + + seq = PySequence_Fast(iterable, "can only join an iterable"); + if (seq == NULL) { + return NULL; + } + + seqlen = PySequence_Fast_GET_SIZE(seq); + if (seqlen == 0) { + Py_DECREF(seq); + return STRINGLIB_NEW(NULL, 0); + } +#ifndef STRINGLIB_MUTABLE + if (seqlen == 1) { + item = PySequence_Fast_GET_ITEM(seq, 0); + if (STRINGLIB_CHECK_EXACT(item)) { + Py_INCREF(item); + Py_DECREF(seq); + return item; + } + } +#endif + if (seqlen > NB_STATIC_BUFFERS) { + buffers = PyMem_NEW(Py_buffer, seqlen); + if (buffers == NULL) { + Py_DECREF(seq); + PyErr_NoMemory(); + return NULL; + } + } + else { + buffers = static_buffers; + } + + /* Here is the general case. Do a pre-pass to figure out the total + * amount of space we'll need (sz), and see whether all arguments are + * bytes-like. + */ + for (i = 0, nbufs = 0; i < seqlen; i++) { + Py_ssize_t itemlen; + item = PySequence_Fast_GET_ITEM(seq, i); + if (PyBytes_CheckExact(item)) { + /* Fast path. */ + Py_INCREF(item); + buffers[i].obj = item; + buffers[i].buf = PyBytes_AS_STRING(item); + buffers[i].len = PyBytes_GET_SIZE(item); + } else { if (PyObject_GetBuffer(item, &buffers[i], PyBUF_SIMPLE) != 0) { PyErr_Format(PyExc_TypeError, @@ -83,52 +83,52 @@ STRINGLIB(bytes_join)(PyObject *sep, PyObject *iterable) * changing the behaviour of that data race. */ drop_gil = 0; - } - nbufs = i + 1; /* for error cleanup */ - itemlen = buffers[i].len; - if (itemlen > PY_SSIZE_T_MAX - sz) { - PyErr_SetString(PyExc_OverflowError, - "join() result is too long"); - goto error; - } - sz += itemlen; - if (i != 0) { - if (seplen > PY_SSIZE_T_MAX - sz) { - PyErr_SetString(PyExc_OverflowError, - "join() result is too long"); - goto error; - } - sz += seplen; - } - if (seqlen != PySequence_Fast_GET_SIZE(seq)) { - PyErr_SetString(PyExc_RuntimeError, - "sequence changed size during iteration"); - goto error; - } - } - - /* Allocate result space. */ - res = STRINGLIB_NEW(NULL, sz); - if (res == NULL) - goto error; - - /* Catenate everything. */ - p = STRINGLIB_STR(res); + } + nbufs = i + 1; /* for error cleanup */ + itemlen = buffers[i].len; + if (itemlen > PY_SSIZE_T_MAX - sz) { + PyErr_SetString(PyExc_OverflowError, + "join() result is too long"); + goto error; + } + sz += itemlen; + if (i != 0) { + if (seplen > PY_SSIZE_T_MAX - sz) { + PyErr_SetString(PyExc_OverflowError, + "join() result is too long"); + goto error; + } + sz += seplen; + } + if (seqlen != PySequence_Fast_GET_SIZE(seq)) { + PyErr_SetString(PyExc_RuntimeError, + "sequence changed size during iteration"); + goto error; + } + } + + /* Allocate result space. */ + res = STRINGLIB_NEW(NULL, sz); + if (res == NULL) + goto error; + + /* Catenate everything. */ + p = STRINGLIB_STR(res); if (sz < GIL_THRESHOLD) { drop_gil = 0; /* Benefits are likely outweighed by the overheads */ } if (drop_gil) { save = PyEval_SaveThread(); } - if (!seplen) { - /* fast path */ - for (i = 0; i < nbufs; i++) { - Py_ssize_t n = buffers[i].len; - char *q = buffers[i].buf; - memcpy(p, q, n); - p += n; - } - } + if (!seplen) { + /* fast path */ + for (i = 0; i < nbufs; i++) { + Py_ssize_t n = buffers[i].len; + char *q = buffers[i].buf; + memcpy(p, q, n); + p += n; + } + } else { for (i = 0; i < nbufs; i++) { Py_ssize_t n; @@ -141,23 +141,23 @@ STRINGLIB(bytes_join)(PyObject *sep, PyObject *iterable) q = buffers[i].buf; memcpy(p, q, n); p += n; - } - } + } + } if (drop_gil) { PyEval_RestoreThread(save); } - goto done; - -error: - res = NULL; -done: - Py_DECREF(seq); - for (i = 0; i < nbufs; i++) - PyBuffer_Release(&buffers[i]); - if (buffers != static_buffers) - PyMem_FREE(buffers); - return res; -} - -#undef NB_STATIC_BUFFERS + goto done; + +error: + res = NULL; +done: + Py_DECREF(seq); + for (i = 0; i < nbufs; i++) + PyBuffer_Release(&buffers[i]); + if (buffers != static_buffers) + PyMem_FREE(buffers); + return res; +} + +#undef NB_STATIC_BUFFERS #undef GIL_THRESHOLD |