9#if PYTHON_VERSION < 0x300
12#define PyStringObject_SIZE (offsetof(PyStringObject, ob_sval) + 1)
14NUITKA_MAY_BE_UNUSED
static bool STRING_RESIZE(PyObject **value, Py_ssize_t newsize) {
18 _Py_ForgetReference(*value);
20 *value = (PyObject *)PyObject_REALLOC((
char *)*value, PyStringObject_SIZE + newsize);
22 if (unlikely(*value == NULL)) {
27 Nuitka_Py_NewReference(*value);
29 sv = (PyStringObject *)*value;
30 Py_SIZE(sv) = newsize;
32 sv->ob_sval[newsize] =
'\0';
38NUITKA_MAY_BE_UNUSED
static bool STRING_ADD_INPLACE(PyObject **operand1, PyObject *operand2) {
39 assert(PyString_CheckExact(*operand1));
40 assert(!PyString_CHECK_INTERNED(*operand1));
41 assert(PyString_CheckExact(operand2));
43 Py_ssize_t operand1_size = PyString_GET_SIZE(*operand1);
44 Py_ssize_t operand2_size = PyString_GET_SIZE(operand2);
46 Py_ssize_t new_size = operand1_size + operand2_size;
48 if (unlikely(new_size < 0)) {
49 PyErr_Format(PyExc_OverflowError,
"strings are too large to concat");
54 if (unlikely(STRING_RESIZE(operand1, new_size) ==
false)) {
58 memcpy(PyString_AS_STRING(*operand1) + operand1_size, PyString_AS_STRING(operand2), operand2_size);
64#if PYTHON_VERSION >= 0x300
65NUITKA_MAY_BE_UNUSED
static bool BYTES_ADD_INCREMENTAL(PyObject **operand1, PyObject *operand2) {
66 assert(PyBytes_CheckExact(*operand1));
67 assert(PyBytes_CheckExact(operand2));
74 NUITKA_MAY_BE_UNUSED
int res = PyObject_GetBuffer(operand2, &wb, PyBUF_SIMPLE);
80 Py_ssize_t oldsize = PyBytes_GET_SIZE(*operand1);
82 if (oldsize > PY_SSIZE_T_MAX - wb.len) {
84 PyBuffer_Release(&wb);
87 if (_PyBytes_Resize(operand1, oldsize + wb.len) < 0) {
88 PyBuffer_Release(&wb);
92 memcpy(PyBytes_AS_STRING(*operand1) + oldsize, wb.buf, wb.len);
93 PyBuffer_Release(&wb);
98NUITKA_MAY_BE_UNUSED
static bool UNICODE_ADD_INCREMENTAL(PyThreadState *tstate, PyObject **operand1,
100 Py_ssize_t operand2_size = PyUnicode_GET_LENGTH(operand2);
101 if (operand2_size == 0) {
105#if PYTHON_VERSION < 0x300
106 Py_ssize_t operand1_size = PyUnicode_GET_SIZE(*operand1);
108 Py_ssize_t new_size = operand1_size + operand2_size;
110 if (unlikely(new_size < 0)) {
111 PyErr_Format(PyExc_OverflowError,
"strings are too large to concat");
116 if (unlikely(PyUnicode_Resize(operand1, new_size) != 0)) {
120 memcpy(PyUnicode_AS_UNICODE(*operand1) + operand1_size, PyUnicode_AS_UNICODE(operand2),
121 operand2_size *
sizeof(Py_UNICODE));
125 assert(!PyUnicode_CHECK_INTERNED(*operand1));
127 return UNICODE_APPEND(tstate, operand1, operand2);