Nuitka
The Python compiler
Loading...
Searching...
No Matches
HelpersBuiltin.c
1// Copyright 2026, 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
250#if defined(_WIN32) && (defined(_NUITKA_ATTACH_CONSOLE_WINDOW) || defined(_NUITKA_DISABLE_CONSOLE_WINDOW))
251// TODO: Have an include file for these and the defines combination.
252extern PyObject *Nuitka_Win32_InputDialog(PyThreadState *tstate, PyObject *prompt);
253#endif
254
255NUITKA_DEFINE_BUILTIN(input);
256
257PyObject *BUILTIN_INPUT(PyThreadState *tstate, PyObject *prompt) {
258#if defined(_WIN32) && (defined(_NUITKA_ATTACH_CONSOLE_WINDOW) || defined(_NUITKA_DISABLE_CONSOLE_WINDOW))
259 // Check if stdin is None, which happens in "windows" mode (pythonw.exe behavior).
260 PyObject *stdin_obj = PySys_GetObject("stdin"); // Borrows reference
261 if (stdin_obj == Py_None) {
262 return Nuitka_Win32_InputDialog(tstate, prompt);
263 }
264#endif
265
266 NUITKA_ASSIGN_BUILTIN(input);
267
268#if NUITKA_STDERR_NOT_VISIBLE && (PYTHON_VERSION >= 0x300 || !defined(_WIN32))
269 if (prompt != NULL) {
270 PRINT_ITEM(prompt);
271 prompt = NULL;
272 }
273#endif
274
275 if (prompt == NULL) {
276 return CALL_FUNCTION_NO_ARGS(tstate, NUITKA_ACCESS_BUILTIN(input));
277 } else {
278 return CALL_FUNCTION_WITH_SINGLE_ARG(tstate, NUITKA_ACCESS_BUILTIN(input), prompt);
279 }
280}
281
286NUITKA_DEFINE_BUILTIN(staticmethod)
287
288PyObject *BUILTIN_STATICMETHOD(PyThreadState *tstate, PyObject *value) {
289 NUITKA_ASSIGN_BUILTIN(staticmethod);
290
291 return CALL_FUNCTION_WITH_SINGLE_ARG(tstate, NUITKA_ACCESS_BUILTIN(staticmethod), value);
292}
293
298NUITKA_DEFINE_BUILTIN(classmethod)
299
300PyObject *BUILTIN_CLASSMETHOD(PyThreadState *tstate, PyObject *value) {
301 NUITKA_ASSIGN_BUILTIN(classmethod);
302
303 return CALL_FUNCTION_WITH_SINGLE_ARG(tstate, NUITKA_ACCESS_BUILTIN(classmethod), value);
304}
305
306#if PYTHON_VERSION >= 0x300
307
316NUITKA_DEFINE_BUILTIN(bytes);
317
318PyObject *BUILTIN_BYTES1(PyThreadState *tstate, PyObject *value) {
319 NUITKA_ASSIGN_BUILTIN(bytes);
320
321 return CALL_FUNCTION_WITH_SINGLE_ARG(tstate, NUITKA_ACCESS_BUILTIN(bytes), value);
322}
323
324PyObject *BUILTIN_BYTES3(PyThreadState *tstate, PyObject *value, PyObject *encoding, PyObject *errors) {
325 NUITKA_ASSIGN_BUILTIN(bytes);
326
327 PyObject *args[] = {value, encoding, errors};
328
329 char const *arg_names[] = {"value", "encoding", "errors"};
330
331 return CALL_BUILTIN_KW_ARGS(tstate, NUITKA_ACCESS_BUILTIN(bytes), args, arg_names, 3, 0);
332}
333#endif
334
339PyObject *BUILTIN_BIN(PyObject *value) {
340 // Note: I don't really know why "oct" and "hex" don't use this as well.
341 PyObject *result = PyNumber_ToBase(value, 2);
342
343 if (unlikely(result == NULL)) {
344 return NULL;
345 }
346
347 return result;
348}
349
354PyObject *BUILTIN_OCT(PyThreadState *tstate, PyObject *value) {
355#if PYTHON_VERSION >= 0x300
356 PyObject *result = PyNumber_ToBase(value, 8);
357
358 if (unlikely(result == NULL)) {
359 return NULL;
360 }
361
362 return result;
363#else
364 if (unlikely(value == NULL)) {
365 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "oct() argument can't be converted to oct");
366 return NULL;
367 }
368
369 PyNumberMethods *nb = Py_TYPE(value)->tp_as_number;
370
371 if (unlikely(nb == NULL || nb->nb_oct == NULL)) {
372 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "oct() argument can't be converted to oct");
373 return NULL;
374 }
375
376 PyObject *result = (*nb->nb_oct)(value);
377
378 if (result) {
379 if (unlikely(!PyString_Check(result))) {
380 PyErr_Format(PyExc_TypeError, "__oct__ returned non-string (type %s)", Py_TYPE(result)->tp_name);
381
382 Py_DECREF(result);
383 return NULL;
384 }
385 }
386
387 return result;
388#endif
389}
390
395PyObject *BUILTIN_HEX(PyThreadState *tstate, PyObject *value) {
396#if PYTHON_VERSION >= 0x300
397 PyObject *result = PyNumber_ToBase(value, 16);
398
399 if (unlikely(result == NULL)) {
400 return NULL;
401 }
402
403 return result;
404#else
405 if (unlikely(value == NULL)) {
406 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "hex() argument can't be converted to hex");
407 return NULL;
408 }
409
410 PyNumberMethods *nb = Py_TYPE(value)->tp_as_number;
411
412 if (unlikely(nb == NULL || nb->nb_hex == NULL)) {
413 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "hex() argument can't be converted to hex");
414 return NULL;
415 }
416
417 PyObject *result = (*nb->nb_hex)(value);
418
419 if (likely(result)) {
420 if (unlikely(!PyString_Check(result))) {
421 PyErr_Format(PyExc_TypeError, "__hex__ returned non-string (type %s)", Py_TYPE(result)->tp_name);
422
423 Py_DECREF(result);
424 return NULL;
425 }
426 }
427
428 return result;
429#endif
430}
431
436static void SET_HASH_NOT_IMPLEMENTED_ERROR(PyThreadState *tstate, PyObject *value) {
437 // TODO: Use our own formatting code.
438 // spell-checker: ignore unhashable
439
440 PyErr_Format(PyExc_TypeError, "unhashable type: '%s'", Py_TYPE(value)->tp_name);
441}
442
443#if PYTHON_VERSION < 0x300
444// Helper to make hash from pointer value, compatible with CPython.
445static long Nuitka_HashFromPointer(void *p) {
446 size_t y = (size_t)p;
447 y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4));
448 long x = (long)y;
449 if (unlikely(x == -1)) {
450 x = -2;
451 }
452 return x;
453}
454#endif
455
456PyObject *BUILTIN_HASH(PyThreadState *tstate, PyObject *value) {
457 PyTypeObject *type = Py_TYPE(value);
458
459 if (likely(type->tp_hash != NULL)) {
460 Py_hash_t hash = (*type->tp_hash)(value);
461
462 if (unlikely(hash == -1)) {
463 return NULL;
464 }
465
466#if PYTHON_VERSION < 0x300
467 return Nuitka_PyInt_FromLong(hash);
468#else
469 // TODO: Have a dedicated helper of ours for this as well.
470 return PyLong_FromSsize_t(hash);
471#endif
472 }
473
474#if PYTHON_VERSION < 0x300
475 if (likely(type->tp_compare == NULL && TP_RICHCOMPARE(type) == NULL)) {
476 Py_hash_t hash = Nuitka_HashFromPointer(value);
477 return Nuitka_PyInt_FromLong(hash);
478 }
479#endif
480
481 SET_HASH_NOT_IMPLEMENTED_ERROR(tstate, value);
482 return NULL;
483}
484
485Py_hash_t HASH_VALUE_WITH_ERROR(PyThreadState *tstate, PyObject *value) {
486 PyTypeObject *type = Py_TYPE(value);
487
488 if (likely(type->tp_hash != NULL)) {
489 Py_hash_t hash = (*type->tp_hash)(value);
490 return hash;
491 }
492
493#if PYTHON_VERSION < 0x300
494 if (likely(type->tp_compare == NULL && TP_RICHCOMPARE(type) == NULL)) {
495 return Nuitka_HashFromPointer(value);
496 }
497#endif
498
499 SET_HASH_NOT_IMPLEMENTED_ERROR(tstate, value);
500 return -1;
501}
502
503Py_hash_t HASH_VALUE_WITHOUT_ERROR(PyThreadState *tstate, PyObject *value) {
504 PyTypeObject *type = Py_TYPE(value);
505
506 if (likely(type->tp_hash != NULL)) {
507 Py_hash_t hash = (*type->tp_hash)(value);
508
509 if (unlikely(hash == -1)) {
510 CLEAR_ERROR_OCCURRED(tstate);
511 }
512
513 return hash;
514 }
515
516#if PYTHON_VERSION < 0x300
517 if (likely(type->tp_compare == NULL && TP_RICHCOMPARE(type) == NULL)) {
518 return Nuitka_HashFromPointer(value);
519 }
520#endif
521
522 return -1;
523}
524
533PyObject *BUILTIN_BYTEARRAY1(PyObject *value) {
534 PyObject *result = PyByteArray_FromObject(value);
535
536 if (unlikely(result == NULL)) {
537 return NULL;
538 }
539
540 return result;
541}
542
543NUITKA_DEFINE_BUILTIN(bytearray)
544
545PyObject *BUILTIN_BYTEARRAY3(PyThreadState *tstate, PyObject *string, PyObject *encoding, PyObject *errors) {
546 CHECK_OBJECT(string);
547 CHECK_OBJECT(encoding);
548
549 NUITKA_ASSIGN_BUILTIN(bytearray);
550
551 if (errors == NULL) {
552 PyObject *args[] = {string, encoding};
553
554 PyObject *result = CALL_FUNCTION_WITH_ARGS2(tstate, NUITKA_ACCESS_BUILTIN(bytearray), args);
555
556 return result;
557 } else {
558 PyObject *args[] = {string, encoding, errors};
559
560 PyObject *result = CALL_FUNCTION_WITH_ARGS3(tstate, NUITKA_ACCESS_BUILTIN(bytearray), args);
561
562 return result;
563 }
564}
565
575// From CPython:
576typedef struct {
577 /* Python object folklore: */
578 PyObject_HEAD
579
580 PyObject *it_callable;
581 PyObject *it_sentinel;
583
584PyObject *BUILTIN_ITER2(PyObject *callable, PyObject *sentinel) {
585 calliterobject *result = (calliterobject *)Nuitka_GC_New(&PyCallIter_Type);
586
587 if (unlikely(result == NULL)) {
588 return NULL;
589 }
590
591 // Note: References were taken at call site already.
592 result->it_callable = callable;
593 Py_INCREF(callable);
594 result->it_sentinel = sentinel;
595 Py_INCREF(sentinel);
596
597 Nuitka_GC_Track(result);
598
599 return (PyObject *)result;
600}
601
609PyObject *BUILTIN_TYPE1(PyObject *arg) {
610 CHECK_OBJECT(arg);
611
612 PyObject *result = (PyObject *)Py_TYPE(arg);
613 CHECK_OBJECT(result);
614
615 Py_INCREF(result);
616 return result;
617}
618
619PyObject *BUILTIN_TYPE3(PyThreadState *tstate, PyObject *module_name, PyObject *name, PyObject *bases, PyObject *dict) {
620 PyObject *pos_args = MAKE_TUPLE3(tstate, name, bases, dict);
621
622 PyObject *result = PyType_Type.tp_new(&PyType_Type, pos_args, NULL);
623
624 if (unlikely(result == NULL)) {
625 Py_DECREF(pos_args);
626 return NULL;
627 }
628
629 PyTypeObject *type = Py_TYPE(result);
630
631 if (likely(Nuitka_Type_IsSubtype(type, &PyType_Type))) {
632 if (NuitkaType_HasFeatureClass(type) && type->tp_init != NULL) {
633 int res = type->tp_init(result, pos_args, NULL);
634
635 if (unlikely(res < 0)) {
636 Py_DECREF(pos_args);
637 Py_DECREF(result);
638 return NULL;
639 }
640 }
641 }
642
643 Py_DECREF(pos_args);
644
645 if (HAS_ATTR_BOOL(tstate, result, const_str_plain___module__) == false) {
646 bool b_res = SET_ATTRIBUTE(tstate, result, const_str_plain___module__, module_name);
647
648 if (b_res == false) {
649 Py_DECREF(result);
650 return NULL;
651 }
652 }
653
654 return result;
655}
656
662NUITKA_DEFINE_BUILTIN(super);
663
664PyObject *BUILTIN_SUPER2(PyThreadState *tstate, PyDictObject *module_dict, PyObject *type, PyObject *object) {
665 CHECK_OBJECT(type);
666 CHECK_OBJECT_X(object);
667
668 PyObject *super_value = GET_STRING_DICT_VALUE(module_dict, (Nuitka_StringObject *)const_str_plain_super);
669
670 if (super_value == NULL) {
671 NUITKA_ASSIGN_BUILTIN(super);
672
673 super_value = NUITKA_ACCESS_BUILTIN(super);
674 }
675
676 if (object != NULL) {
677 PyObject *args[] = {type, object};
678
679 return CALL_FUNCTION_WITH_ARGS2(tstate, super_value, args);
680 } else {
681 return CALL_FUNCTION_WITH_SINGLE_ARG(tstate, super_value, type);
682 }
683}
684
685PyObject *BUILTIN_SUPER0(PyThreadState *tstate, PyDictObject *module_dict, PyObject *type, PyObject *object) {
686 if (object == Py_None) {
687 object = NULL;
688 }
689
690 return BUILTIN_SUPER2(tstate, module_dict, type, object);
691}
692
697PyObject *BUILTIN_CALLABLE(PyObject *value) {
698 int res = PyCallable_Check(value);
699 PyObject *result = BOOL_FROM(res != 0);
700 Py_INCREF_IMMORTAL(result);
701 return result;
702}
703
704/* The "getattr" built-in with default value.
705 *
706 * We might want to split it off for a variant without default value.
707 *
708 **/
709
710PyObject *BUILTIN_GETATTR(PyThreadState *tstate, PyObject *object, PyObject *attribute, PyObject *default_value) {
711 CHECK_OBJECT(object);
712 CHECK_OBJECT(attribute);
713 CHECK_OBJECT_X(default_value);
714
715#if PYTHON_VERSION < 0x300
716 if (PyUnicode_Check(attribute)) {
717 attribute = _PyUnicode_AsDefaultEncodedString(attribute, NULL);
718
719 if (unlikely(attribute == NULL)) {
720 return NULL;
721 }
722 }
723
724 if (unlikely(!PyString_Check(attribute))) {
725 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "getattr(): attribute name must be string");
726 return NULL;
727 }
728#else
729 if (!PyUnicode_Check(attribute)) {
730 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "getattr(): attribute name must be string");
731 return NULL;
732 }
733#endif
734
735 // TODO: Once we can easily generate a variant that avoids the creation of
736 // the exception as best as it can, we can switch to our own code helper.
737 PyObject *result = PyObject_GetAttr(object, attribute);
738
739 if (result == NULL) {
740 if (default_value != NULL) {
741 if (HAS_ERROR_OCCURRED(tstate)) {
742 if (EXCEPTION_MATCH_BOOL_SINGLE(tstate, GET_ERROR_OCCURRED(tstate), PyExc_AttributeError)) {
743 CLEAR_ERROR_OCCURRED(tstate);
744 }
745 }
746
747 Py_INCREF(default_value);
748 return default_value;
749 } else {
750 assert(HAS_ERROR_OCCURRED(tstate));
751
752 return NULL;
753 }
754 } else {
755 return result;
756 }
757}
758
763PyObject *BUILTIN_SETATTR(PyObject *object, PyObject *attribute, PyObject *value) {
764 int res = PyObject_SetAttr(object, attribute, value);
765
766 if (unlikely(res < 0)) {
767 return NULL;
768 }
769
770 // No reference returned.
771 return Py_None;
772}
773
774PyObject *BUILTIN_INT2(PyThreadState *tstate, PyObject *value, PyObject *base) {
775#if PYTHON_VERSION < 0x300
776 long base_int = PyInt_AsLong(base);
777#else
778 Py_ssize_t base_int = PyNumber_AsSsize_t(base, NULL);
779#endif
780
781 if (unlikely(base_int == -1)) {
782 PyObject *error = GET_ERROR_OCCURRED(tstate);
783
784 if (likely(error != NULL)) {
785 assert(HAS_ERROR_OCCURRED(tstate));
786
787#if PYTHON_VERSION >= 0x300
788 if (EXCEPTION_MATCH_BOOL_SINGLE(tstate, error, PyExc_OverflowError)) {
789 PyErr_Format(PyExc_ValueError,
790#if PYTHON_VERSION < 0x324
791 "int() arg 2 must be >= 2 and <= 36"
792#elif PYTHON_VERSION < 0x364
793 "int() base must be >= 2 and <= 36"
794#else
795 "int() base must be >= 2 and <= 36, or 0"
796#endif
797 );
798 }
799#endif
800 return NULL;
801 }
802 }
803
804#if PYTHON_VERSION >= 0x300
805 if (unlikely((base_int != 0 && base_int < 2) || base_int > 36)) {
806 PyErr_Format(PyExc_ValueError,
807#if PYTHON_VERSION < 0x324
808 "int() arg 2 must be >= 2 and <= 36"
809#elif PYTHON_VERSION < 0x364
810 "int() base must be >= 2 and <= 36"
811#else
812 "int() base must be >= 2 and <= 36, or 0"
813#endif
814 );
815
816 return NULL;
817 }
818#endif
819
820#if PYTHON_VERSION < 0x300
821 if (unlikely(!Nuitka_String_Check(value) && !PyUnicode_Check(value))) {
822 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "int() can't convert non-string with explicit base");
823 return NULL;
824 }
825
826 char *value_str = Nuitka_String_AsString(value);
827 if (unlikely(value_str == NULL)) {
828 return NULL;
829 }
830
831 PyObject *result = PyInt_FromString(value_str, NULL, base_int);
832 if (unlikely(result == NULL)) {
833 return NULL;
834 }
835
836 return result;
837#else
838 if (PyUnicode_Check(value)) {
839 return PyLong_FromUnicodeObject(value, (int)base_int);
840 } else if (PyBytes_Check(value) || PyByteArray_Check(value)) {
841 // Check for "NUL" as PyLong_FromString has no length parameter,
842 Py_ssize_t size = Py_SIZE(value);
843 char const *value_str;
844
845 if (PyByteArray_Check(value)) {
846 value_str = PyByteArray_AS_STRING(value);
847 } else {
848 value_str = PyBytes_AS_STRING(value);
849 }
850
851 PyObject *result = NULL;
852
853 if (size != 0 && strlen(value_str) == (size_t)size) {
854 result = PyLong_FromString((char *)value_str, NULL, (int)base_int);
855 }
856
857 if (unlikely(result == NULL)) {
858 PyErr_Format(PyExc_ValueError, "invalid literal for int() with base %d: %R", base_int, value);
859
860 return NULL;
861 }
862
863 return result;
864 } else {
865 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "int() can't convert non-string with explicit base");
866 return NULL;
867 }
868#endif
869}
870
871#if PYTHON_VERSION < 0x300
872// Note: Python3 uses TO_INT2 function.
873PyObject *BUILTIN_LONG2(PyThreadState *tstate, PyObject *value, PyObject *base) {
874 long base_int = PyInt_AsLong(base);
875
876 if (unlikely(base_int == -1)) {
877 if (likely(HAS_ERROR_OCCURRED(tstate))) {
878 return NULL;
879 }
880 }
881
882 if (unlikely(!Nuitka_String_Check(value) && !PyUnicode_Check(value))) {
883 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "long() can't convert non-string with explicit base");
884 return NULL;
885 }
886
887 char *value_str = Nuitka_String_AsString(value);
888 if (unlikely(value_str == NULL)) {
889 return NULL;
890 }
891
892 PyObject *result = PyLong_FromString(value_str, NULL, base_int);
893 if (unlikely(result == NULL)) {
894 return NULL;
895 }
896
897 return result;
898}
899#endif
900
901// Part of "Nuitka", an optimizing Python compiler that is compatible and
902// integrates with CPython, but also works on its own.
903//
904// Licensed under the GNU Affero General Public License, Version 3 (the "License");
905// you may not use this file except in compliance with the License.
906// You may obtain a copy of the License at
907//
908// http://www.gnu.org/licenses/agpl.txt
909//
910// Unless required by applicable law or agreed to in writing, software
911// distributed under the License is distributed on an "AS IS" BASIS,
912// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
913// See the License for the specific language governing permissions and
914// limitations under the License.
Definition HelpersBuiltin.c:576