12#include "nuitka/prelude.h"
15PyObject *STRING_FROM_CHAR(
unsigned char c) {
21 return Nuitka_String_FromStringAndSize(s, 1);
30PyObject *BUILTIN_CHR(PyThreadState *tstate, PyObject *value) {
31 long x = PyInt_AsLong(value);
33 if (unlikely(x == -1 && HAS_ERROR_OCCURRED(tstate))) {
34#if PYTHON_VERSION < 0x300 && defined(_NUITKA_FULL_COMPAT)
35 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError,
"an integer is required");
37 PyErr_Format(PyExc_TypeError,
"an integer is required (got type %s)", Py_TYPE(value)->tp_name);
42#if PYTHON_VERSION < 0x300
43 if (unlikely(x < 0 || x >= 256)) {
44 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_ValueError,
"chr() arg not in range(256)");
53 return PyString_FromStringAndSize(s, 1);
55 PyObject *result = PyUnicode_FromOrdinal(x);
57 if (unlikely(result == NULL)) {
61 assert(PyUnicode_Check(result));
71PyObject *BUILTIN_ORD(PyObject *value) {
74 if (likely(PyBytes_Check(value))) {
75 Py_ssize_t size = PyBytes_GET_SIZE(value);
77 if (likely(size == 1)) {
78 result = (long)(((
unsigned char *)PyBytes_AS_STRING(value))[0]);
80 PyErr_Format(PyExc_TypeError,
"ord() expected a character, but string of length %zd found", size);
83 }
else if (PyByteArray_Check(value)) {
84 Py_ssize_t size = PyByteArray_GET_SIZE(value);
86 if (likely(size == 1)) {
87 result = (long)(((
unsigned char *)PyByteArray_AS_STRING(value))[0]);
89 PyErr_Format(PyExc_TypeError,
90 "ord() expected a character, but byte array of length "
95 }
else if (PyUnicode_Check(value)) {
96#if PYTHON_VERSION >= 0x300
97 if (unlikely(PyUnicode_READY(value) == -1)) {
101 Py_ssize_t size = PyUnicode_GET_LENGTH(value);
103 Py_ssize_t size = PyUnicode_GET_SIZE(value);
106 if (likely(size == 1)) {
107#if PYTHON_VERSION >= 0x300
108 result = (long)(PyUnicode_READ_CHAR(value, 0));
110 result = (long)(*PyUnicode_AS_UNICODE(value));
113 PyErr_Format(PyExc_TypeError,
114 "ord() expected a character, but unicode string of "
120 PyErr_Format(PyExc_TypeError,
"ord() expected string of length 1, but %s found", Py_TYPE(value)->tp_name);
124 return Nuitka_PyInt_FromLong(result);
127#if PYTHON_VERSION >= 0x300
129#define _PyUnicode_UTF8_LENGTH(op) (((PyCompactUnicodeObject *)(op))->utf8_length)
130#define PyUnicode_UTF8_LENGTH(op) \
131 (assert(_PyUnicode_CHECK(op)), assert(PyUnicode_IS_READY(op)), \
132 PyUnicode_IS_COMPACT_ASCII(op) ? ((PyASCIIObject *)(op))->length : _PyUnicode_UTF8_LENGTH(op))
133#define _PyUnicode_WSTR(op) (((PyASCIIObject *)(op))->wstr)
134#if PYTHON_VERSION < 0x3c0
135#define _PyUnicode_WSTR_LENGTH(op) (((PyCompactUnicodeObject *)(op))->wstr_length)
137#define _PyUnicode_LENGTH(op) (((PyASCIIObject *)(op))->length)
138#define _PyUnicode_STATE(op) (((PyASCIIObject *)(op))->state)
139#define _PyUnicode_HASH(op) (((PyASCIIObject *)(op))->hash)
140#define _PyUnicode_KIND(op) (((PyASCIIObject *)(op))->state.kind)
141#define _PyUnicode_DATA_ANY(op) (((PyUnicodeObject *)(op))->data.any)
143#if PYTHON_VERSION < 0x3c0
144#undef PyUnicode_READY
145#define PyUnicode_READY(op) ((PyUnicode_IS_READY(op) ? 0 : _PyUnicode_Ready(op)))
148#define _PyUnicode_SHARE_UTF8(op) (assert(!PyUnicode_IS_COMPACT_ASCII(op)), (_PyUnicode_UTF8(op) == PyUnicode_DATA(op)))
149#define _PyUnicode_SHARE_WSTR(op) ((_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op)))
151#define _PyUnicode_HAS_UTF8_MEMORY(op) \
152 ((!PyUnicode_IS_COMPACT_ASCII(op) && _PyUnicode_UTF8(op) && _PyUnicode_UTF8(op) != PyUnicode_DATA(op)))
154#define _PyUnicode_HAS_WSTR_MEMORY(op) \
155 ((_PyUnicode_WSTR(op) && (!PyUnicode_IS_READY(op) || _PyUnicode_WSTR(op) != PyUnicode_DATA(op))))
157#define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \
159 to_type *_to = (to_type *)(to); \
160 const from_type *_iter = (from_type *)(begin); \
161 const from_type *_end = (from_type *)(end); \
162 Py_ssize_t n = (_end) - (_iter); \
163 const from_type *_unrolled_end = _iter + _Py_SIZE_ROUND_DOWN(n, 4); \
164 while (_iter < (_unrolled_end)) { \
165 _to[0] = (to_type)_iter[0]; \
166 _to[1] = (to_type)_iter[1]; \
167 _to[2] = (to_type)_iter[2]; \
168 _to[3] = (to_type)_iter[3]; \
172 while (_iter < (_end)) \
173 *_to++ = (to_type)(*_iter++); \
176extern int ucs1lib_find_max_char(
const Py_UCS1 *begin,
const Py_UCS1 *end);
178static void _NuitkaUnicode_FastCopyCharacters(PyObject *to, Py_ssize_t to_start, PyObject *from, Py_ssize_t from_start,
179 Py_ssize_t how_many) {
180 assert(from_start + how_many <= PyUnicode_GET_LENGTH(from));
181 assert(to_start + how_many <= PyUnicode_GET_LENGTH(to));
183 assert(how_many > 0);
185 unsigned int from_kind = PyUnicode_KIND(from);
186 void *from_data = PyUnicode_DATA(from);
188 unsigned int to_kind = PyUnicode_KIND(to);
189 void *to_data = PyUnicode_DATA(to);
191 if (from_kind == to_kind) {
192 memcpy((
char *)to_data + to_kind * to_start, (
char *)from_data + from_kind * from_start, to_kind * how_many);
193 }
else if (from_kind == PyUnicode_1BYTE_KIND && to_kind == PyUnicode_2BYTE_KIND) {
194 _PyUnicode_CONVERT_BYTES(Py_UCS1, Py_UCS2, PyUnicode_1BYTE_DATA(from) + from_start,
195 PyUnicode_1BYTE_DATA(from) + from_start + how_many,
196 PyUnicode_2BYTE_DATA(to) + to_start);
197 }
else if (from_kind == PyUnicode_1BYTE_KIND && to_kind == PyUnicode_4BYTE_KIND) {
198 _PyUnicode_CONVERT_BYTES(Py_UCS1, Py_UCS4, PyUnicode_1BYTE_DATA(from) + from_start,
199 PyUnicode_1BYTE_DATA(from) + from_start + how_many,
200 PyUnicode_4BYTE_DATA(to) + to_start);
201 }
else if (from_kind == PyUnicode_2BYTE_KIND && to_kind == PyUnicode_4BYTE_KIND) {
202 _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, PyUnicode_2BYTE_DATA(from) + from_start,
203 PyUnicode_2BYTE_DATA(from) + from_start + how_many,
204 PyUnicode_4BYTE_DATA(to) + to_start);
206 assert(PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to));
208 if (from_kind == PyUnicode_2BYTE_KIND && to_kind == PyUnicode_1BYTE_KIND) {
209 _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS1, PyUnicode_2BYTE_DATA(from) + from_start,
210 PyUnicode_2BYTE_DATA(from) + from_start + how_many,
211 PyUnicode_1BYTE_DATA(to) + to_start);
212 }
else if (from_kind == PyUnicode_4BYTE_KIND && to_kind == PyUnicode_1BYTE_KIND) {
213 _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, PyUnicode_4BYTE_DATA(from) + from_start,
214 PyUnicode_4BYTE_DATA(from) + from_start + how_many,
215 PyUnicode_1BYTE_DATA(to) + to_start);
216 }
else if (from_kind == PyUnicode_4BYTE_KIND && to_kind == PyUnicode_2BYTE_KIND) {
217 _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, PyUnicode_4BYTE_DATA(from) + from_start,
218 PyUnicode_4BYTE_DATA(from) + from_start + how_many,
219 PyUnicode_2BYTE_DATA(to) + to_start);
226static int _NuitkaUnicode_modifiable(PyObject *unicode) {
227 if (Py_REFCNT(unicode) != 1)
229 if (_PyUnicode_HASH(unicode) != -1)
232 if (PyUnicode_CHECK_INTERNED(unicode))
237#if PYTHON_VERSION < 0x3c0
238static PyObject *_NuitkaUnicode_New(Py_ssize_t length) {
241 if (length > ((PY_SSIZE_T_MAX / (Py_ssize_t)
sizeof(Py_UNICODE)) - 1)) {
242 return PyErr_NoMemory();
245 PyUnicodeObject *unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type);
247 if (unlikely(unicode == NULL)) {
250 Py_ssize_t new_size =
sizeof(Py_UNICODE) * ((
size_t)length + 1);
252 _PyUnicode_WSTR_LENGTH(unicode) = length;
253 _PyUnicode_HASH(unicode) = -1;
254 _PyUnicode_STATE(unicode).interned = 0;
255 _PyUnicode_STATE(unicode).kind = 0;
256 _PyUnicode_STATE(unicode).compact = 0;
257 _PyUnicode_STATE(unicode).ready = 0;
258 _PyUnicode_STATE(unicode).ascii = 0;
259 _PyUnicode_DATA_ANY(unicode) = NULL;
260 _PyUnicode_LENGTH(unicode) = 0;
261 _PyUnicode_UTF8(unicode) = NULL;
262 _PyUnicode_UTF8_LENGTH(unicode) = 0;
264 _PyUnicode_WSTR(unicode) = (Py_UNICODE *)NuitkaObject_Malloc(new_size);
265 if (!_PyUnicode_WSTR(unicode)) {
271 _PyUnicode_WSTR(unicode)[0] = 0;
272 _PyUnicode_WSTR(unicode)[length] = 0;
274 return (PyObject *)unicode;
277static PyObject *_NuitkaUnicode_resize_copy(PyObject *unicode, Py_ssize_t length) {
278 if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) {
279 PyObject *copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode));
280 if (unlikely(copy == NULL)) {
284 Py_ssize_t copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode));
285 _NuitkaUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length);
289 PyObject *w = _NuitkaUnicode_New(length);
290 if (unlikely(w == NULL)) {
293 Py_ssize_t copy_length = _PyUnicode_WSTR_LENGTH(unicode);
294 copy_length = Py_MIN(copy_length, length);
295 memcpy(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), copy_length *
sizeof(
wchar_t));
302static PyObject *_NuitkaUnicode_resize_copy(PyObject *unicode, Py_ssize_t length) {
305 PyObject *copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode));
307 if (unlikely(copy == NULL)) {
311 Py_ssize_t copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode));
312 _NuitkaUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length);
319#if PYTHON_VERSION >= 0x390
322#define _Py_DEC_REFTOTAL _Py_RefTotal--;
324#define _Py_DEC_REFTOTAL
328#define _Py_ForgetReference(unicode) _Py_ForgetReference(unicode)
330#define _Py_ForgetReference(unicode)
335#if PYTHON_VERSION < 0x3c0
336static PyObject *_NuitkaUnicode_resize_compact(PyObject *unicode, Py_ssize_t length) {
337 assert(PyUnicode_IS_COMPACT(unicode));
339 Py_ssize_t char_size = PyUnicode_KIND(unicode);
340 Py_ssize_t struct_size;
342 if (PyUnicode_IS_ASCII(unicode)) {
343 struct_size =
sizeof(PyASCIIObject);
345 struct_size =
sizeof(PyCompactUnicodeObject);
348 int share_wstr = _PyUnicode_SHARE_WSTR(unicode);
350 if (unlikely(length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1))) {
354 Py_ssize_t new_size = (struct_size + (length + 1) * char_size);
356 if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) {
357 PyObject_DEL(_PyUnicode_UTF8(unicode));
358 _PyUnicode_UTF8(unicode) = NULL;
359 _PyUnicode_UTF8_LENGTH(unicode) = 0;
363 _Py_ForgetReference(unicode);
365 PyObject *new_unicode = (PyObject *)PyObject_REALLOC(unicode, new_size);
366 if (unlikely(new_unicode == NULL)) {
367 Nuitka_Py_NewReference(unicode);
373 unicode = new_unicode;
374 Nuitka_Py_NewReference(unicode);
376 _PyUnicode_LENGTH(unicode) = length;
379 _PyUnicode_WSTR(unicode) = (
wchar_t *)PyUnicode_DATA(unicode);
380 if (!PyUnicode_IS_ASCII(unicode)) {
381 _PyUnicode_WSTR_LENGTH(unicode) = length;
383 }
else if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) {
384 PyObject_DEL(_PyUnicode_WSTR(unicode));
385 _PyUnicode_WSTR(unicode) = NULL;
386 if (!PyUnicode_IS_ASCII(unicode)) {
387 _PyUnicode_WSTR_LENGTH(unicode) = 0;
391 PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), length, 0);
396static int _NuitkaUnicode_resize_inplace(PyObject *unicode, Py_ssize_t length) {
397 assert(!PyUnicode_IS_COMPACT(unicode));
398 assert(Py_REFCNT(unicode) == 1);
400 if (PyUnicode_IS_READY(unicode)) {
401 void *data = _PyUnicode_DATA_ANY(unicode);
402 Py_ssize_t char_size = PyUnicode_KIND(unicode);
403 int share_wstr = _PyUnicode_SHARE_WSTR(unicode);
404 int share_utf8 = _PyUnicode_SHARE_UTF8(unicode);
406 if (unlikely(length > (PY_SSIZE_T_MAX / char_size - 1))) {
411 Py_ssize_t new_size = (length + 1) * char_size;
413 if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) {
414 PyObject_DEL(_PyUnicode_UTF8(unicode));
415 _PyUnicode_UTF8(unicode) = NULL;
416 _PyUnicode_UTF8_LENGTH(unicode) = 0;
419 data = (PyObject *)PyObject_REALLOC(data, new_size);
425 _PyUnicode_DATA_ANY(unicode) = data;
427 _PyUnicode_WSTR(unicode) = (
wchar_t *)data;
428 _PyUnicode_WSTR_LENGTH(unicode) = length;
431 _PyUnicode_UTF8(unicode) = (
char *)data;
432 _PyUnicode_UTF8_LENGTH(unicode) = length;
434 _PyUnicode_LENGTH(unicode) = length;
435 PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0);
437 if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) {
441 assert(_PyUnicode_WSTR(unicode) != NULL);
443 if (length > PY_SSIZE_T_MAX / (Py_ssize_t)
sizeof(
wchar_t) - 1) {
447 Py_ssize_t new_size =
sizeof(wchar_t) * (length + 1);
448 wchar_t *wstr = _PyUnicode_WSTR(unicode);
449 wstr = (
wchar_t *)PyObject_REALLOC(wstr, new_size);
455 _PyUnicode_WSTR(unicode) = wstr;
456 _PyUnicode_WSTR(unicode)[length] = 0;
457 _PyUnicode_WSTR_LENGTH(unicode) = length;
462static int _NuitkaUnicode_resize(PyObject **p_unicode, Py_ssize_t length) {
463 assert(p_unicode != NULL);
464 assert(*p_unicode != NULL);
467 PyObject *unicode = *p_unicode;
468 assert(PyUnicode_Check(unicode));
469 Py_ssize_t old_length;
471 if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) {
472 old_length = PyUnicode_WSTR_LENGTH(unicode);
474 old_length = PyUnicode_GET_LENGTH(unicode);
477 if (old_length == length) {
482 Py_DECREF(*p_unicode);
483 *p_unicode = const_str_empty;
487 if (!_NuitkaUnicode_modifiable(unicode)) {
488 PyObject *copy = _NuitkaUnicode_resize_copy(unicode, length);
489 if (unlikely(copy == NULL)) {
492 Py_DECREF(*p_unicode);
498 if (PyUnicode_IS_COMPACT(unicode)) {
499 PyObject *new_unicode = _NuitkaUnicode_resize_compact(unicode, length);
501 if (unlikely(new_unicode == NULL)) {
505 *p_unicode = new_unicode;
509 return _NuitkaUnicode_resize_inplace(unicode, length);
513#ifndef __NUITKA_NO_ASSERT__
514static void _Nuitka_unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length) {
515 int kind = PyUnicode_KIND(unicode);
516 Py_UCS1 *data = PyUnicode_1BYTE_DATA(unicode);
517 Py_ssize_t length = _PyUnicode_LENGTH(unicode);
518 if (length <= old_length)
520 memset(data + old_length * kind, 0xff, (length - old_length) * kind);
524static PyObject *_NuitkaUnicode_resize_compact(PyObject *unicode, Py_ssize_t length) {
525 assert(PyUnicode_IS_COMPACT(unicode));
527 Py_ssize_t char_size = PyUnicode_KIND(unicode);
528 Py_ssize_t struct_size;
530 if (PyUnicode_IS_ASCII(unicode)) {
531 struct_size =
sizeof(PyASCIIObject);
533 struct_size =
sizeof(PyCompactUnicodeObject);
536#ifndef __NUITKA_NO_ASSERT__
537 Py_ssize_t old_length = _PyUnicode_LENGTH(unicode);
542 if (unlikely(length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1))) {
546 Py_ssize_t new_size = (struct_size + (length + 1) * char_size);
548 if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) {
549 PyObject_DEL(_PyUnicode_UTF8(unicode));
550 _PyUnicode_UTF8(unicode) = NULL;
551 _PyUnicode_UTF8_LENGTH(unicode) = 0;
553 _Py_ForgetReference(unicode);
555 PyObject *new_unicode = (PyObject *)PyObject_Realloc(unicode, new_size);
556 if (unlikely(new_unicode == NULL)) {
557 Nuitka_Py_NewReferenceNoTotal(unicode);
563 unicode = new_unicode;
564 Nuitka_Py_NewReferenceNoTotal(unicode);
566 _PyUnicode_LENGTH(unicode) = length;
568#ifndef __NUITKA_NO_ASSERT__
569 _Nuitka_unicode_fill_invalid(unicode, old_length);
572 PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), length, 0);
573 assert(_PyUnicode_CheckConsistency(unicode, 0));
578static int _NuitkaUnicode_resize_inplace(PyObject *unicode, Py_ssize_t length) {
579 assert(!PyUnicode_IS_COMPACT(unicode));
580 assert(Py_REFCNT(unicode) == 1);
582#ifndef __NUITKA_NO_ASSERT__
583 Py_ssize_t old_length = _PyUnicode_LENGTH(unicode);
586 void *data = _PyUnicode_DATA_ANY(unicode);
587 Py_ssize_t char_size = PyUnicode_KIND(unicode);
588 int share_utf8 = _PyUnicode_SHARE_UTF8(unicode);
590 if (unlikely(length > (PY_SSIZE_T_MAX / char_size - 1))) {
595 Py_ssize_t new_size = (length + 1) * char_size;
597 if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) {
598 PyObject_DEL(_PyUnicode_UTF8(unicode));
599 _PyUnicode_UTF8(unicode) = NULL;
600 _PyUnicode_UTF8_LENGTH(unicode) = 0;
603 data = (PyObject *)PyObject_REALLOC(data, new_size);
609 _PyUnicode_DATA_ANY(unicode) = data;
611 _PyUnicode_UTF8(unicode) = data;
612 _PyUnicode_UTF8_LENGTH(unicode) = length;
614 _PyUnicode_LENGTH(unicode) = length;
615 PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0);
616#ifndef __NUITKA_NO_ASSERT__
617 _Nuitka_unicode_fill_invalid(unicode, old_length);
621 if (length > PY_SSIZE_T_MAX / (Py_ssize_t)
sizeof(
wchar_t) - 1) {
625 assert(_PyUnicode_CheckConsistency(unicode, 0));
629static int _NuitkaUnicode_resize(PyObject **p_unicode, Py_ssize_t length) {
630 assert(p_unicode != NULL);
631 assert(*p_unicode != NULL);
634 PyObject *unicode = *p_unicode;
635 assert(PyUnicode_Check(unicode));
636 Py_ssize_t old_length;
638 old_length = PyUnicode_GET_LENGTH(unicode);
639 if (old_length == length) {
644 Py_SETREF(*p_unicode, const_str_empty);
648 if (!_NuitkaUnicode_modifiable(unicode)) {
649 PyObject *copy = _NuitkaUnicode_resize_copy(unicode, length);
650 if (unlikely(copy == NULL)) {
653 Py_SETREF(*p_unicode, copy);
658 if (PyUnicode_IS_COMPACT(unicode)) {
659 PyObject *new_unicode = _NuitkaUnicode_resize_compact(unicode, length);
661 if (unlikely(new_unicode == NULL)) {
665 *p_unicode = new_unicode;
669 return _NuitkaUnicode_resize_inplace(unicode, length);
673PyObject *UNICODE_CONCAT(PyThreadState *tstate, PyObject *left, PyObject *right) {
674 if (left == const_str_empty) {
678 if (right == const_str_empty) {
683 if (PyUnicode_READY(left) == -1 || PyUnicode_READY(right) == -1) {
687 Py_ssize_t left_len = PyUnicode_GET_LENGTH(left);
688 Py_ssize_t right_len = PyUnicode_GET_LENGTH(right);
689 if (left_len > PY_SSIZE_T_MAX - right_len) {
690 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_OverflowError,
"strings are too large to concat");
693 Py_ssize_t new_len = left_len + right_len;
695 Py_UCS4 max_char = PyUnicode_MAX_CHAR_VALUE(left);
696 Py_UCS4 max_char2 = PyUnicode_MAX_CHAR_VALUE(right);
697 max_char = Py_MAX(max_char, max_char2);
699 PyObject *result = PyUnicode_New(new_len, max_char);
700 if (unlikely(result == NULL)) {
704 _NuitkaUnicode_FastCopyCharacters(result, 0, left, 0, left_len);
705 _NuitkaUnicode_FastCopyCharacters(result, left_len, right, 0, right_len);
710bool UNICODE_APPEND(PyThreadState *tstate, PyObject **p_left, PyObject *right) {
713 PyObject *left = *p_left;
715 if (left == const_str_empty) {
722 if (right == const_str_empty)
725 if (PyUnicode_READY(left) == -1 || PyUnicode_READY(right) == -1) {
729 Py_ssize_t left_len = PyUnicode_GET_LENGTH(left);
730 Py_ssize_t right_len = PyUnicode_GET_LENGTH(right);
732 if (left_len > PY_SSIZE_T_MAX - right_len) {
733 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_OverflowError,
"strings are too large to concat");
736 Py_ssize_t new_len = left_len + right_len;
738 if (_NuitkaUnicode_modifiable(left) && PyUnicode_KIND(right) <= PyUnicode_KIND(left) &&
739 !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) {
740 if (unlikely(_NuitkaUnicode_resize(p_left, new_len) != 0)) {
744 _NuitkaUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len);
746 Py_UCS4 max_char = PyUnicode_MAX_CHAR_VALUE(left);
747 Py_UCS4 max_char2 = PyUnicode_MAX_CHAR_VALUE(right);
749 max_char = Py_MAX(max_char, max_char2);
751 PyObject *res = PyUnicode_New(new_len, max_char);
752 if (unlikely(res == NULL)) {
756 _NuitkaUnicode_FastCopyCharacters(res, 0, left, 0, left_len);
757 _NuitkaUnicode_FastCopyCharacters(res, left_len, right, 0, right_len);
767PyObject *UNICODE_JOIN(PyThreadState *tstate, PyObject *str, PyObject *iterable) {
769 CHECK_OBJECT(iterable);
770 assert(PyUnicode_CheckExact(str));
772 return PyUnicode_Join(str, iterable);
775PyObject *UNICODE_PARTITION(PyThreadState *tstate, PyObject *str, PyObject *sep) {
778 assert(PyUnicode_CheckExact(str));
780 return PyUnicode_Partition(str, sep);
783PyObject *UNICODE_RPARTITION(PyThreadState *tstate, PyObject *str, PyObject *sep) {
786 assert(PyUnicode_CheckExact(str));
788 return PyUnicode_RPartition(str, sep);
790#if PYTHON_VERSION < 0x300
792PyObject *STR_JOIN(PyThreadState *tstate, PyObject *str, PyObject *iterable) {
794 CHECK_OBJECT(iterable);
795 assert(PyString_CheckExact(str));
797 return _PyString_Join(str, iterable);
802PyObject *NuitkaUnicode_FromWideChar(
wchar_t const *str, Py_ssize_t size) {
803#if PYTHON_VERSION < 0x300
809 return PyUnicode_FromWideChar(str, size);
812PyObject *BUILTIN_UNICODE1(PyObject *value) {
813#if PYTHON_VERSION >= 0x300
814 if (PyUnicode_CheckExact(value)) {
815#if !defined(Py_DEBUG) && PYTHON_VERSION >= 0x300
816 if (PyUnicode_READY(value) < 0) {
825 if (Py_TYPE(value)->tp_str == NULL) {
826 return PyObject_Repr(value);
831 if (Py_EnterRecursiveCall((
char *)
" while getting the str of an object")) {
835 PyObject *result = (*Py_TYPE(value)->tp_str)(value);
837 Py_LeaveRecursiveCall();
839 if (unlikely(result == NULL)) {
843 if (unlikely(!PyUnicode_Check(result))) {
844 SET_CURRENT_EXCEPTION_TYPE_COMPLAINT(
"__str__ returned non-string (type %s)", result);
850#if !defined(Py_DEBUG) && PYTHON_VERSION >= 0x300
851 if (PyUnicode_READY(result) < 0) {
860 return PyObject_Unicode(value);
864PyObject *BUILTIN_UNICODE3(PyObject *value, PyObject *encoding, PyObject *errors) {
866 CHECK_OBJECT_X(encoding);
867 CHECK_OBJECT_X(errors);
869 char const *encoding_str;
871 if (encoding == NULL) {
873 }
else if (Nuitka_String_Check(encoding)) {
874 encoding_str = Nuitka_String_AsString_Unchecked(encoding);
876#if PYTHON_VERSION < 0x300
877 else if (PyUnicode_Check(encoding)) {
878 PyObject *uarg2 = _PyUnicode_AsDefaultEncodedString(encoding, NULL);
881 encoding_str = Nuitka_String_AsString_Unchecked(uarg2);
885 SET_CURRENT_EXCEPTION_TYPE_COMPLAINT(
"unicode() argument 2 must be string, not %s", encoding);
889 char const *errors_str;
891 if (errors == NULL) {
893 }
else if (Nuitka_String_Check(errors)) {
894 errors_str = Nuitka_String_AsString_Unchecked(errors);
896#if PYTHON_VERSION < 0x300
897 else if (PyUnicode_Check(errors)) {
898 PyObject *uarg3 = _PyUnicode_AsDefaultEncodedString(errors, NULL);
901 errors_str = Nuitka_String_AsString_Unchecked(uarg3);
905 SET_CURRENT_EXCEPTION_TYPE_COMPLAINT(
"unicode() argument 3 must be string, not %s", errors);
909 PyObject *result = PyUnicode_FromEncodedObject(value, encoding_str, errors_str);
911 if (unlikely(result == NULL)) {
915 assert(PyUnicode_Check(result));
920#if PYTHON_VERSION < 0x300
921PyObject *_BUILTIN_STR(PyObject *value) {
924 if (PyString_CheckExact(value) || PyUnicode_CheckExact(value)) {
931 if (Py_EnterRecursiveCall((
char *)
" while getting the str of an object")) {
937 if (Py_TYPE(value)->tp_str == NULL) {
938 result = PyObject_Repr(value);
940 result = (*Py_TYPE(value)->tp_str)(value);
943 Py_LeaveRecursiveCall();
945 if (unlikely(result == NULL)) {
949 if (unlikely(!PyString_Check(result) && !PyUnicode_Check(result))) {
950 SET_CURRENT_EXCEPTION_TYPE_COMPLAINT(
"__str__ returned non-string (type %s)", result);
955 if (PyUnicode_Check(result)) {
956 PyObject *str = PyUnicode_AsEncodedString(result, NULL, NULL);
959 if (likely(str != NULL)) {
966 assert(PyString_Check(result));
970PyObject *BUILTIN_STR(PyObject *value) {
971 PyObject *result = _BUILTIN_STR(value);
973 if (result != NULL && PyUnicode_CheckExact(result)) {
974 PyObject *converted = PyUnicode_AsEncodedString(value, NULL, NULL);