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