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