Nuitka
The Python compiler
Loading...
Searching...
No Matches
CompiledCodeHelpers.c
1// Copyright 2026, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
2
3/* Implementations of compiled code helpers.
4
5 * The definition of a compiled code helper is that it's being used in
6 * generated C code and provides part of the operations implementation.
7 *
8 * Currently we also have standalone mode related code here, patches to CPython
9 * runtime that we do, and e.g. the built-in module. TODO: Move these to their
10 * own files for clarity.
11 */
12
13#include "nuitka/prelude.h"
14
15#include "HelpersBuiltinTypeMethods.c"
16
17static void _initBuiltinTypeMethods(void) {
18#if PYTHON_VERSION < 0x300
19 NUITKA_PRINT_TRACE("main(): Calling _initStrBuiltinMethods().");
20 _initStrBuiltinMethods();
21#else
22 NUITKA_PRINT_TRACE("main(): Calling _initBytesBuiltinMethods().");
23 _initBytesBuiltinMethods();
24#endif
25 NUITKA_PRINT_TRACE("main(): Calling _initUnicodeBuiltinMethods().");
26 _initUnicodeBuiltinMethods();
27 NUITKA_PRINT_TRACE("main(): Calling _initDictBuiltinMethods().");
28 _initDictBuiltinMethods();
29 NUITKA_PRINT_TRACE("main(): Calling _initListBuiltinMethods().");
30 _initListBuiltinMethods();
31}
32
33#if PYTHON_VERSION >= 0x350
34#include "HelpersAllocator.c"
35#endif
36
37#include "HelpersBuiltin.c"
38#include "HelpersBytes.c"
39#include "HelpersClasses.c"
40#include "HelpersDictionaries.c"
41#include "HelpersExceptions.c"
42#include "HelpersFiles.c"
43#include "HelpersFloats.c"
44#include "HelpersHeapStorage.c"
45#include "HelpersImport.c"
46#include "HelpersImportHard.c"
47#include "HelpersLists.c"
48#include "HelpersMappings.c"
49#include "HelpersRaising.c"
50#include "HelpersSequences.c"
51#include "HelpersSlices.c"
52#include "HelpersStrings.c"
53#include "HelpersTuples.c"
54
55#include "HelpersEnvironmentVariables.c"
56#include "HelpersFilesystemPaths.c"
57#include "HelpersSafeStrings.c"
58
59#if PYTHON_VERSION >= 0x3a0
60#include "HelpersMatching.c"
61#endif
62
63#if PYTHON_VERSION < 0x300
64
65static Py_ssize_t ESTIMATE_RANGE(long low, long high, long step) {
66 if (low >= high) {
67 return 0;
68 } else {
69 return (high - low - 1) / step + 1;
70 }
71}
72
73static PyObject *_BUILTIN_RANGE_INT3(long low, long high, long step) {
74 assert(step != 0);
75
76 Py_ssize_t size;
77
78 if (step > 0) {
79 size = ESTIMATE_RANGE(low, high, step);
80 } else {
81 size = ESTIMATE_RANGE(high, low, -step);
82 }
83
84 PyObject *result = MAKE_LIST_EMPTY(tstate, size);
85
86 long current = low;
87
88 for (int i = 0; i < size; i++) {
89 PyList_SET_ITEM(result, i, Nuitka_PyInt_FromLong(current));
90 current += step;
91 }
92
93 return result;
94}
95
96static PyObject *_BUILTIN_RANGE_INT2(long low, long high) { return _BUILTIN_RANGE_INT3(low, high, 1); }
97
98static PyObject *_BUILTIN_RANGE_INT(long boundary) {
99 PyObject *result = MAKE_LIST_EMPTY(tstate, boundary > 0 ? boundary : 0);
100
101 for (int i = 0; i < boundary; i++) {
102 PyList_SET_ITEM(result, i, Nuitka_PyInt_FromLong(i));
103 }
104
105 return result;
106}
107
108static PyObject *TO_RANGE_ARG(PyObject *value, char const *name) {
109 if (likely(PyInt_Check(value) || PyLong_Check(value))) {
110 Py_INCREF(value);
111 return value;
112 }
113
114 PyTypeObject *type = Py_TYPE(value);
115 PyNumberMethods *tp_as_number = type->tp_as_number;
116
117 // Everything that casts to int is allowed.
118 if (
119#if PYTHON_VERSION >= 0x270
120 PyFloat_Check(value) ||
121#endif
122 tp_as_number == NULL || tp_as_number->nb_int == NULL) {
123 PyErr_Format(PyExc_TypeError, "range() integer %s argument expected, got %s.", name, type->tp_name);
124 return NULL;
125 }
126
127 PyObject *result = tp_as_number->nb_int(value);
128
129 if (unlikely(result == NULL)) {
130 return NULL;
131 }
132
133 return result;
134}
135#endif
136
137#if PYTHON_VERSION < 0x300
138
139NUITKA_DEFINE_BUILTIN(range);
140
141PyObject *BUILTIN_RANGE(PyThreadState *tstate, PyObject *boundary) {
142 PyObject *boundary_temp = TO_RANGE_ARG(boundary, "end");
143
144 if (unlikely(boundary_temp == NULL)) {
145 return NULL;
146 }
147
148 long start = PyInt_AsLong(boundary_temp);
149
150 if (start == -1 && DROP_ERROR_OCCURRED(tstate)) {
151 NUITKA_ASSIGN_BUILTIN(range);
152
153 PyObject *result = CALL_FUNCTION_WITH_SINGLE_ARG(tstate, NUITKA_ACCESS_BUILTIN(range), boundary_temp);
154
155 Py_DECREF(boundary_temp);
156
157 return result;
158 }
159 Py_DECREF(boundary_temp);
160
161 return _BUILTIN_RANGE_INT(start);
162}
163
164PyObject *BUILTIN_RANGE2(PyThreadState *tstate, PyObject *low, PyObject *high) {
165 PyObject *low_temp = TO_RANGE_ARG(low, "start");
166
167 if (unlikely(low_temp == NULL)) {
168 return NULL;
169 }
170
171 PyObject *high_temp = TO_RANGE_ARG(high, "end");
172
173 if (unlikely(high_temp == NULL)) {
174 Py_DECREF(low_temp);
175 return NULL;
176 }
177
178 bool fallback = false;
179
180 long start = PyInt_AsLong(low_temp);
181
182 if (unlikely(start == -1 && DROP_ERROR_OCCURRED(tstate))) {
183 fallback = true;
184 }
185
186 long end = PyInt_AsLong(high_temp);
187
188 if (unlikely(end == -1 && DROP_ERROR_OCCURRED(tstate))) {
189 fallback = true;
190 }
191
192 if (fallback) {
193 // Transfers references to tuple.
194 PyObject *pos_args = MAKE_TUPLE2_0(tstate, low_temp, high_temp);
195 NUITKA_ASSIGN_BUILTIN(range);
196
197 PyObject *result = CALL_FUNCTION_WITH_POS_ARGS2(tstate, NUITKA_ACCESS_BUILTIN(range), pos_args);
198
199 Py_DECREF(pos_args);
200
201 return result;
202 } else {
203 Py_DECREF(low_temp);
204 Py_DECREF(high_temp);
205
206 return _BUILTIN_RANGE_INT2(start, end);
207 }
208}
209
210PyObject *BUILTIN_RANGE3(PyThreadState *tstate, PyObject *low, PyObject *high, PyObject *step) {
211 PyObject *low_temp = TO_RANGE_ARG(low, "start");
212
213 if (unlikely(low_temp == NULL)) {
214 return NULL;
215 }
216
217 PyObject *high_temp = TO_RANGE_ARG(high, "end");
218
219 if (unlikely(high_temp == NULL)) {
220 Py_DECREF(low_temp);
221 return NULL;
222 }
223
224 PyObject *step_temp = TO_RANGE_ARG(step, "step");
225
226 if (unlikely(high_temp == NULL)) {
227 Py_DECREF(low_temp);
228 Py_DECREF(high_temp);
229 return NULL;
230 }
231
232 bool fallback = false;
233
234 long start = PyInt_AsLong(low_temp);
235
236 if (unlikely(start == -1 && DROP_ERROR_OCCURRED(tstate))) {
237 fallback = true;
238 }
239
240 long end = PyInt_AsLong(high_temp);
241
242 if (unlikely(end == -1 && DROP_ERROR_OCCURRED(tstate))) {
243 fallback = true;
244 }
245
246 long step_long = PyInt_AsLong(step_temp);
247
248 if (unlikely(step_long == -1 && DROP_ERROR_OCCURRED(tstate))) {
249 fallback = true;
250 }
251
252 if (fallback) {
253 PyObject *pos_args = MAKE_TUPLE3_0(tstate, low_temp, high_temp, step_temp);
254
255 NUITKA_ASSIGN_BUILTIN(range);
256
257 PyObject *result = CALL_FUNCTION_WITH_POS_ARGS3(tstate, NUITKA_ACCESS_BUILTIN(range), pos_args);
258
259 Py_DECREF(pos_args);
260
261 return result;
262 } else {
263 Py_DECREF(low_temp);
264 Py_DECREF(high_temp);
265 Py_DECREF(step_temp);
266
267 if (unlikely(step_long == 0)) {
268 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_ValueError, "range() step argument must not be zero");
269 return NULL;
270 }
271
272 return _BUILTIN_RANGE_INT3(start, end, step_long);
273 }
274}
275
276#endif
277
278#if PYTHON_VERSION < 0x300
279
280/* Same as CPython2: */
281static unsigned long getLengthOfRange(PyThreadState *tstate, long lo, long hi, long step) {
282 assert(step != 0);
283
284 if (step > 0 && lo < hi) {
285 return 1UL + (hi - 1UL - lo) / step;
286 } else if (step < 0 && lo > hi) {
287 return 1UL + (lo - 1UL - hi) / (0UL - step);
288 } else {
289 return 0UL;
290 }
291}
292
293/* Create a "xrange" object from C long values. Used for constant ranges. */
294PyObject *MAKE_XRANGE(PyThreadState *tstate, long start, long stop, long step) {
295 /* TODO: It would be sweet to calculate that on user side already. */
296 unsigned long n = getLengthOfRange(tstate, start, stop, step);
297
298#if defined(__clang__)
299#pragma clang diagnostic push
300#pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
301#endif
302 if (n > (unsigned long)LONG_MAX || (long)n > PY_SSIZE_T_MAX) {
303#if defined(__clang__)
304#pragma clang diagnostic pop
305#endif
306 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_OverflowError, "xrange() result has too many items");
307
308 return NULL;
309 }
310
311 // spell-checker: ignore rangeobject
312
313 struct _rangeobject2 *result = (struct _rangeobject2 *)PyObject_New(struct _rangeobject2, &PyRange_Type);
314 assert(result != NULL);
315
316 result->start = start;
317 result->len = (long)n;
318 result->step = step;
319
320 return (PyObject *)result;
321}
322
323#else
324
325/* Same as CPython3: */
326static PyObject *getLengthOfRange(PyThreadState *tstate, PyObject *start, PyObject *stop, PyObject *step) {
327 nuitka_bool nbool_res = RICH_COMPARE_GT_NBOOL_OBJECT_LONG(step, const_int_0);
328
329 if (unlikely(nbool_res == NUITKA_BOOL_EXCEPTION)) {
330 return NULL;
331 }
332
333 PyObject *lo, *hi;
334
335 // Make sure we use step as a positive number.
336 if (nbool_res == NUITKA_BOOL_TRUE) {
337 lo = start;
338 hi = stop;
339
340 Py_INCREF(step);
341 } else {
342 lo = stop;
343 hi = start;
344
345 step = PyNumber_Negative(step);
346
347 if (unlikely(step == NULL)) {
348 return NULL;
349 }
350
351 nbool_res = RICH_COMPARE_EQ_NBOOL_OBJECT_LONG(step, const_int_0);
352
353 if (unlikely(nbool_res == NUITKA_BOOL_EXCEPTION)) {
354 Py_DECREF(step);
355 return NULL;
356 }
357
358 if (unlikely(nbool_res == NUITKA_BOOL_TRUE)) {
359 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_ValueError, "range() arg 3 must not be zero");
360 Py_DECREF(step);
361
362 return NULL;
363 }
364 }
365
366 // Negative difference, we got zero length.
367 nbool_res = RICH_COMPARE_GE_NBOOL_OBJECT_OBJECT(lo, hi);
368
369 // No distance means we do not have any length to go.
370 if (nbool_res != NUITKA_BOOL_FALSE) {
371 Py_DECREF(step);
372
373 if (unlikely(nbool_res == NUITKA_BOOL_EXCEPTION)) {
374 return NULL;
375 }
376
377 Py_INCREF(const_int_0);
378 return const_int_0;
379 }
380
381 // TODO: Use binary operations here, for now we only eliminated rich comparison API
382 PyObject *tmp1 = PyNumber_Subtract(hi, lo);
383
384 if (unlikely(tmp1 == NULL)) {
385 Py_DECREF(step);
386
387 return NULL;
388 }
389
390 PyObject *diff = PyNumber_Subtract(tmp1, const_int_pos_1);
391 Py_DECREF(tmp1);
392
393 if (unlikely(diff == NULL)) {
394 Py_DECREF(step);
395
396 return NULL;
397 }
398
399 tmp1 = PyNumber_FloorDivide(diff, step);
400 Py_DECREF(diff);
401 Py_DECREF(step);
402
403 if (unlikely(tmp1 == NULL)) {
404 return NULL;
405 }
406
407 PyObject *result = PyNumber_Add(tmp1, const_int_pos_1);
408 Py_DECREF(tmp1);
409
410 return result;
411}
412
413static PyObject *MAKE_XRANGE(PyThreadState *tstate, PyObject *start, PyObject *stop, PyObject *step) {
414 start = Nuitka_Number_IndexAsLong(start);
415 if (unlikely(start == NULL)) {
416 return NULL;
417 }
418 stop = Nuitka_Number_IndexAsLong(stop);
419 if (unlikely(stop == NULL)) {
420 return NULL;
421 }
422 step = Nuitka_Number_IndexAsLong(step);
423 if (unlikely(step == NULL)) {
424 return NULL;
425 }
426
427 PyObject *length = getLengthOfRange(tstate, start, stop, step);
428 if (unlikely(length == NULL)) {
429 return NULL;
430 }
431
432 struct _rangeobject3 *result = (struct _rangeobject3 *)PyObject_New(struct _rangeobject3, &PyRange_Type);
433 assert(result != NULL);
434
435 result->start = start;
436 result->stop = stop;
437 result->step = step;
438 result->length = length;
439
440 return (PyObject *)result;
441}
442#endif
443
444/* Built-in xrange (Python2) or xrange (Python3) with one argument. */
445PyObject *BUILTIN_XRANGE1(PyThreadState *tstate, PyObject *high) {
446#if PYTHON_VERSION < 0x300
447 if (unlikely(PyFloat_Check(high))) {
448 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "integer argument expected, got float");
449
450 return NULL;
451 }
452
453 long int_high = PyInt_AsLong(high);
454
455 if (unlikely(int_high == -1 && HAS_ERROR_OCCURRED(tstate))) {
456 return NULL;
457 }
458
459 return MAKE_XRANGE(tstate, 0, int_high, 1);
460#else
461 PyObject *stop = Nuitka_Number_IndexAsLong(high);
462
463 if (unlikely(stop == NULL)) {
464 return NULL;
465 }
466
467 PyObject *length = getLengthOfRange(tstate, const_int_0, stop, const_int_pos_1);
468 if (unlikely(length == NULL)) {
469 Py_DECREF(stop);
470
471 return NULL;
472 }
473
474 struct _rangeobject3 *result = (struct _rangeobject3 *)PyObject_New(struct _rangeobject3, &PyRange_Type);
475 assert(result != NULL);
476
477 result->start = const_int_0;
478 Py_INCREF(const_int_0);
479 result->stop = stop;
480 result->step = const_int_pos_1;
481 Py_INCREF(const_int_pos_1);
482
483 result->length = length;
484
485 return (PyObject *)result;
486#endif
487}
488
489/* Built-in xrange (Python2) or xrange (Python3) with two arguments. */
490PyObject *BUILTIN_XRANGE2(PyThreadState *tstate, PyObject *low, PyObject *high) {
491#if PYTHON_VERSION < 0x300
492 if (unlikely(PyFloat_Check(low))) {
493 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "integer argument expected, got float");
494
495 return NULL;
496 }
497
498 long int_low = PyInt_AsLong(low);
499
500 if (unlikely(int_low == -1 && HAS_ERROR_OCCURRED(tstate))) {
501 return NULL;
502 }
503
504 if (unlikely(PyFloat_Check(high))) {
505 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "integer argument expected, got float");
506
507 return NULL;
508 }
509
510 long int_high = PyInt_AsLong(high);
511
512 if (unlikely(int_high == -1 && HAS_ERROR_OCCURRED(tstate))) {
513 return NULL;
514 }
515
516 return MAKE_XRANGE(tstate, int_low, int_high, 1);
517#else
518 return MAKE_XRANGE(tstate, low, high, const_int_pos_1);
519#endif
520}
521
522/* Built-in xrange (Python2) or xrange (Python3) with three arguments. */
523PyObject *BUILTIN_XRANGE3(PyThreadState *tstate, PyObject *low, PyObject *high, PyObject *step) {
524#if PYTHON_VERSION < 0x300
525 if (unlikely(PyFloat_Check(low))) {
526 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "integer argument expected, got float");
527
528 return NULL;
529 }
530
531 long int_low = PyInt_AsLong(low);
532
533 if (unlikely(int_low == -1 && HAS_ERROR_OCCURRED(tstate))) {
534 return NULL;
535 }
536
537 if (unlikely(PyFloat_Check(high))) {
538 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "integer argument expected, got float");
539
540 return NULL;
541 }
542
543 long int_high = PyInt_AsLong(high);
544
545 if (unlikely(int_high == -1 && HAS_ERROR_OCCURRED(tstate))) {
546 return NULL;
547 }
548
549 if (unlikely(PyFloat_Check(step))) {
550 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "integer argument expected, got float");
551
552 return NULL;
553 }
554
555 long int_step = PyInt_AsLong(step);
556
557 if (unlikely(int_step == -1 && HAS_ERROR_OCCURRED(tstate))) {
558 return NULL;
559 }
560
561 if (unlikely(int_step == 0)) {
562 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_ValueError, "range() arg 3 must not be zero");
563
564 return NULL;
565 }
566
567 return MAKE_XRANGE(tstate, int_low, int_high, int_step);
568#else
569 return MAKE_XRANGE(tstate, low, high, step);
570#endif
571}
572
573PyObject *BUILTIN_ALL(PyThreadState *tstate, PyObject *value) {
574 CHECK_OBJECT(value);
575
576 PyObject *it = PyObject_GetIter(value);
577
578 if (unlikely((it == NULL))) {
579 return NULL;
580 }
581
582 iternextfunc iternext = Py_TYPE(it)->tp_iternext;
583 for (;;) {
584 PyObject *item = iternext(it);
585
586 if (unlikely((item == NULL)))
587 break;
588 int cmp = PyObject_IsTrue(item);
589 Py_DECREF(item);
590 if (unlikely(cmp < 0)) {
591 Py_DECREF(it);
592 return NULL;
593 }
594
595 if (cmp == 0) {
596 Py_DECREF(it);
597 Py_INCREF_IMMORTAL(Py_False);
598 return Py_False;
599 }
600 }
601
602 Py_DECREF(it);
603
604 if (unlikely(!CHECK_AND_CLEAR_STOP_ITERATION_OCCURRED(tstate))) {
605 return NULL;
606 }
607
608 Py_INCREF_IMMORTAL(Py_True);
609 return Py_True;
610}
611
612PyObject *BUILTIN_LEN(PyThreadState *tstate, PyObject *value) {
613 CHECK_OBJECT(value);
614
615 Py_ssize_t res = Nuitka_PyObject_Size(value);
616
617 if (unlikely(res < 0 && HAS_ERROR_OCCURRED(tstate))) {
618 return NULL;
619 }
620
621 return PyInt_FromSsize_t(res);
622}
623
624PyObject *BUILTIN_ANY(PyThreadState *tstate, PyObject *value) {
625 CHECK_OBJECT(value);
626
627 PyObject *it = PyObject_GetIter(value);
628
629 if (unlikely((it == NULL))) {
630 return NULL;
631 }
632
633 iternextfunc iternext = Py_TYPE(it)->tp_iternext;
634 for (;;) {
635 PyObject *item = iternext(it);
636
637 if (unlikely((item == NULL)))
638 break;
639 int cmp = PyObject_IsTrue(item);
640 Py_DECREF(item);
641 if (unlikely(cmp < 0)) {
642 Py_DECREF(it);
643 return NULL;
644 }
645 if (cmp > 0) {
646 Py_DECREF(it);
647 Py_INCREF_IMMORTAL(Py_True);
648 return Py_True;
649 }
650 }
651
652 Py_DECREF(it);
653 if (unlikely(!CHECK_AND_CLEAR_STOP_ITERATION_OCCURRED(tstate))) {
654 return NULL;
655 }
656
657 Py_INCREF_IMMORTAL(Py_False);
658 return Py_False;
659}
660
661PyObject *BUILTIN_ABS(PyObject *o) {
662 CHECK_OBJECT(o);
663
664 PyNumberMethods *m = o->ob_type->tp_as_number;
665 if (likely(m && m->nb_absolute)) {
666 return m->nb_absolute(o);
667 }
668
669 return PyErr_Format(PyExc_TypeError, "bad operand type for abs(): '%s'", Py_TYPE(o)->tp_name);
670}
671
672NUITKA_DEFINE_BUILTIN(format);
673
674PyObject *BUILTIN_FORMAT(PyThreadState *tstate, PyObject *value, PyObject *format_spec) {
675 CHECK_OBJECT(value);
676 CHECK_OBJECT(format_spec);
677
678 NUITKA_ASSIGN_BUILTIN(format);
679
680 PyObject *args[2] = {value, format_spec};
681
682 return CALL_FUNCTION_WITH_ARGS2(tstate, NUITKA_ACCESS_BUILTIN(format), args);
683}
684
685// Helper functions for print. Need to play nice with Python softspace
686// behavior. spell-checker: ignore softspace
687
688#if PYTHON_VERSION >= 0x300
689NUITKA_DEFINE_BUILTIN(print);
690#endif
691
692bool PRINT_NEW_LINE_TO(PyObject *file) {
693 PyThreadState *tstate = PyThreadState_GET();
694
695#if PYTHON_VERSION < 0x300
696 if (file == NULL || file == Py_None) {
697 file = GET_STDOUT();
698
699 if (unlikely(file == NULL)) {
700 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_RuntimeError, "lost sys.stdout");
701 return false;
702 }
703 }
704
705 // need to hold a reference to the file or else __getattr__ may release
706 // "file" in the mean time.
707 Py_INCREF(file);
708
709 if (unlikely(PyFile_WriteString("\n", file) == -1)) {
710 Py_DECREF(file);
711 return false;
712 }
713
714 PyFile_SoftSpace(file, 0);
715 CHECK_OBJECT(file);
716
717 Py_DECREF(file);
718 return true;
719#else
720 NUITKA_ASSIGN_BUILTIN(print);
721
722 struct Nuitka_ExceptionPreservationItem saved_exception_state;
723
724 FETCH_ERROR_OCCURRED_STATE_UNTRACED(tstate, &saved_exception_state);
725
726 PyObject *result;
727
728 if (likely(file == NULL)) {
729 result = CALL_FUNCTION_NO_ARGS(tstate, NUITKA_ACCESS_BUILTIN(print));
730 } else {
731 PyObject *kw_pairs[2] = {const_str_plain_file, GET_STDOUT()};
732 PyObject *kw_args = MAKE_DICT(kw_pairs, 1);
733
734 // TODO: This should use something that does not build a dictionary at all, and not
735 // uses a tuple.
736 result = CALL_FUNCTION_WITH_KW_ARGS(tstate, NUITKA_ACCESS_BUILTIN(print), kw_args);
737
738 Py_DECREF(kw_args);
739 }
740
741 Py_XDECREF(result);
742
743 RESTORE_ERROR_OCCURRED_STATE_UNTRACED(tstate, &saved_exception_state);
744
745 return result != NULL;
746#endif
747}
748
749bool PRINT_ITEM_TO(PyObject *file, PyObject *object) {
750 PyThreadState *tstate = PyThreadState_GET();
751
752// The print built-in function cannot replace "softspace" behavior of CPython
753// print statement, so this code is really necessary.
754#if PYTHON_VERSION < 0x300
755 if (file == NULL || file == Py_None) {
756 file = GET_STDOUT();
757
758 if (unlikely(file == NULL)) {
759 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_RuntimeError, "lost sys.stdout");
760 return false;
761 }
762 }
763
764 CHECK_OBJECT(file);
765 CHECK_OBJECT(object);
766
767 // need to hold a reference to the file or else "__getattr__" code may
768 // release "file" in the mean time.
769 Py_INCREF(file);
770
771 // Check for soft space indicator
772 if (PyFile_SoftSpace(file, 0)) {
773 if (unlikely(PyFile_WriteString(" ", file) == -1)) {
774 Py_DECREF(file);
775 return false;
776 }
777 }
778
779 if (unlikely(PyFile_WriteObject(object, file, Py_PRINT_RAW) == -1)) {
780 Py_DECREF(file);
781 return false;
782 }
783
784 if (PyString_Check(object)) {
785 char *buffer;
786 Py_ssize_t length;
787
788#ifndef __NUITKA_NO_ASSERT__
789 int status =
790#endif
791 PyString_AsStringAndSize(object, &buffer, &length);
792 assert(status != -1);
793
794 if (length == 0 || !isspace(Py_CHARMASK(buffer[length - 1])) || buffer[length - 1] == ' ') {
795 PyFile_SoftSpace(file, 1);
796 }
797 } else if (PyUnicode_Check(object)) {
798 Py_UNICODE *buffer = PyUnicode_AS_UNICODE(object);
799 Py_ssize_t length = PyUnicode_GET_SIZE(object);
800
801 if (length == 0 || !Py_UNICODE_ISSPACE(buffer[length - 1]) || buffer[length - 1] == ' ') {
802 PyFile_SoftSpace(file, 1);
803 }
804 } else {
805 PyFile_SoftSpace(file, 1);
806 }
807
808 CHECK_OBJECT(file);
809 Py_DECREF(file);
810
811 return true;
812#else
813 NUITKA_ASSIGN_BUILTIN(print);
814
815 struct Nuitka_ExceptionPreservationItem saved_exception_state;
816
817 FETCH_ERROR_OCCURRED_STATE_UNTRACED(tstate, &saved_exception_state);
818
819 // TODO: Have a helper that creates a dictionary for PyObject **
820 PyObject *print_kw = MAKE_DICT_EMPTY(tstate);
821 DICT_SET_ITEM(print_kw, const_str_plain_end, const_str_empty);
822
823 if (file == NULL) {
824 DICT_SET_ITEM(print_kw, const_str_plain_file, GET_STDOUT());
825 } else {
826 DICT_SET_ITEM(print_kw, const_str_plain_file, file);
827 }
828
829 PyObject *print_args = MAKE_TUPLE1(tstate, object);
830
831 PyObject *result = CALL_FUNCTION(tstate, NUITKA_ACCESS_BUILTIN(print), print_args, print_kw);
832
833 Py_DECREF(print_args);
834 Py_DECREF(print_kw);
835
836 Py_XDECREF(result);
837
838 RESTORE_ERROR_OCCURRED_STATE_UNTRACED(tstate, &saved_exception_state);
839
840 return result != NULL;
841#endif
842}
843
844void PRINT_REFCOUNT(PyObject *object) {
845 if (object) {
846#if PYTHON_VERSION >= 0x3c0
847 if (_Py_IsImmortal(object)) {
848 PRINT_STRING(" refcnt IMMORTAL");
849 return;
850 }
851#endif
852 char buffer[1024];
853 snprintf(buffer, sizeof(buffer) - 1, " refcnt %" PY_FORMAT_SIZE_T "d ", Py_REFCNT(object));
854
855 PRINT_STRING(buffer);
856 } else {
857 PRINT_STRING(" <null>");
858 }
859}
860
861bool PRINT_STRING(char const *str) {
862 if (str) {
863 PyObject *tmp = PyUnicode_FromString(str);
864 bool res = PRINT_ITEM(tmp);
865 Py_DECREF(tmp);
866 return res;
867 } else {
868 return PRINT_STRING("<nullstr>");
869 }
870}
871
872bool PRINT_STRING_W(wchar_t const *str) {
873 if (str) {
874 PyObject *tmp = NuitkaUnicode_FromWideChar(str, -1);
875 bool res = PRINT_ITEM(tmp);
876 Py_DECREF(tmp);
877 return res;
878 } else {
879 return PRINT_STRING("<nullstr>");
880 }
881}
882
883bool PRINT_FORMAT(char const *fmt, ...) {
884 va_list args;
885 va_start(args, fmt);
886
887 // Only used for debug purposes, lets be unsafe here.
888 char buffer[4096];
889
890 vsprintf(buffer, fmt, args);
891 va_end(args);
892
893 return PRINT_STRING(buffer);
894}
895
896bool PRINT_REPR(PyObject *object) {
897 PyThreadState *tstate = PyThreadState_GET();
898
899 struct Nuitka_ExceptionPreservationItem saved_exception_state;
900
901 FETCH_ERROR_OCCURRED_STATE_UNTRACED(tstate, &saved_exception_state);
902
903 bool res;
904
905 if (object != NULL) {
906 CHECK_OBJECT(object);
907
908 // Cannot have error set for this function, it asserts against that
909 // in debug builds.
910 PyObject *repr = PyObject_Repr(object);
911
912 res = PRINT_ITEM(repr);
913 Py_DECREF(repr);
914 } else {
915 res = PRINT_NULL();
916 }
917
918 RESTORE_ERROR_OCCURRED_STATE_UNTRACED(tstate, &saved_exception_state);
919
920 return res;
921}
922
923bool PRINT_NULL(void) { return PRINT_STRING("<NULL>"); }
924
925bool PRINT_TYPE(PyObject *object) { return PRINT_ITEM((PyObject *)Py_TYPE(object)); }
926
927void _PRINT_EXCEPTION3(PyObject *exception_type, PyObject *exception_value, PyTracebackObject *exception_tb) {
928 PRINT_REPR(exception_type);
929 if (exception_type != NULL) {
930 PRINT_REFCOUNT(exception_type);
931 }
932 PRINT_STRING("|");
933 PRINT_REPR(exception_value);
934 if (exception_value != NULL) {
935 PRINT_REFCOUNT(exception_value);
936 }
937#if PYTHON_VERSION >= 0x300
938 if (exception_value != NULL && PyExceptionInstance_Check(exception_value)) {
939 PRINT_STRING(" <- context ");
940 PyObject *context = Nuitka_Exception_GetContext(exception_value);
941 PRINT_REPR(context);
942 }
943#endif
944 PRINT_STRING("|");
945 PRINT_REPR((PyObject *)exception_tb);
946 if (exception_tb != NULL) {
947 PRINT_REFCOUNT((PyObject *)exception_tb);
948 }
949
950 PRINT_NEW_LINE();
951}
952
953#if PYTHON_VERSION >= 0x3b0
954void _PRINT_EXCEPTION1(PyObject *exception_value) {
955 PyObject *exception_type = exception_value ? PyExceptionInstance_Class(exception_value) : NULL;
956 PyTracebackObject *exception_tb = (exception_value && PyExceptionInstance_Check(exception_value))
957 ? GET_EXCEPTION_TRACEBACK(exception_value)
958 : NULL;
959
960 _PRINT_EXCEPTION3(exception_type, exception_value, exception_tb);
961}
962#endif
963
964void PRINT_CURRENT_EXCEPTION(void) {
965 PyThreadState *tstate = PyThreadState_GET();
966
967 PRINT_STRING("current_exc=");
968#if PYTHON_VERSION < 0x3c0
969 PRINT_EXCEPTION(tstate->curexc_type, tstate->curexc_value, (PyTracebackObject *)tstate->curexc_traceback);
970#else
971 _PRINT_EXCEPTION1(tstate->current_exception);
972#endif
973}
974
975void PRINT_PUBLISHED_EXCEPTION(void) {
976 PyThreadState *tstate = PyThreadState_GET();
977
978 PRINT_STRING("thread_exc=");
979#if PYTHON_VERSION < 0x3b0
980 PRINT_EXCEPTION(EXC_TYPE(tstate), EXC_VALUE(tstate), EXC_TRACEBACK(tstate));
981#else
982 PyObject *exc_value = EXC_VALUE(tstate);
983#if PYTHON_VERSION < 0x3c0
984 PyTracebackObject *exc_tb = (exc_value != NULL && exc_value != Py_None) ? GET_EXCEPTION_TRACEBACK(exc_value) : NULL;
985#endif
986 PRINT_EXCEPTION(EXC_TYPE(tstate), exc_value, exc_tb);
987#endif
988}
989
990// TODO: Could be ported, the "printf" stuff would need to be split. On Python3
991// the normal C print output gets lost.
992#if PYTHON_VERSION < 0x300
993void PRINT_TRACEBACK(PyTracebackObject *traceback) {
994 PRINT_STRING("Dumping traceback:\n");
995
996 if (traceback == NULL)
997 PRINT_STRING("<NULL traceback?!>\n");
998
999 while (traceback != NULL) {
1000 printf(" line %d (frame object chain):\n", traceback->tb_lineno);
1001
1002 PyFrameObject *frame = traceback->tb_frame;
1003
1004 while (frame != NULL) {
1005 printf(" Frame at %s\n", PyString_AsString(PyObject_Str((PyObject *)Nuitka_Frame_GetCodeObject(frame))));
1006
1007 frame = frame->f_back;
1008 }
1009
1010 assert(traceback->tb_next != traceback);
1011 traceback = traceback->tb_next;
1012 }
1013
1014 PRINT_STRING("End of Dump.\n");
1015}
1016#endif
1017
1018PyObject *GET_STDOUT(void) {
1019 PyObject *result = Nuitka_SysGetObject("stdout");
1020
1021 if (unlikely(result == NULL)) {
1022 PyThreadState *tstate = PyThreadState_GET();
1023
1024 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_RuntimeError, "lost sys.stdout");
1025 return NULL;
1026 }
1027
1028 return result;
1029}
1030
1031PyObject *GET_STDERR(void) {
1032 PyObject *result = Nuitka_SysGetObject("stderr");
1033
1034 if (unlikely(result == NULL)) {
1035 PyThreadState *tstate = PyThreadState_GET();
1036
1037 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_RuntimeError, "lost sys.stderr");
1038 return NULL;
1039 }
1040
1041 return result;
1042}
1043
1044void FLUSH_STDOUT(void) {
1045 PyObject *stdout_handle = GET_STDOUT();
1046
1047 PyObject *method = PyObject_GetAttrString(stdout_handle, "flush");
1048
1049 PyThreadState *tstate = PyThreadState_GET();
1050 PyObject *result = CALL_FUNCTION_NO_ARGS(tstate, method);
1051
1052 Py_XDECREF(result);
1053}
1054
1055void FLUSH_STDERR(void) {
1056 PyObject *stderr_handle = GET_STDERR();
1057
1058 PyObject *method = PyObject_GetAttrString(stderr_handle, "flush");
1059
1060 PyThreadState *tstate = PyThreadState_GET();
1061 PyObject *result = CALL_FUNCTION_NO_ARGS(tstate, method);
1062
1063 Py_XDECREF(result);
1064}
1065
1066bool PRINT_NEW_LINE(void) { return PRINT_NEW_LINE_TO(NULL); }
1067
1068bool PRINT_ITEM(PyObject *object) {
1069 if (object == NULL) {
1070 return PRINT_NULL();
1071 } else {
1072 return PRINT_ITEM_TO(NULL, object);
1073 }
1074}
1075
1076bool PRINT_ITEM_LINE(PyObject *object) { return PRINT_ITEM(object) && PRINT_NEW_LINE(); }
1077
1078#if PYTHON_VERSION < 0x300
1079
1080static void set_slot(PyObject **slot, PyObject *value) {
1081 PyObject *temp = *slot;
1082 Py_XINCREF(value);
1083 *slot = value;
1084 Py_XDECREF(temp);
1085}
1086
1087static void set_attr_slots(PyClassObject *class_object) {
1088 set_slot(&class_object->cl_getattr, FIND_ATTRIBUTE_IN_CLASS(class_object, const_str_plain___getattr__));
1089 set_slot(&class_object->cl_setattr, FIND_ATTRIBUTE_IN_CLASS(class_object, const_str_plain___setattr__));
1090 set_slot(&class_object->cl_delattr, FIND_ATTRIBUTE_IN_CLASS(class_object, const_str_plain___delattr__));
1091}
1092
1093static bool set_dict(PyClassObject *class_object, PyObject *value) {
1094 if (value == NULL || !PyDict_Check(value)) {
1095 PyThreadState *tstate = PyThreadState_GET();
1096 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "__dict__ must be a dictionary object");
1097 return false;
1098 } else {
1099 set_slot(&class_object->cl_dict, value);
1100 set_attr_slots(class_object);
1101
1102 return true;
1103 }
1104}
1105
1106static bool set_bases(PyClassObject *class_object, PyObject *value) {
1107 if (value == NULL || !PyTuple_Check(value)) {
1108
1109 PyThreadState *tstate = PyThreadState_GET();
1110
1111 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "__bases__ must be a tuple object");
1112 return false;
1113 } else {
1114 Py_ssize_t n = PyTuple_GET_SIZE(value);
1115
1116 for (Py_ssize_t i = 0; i < n; i++) {
1117 PyObject *base = PyTuple_GET_ITEM(value, i);
1118
1119 if (unlikely(!PyClass_Check(base))) {
1120 PyThreadState *tstate = PyThreadState_GET();
1121
1122 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "__bases__ items must be classes");
1123 return false;
1124 }
1125
1126 if (unlikely(PyClass_IsSubclass(base, (PyObject *)class_object))) {
1127 PyThreadState *tstate = PyThreadState_GET();
1128
1129 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError,
1130 "a __bases__ item causes an inheritance cycle");
1131 return false;
1132 }
1133 }
1134
1135 set_slot(&class_object->cl_bases, value);
1136 set_attr_slots(class_object);
1137
1138 return true;
1139 }
1140}
1141
1142static bool set_name(PyClassObject *class_object, PyObject *value) {
1143 if (value == NULL || !PyDict_Check(value)) {
1144 PyThreadState *tstate = PyThreadState_GET();
1145
1146 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "__name__ must be a string object");
1147 return false;
1148 }
1149
1150 if (strlen(PyString_AS_STRING(value)) != (size_t)PyString_GET_SIZE(value)) {
1151 PyThreadState *tstate = PyThreadState_GET();
1152
1153 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "__name__ must not contain null bytes");
1154 return false;
1155 }
1156
1157 set_slot(&class_object->cl_name, value);
1158 return true;
1159}
1160
1161static int nuitka_class_setattr(PyClassObject *class_object, PyObject *attr_name, PyObject *value) {
1162 char const *sattr_name = PyString_AsString(attr_name);
1163
1164 if (sattr_name[0] == '_' && sattr_name[1] == '_') {
1165 Py_ssize_t n = PyString_Size(attr_name);
1166
1167 if (sattr_name[n - 2] == '_' && sattr_name[n - 1] == '_') {
1168 if (strcmp(sattr_name, "__dict__") == 0) {
1169 if (set_dict(class_object, value) == false) {
1170 return -1;
1171 } else {
1172 return 0;
1173 }
1174 } else if (strcmp(sattr_name, "__bases__") == 0) {
1175 if (set_bases(class_object, value) == false) {
1176 return -1;
1177 } else {
1178 return 0;
1179 }
1180 } else if (strcmp(sattr_name, "__name__") == 0) {
1181 if (set_name(class_object, value) == false) {
1182 return -1;
1183 } else {
1184 return 0;
1185 }
1186 } else if (strcmp(sattr_name, "__getattr__") == 0) {
1187 set_slot(&class_object->cl_getattr, value);
1188 } else if (strcmp(sattr_name, "__setattr__") == 0) {
1189 set_slot(&class_object->cl_setattr, value);
1190 } else if (strcmp(sattr_name, "__delattr__") == 0) {
1191 set_slot(&class_object->cl_delattr, value);
1192 }
1193 }
1194 }
1195
1196 if (value == NULL) {
1197 int status = DICT_REMOVE_ITEM(class_object->cl_dict, attr_name);
1198
1199 if (status < 0) {
1200 PyErr_Format(PyExc_AttributeError, "class %s has no attribute '%s'",
1201 PyString_AS_STRING(class_object->cl_name), sattr_name);
1202 }
1203
1204 return status;
1205 } else {
1206 return DICT_SET_ITEM(class_object->cl_dict, attr_name, value) ? 0 : -1;
1207 }
1208}
1209
1210static PyObject *nuitka_class_getattr(PyClassObject *class_object, PyObject *attr_name) {
1211 char const *sattr_name = PyString_AsString(attr_name);
1212
1213 if (sattr_name[0] == '_' && sattr_name[1] == '_') {
1214 if (strcmp(sattr_name, "__dict__") == 0) {
1215 Py_INCREF(class_object->cl_dict);
1216 return class_object->cl_dict;
1217 } else if (strcmp(sattr_name, "__bases__") == 0) {
1218 Py_INCREF(class_object->cl_bases);
1219 return class_object->cl_bases;
1220 } else if (strcmp(sattr_name, "__name__") == 0) {
1221 if (class_object->cl_name == NULL) {
1222 Py_INCREF_IMMORTAL(Py_None);
1223 return Py_None;
1224 } else {
1225 Py_INCREF(class_object->cl_name);
1226 return class_object->cl_name;
1227 }
1228 }
1229 }
1230
1231 PyObject *value = FIND_ATTRIBUTE_IN_CLASS(class_object, attr_name);
1232
1233 if (unlikely(value == NULL)) {
1234 PyErr_Format(PyExc_AttributeError, "class %s has no attribute '%s'", PyString_AS_STRING(class_object->cl_name),
1235 sattr_name);
1236 return NULL;
1237 }
1238
1239 PyTypeObject *type = Py_TYPE(value);
1240
1241 descrgetfunc tp_descr_get = NuitkaType_HasFeatureClass(type) ? type->tp_descr_get : NULL;
1242
1243 if (tp_descr_get == NULL) {
1244 Py_INCREF(value);
1245 return value;
1246 } else {
1247 return tp_descr_get(value, (PyObject *)NULL, (PyObject *)class_object);
1248 }
1249}
1250
1251#endif
1252
1253void enhancePythonTypes(void) {
1254#if PYTHON_VERSION < 0x300
1255 // Our own variant won't call PyEval_GetRestricted, saving quite some cycles
1256 // not doing that.
1257 PyClass_Type.tp_setattro = (setattrofunc)nuitka_class_setattr;
1258 PyClass_Type.tp_getattro = (getattrofunc)nuitka_class_getattr;
1259#endif
1260}
1261
1262#ifdef __FreeBSD__
1263#include <floatingpoint.h>
1264#endif
1265
1266#define ITERATOR_GENERIC 0
1267#define ITERATOR_COMPILED_GENERATOR 1
1268#define ITERATOR_TUPLE 2
1269#define ITERATOR_LIST 3
1270
1272 int iterator_mode;
1273
1274 union {
1275 // ITERATOR_GENERIC
1276 PyObject *iter;
1277
1278 // ITERATOR_COMPILED_GENERATOR
1279 struct Nuitka_GeneratorObject *generator;
1280
1281 // ITERATOR_TUPLE
1282 struct {
1283 PyTupleObject *tuple;
1284 Py_ssize_t tuple_index;
1285 } tuple_data;
1286
1287 // ITERATOR_LIST
1288 struct {
1289 PyListObject *list;
1290 Py_ssize_t list_index;
1291 } list_data;
1292 } iterator_data;
1293};
1294
1295static bool MAKE_QUICK_ITERATOR(PyThreadState *tstate, PyObject *sequence, struct Nuitka_QuickIterator *qiter) {
1296 if (Nuitka_Generator_Check(sequence)) {
1297 qiter->iterator_mode = ITERATOR_COMPILED_GENERATOR;
1298 qiter->iterator_data.generator = (struct Nuitka_GeneratorObject *)sequence;
1299 } else if (PyTuple_CheckExact(sequence)) {
1300 qiter->iterator_mode = ITERATOR_TUPLE;
1301 qiter->iterator_data.tuple_data.tuple = (PyTupleObject *)sequence;
1302 qiter->iterator_data.tuple_data.tuple_index = 0;
1303 } else if (PyList_CheckExact(sequence)) {
1304 qiter->iterator_mode = ITERATOR_LIST;
1305 qiter->iterator_data.list_data.list = (PyListObject *)sequence;
1306 qiter->iterator_data.list_data.list_index = 0;
1307 } else {
1308 qiter->iterator_mode = ITERATOR_GENERIC;
1309
1310 qiter->iterator_data.iter = MAKE_ITERATOR(tstate, sequence);
1311 if (unlikely(qiter->iterator_data.iter == NULL)) {
1312 return false;
1313 }
1314 }
1315
1316 return true;
1317}
1318
1319static PyObject *QUICK_ITERATOR_NEXT(PyThreadState *tstate, struct Nuitka_QuickIterator *qiter, bool *finished) {
1320 PyObject *result;
1321
1322 switch (qiter->iterator_mode) {
1323 case ITERATOR_GENERIC:
1324 result = ITERATOR_NEXT_ITERATOR(qiter->iterator_data.iter);
1325
1326 if (result == NULL) {
1327 Py_DECREF(qiter->iterator_data.iter);
1328
1329 if (unlikely(!CHECK_AND_CLEAR_STOP_ITERATION_OCCURRED(tstate))) {
1330 *finished = false;
1331 return NULL;
1332 }
1333
1334 *finished = true;
1335 return NULL;
1336 }
1337
1338 *finished = false;
1339 return result;
1340 case ITERATOR_COMPILED_GENERATOR:
1341 result = Nuitka_Generator_qiter(tstate, qiter->iterator_data.generator, finished);
1342
1343 return result;
1344 case ITERATOR_TUPLE:
1345 if (qiter->iterator_data.tuple_data.tuple_index < PyTuple_GET_SIZE(qiter->iterator_data.tuple_data.tuple)) {
1346 result =
1347 PyTuple_GET_ITEM(qiter->iterator_data.tuple_data.tuple, qiter->iterator_data.tuple_data.tuple_index);
1348 qiter->iterator_data.tuple_data.tuple_index += 1;
1349
1350 *finished = false;
1351
1352 Py_INCREF(result);
1353 return result;
1354 } else {
1355 *finished = true;
1356 return NULL;
1357 }
1358 case ITERATOR_LIST:
1359 if (qiter->iterator_data.list_data.list_index < PyList_GET_SIZE(qiter->iterator_data.list_data.list)) {
1360 result = PyList_GET_ITEM(qiter->iterator_data.list_data.list, qiter->iterator_data.list_data.list_index);
1361 qiter->iterator_data.list_data.list_index += 1;
1362
1363 *finished = false;
1364
1365 Py_INCREF(result);
1366 return result;
1367 } else {
1368 *finished = true;
1369 return NULL;
1370 }
1371 }
1372
1373 assert(false);
1374 return NULL;
1375}
1376
1377PyObject *BUILTIN_SUM1(PyThreadState *tstate, PyObject *sequence) {
1378 struct Nuitka_QuickIterator qiter;
1379
1380 if (unlikely(MAKE_QUICK_ITERATOR(tstate, sequence, &qiter) == false)) {
1381 return NULL;
1382 }
1383
1384 PyObject *result;
1385
1386 long int_result = 0;
1387
1388 PyObject *item;
1389
1390 for (;;) {
1391 bool finished;
1392
1393 item = QUICK_ITERATOR_NEXT(tstate, &qiter, &finished);
1394
1395 if (finished) {
1396 return Nuitka_PyInt_FromLong(int_result);
1397 } else if (item == NULL) {
1398 return NULL;
1399 }
1400
1401 CHECK_OBJECT(item);
1402
1403// For Python2 int objects:
1404#if PYTHON_VERSION < 0x300
1405 if (PyInt_CheckExact(item)) {
1406 long b = PyInt_AS_LONG(item);
1407 long x = int_result + b;
1408
1409 if ((x ^ int_result) >= 0 || (x ^ b) >= 0) {
1410 int_result = x;
1411 Py_DECREF(item);
1412
1413 continue;
1414 }
1415 }
1416#endif
1417
1418// For Python2 long, Python3 int objects
1419#if PYTHON_VERSION >= 0x270
1420 if (PyLong_CheckExact(item)) {
1421 int overflow;
1422 long b = Nuitka_PyLong_AsLongAndOverflow(item, &overflow);
1423
1424 if (overflow) {
1425 break;
1426 }
1427
1428 long x = int_result + b;
1429
1430 if ((x ^ int_result) >= 0 || (x ^ b) >= 0) {
1431 int_result = x;
1432 Py_DECREF(item);
1433
1434 continue;
1435 }
1436 }
1437#endif
1438
1439 if (item == Py_False) {
1440 Py_DECREF_IMMORTAL(item);
1441 continue;
1442 }
1443
1444 if (item == Py_True) {
1445 long b = 1;
1446 long x = int_result + b;
1447
1448 if ((x ^ int_result) >= 0 || (x ^ b) >= 0) {
1449 int_result = x;
1450 Py_DECREF(item);
1451
1452 continue;
1453 }
1454 }
1455
1456 /* Either overflowed or not one of the supported int alike types. */
1457 break;
1458 }
1459
1460 /* Switch over to objects, and redo last step. */
1461 result = Nuitka_PyInt_FromLong(int_result);
1462 CHECK_OBJECT(result);
1463
1464 PyObject *temp = PyNumber_Add(result, item);
1465 Py_DECREF(result);
1466 Py_DECREF(item);
1467 result = temp;
1468
1469 if (unlikely(result == NULL)) {
1470 return NULL;
1471 }
1472
1473#if PYTHON_VERSION >= 0x3c0
1474 if (PyFloat_CheckExact(result)) {
1475 double f_result = PyFloat_AS_DOUBLE(result);
1476 double c = 0.0;
1477 Py_SETREF(result, NULL);
1478
1479 for (;;) {
1480 bool finished;
1481 item = QUICK_ITERATOR_NEXT(tstate, &qiter, &finished);
1482
1483 if (finished) {
1484 if (c && Py_IS_FINITE(c)) {
1485 f_result += c;
1486 }
1487
1488 return MAKE_FLOAT_FROM_DOUBLE(f_result);
1489 } else if (item == NULL) {
1490 return NULL;
1491 }
1492
1493 CHECK_OBJECT(item);
1494
1495 if (PyFloat_CheckExact(item)) {
1496 /* Improved Kahan-Babuska algorithm by Arnold Neumaier. */
1497 double x = PyFloat_AS_DOUBLE(item);
1498 double t = f_result + x;
1499
1500 if (fabs(f_result) >= fabs(x)) {
1501 c += (f_result - t) + x;
1502 } else {
1503 c += (x - t) + f_result;
1504 }
1505
1506 f_result = t;
1507 Py_DECREF(item);
1508
1509 continue;
1510 }
1511
1512 if (PyLong_Check(item)) {
1513 int overflow;
1514 long value = Nuitka_PyLong_AsLongAndOverflow(item, &overflow);
1515
1516 if (!overflow) {
1517#if PYTHON_VERSION >= 0x3e0
1518 double x = (double)value;
1519 double t = f_result + x;
1520
1521 if (fabs(f_result) >= fabs(x)) {
1522 c += (f_result - t) + x;
1523 } else {
1524 c += (x - t) + f_result;
1525 }
1526
1527 f_result = t;
1528#else
1529 f_result += (double)value;
1530#endif
1531 Py_DECREF(item);
1532
1533 continue;
1534 }
1535 }
1536
1537 if (c && Py_IS_FINITE(c)) {
1538 f_result += c;
1539 }
1540
1541 result = MAKE_FLOAT_FROM_DOUBLE(f_result);
1542
1543 if (result == NULL) {
1544 Py_DECREF(item);
1545 return NULL;
1546 }
1547
1548 PyObject *temp2 = PyNumber_Add(result, item);
1549 Py_DECREF(result);
1550 Py_DECREF(item);
1551 result = temp2;
1552
1553 if (unlikely(result == NULL)) {
1554 return NULL;
1555 }
1556
1557 break;
1558 }
1559 }
1560#endif
1561
1562 for (;;) {
1563 CHECK_OBJECT(result);
1564
1565 bool finished;
1566 item = QUICK_ITERATOR_NEXT(tstate, &qiter, &finished);
1567
1568 if (finished) {
1569 break;
1570 } else if (item == NULL) {
1571 Py_DECREF(result);
1572 return NULL;
1573 }
1574
1575 CHECK_OBJECT(item);
1576
1577 PyObject *temp2 = PyNumber_Add(result, item);
1578
1579 Py_DECREF(item);
1580 Py_DECREF(result);
1581
1582 if (unlikely(temp2 == NULL)) {
1583 return NULL;
1584 }
1585
1586 result = temp2;
1587 }
1588
1589 CHECK_OBJECT(result);
1590
1591 return result;
1592}
1593
1594NUITKA_DEFINE_BUILTIN(sum);
1595
1596PyObject *BUILTIN_SUM2(PyThreadState *tstate, PyObject *sequence, PyObject *start) {
1597 NUITKA_ASSIGN_BUILTIN(sum);
1598
1599 CHECK_OBJECT(sequence);
1600 CHECK_OBJECT(start);
1601
1602 PyObject *pos_args = MAKE_TUPLE2(tstate, sequence, start);
1603
1604 PyObject *result = CALL_FUNCTION_WITH_POS_ARGS2(tstate, NUITKA_ACCESS_BUILTIN(sum), pos_args);
1605
1606 Py_DECREF(pos_args);
1607
1608 return result;
1609}
1610
1611PyDictObject *dict_builtin = NULL;
1612PyModuleObject *builtin_module = NULL;
1613
1614static PyTypeObject Nuitka_BuiltinModule_Type = {
1615 PyVarObject_HEAD_INIT(NULL, 0) "compiled_module", // tp_name
1616 sizeof(PyModuleObject), // tp_size
1617};
1618
1619int Nuitka_BuiltinModule_SetAttr(PyModuleObject *module, PyObject *name, PyObject *value) {
1620 CHECK_OBJECT(module);
1621 CHECK_OBJECT(name);
1622
1623 // This is used for "del" as well.
1624 assert(value == NULL || Py_REFCNT(value) > 0);
1625
1626 // only checks the builtins that we can refresh at this time, if we have
1627 // many value to check maybe need create a dict first.
1628 bool found = false;
1629
1630 int res = PyObject_RichCompareBool(name, const_str_plain_open, Py_EQ);
1631
1632 if (unlikely(res == -1)) {
1633 return -1;
1634 }
1635 if (res == 1) {
1636 NUITKA_UPDATE_BUILTIN(open, value);
1637 found = true;
1638 }
1639
1640 if (found == false) {
1641 res = PyObject_RichCompareBool(name, const_str_plain___import__, Py_EQ);
1642
1643 if (unlikely(res == -1)) {
1644 return -1;
1645 }
1646
1647 if (res == 1) {
1648 NUITKA_UPDATE_BUILTIN(__import__, value);
1649 found = true;
1650 }
1651 }
1652
1653#if PYTHON_VERSION >= 0x300
1654 if (found == false) {
1655 res = PyObject_RichCompareBool(name, const_str_plain_print, Py_EQ);
1656
1657 if (unlikely(res == -1)) {
1658 return -1;
1659 }
1660
1661 if (res == 1) {
1662 NUITKA_UPDATE_BUILTIN(print, value);
1663 found = true;
1664 }
1665 }
1666#endif
1667
1668 if (found == false) {
1669 res = PyObject_RichCompareBool(name, const_str_plain_super, Py_EQ);
1670
1671 if (unlikely(res == -1)) {
1672 return -1;
1673 }
1674
1675 if (res == 1) {
1676 NUITKA_UPDATE_BUILTIN(super, value);
1677 found = true;
1678 }
1679 }
1680
1681 return PyObject_GenericSetAttr((PyObject *)module, name, value);
1682}
1683
1684#if defined(__FreeBSD__) || defined(__OpenBSD__)
1685#include <sys/sysctl.h>
1686#endif
1687
1688static PyObject *getPathSeparatorStringObject(void) {
1689 static char const sep[2] = {SEP, 0};
1690
1691 static PyObject *sep_str = NULL;
1692
1693 if (sep_str == NULL) {
1694 sep_str = Nuitka_String_FromString(sep);
1695 }
1696
1697 CHECK_OBJECT(sep_str);
1698
1699 return sep_str;
1700}
1701
1702PyObject *JOIN_PATH2(PyObject *dirname, PyObject *filename) {
1703 CHECK_OBJECT(dirname);
1704 CHECK_OBJECT(filename);
1705
1706 // Avoid string APIs, so str, unicode doesn't matter for input.
1707 PyObject *result = dirname;
1708
1709 if (dirname != const_str_empty) {
1710 result = PyNumber_InPlaceAdd(result, getPathSeparatorStringObject());
1711 CHECK_OBJECT(result);
1712 }
1713
1714 result = PyNumber_InPlaceAdd(result, filename);
1715 CHECK_OBJECT(result);
1716
1717 return result;
1718}
1719
1720#if _NUITKA_EXE_MODE || _NUITKA_DLL_MODE
1721
1722wchar_t const *getBinaryDirectoryWideChars(bool resolve_symlinks) {
1723 static wchar_t binary_directory[MAXPATHLEN + 1];
1724 static bool init_done = false;
1725
1726 if (init_done == false) {
1727 binary_directory[0] = 0;
1728
1729#if defined(_WIN32)
1730 copyStringSafeW(binary_directory, getBinaryFilenameWideChars(resolve_symlinks),
1731 sizeof(binary_directory) / sizeof(wchar_t));
1732
1733 stripFilenameW(binary_directory);
1734 makeShortFilename(binary_directory, sizeof(binary_directory) / sizeof(wchar_t));
1735#else
1736 appendStringSafeW(binary_directory, getBinaryDirectoryHostEncoded(true),
1737 sizeof(binary_directory) / sizeof(wchar_t));
1738#endif
1739
1740 init_done = true;
1741 }
1742 return (wchar_t const *)binary_directory;
1743}
1744
1745#if defined(_WIN32)
1746char const *getBinaryDirectoryHostEncoded(bool resolve_symlinks) {
1747 static char *binary_directory = NULL;
1748 static char *binary_directory_resolved = NULL;
1749
1750 char *binary_directory_target;
1751
1752 if (resolve_symlinks) {
1753 binary_directory_target = binary_directory_resolved;
1754 } else {
1755 binary_directory_target = binary_directory;
1756 }
1757
1758 if (binary_directory_target != NULL) {
1759 return binary_directory_target;
1760 }
1761 wchar_t const *w = getBinaryDirectoryWideChars(resolve_symlinks);
1762
1763 DWORD bufsize = WideCharToMultiByte(CP_ACP, 0, w, -1, NULL, 0, NULL, NULL);
1764 assert(bufsize != 0);
1765
1766 binary_directory_target = (char *)malloc(bufsize + 1);
1767 assert(binary_directory_target);
1768
1769 DWORD res2 = WideCharToMultiByte(CP_ACP, 0, w, -1, binary_directory_target, bufsize, NULL, NULL);
1770 assert(res2 != 0);
1771
1772 if (unlikely(res2 > bufsize)) {
1773 abort();
1774 }
1775
1776 return (char const *)binary_directory_target;
1777}
1778
1779#else
1780
1781char const *getBinaryDirectoryHostEncoded(bool resolve_symlinks) {
1782 const int buffer_size = MAXPATHLEN + 1;
1783
1784 static char binary_directory[MAXPATHLEN + 1] = {0};
1785 static char binary_directory_resolved[MAXPATHLEN + 1] = {0};
1786
1787 char *binary_directory_target;
1788
1789 if (resolve_symlinks) {
1790 binary_directory_target = binary_directory_resolved;
1791 } else {
1792 binary_directory_target = binary_directory;
1793 }
1794
1795 if (*binary_directory_target != 0) {
1796 return binary_directory_target;
1797 }
1798
1799 // Get the filename first.
1800 copyStringSafe(binary_directory_target, getBinaryFilenameHostEncoded(resolve_symlinks), buffer_size);
1801
1802 // We want the directory name, the above gives the full executable name.
1803 copyStringSafe(binary_directory_target, dirname(binary_directory_target), buffer_size);
1804
1805 return binary_directory_target;
1806}
1807
1808#endif
1809
1810#if _NUITKA_EXE_MODE || _NUITKA_ONEFILE_DLL_MODE
1811PyObject *getBinaryFilenameObject(bool resolve_symlinks) {
1812 static PyObject *binary_filename = NULL;
1813 static PyObject *binary_filename_resolved = NULL;
1814
1815 PyObject **binary_object_target;
1816
1817 if (resolve_symlinks) {
1818 binary_object_target = &binary_filename_resolved;
1819 } else {
1820 binary_object_target = &binary_filename;
1821 }
1822
1823 if (*binary_object_target != NULL) {
1824 CHECK_OBJECT(*binary_object_target);
1825
1826 return *binary_object_target;
1827 }
1828
1829// On Python3, this must be a unicode object, it cannot be on Python2,
1830// there e.g. code objects expect Python2 strings.
1831#if PYTHON_VERSION >= 0x300
1832#ifdef _WIN32
1833 wchar_t const *exe_filename = getBinaryFilenameWideChars(resolve_symlinks);
1834 *binary_object_target = NuitkaUnicode_FromWideChar(exe_filename, -1);
1835#else
1836 *binary_object_target = PyUnicode_DecodeFSDefault(getBinaryFilenameHostEncoded(resolve_symlinks));
1837#endif
1838#else
1839 *binary_object_target = PyString_FromString(getBinaryFilenameHostEncoded(resolve_symlinks));
1840#endif
1841
1842 if (unlikely(*binary_object_target == NULL)) {
1843 PyErr_Print();
1844 abort();
1845 }
1846
1847 // Make sure it's usable for caching.
1848 Py_INCREF(*binary_object_target);
1849
1850 return *binary_object_target;
1851}
1852#endif
1853
1854PyObject *getBinaryDirectoryObject(bool resolve_symlinks) {
1855 static PyObject *binary_directory = NULL;
1856 static PyObject *binary_directory_resolved = NULL;
1857
1858 PyObject **binary_object_target;
1859
1860 if (resolve_symlinks) {
1861 binary_object_target = &binary_directory_resolved;
1862 } else {
1863 binary_object_target = &binary_directory;
1864 }
1865
1866 if (*binary_object_target != NULL) {
1867 CHECK_OBJECT(*binary_object_target);
1868
1869 return *binary_object_target;
1870 }
1871
1872// On Python3, this must be a unicode object, it cannot be on Python2,
1873// there e.g. code objects expect Python2 strings.
1874#if PYTHON_VERSION >= 0x300
1875#ifdef _WIN32
1876 wchar_t const *bin_directory = getBinaryDirectoryWideChars(resolve_symlinks);
1877 *binary_object_target = NuitkaUnicode_FromWideChar(bin_directory, -1);
1878#else
1879 *binary_object_target = PyUnicode_DecodeFSDefault(getBinaryDirectoryHostEncoded(resolve_symlinks));
1880#endif
1881#else
1882 *binary_object_target = PyString_FromString(getBinaryDirectoryHostEncoded(resolve_symlinks));
1883#endif
1884
1885 if (unlikely(*binary_object_target == NULL)) {
1886 PyErr_Print();
1887 abort();
1888 }
1889
1890 // Make sure it's usable for caching.
1891 Py_INCREF(*binary_object_target);
1892
1893 return *binary_object_target;
1894}
1895#endif
1896
1897#if _NUITKA_DLL_MODE || _NUITKA_MODULE_MODE
1898static PyObject *getDllDirectoryObject(void) {
1899 static PyObject *dll_directory = NULL;
1900
1901 if (dll_directory == NULL) {
1902 filename_char_t const *dll_directory_filename = getDllDirectory();
1903
1904 dll_directory = Nuitka_String_FromFilename(dll_directory_filename);
1905
1906#if PYTHON_VERSION < 0x300
1907 // Avoid unnecessary unicode values.
1908 PyObject *decoded_dll_directory = PyObject_Str(dll_directory);
1909
1910 if (decoded_dll_directory == NULL) {
1911 PyThreadState *tstate = PyThreadState_GET();
1912 DROP_ERROR_OCCURRED(tstate);
1913 } else {
1914 Py_DECREF(dll_directory);
1915 dll_directory = decoded_dll_directory;
1916 }
1917#endif
1918 }
1919
1920 CHECK_OBJECT(dll_directory);
1921
1922 return dll_directory;
1923}
1924
1925PyObject *getDllFilenameObject(void) {
1926 static PyObject *dll_filename = NULL;
1927
1928 if (dll_filename == NULL) {
1929 filename_char_t const *dll_filename_str = getDllFilename();
1930
1931 dll_filename = Nuitka_String_FromFilename(dll_filename_str);
1932
1933#if PYTHON_VERSION < 0x300
1934 // Avoid unnecessary unicode values.
1935 PyObject *decoded_dll_filename = PyObject_Str(dll_filename);
1936
1937 if (decoded_dll_filename == NULL) {
1938 PyThreadState *tstate = PyThreadState_GET();
1939 DROP_ERROR_OCCURRED(tstate);
1940 } else {
1941 Py_DECREF(dll_filename);
1942 dll_filename = decoded_dll_filename;
1943 }
1944#endif
1945 }
1946
1947 CHECK_OBJECT(dll_filename);
1948
1949 return dll_filename;
1950}
1951#endif
1952
1953PyObject *getPythonProgramDirectoryObject(bool resolve_symlinks) {
1954#if _NUITKA_EXE_MODE
1955 return getBinaryDirectoryObject(resolve_symlinks);
1956#else
1957 return getDllDirectoryObject();
1958#endif
1959}
1960
1961PyObject *getContainingDirectoryObject(bool resolve_symlinks) {
1962#if _NUITKA_ONEFILE_MODE
1963 environment_char_t const *onefile_directory = getEnvironmentVariable("NUITKA_ONEFILE_DIRECTORY");
1964 if (onefile_directory != NULL) {
1965 PyObject *result = Nuitka_String_FromFilename(onefile_directory);
1966 unsetEnvironmentVariable("NUITKA_ONEFILE_DIRECTORY");
1967
1968 return result;
1969 }
1970#endif
1971 return getPythonProgramDirectoryObject(resolve_symlinks);
1972}
1973
1974#if _NUITKA_STANDALONE_MODE
1975// Helper function to create path.
1976PyObject *getStandaloneSysExecutablePath(PyObject *basename) {
1977#if _NUITKA_EXE_MODE
1978 PyObject *dir_name = getBinaryDirectoryObject(false);
1979#else
1980 PyObject *dir_name = getDllDirectoryObject();
1981#endif
1982 PyObject *sys_executable = JOIN_PATH2(dir_name, basename);
1983
1984 return sys_executable;
1985}
1986#endif
1987
1988static void _initDeepCopy(PyThreadState *tstate);
1989
1990void _initBuiltinModule(PyThreadState *tstate) {
1991 NUITKA_PRINT_TRACE("main(): Calling _initBuiltinTypeMethods().");
1992 _initBuiltinTypeMethods();
1993
1994 NUITKA_PRINT_TRACE("main(): Calling _initDeepCopy().");
1995 _initDeepCopy(tstate);
1996
1997#if _NUITKA_MODULE_MODE
1998 if (builtin_module != NULL) {
1999 return;
2000 }
2001#else
2002 assert(builtin_module == NULL);
2003#endif
2004
2005#if PYTHON_VERSION < 0x300
2006 builtin_module = (PyModuleObject *)PyImport_ImportModule("__builtin__");
2007#else
2008 builtin_module = (PyModuleObject *)PyImport_ImportModule("builtins");
2009#endif
2010 assert(builtin_module);
2011 dict_builtin = (PyDictObject *)builtin_module->md_dict;
2012 assert(PyDict_Check(dict_builtin));
2013
2014#if _NUITKA_STANDALONE_MODE
2015 {
2016#if _NUITKA_EXE_MODE
2017 PyObject *nuitka_binary_dir = getBinaryDirectoryObject(true);
2018#else
2019 PyObject *nuitka_binary_dir = getDllDirectoryObject();
2020#endif
2021 NUITKA_MAY_BE_UNUSED int res =
2022 PyDict_SetItemString((PyObject *)dict_builtin, "__nuitka_binary_dir", nuitka_binary_dir);
2023 assert(res == 0);
2024
2025 // For actual DLL mode, we don't have this, but the form used in onefile
2026 // will providing our own executable that knows what to do.
2027#if _NUITKA_EXE_MODE || _NUITKA_ONEFILE_DLL_MODE
2028 PyDict_SetItemString((PyObject *)dict_builtin, "__nuitka_binary_exe", getBinaryFilenameObject(true));
2029 assert(res == 0);
2030#endif
2031 }
2032#endif
2033
2034 // init Nuitka_BuiltinModule_Type, PyType_Ready won't copy all member from
2035 // base type, so we need copy all members from PyModule_Type manual for
2036 // safety. PyType_Ready will change tp_flags, we need define it again. Set
2037 // tp_setattro to Nuitka_BuiltinModule_SetAttr and we can detect value
2038 // change. Set tp_base to PyModule_Type and PyModule_Check will pass.
2039 Nuitka_BuiltinModule_Type.tp_dealloc = PyModule_Type.tp_dealloc;
2040 Nuitka_BuiltinModule_Type.tp_repr = PyModule_Type.tp_repr;
2041 Nuitka_BuiltinModule_Type.tp_setattro = (setattrofunc)Nuitka_BuiltinModule_SetAttr;
2042 Nuitka_BuiltinModule_Type.tp_getattro = PyModule_Type.tp_getattro;
2043 Nuitka_BuiltinModule_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE;
2044 Nuitka_BuiltinModule_Type.tp_doc = PyModule_Type.tp_doc;
2045 Nuitka_BuiltinModule_Type.tp_traverse = PyModule_Type.tp_traverse;
2046 Nuitka_BuiltinModule_Type.tp_members = PyModule_Type.tp_members;
2047 Nuitka_BuiltinModule_Type.tp_base = &PyModule_Type;
2048 Nuitka_BuiltinModule_Type.tp_dictoffset = PyModule_Type.tp_dictoffset;
2049 Nuitka_BuiltinModule_Type.tp_init = PyModule_Type.tp_init;
2050 Nuitka_BuiltinModule_Type.tp_alloc = PyModule_Type.tp_alloc;
2051 Nuitka_BuiltinModule_Type.tp_new = PyModule_Type.tp_new;
2052 Nuitka_BuiltinModule_Type.tp_free = PyModule_Type.tp_free;
2053 NUITKA_MAY_BE_UNUSED int res2 = PyType_Ready(&Nuitka_BuiltinModule_Type);
2054 assert(res2 >= 0);
2055
2056 // Replace type of builtin module to take over.
2057 ((PyObject *)builtin_module)->ob_type = &Nuitka_BuiltinModule_Type;
2058 assert(PyModule_Check(builtin_module) == 1);
2059}
2060
2061#include "HelpersCalling.c"
2062
2063PyObject *MAKE_RELATIVE_PATH(PyObject *relative) {
2064 CHECK_OBJECT(relative);
2065
2066 static PyObject *our_path_object = NULL;
2067
2068 if (our_path_object == NULL) {
2069 our_path_object = getPythonProgramDirectoryObject(true);
2070 }
2071
2072 return JOIN_PATH2(our_path_object, relative);
2073}
2074
2075#if !_NUITKA_MODULE_MODE
2076
2077NUITKA_DEFINE_BUILTIN(type)
2078NUITKA_DEFINE_BUILTIN(len)
2079NUITKA_DEFINE_BUILTIN(repr)
2080NUITKA_DEFINE_BUILTIN(int)
2081NUITKA_DEFINE_BUILTIN(iter)
2082#if PYTHON_VERSION < 0x300
2083NUITKA_DEFINE_BUILTIN(long)
2084#else
2085NUITKA_DEFINE_BUILTIN(range);
2086#endif
2087
2088void _initBuiltinOriginalValues(void) {
2089 NUITKA_ASSIGN_BUILTIN(type);
2090 NUITKA_ASSIGN_BUILTIN(len);
2091 NUITKA_ASSIGN_BUILTIN(range);
2092 NUITKA_ASSIGN_BUILTIN(repr);
2093 NUITKA_ASSIGN_BUILTIN(int);
2094 NUITKA_ASSIGN_BUILTIN(iter);
2095#if PYTHON_VERSION < 0x300
2096 NUITKA_ASSIGN_BUILTIN(long);
2097#endif
2098
2099 CHECK_OBJECT(_python_original_builtin_value_range);
2100}
2101
2102#endif
2103
2104// Used for threading.
2105#if PYTHON_VERSION >= 0x300 && !defined(NUITKA_USE_PYCORE_THREAD_STATE)
2106volatile int _Py_Ticker = _Py_CheckInterval;
2107#endif
2108
2109#if PYTHON_VERSION >= 0x270
2110iternextfunc default_iternext;
2111
2112void _initSlotIterNext(void) {
2113 PyThreadState *tstate = PyThreadState_GET();
2114
2115 PyObject *pos_args = MAKE_TUPLE1(tstate, (PyObject *)&PyBaseObject_Type);
2116
2117 // Note: Not using MAKE_DICT_EMPTY on purpose, this is called early on.
2118 PyObject *kw_args = PyDict_New();
2119 PyDict_SetItem(kw_args, const_str_plain___iter__, Py_True);
2120
2121 PyObject *c =
2122 PyObject_CallFunctionObjArgs((PyObject *)&PyType_Type, const_str_plain___iter__, pos_args, kw_args, NULL);
2123 Py_DECREF(pos_args);
2124 Py_DECREF(kw_args);
2125
2126 PyObject *r = PyObject_CallFunctionObjArgs(c, NULL);
2127 Py_DECREF(c);
2128
2129 CHECK_OBJECT(r);
2130 assert(Py_TYPE(r)->tp_iternext);
2131
2132 default_iternext = Py_TYPE(r)->tp_iternext;
2133
2134 Py_DECREF(r);
2135}
2136#endif
2137
2138#if PYTHON_VERSION >= 0x3a0
2139PyObject *MAKE_UNION_TYPE(PyObject *args) {
2140 assert(PyTuple_CheckExact(args));
2141 assert(PyTuple_GET_SIZE(args) > 1);
2142
2143 CHECK_OBJECT_DEEP(args);
2144
2145 PyObject *result = NULL;
2146
2147 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(args); i++) {
2148 PyObject *value = PyTuple_GET_ITEM(args, i);
2149
2150 if (result == NULL) {
2151 assert(i == 0);
2152 result = value;
2153 } else {
2154 result = PyNumber_InPlaceBitor(result, value);
2155 }
2156 }
2157
2158 return result;
2159}
2160#endif
2161
2162#include "HelpersAttributes.c"
2163#include "HelpersDeepcopy.c"
2164#include "HelpersOperationBinaryAdd.c"
2165#include "HelpersOperationBinaryBitand.c"
2166#include "HelpersOperationBinaryBitor.c"
2167#include "HelpersOperationBinaryBitxor.c"
2168#include "HelpersOperationBinaryDivmod.c"
2169#include "HelpersOperationBinaryFloordiv.c"
2170#include "HelpersOperationBinaryLshift.c"
2171#include "HelpersOperationBinaryMod.c"
2172#include "HelpersOperationBinaryMult.c"
2173#include "HelpersOperationBinaryPow.c"
2174#include "HelpersOperationBinaryRshift.c"
2175#include "HelpersOperationBinarySub.c"
2176#include "HelpersOperationBinaryTruediv.c"
2177#include "HelpersTypes.c"
2178#if PYTHON_VERSION < 0x300
2179#include "HelpersOperationBinaryOlddiv.c"
2180#endif
2181#if PYTHON_VERSION >= 0x350
2182#include "HelpersOperationBinaryMatmult.c"
2183#endif
2184
2185#include "HelpersOperationBinaryDualAdd.c"
2186
2187#include "HelpersOperationInplaceAdd.c"
2188#include "HelpersOperationInplaceBitand.c"
2189#include "HelpersOperationInplaceBitor.c"
2190#include "HelpersOperationInplaceBitxor.c"
2191#include "HelpersOperationInplaceFloordiv.c"
2192#include "HelpersOperationInplaceLshift.c"
2193#include "HelpersOperationInplaceMod.c"
2194#include "HelpersOperationInplaceMult.c"
2195#include "HelpersOperationInplacePow.c"
2196#include "HelpersOperationInplaceRshift.c"
2197#include "HelpersOperationInplaceSub.c"
2198#include "HelpersOperationInplaceTruediv.c"
2199#if PYTHON_VERSION < 0x300
2200#include "HelpersOperationInplaceOlddiv.c"
2201#endif
2202#if PYTHON_VERSION >= 0x350
2203#include "HelpersOperationInplaceMatmult.c"
2204#endif
2205
2206#include "HelpersComparisonEq.c"
2207#include "HelpersComparisonLe.c"
2208#include "HelpersComparisonLt.c"
2209
2210#include "HelpersComparisonGe.c"
2211#include "HelpersComparisonGt.c"
2212#include "HelpersComparisonNe.c"
2213
2214#include "HelpersComparisonDualEq.c"
2215#include "HelpersComparisonDualLe.c"
2216#include "HelpersComparisonDualLt.c"
2217
2218#include "HelpersComparisonDualGe.c"
2219#include "HelpersComparisonDualGt.c"
2220#include "HelpersComparisonDualNe.c"
2221
2222#include "HelpersChecksumTools.c"
2223#include "HelpersConstantsBlob.c"
2224
2225#if _NUITKA_PROFILE
2226#include "HelpersProfiling.c"
2227#endif
2228
2229#if _NUITKA_PGO_PYTHON
2230#include "HelpersPythonPgo.c"
2231#endif
2232
2233#include "MetaPathBasedLoader.c"
2234
2235#ifdef _NUITKA_EXPERIMENTAL_DUMP_C_TRACEBACKS
2236#include "HelpersDumpBacktraces.c"
2237#endif
2238
2239#ifdef _NUITKA_INLINE_COPY_HACL
2240#include "Hacl_Hash_SHA2.c"
2241#endif
2242
2243#include "HelpersJitSources.c"
2244
2245// Part of "Nuitka", an optimizing Python compiler that is compatible and
2246// integrates with CPython, but also works on its own.
2247//
2248// Licensed under the GNU Affero General Public License, Version 3 (the "License");
2249// you may not use this file except in compliance with the License.
2250// You may obtain a copy of the License at
2251//
2252// http://www.gnu.org/licenses/agpl.txt
2253//
2254// Unless required by applicable law or agreed to in writing, software
2255// distributed under the License is distributed on an "AS IS" BASIS,
2256// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2257// See the License for the specific language governing permissions and
2258// limitations under the License.
Definition exceptions.h:712
Definition compiled_generator.h:41
Definition CompiledCodeHelpers.c:1271
Definition helpers.h:10
Definition rangeobjects.h:38