Nuitka
The Python compiler
Loading...
Searching...
No Matches
HelpersBuiltin.c
1// Copyright 2025, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
2
12// This file is included from another C file, help IDEs to still parse it on
13// its own.
14#ifdef __IDE_ONLY__
15#include "nuitka/prelude.h"
16#endif
17
18PyObject *CALL_BUILTIN_KW_ARGS(PyThreadState *tstate, PyObject *callable, PyObject **args, char const **arg_names,
19 int max_args, int kw_only_args) {
20 int i = 0;
21
22 while (i < max_args - kw_only_args) {
23 if (args[i] == NULL) {
24 break;
25 }
26
27 CHECK_OBJECT(args[i]);
28
29 i++;
30 }
31
32 int usable_args = i;
33
34 PyObject *kw_dict = NULL;
35
36 while (i < max_args) {
37 if (args[i] != NULL) {
38 CHECK_OBJECT(args[i]);
39
40 if (kw_dict == NULL) {
41 kw_dict = MAKE_DICT_EMPTY(tstate);
42 }
43
44 NUITKA_MAY_BE_UNUSED int res = PyDict_SetItemString(kw_dict, arg_names[i], args[i]);
45 assert(res == 0);
46 }
47
48 i++;
49 }
50
51 PyObject *args_tuple = MAKE_TUPLE_VAR(tstate, args, usable_args);
52
53 PyObject *result = CALL_FUNCTION(tstate, callable, args_tuple, kw_dict);
54 Py_XDECREF(kw_dict);
55 Py_DECREF(args_tuple);
56
57 return result;
58}
59
64NUITKA_DEFINE_BUILTIN(compile)
65
66#if PYTHON_VERSION < 0x300
67PyObject *COMPILE_CODE(PyThreadState *tstate, PyObject *source_code, PyObject *file_name, PyObject *mode,
68 PyObject *flags, PyObject *dont_inherit)
69#else
70PyObject *COMPILE_CODE(PyThreadState *tstate, PyObject *source_code, PyObject *file_name, PyObject *mode,
71 PyObject *flags, PyObject *dont_inherit, PyObject *optimize)
72#endif
73{
74 // May be a source, but also could already be a compiled object, in which
75 // case this should just return it.
76 if (PyCode_Check(source_code)) {
77 Py_INCREF(source_code);
78 return source_code;
79 }
80
81 PyObject *pos_args = MAKE_TUPLE3(tstate, source_code, file_name, mode);
82
83 PyObject *kw_values[] = {
84 flags,
85 dont_inherit,
86#if PYTHON_VERSION >= 0x300
87 optimize,
88#endif
89 };
90
91 char const *kw_keys[] = {
92 "flags",
93 "dont_inherit",
94#if PYTHON_VERSION >= 0x300
95 "optimize",
96#endif
97 };
98
99 PyObject *kw_args = MAKE_DICT_X_CSTR(kw_keys, kw_values, sizeof(kw_values) / sizeof(PyObject *));
100
101 NUITKA_ASSIGN_BUILTIN(compile);
102
103 PyObject *result = CALL_FUNCTION(tstate, NUITKA_ACCESS_BUILTIN(compile), pos_args, kw_args);
104
105 Py_DECREF(pos_args);
106 Py_XDECREF(kw_args);
107
108 return result;
109}
110
115#if PYTHON_VERSION < 0x300
116
117bool EXEC_FILE_ARG_HANDLING(PyThreadState *tstate, PyObject **prog, PyObject **name) {
118 CHECK_OBJECT(*prog);
119 CHECK_OBJECT(*name);
120
121 if (PyFile_Check(*prog)) {
122 PyObject *old = *name;
123 *name = PyFile_Name(*prog);
124 Py_INCREF(*name);
125 Py_DECREF(old);
126
127 if (unlikely(*name == NULL)) {
128 return false;
129 }
130
131 old = *prog;
132 *prog = CALL_METHOD_NO_ARGS(tstate, *prog, const_str_plain_read);
133 Py_DECREF(old);
134
135 if (unlikely(*prog == NULL)) {
136 return false;
137 }
138 }
139
140 return true;
141}
142#endif
143
148PyObject *EVAL_CODE(PyThreadState *tstate, PyObject *code, PyObject *globals, PyObject *locals, PyObject *closure) {
149 CHECK_OBJECT(code);
150 CHECK_OBJECT(globals);
151 CHECK_OBJECT(locals);
152
153 if (PyDict_Check(globals) == 0) {
154 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "exec: arg 2 must be a dictionary or None");
155 return NULL;
156 }
157
158 // TODO: Our re-formulation prevents this externally, doesn't it.
159 if (locals == Py_None) {
160 locals = globals;
161 }
162
163 if (PyMapping_Check(locals) == 0) {
164 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "exec: arg 3 must be a mapping or None");
165 return NULL;
166 }
167
168 // Set the "__builtins__" value in globals, it is expected to be present.
169 assert(builtin_module != NULL);
170
171 if (PyDict_Check(globals)) {
172 const int res = DICT_HAS_ITEM(tstate, globals, const_str_plain___builtins__);
173
174 if (res == 0) {
175 if (PyDict_SetItem(globals, const_str_plain___builtins__, (PyObject *)builtin_module) != 0) {
176 // Not really allowed to happen, so far this was seen only with
177 // C compilers getting the value of "res" wrong.
178 assert(false);
179 return NULL;
180 }
181 }
182 }
183
184 if (isFakeCodeObject((PyCodeObject *)code)) {
185 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_RuntimeError,
186 "compiled function code objects do not work with exec/eval");
187 return NULL;
188 }
189
190#if PYTHON_VERSION < 0x300
191 PyObject *result = PyEval_EvalCode((PyCodeObject *)code, globals, locals);
192#elif PYTHON_VERSION < 0x3b0
193 PyObject *result = PyEval_EvalCode(code, globals, locals);
194#else
195 PyObject *result = PyEval_EvalCodeEx(code, globals, locals, NULL, 0, NULL, 0, NULL, 0, NULL, closure);
196#endif
197
198 if (unlikely(result == NULL)) {
199 return NULL;
200 }
201
202 return result;
203}
204
212NUITKA_DEFINE_BUILTIN(open);
213
214#if PYTHON_VERSION < 0x300
215PyObject *BUILTIN_OPEN(PyThreadState *tstate, PyObject *file_name, PyObject *mode, PyObject *buffering) {
216 NUITKA_ASSIGN_BUILTIN(open);
217
218 PyObject *result;
219
220 if (TRACE_FILE_OPEN(tstate, file_name, mode, buffering, &result)) {
221 return result;
222 }
223
224 PyObject *args[] = {file_name, mode, buffering};
225
226 char const *arg_names[] = {"name", "mode", "buffering"};
227
228 return CALL_BUILTIN_KW_ARGS(tstate, NUITKA_ACCESS_BUILTIN(open), args, arg_names, 3, 0);
229}
230#else
231PyObject *BUILTIN_OPEN(PyThreadState *tstate, PyObject *file_name, PyObject *mode, PyObject *buffering,
232 PyObject *encoding, PyObject *errors, PyObject *newline, PyObject *closefd, PyObject *opener) {
233 NUITKA_ASSIGN_BUILTIN(open);
234
235 PyObject *result;
236
237 if (TRACE_FILE_OPEN(tstate, file_name, mode, buffering, encoding, errors, newline, closefd, opener, &result)) {
238 return result;
239 }
240
241 PyObject *args[] = {file_name, mode, buffering, encoding, errors, newline, closefd, opener};
242
243 char const *arg_names[] = {"file", "mode", "buffering", "encoding", "errors", "newline", "closefd", "opener"};
244
245 return CALL_BUILTIN_KW_ARGS(tstate, NUITKA_ACCESS_BUILTIN(open), args, arg_names, 8, 0);
246}
247
248#endif
249
250NUITKA_DEFINE_BUILTIN(input);
251
252PyObject *BUILTIN_INPUT(PyThreadState *tstate, PyObject *prompt) {
253 NUITKA_ASSIGN_BUILTIN(input);
254
255#if NUITKA_STDERR_NOT_VISIBLE && (PYTHON_VERSION >= 0x300 || !defined(_WIN32))
256 if (prompt != NULL) {
257 PRINT_ITEM(prompt);
258 prompt = NULL;
259 }
260#endif
261
262 if (prompt == NULL) {
263 return CALL_FUNCTION_NO_ARGS(tstate, NUITKA_ACCESS_BUILTIN(input));
264 } else {
265 return CALL_FUNCTION_WITH_SINGLE_ARG(tstate, NUITKA_ACCESS_BUILTIN(input), prompt);
266 }
267}
268
273NUITKA_DEFINE_BUILTIN(staticmethod)
274
275PyObject *BUILTIN_STATICMETHOD(PyThreadState *tstate, PyObject *value) {
276 NUITKA_ASSIGN_BUILTIN(staticmethod);
277
278 return CALL_FUNCTION_WITH_SINGLE_ARG(tstate, NUITKA_ACCESS_BUILTIN(staticmethod), value);
279}
280
285NUITKA_DEFINE_BUILTIN(classmethod)
286
287PyObject *BUILTIN_CLASSMETHOD(PyThreadState *tstate, PyObject *value) {
288 NUITKA_ASSIGN_BUILTIN(classmethod);
289
290 return CALL_FUNCTION_WITH_SINGLE_ARG(tstate, NUITKA_ACCESS_BUILTIN(classmethod), value);
291}
292
293#if PYTHON_VERSION >= 0x300
294
303NUITKA_DEFINE_BUILTIN(bytes);
304
305PyObject *BUILTIN_BYTES1(PyThreadState *tstate, PyObject *value) {
306 NUITKA_ASSIGN_BUILTIN(bytes);
307
308 return CALL_FUNCTION_WITH_SINGLE_ARG(tstate, NUITKA_ACCESS_BUILTIN(bytes), value);
309}
310
311PyObject *BUILTIN_BYTES3(PyThreadState *tstate, PyObject *value, PyObject *encoding, PyObject *errors) {
312 NUITKA_ASSIGN_BUILTIN(bytes);
313
314 PyObject *args[] = {value, encoding, errors};
315
316 char const *arg_names[] = {"value", "encoding", "errors"};
317
318 return CALL_BUILTIN_KW_ARGS(tstate, NUITKA_ACCESS_BUILTIN(bytes), args, arg_names, 3, 0);
319}
320#endif
321
326PyObject *BUILTIN_BIN(PyObject *value) {
327 // Note: I don't really know why "oct" and "hex" don't use this as well.
328 PyObject *result = PyNumber_ToBase(value, 2);
329
330 if (unlikely(result == NULL)) {
331 return NULL;
332 }
333
334 return result;
335}
336
341PyObject *BUILTIN_OCT(PyThreadState *tstate, PyObject *value) {
342#if PYTHON_VERSION >= 0x300
343 PyObject *result = PyNumber_ToBase(value, 8);
344
345 if (unlikely(result == NULL)) {
346 return NULL;
347 }
348
349 return result;
350#else
351 if (unlikely(value == NULL)) {
352 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "oct() argument can't be converted to oct");
353 return NULL;
354 }
355
356 PyNumberMethods *nb = Py_TYPE(value)->tp_as_number;
357
358 if (unlikely(nb == NULL || nb->nb_oct == NULL)) {
359 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "oct() argument can't be converted to oct");
360 return NULL;
361 }
362
363 PyObject *result = (*nb->nb_oct)(value);
364
365 if (result) {
366 if (unlikely(!PyString_Check(result))) {
367 PyErr_Format(PyExc_TypeError, "__oct__ returned non-string (type %s)", Py_TYPE(result)->tp_name);
368
369 Py_DECREF(result);
370 return NULL;
371 }
372 }
373
374 return result;
375#endif
376}
377
382PyObject *BUILTIN_HEX(PyThreadState *tstate, PyObject *value) {
383#if PYTHON_VERSION >= 0x300
384 PyObject *result = PyNumber_ToBase(value, 16);
385
386 if (unlikely(result == NULL)) {
387 return NULL;
388 }
389
390 return result;
391#else
392 if (unlikely(value == NULL)) {
393 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "hex() argument can't be converted to hex");
394 return NULL;
395 }
396
397 PyNumberMethods *nb = Py_TYPE(value)->tp_as_number;
398
399 if (unlikely(nb == NULL || nb->nb_hex == NULL)) {
400 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "hex() argument can't be converted to hex");
401 return NULL;
402 }
403
404 PyObject *result = (*nb->nb_hex)(value);
405
406 if (likely(result)) {
407 if (unlikely(!PyString_Check(result))) {
408 PyErr_Format(PyExc_TypeError, "__hex__ returned non-string (type %s)", Py_TYPE(result)->tp_name);
409
410 Py_DECREF(result);
411 return NULL;
412 }
413 }
414
415 return result;
416#endif
417}
418
423static void SET_HASH_NOT_IMPLEMENTED_ERROR(PyThreadState *tstate, PyObject *value) {
424 // TODO: Use our own formatting code.
425 // spell-checker: ignore unhashable
426
427 PyErr_Format(PyExc_TypeError, "unhashable type: '%s'", Py_TYPE(value)->tp_name);
428}
429
430#if PYTHON_VERSION < 0x300
431// Helper to make hash from pointer value, compatible with CPython.
432static long Nuitka_HashFromPointer(void *p) {
433 size_t y = (size_t)p;
434 y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4));
435 long x = (long)y;
436 if (unlikely(x == -1)) {
437 x = -2;
438 }
439 return x;
440}
441#endif
442
443PyObject *BUILTIN_HASH(PyThreadState *tstate, PyObject *value) {
444 PyTypeObject *type = Py_TYPE(value);
445
446 if (likely(type->tp_hash != NULL)) {
447 Py_hash_t hash = (*type->tp_hash)(value);
448
449 if (unlikely(hash == -1)) {
450 return NULL;
451 }
452
453#if PYTHON_VERSION < 0x300
454 return Nuitka_PyInt_FromLong(hash);
455#else
456 // TODO: Have a dedicated helper of ours for this as well.
457 return PyLong_FromSsize_t(hash);
458#endif
459 }
460
461#if PYTHON_VERSION < 0x300
462 if (likely(type->tp_compare == NULL && TP_RICHCOMPARE(type) == NULL)) {
463 Py_hash_t hash = Nuitka_HashFromPointer(value);
464 return Nuitka_PyInt_FromLong(hash);
465 }
466#endif
467
468 SET_HASH_NOT_IMPLEMENTED_ERROR(tstate, value);
469 return NULL;
470}
471
472Py_hash_t HASH_VALUE_WITH_ERROR(PyThreadState *tstate, PyObject *value) {
473 PyTypeObject *type = Py_TYPE(value);
474
475 if (likely(type->tp_hash != NULL)) {
476 Py_hash_t hash = (*type->tp_hash)(value);
477 return hash;
478 }
479
480#if PYTHON_VERSION < 0x300
481 if (likely(type->tp_compare == NULL && TP_RICHCOMPARE(type) == NULL)) {
482 return Nuitka_HashFromPointer(value);
483 }
484#endif
485
486 SET_HASH_NOT_IMPLEMENTED_ERROR(tstate, value);
487 return -1;
488}
489
490Py_hash_t HASH_VALUE_WITHOUT_ERROR(PyThreadState *tstate, PyObject *value) {
491 PyTypeObject *type = Py_TYPE(value);
492
493 if (likely(type->tp_hash != NULL)) {
494 Py_hash_t hash = (*type->tp_hash)(value);
495
496 if (unlikely(hash == -1)) {
497 CLEAR_ERROR_OCCURRED(tstate);
498 }
499
500 return hash;
501 }
502
503#if PYTHON_VERSION < 0x300
504 if (likely(type->tp_compare == NULL && TP_RICHCOMPARE(type) == NULL)) {
505 return Nuitka_HashFromPointer(value);
506 }
507#endif
508
509 return -1;
510}
511
520PyObject *BUILTIN_BYTEARRAY1(PyObject *value) {
521 PyObject *result = PyByteArray_FromObject(value);
522
523 if (unlikely(result == NULL)) {
524 return NULL;
525 }
526
527 return result;
528}
529
530NUITKA_DEFINE_BUILTIN(bytearray)
531
532PyObject *BUILTIN_BYTEARRAY3(PyThreadState *tstate, PyObject *string, PyObject *encoding, PyObject *errors) {
533 CHECK_OBJECT(string);
534 CHECK_OBJECT(encoding);
535
536 NUITKA_ASSIGN_BUILTIN(bytearray);
537
538 if (errors == NULL) {
539 PyObject *args[] = {string, encoding};
540
541 PyObject *result = CALL_FUNCTION_WITH_ARGS2(tstate, NUITKA_ACCESS_BUILTIN(bytearray), args);
542
543 return result;
544 } else {
545 PyObject *args[] = {string, encoding, errors};
546
547 PyObject *result = CALL_FUNCTION_WITH_ARGS3(tstate, NUITKA_ACCESS_BUILTIN(bytearray), args);
548
549 return result;
550 }
551}
552
562// From CPython:
563typedef struct {
564 /* Python object folklore: */
565 PyObject_HEAD
566
567 PyObject *it_callable;
568 PyObject *it_sentinel;
570
571PyObject *BUILTIN_ITER2(PyObject *callable, PyObject *sentinel) {
572 calliterobject *result = (calliterobject *)Nuitka_GC_New(&PyCallIter_Type);
573
574 if (unlikely(result == NULL)) {
575 return NULL;
576 }
577
578 // Note: References were taken at call site already.
579 result->it_callable = callable;
580 Py_INCREF(callable);
581 result->it_sentinel = sentinel;
582 Py_INCREF(sentinel);
583
584 Nuitka_GC_Track(result);
585
586 return (PyObject *)result;
587}
588
596PyObject *BUILTIN_TYPE1(PyObject *arg) {
597 CHECK_OBJECT(arg);
598
599 PyObject *result = (PyObject *)Py_TYPE(arg);
600 CHECK_OBJECT(result);
601
602 Py_INCREF(result);
603 return result;
604}
605
606PyObject *BUILTIN_TYPE3(PyThreadState *tstate, PyObject *module_name, PyObject *name, PyObject *bases, PyObject *dict) {
607 PyObject *pos_args = MAKE_TUPLE3(tstate, name, bases, dict);
608
609 PyObject *result = PyType_Type.tp_new(&PyType_Type, pos_args, NULL);
610
611 if (unlikely(result == NULL)) {
612 Py_DECREF(pos_args);
613 return NULL;
614 }
615
616 PyTypeObject *type = Py_TYPE(result);
617
618 if (likely(Nuitka_Type_IsSubtype(type, &PyType_Type))) {
619 if (NuitkaType_HasFeatureClass(type) && type->tp_init != NULL) {
620 int res = type->tp_init(result, pos_args, NULL);
621
622 if (unlikely(res < 0)) {
623 Py_DECREF(pos_args);
624 Py_DECREF(result);
625 return NULL;
626 }
627 }
628 }
629
630 Py_DECREF(pos_args);
631
632 if (HAS_ATTR_BOOL(tstate, result, const_str_plain___module__) == false) {
633 bool b_res = SET_ATTRIBUTE(tstate, result, const_str_plain___module__, module_name);
634
635 if (b_res == false) {
636 Py_DECREF(result);
637 return NULL;
638 }
639 }
640
641 return result;
642}
643
649NUITKA_DEFINE_BUILTIN(super);
650
651PyObject *BUILTIN_SUPER2(PyThreadState *tstate, PyDictObject *module_dict, PyObject *type, PyObject *object) {
652 CHECK_OBJECT(type);
653 CHECK_OBJECT_X(object);
654
655 PyObject *super_value = GET_STRING_DICT_VALUE(module_dict, (Nuitka_StringObject *)const_str_plain_super);
656
657 if (super_value == NULL) {
658 NUITKA_ASSIGN_BUILTIN(super);
659
660 super_value = NUITKA_ACCESS_BUILTIN(super);
661 }
662
663 if (object != NULL) {
664 PyObject *args[] = {type, object};
665
666 return CALL_FUNCTION_WITH_ARGS2(tstate, super_value, args);
667 } else {
668 return CALL_FUNCTION_WITH_SINGLE_ARG(tstate, super_value, type);
669 }
670}
671
672PyObject *BUILTIN_SUPER0(PyThreadState *tstate, PyDictObject *module_dict, PyObject *type, PyObject *object) {
673 if (object == Py_None) {
674 object = NULL;
675 }
676
677 return BUILTIN_SUPER2(tstate, module_dict, type, object);
678}
679
684PyObject *BUILTIN_CALLABLE(PyObject *value) {
685 int res = PyCallable_Check(value);
686 PyObject *result = BOOL_FROM(res != 0);
687 Py_INCREF_IMMORTAL(result);
688 return result;
689}
690
691/* The "getattr" built-in with default value.
692 *
693 * We might want to split it off for a variant without default value.
694 *
695 **/
696
697PyObject *BUILTIN_GETATTR(PyThreadState *tstate, PyObject *object, PyObject *attribute, PyObject *default_value) {
698 CHECK_OBJECT(object);
699 CHECK_OBJECT(attribute);
700 CHECK_OBJECT_X(default_value);
701
702#if PYTHON_VERSION < 0x300
703 if (PyUnicode_Check(attribute)) {
704 attribute = _PyUnicode_AsDefaultEncodedString(attribute, NULL);
705
706 if (unlikely(attribute == NULL)) {
707 return NULL;
708 }
709 }
710
711 if (unlikely(!PyString_Check(attribute))) {
712 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "getattr(): attribute name must be string");
713 return NULL;
714 }
715#else
716 if (!PyUnicode_Check(attribute)) {
717 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "getattr(): attribute name must be string");
718 return NULL;
719 }
720#endif
721
722 // TODO: Once we can easily generate a variant that avoids the creation of
723 // the exception as best as it can, we can switch to our own code helper.
724 PyObject *result = PyObject_GetAttr(object, attribute);
725
726 if (result == NULL) {
727 if (default_value != NULL) {
728 if (HAS_ERROR_OCCURRED(tstate)) {
729 if (EXCEPTION_MATCH_BOOL_SINGLE(tstate, GET_ERROR_OCCURRED(tstate), PyExc_AttributeError)) {
730 CLEAR_ERROR_OCCURRED(tstate);
731 }
732 }
733
734 Py_INCREF(default_value);
735 return default_value;
736 } else {
737 assert(HAS_ERROR_OCCURRED(tstate));
738
739 return NULL;
740 }
741 } else {
742 return result;
743 }
744}
745
750PyObject *BUILTIN_SETATTR(PyObject *object, PyObject *attribute, PyObject *value) {
751 int res = PyObject_SetAttr(object, attribute, value);
752
753 if (unlikely(res < 0)) {
754 return NULL;
755 }
756
757 // No reference returned.
758 return Py_None;
759}
760
761PyObject *BUILTIN_INT2(PyThreadState *tstate, PyObject *value, PyObject *base) {
762#if PYTHON_VERSION < 0x300
763 long base_int = PyInt_AsLong(base);
764#else
765 Py_ssize_t base_int = PyNumber_AsSsize_t(base, NULL);
766#endif
767
768 if (unlikely(base_int == -1)) {
769 PyObject *error = GET_ERROR_OCCURRED(tstate);
770
771 if (likely(error != NULL)) {
772 assert(HAS_ERROR_OCCURRED(tstate));
773
774#if PYTHON_VERSION >= 0x300
775 if (EXCEPTION_MATCH_BOOL_SINGLE(tstate, error, PyExc_OverflowError)) {
776 PyErr_Format(PyExc_ValueError,
777#if PYTHON_VERSION < 0x324
778 "int() arg 2 must be >= 2 and <= 36"
779#elif PYTHON_VERSION < 0x364
780 "int() base must be >= 2 and <= 36"
781#else
782 "int() base must be >= 2 and <= 36, or 0"
783#endif
784 );
785 }
786#endif
787 return NULL;
788 }
789 }
790
791#if PYTHON_VERSION >= 0x300
792 if (unlikely((base_int != 0 && base_int < 2) || base_int > 36)) {
793 PyErr_Format(PyExc_ValueError,
794#if PYTHON_VERSION < 0x324
795 "int() arg 2 must be >= 2 and <= 36"
796#elif PYTHON_VERSION < 0x364
797 "int() base must be >= 2 and <= 36"
798#else
799 "int() base must be >= 2 and <= 36, or 0"
800#endif
801 );
802
803 return NULL;
804 }
805#endif
806
807#if PYTHON_VERSION < 0x300
808 if (unlikely(!Nuitka_String_Check(value) && !PyUnicode_Check(value))) {
809 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "int() can't convert non-string with explicit base");
810 return NULL;
811 }
812
813 char *value_str = Nuitka_String_AsString(value);
814 if (unlikely(value_str == NULL)) {
815 return NULL;
816 }
817
818 PyObject *result = PyInt_FromString(value_str, NULL, base_int);
819 if (unlikely(result == NULL)) {
820 return NULL;
821 }
822
823 return result;
824#else
825 if (PyUnicode_Check(value)) {
826 return PyLong_FromUnicodeObject(value, (int)base_int);
827 } else if (PyBytes_Check(value) || PyByteArray_Check(value)) {
828 // Check for "NUL" as PyLong_FromString has no length parameter,
829 Py_ssize_t size = Py_SIZE(value);
830 char const *value_str;
831
832 if (PyByteArray_Check(value)) {
833 value_str = PyByteArray_AS_STRING(value);
834 } else {
835 value_str = PyBytes_AS_STRING(value);
836 }
837
838 PyObject *result = NULL;
839
840 if (size != 0 && strlen(value_str) == (size_t)size) {
841 result = PyLong_FromString((char *)value_str, NULL, (int)base_int);
842 }
843
844 if (unlikely(result == NULL)) {
845 PyErr_Format(PyExc_ValueError, "invalid literal for int() with base %d: %R", base_int, value);
846
847 return NULL;
848 }
849
850 return result;
851 } else {
852 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "int() can't convert non-string with explicit base");
853 return NULL;
854 }
855#endif
856}
857
858#if PYTHON_VERSION < 0x300
859// Note: Python3 uses TO_INT2 function.
860PyObject *BUILTIN_LONG2(PyThreadState *tstate, PyObject *value, PyObject *base) {
861 long base_int = PyInt_AsLong(base);
862
863 if (unlikely(base_int == -1)) {
864 if (likely(HAS_ERROR_OCCURRED(tstate))) {
865 return NULL;
866 }
867 }
868
869 if (unlikely(!Nuitka_String_Check(value) && !PyUnicode_Check(value))) {
870 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "long() can't convert non-string with explicit base");
871 return NULL;
872 }
873
874 char *value_str = Nuitka_String_AsString(value);
875 if (unlikely(value_str == NULL)) {
876 return NULL;
877 }
878
879 PyObject *result = PyLong_FromString(value_str, NULL, base_int);
880 if (unlikely(result == NULL)) {
881 return NULL;
882 }
883
884 return result;
885}
886#endif
887
888// Part of "Nuitka", an optimizing Python compiler that is compatible and
889// integrates with CPython, but also works on its own.
890//
891// Licensed under the Apache License, Version 2.0 (the "License");
892// you may not use this file except in compliance with the License.
893// You may obtain a copy of the License at
894//
895// http://www.apache.org/licenses/LICENSE-2.0
896//
897// Unless required by applicable law or agreed to in writing, software
898// distributed under the License is distributed on an "AS IS" BASIS,
899// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
900// See the License for the specific language governing permissions and
901// limitations under the License.
Definition HelpersBuiltin.c:563