15#include "nuitka/prelude.h"
18#if _NUITKA_EXPERIMENTAL_WRITEABLE_CONSTANTS
21#define CONST_CONSTANT const
24#if defined(_NUITKA_CONSTANTS_FROM_LINKER)
27extern "C" CONST_CONSTANT
unsigned char constant_bin_data[];
29extern CONST_CONSTANT
unsigned char constant_bin_data[0];
32unsigned char const *constant_bin = &constant_bin_data[0];
34#elif defined(_NUITKA_CONSTANTS_FROM_CODE)
36extern "C" CONST_CONSTANT
unsigned char constant_bin_data[];
38extern CONST_CONSTANT
unsigned char constant_bin_data[];
41unsigned char const *constant_bin = &constant_bin_data[0];
44unsigned char const *constant_bin = NULL;
47#if defined(_NUITKA_CONSTANTS_FROM_INCBIN)
48extern unsigned const char *getConstantsBlobData(
void);
51#if PYTHON_VERSION < 0x300
52static PyObject *int_cache = NULL;
55static PyObject *long_cache = NULL;
57static PyObject *float_cache = NULL;
59#if PYTHON_VERSION >= 0x300
60static PyObject *bytes_cache = NULL;
63#if PYTHON_VERSION < 0x300
64static PyObject *unicode_cache = NULL;
67static PyObject *tuple_cache = NULL;
69static PyObject *list_cache = NULL;
71static PyObject *dict_cache = NULL;
73static PyObject *set_cache = NULL;
75static PyObject *frozenset_cache = NULL;
79static Py_hash_t Nuitka_FastHashBytes(
const void *value, Py_ssize_t size) {
80 if (unlikely(size == 0)) {
84 unsigned char *w = (
unsigned char *)value;
88 x = (1000003 * x) ^ *w++;
101static Py_hash_t our_list_hash(PyListObject *list) {
102 return Nuitka_FastHashBytes(&list->ob_item[0], Py_SIZE(list) *
sizeof(PyObject *));
105static PyObject *our_list_tp_richcompare(PyListObject *list1, PyListObject *list2,
int op) {
110 if (list1 == list2) {
112 }
else if (Py_SIZE(list1) != Py_SIZE(list2)) {
114 }
else if (memcmp(&list1->ob_item[0], &list2->ob_item[0], Py_SIZE(list1) *
sizeof(PyObject *)) == 0) {
120 Py_INCREF_IMMORTAL(result);
124static Py_hash_t our_tuple_hash(PyTupleObject *tuple) {
125 return Nuitka_FastHashBytes(&tuple->ob_item[0], Py_SIZE(tuple) *
sizeof(PyObject *));
128static PyObject *our_tuple_tp_richcompare(PyTupleObject *tuple1, PyTupleObject *tuple2,
int op) {
133 if (tuple1 == tuple2) {
135 }
else if (Py_SIZE(tuple1) != Py_SIZE(tuple2)) {
137 }
else if (memcmp(&tuple1->ob_item[0], &tuple2->ob_item[0], Py_SIZE(tuple1) *
sizeof(PyObject *)) == 0) {
143 Py_INCREF_IMMORTAL(result);
147static Py_hash_t our_set_hash(PyObject *set) {
148 Py_hash_t result = 0;
152#if PYTHON_VERSION < 0x300
155 while (_PySet_Next(set, &pos, &key)) {
157 result ^= Nuitka_FastHashBytes(&key,
sizeof(PyObject *));
162 while (_PySet_NextEntry(set, &pos, &key, &unused)) {
164 result ^= Nuitka_FastHashBytes(&key,
sizeof(PyObject *));
171static PyObject *our_set_tp_richcompare(PyObject *set1, PyObject *set2,
int op) {
176 Py_ssize_t pos1 = 0, pos2 = 0;
177 PyObject *key1, *key2;
179 if (Py_SIZE(set1) != Py_SIZE(set2)) {
184#if PYTHON_VERSION < 0x300
187 while (_PySet_Next(set1, &pos1, &key1)) {
189 NUITKA_MAY_BE_UNUSED
int res = _PySet_Next(set2, &pos2, &key2);
199 Py_hash_t unused1, unused2;
203 while (_PySet_NextEntry(set1, &pos1, &key1, &unused1)) {
205 NUITKA_MAY_BE_UNUSED
int res = _PySet_NextEntry(set2, &pos2, &key2, &unused2);
217 Py_INCREF_IMMORTAL(result);
221static PyObject *our_float_tp_richcompare(PyFloatObject *a, PyFloatObject *b,
int op) {
227 if (memcmp(&a->ob_fval, &b->ob_fval,
sizeof(b->ob_fval)) == 0) {
233 Py_INCREF_IMMORTAL(result);
237static Py_hash_t our_dict_hash(PyObject *dict) {
238 Py_hash_t result = 0;
241 PyObject *key, *value;
243 while (Nuitka_DictNext(dict, &pos, &key, &value)) {
245 result ^= Nuitka_FastHashBytes(&key,
sizeof(PyObject *));
247 result ^= Nuitka_FastHashBytes(&value,
sizeof(PyObject *));
253static PyObject *our_dict_tp_richcompare(PyObject *a, PyObject *b,
int op) {
256 if (Py_SIZE(a) != Py_SIZE(b)) {
261 Py_ssize_t pos1 = 0, pos2 = 0;
262 PyObject *key1, *value1;
263 PyObject *key2, *value2;
268 while (Nuitka_DictNext(a, &pos1, &key1, &value1)) {
270 NUITKA_MAY_BE_UNUSED
int res = Nuitka_DictNext(b, &pos2, &key2, &value2);
274 if (key1 != key2 || value1 != value2) {
281 Py_INCREF_IMMORTAL(result);
286#if PYTHON_VERSION < 0x3b0
287#if PYTHON_VERSION >= 0x390
288PyObject **Nuitka_Long_SmallValues;
289#elif PYTHON_VERSION >= 0x300
290PyObject *Nuitka_Long_SmallValues[NUITKA_STATIC_SMALLINT_VALUE_MAX - NUITKA_STATIC_SMALLINT_VALUE_MIN + 1];
294static void initCaches(
void) {
295 static bool init_done =
false;
296 if (init_done ==
true) {
300#if PYTHON_VERSION < 0x300
301 int_cache = PyDict_New();
304 long_cache = PyDict_New();
306 float_cache = PyDict_New();
308#if PYTHON_VERSION >= 0x300
309 bytes_cache = PyDict_New();
312#if PYTHON_VERSION < 0x300
313 unicode_cache = PyDict_New();
316 tuple_cache = PyDict_New();
318 list_cache = PyDict_New();
320 dict_cache = PyDict_New();
322 set_cache = PyDict_New();
324 frozenset_cache = PyDict_New();
326#if PYTHON_VERSION < 0x3b0
327#if PYTHON_VERSION >= 0x390
329 Nuitka_Long_SmallValues = (PyObject **)_PyInterpreterState_GET()->small_ints;
330#elif PYTHON_VERSION >= 0x300
331 for (
long i = NUITKA_STATIC_SMALLINT_VALUE_MIN; i < NUITKA_STATIC_SMALLINT_VALUE_MAX; i++) {
334 PyObject *value = PyLong_FromLong(i);
335 Nuitka_Long_SmallValues[NUITKA_TO_SMALL_VALUE_OFFSET(i)] = value;
343static void insertToDictCache(PyObject *dict, PyObject **value) {
344 PyObject *item = PyDict_GetItem(dict, *value);
349 PyDict_SetItem(dict, *value, *value);
353static void insertToDictCacheForcedHash(PyObject *dict, PyObject **value, hashfunc tp_hash,
354 richcmpfunc tp_richcompare) {
355 hashfunc old_hash = Py_TYPE(*value)->tp_hash;
356 richcmpfunc old_richcmpfunc = Py_TYPE(*value)->tp_richcompare;
360 if (tp_hash != NULL) {
361 Py_TYPE(*value)->tp_hash = tp_hash;
363 Py_TYPE(*value)->tp_richcompare = tp_richcompare;
365 insertToDictCache(dict, value);
367 Py_TYPE(*value)->tp_hash = old_hash;
368 Py_TYPE(*value)->tp_richcompare = old_richcmpfunc;
371static uint16_t unpackValueUint16(
unsigned char const **data) {
374 memcpy(&value, *data,
sizeof(value));
376 assert(
sizeof(value) == 2);
378 *data +=
sizeof(value);
383static uint32_t unpackValueUint32(
unsigned char const **data) {
386 memcpy(&value, *data,
sizeof(value));
388 assert(
sizeof(value) == 4);
390 *data +=
sizeof(value);
395static double unpackValueFloat(
unsigned char const **data) {
398 memcpy(&size, *data,
sizeof(size));
399 *data +=
sizeof(size);
404static unsigned char const *_unpackValueCString(
unsigned char const *data) {
405 while (*(data++) != 0) {
412static uint64_t _unpackVariableLength(
unsigned char const **data) {
417 unsigned char value = **data;
420 result += (value & 127) * factor;
432static PyObject *_unpackAnonValue(
unsigned char anon_index) {
433 switch (anon_index) {
435 return (PyObject *)Py_TYPE(Py_None);
437 return (PyObject *)&PyEllipsis_Type;
439 return (PyObject *)Py_TYPE(Py_NotImplemented);
441 return (PyObject *)&PyFunction_Type;
443 return (PyObject *)&PyGen_Type;
445 return (PyObject *)&PyCFunction_Type;
447 return (PyObject *)&PyCode_Type;
449 return (PyObject *)&PyModule_Type;
451#if PYTHON_VERSION < 0x300
453 return (PyObject *)&PyFile_Type;
455 return (PyObject *)&PyClass_Type;
457 return (PyObject *)&PyInstance_Type;
459 return (PyObject *)&PyMethod_Type;
463 PRINT_FORMAT(
"Missing anon value for %d\n", (
int)anon_index);
464 NUITKA_CANNOT_GET_HERE(
"Corrupt constants blob");
468PyObject *_unpackSpecialValue(
unsigned char special_index) {
469 switch (special_index) {
471 return PyObject_GetAttrString((PyObject *)builtin_module,
"Ellipsis");
473 return PyObject_GetAttrString((PyObject *)builtin_module,
"NotImplemented");
475 return Py_SysVersionInfo;
477 PRINT_FORMAT(
"Missing special value for %d\n", (
int)special_index);
478 NUITKA_CANNOT_GET_HERE(
"Corrupt constants blob");
482static PyObject *_Nuitka_Unicode_ImmortalFromStringAndSize(PyThreadState *tstate,
const char *data, Py_ssize_t size,
484#if PYTHON_VERSION < 0x300
485 PyObject *u = PyUnicode_FromStringAndSize((
const char *)data, size);
488 PyObject *u = PyUnicode_DecodeUTF8((
const char *)data, size,
"surrogatepass");
491#if PYTHON_VERSION >= 0x3d0
492 _PyUnicode_InternImmortal(tstate->interp, &u);
493#elif PYTHON_VERSION >= 0x3c0
495 PyUnicode_InternInPlace(&u);
498#if PYTHON_VERSION >= 0x3c7
499 _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL_STATIC;
502 _PyUnicode_STATE(u).statically_allocated = 1;
504 if (Py_Version >= 0x30c0700) {
505 _PyUnicode_STATE(u).statically_allocated = 1;
510#elif PYTHON_VERSION >= 0x300
512 PyUnicode_InternInPlace(&u);
515 insertToDictCache(unicode_cache, &u);
519#if PYTHON_VERSION >= 0x3c0 && !defined(__NUITKA_NO_ASSERT__)
521 Py_SET_REFCNT_IMMORTAL(u);
523 _PyUnicode_CheckConsistency(u, 1);
529static unsigned char const *_unpackBlobConstants(PyThreadState *tstate, PyObject **output,
unsigned char const *data,
532static unsigned char const *_unpackBlobConstant(PyThreadState *tstate, PyObject **output,
unsigned char const *data) {
538 char c = *((
char const *)data++);
539#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
540 unsigned char const *data_old = data;
541 printf(
"Type %c:\n", c);
546 *output = *(output - 1);
552 int size = (int)_unpackVariableLength(&data);
554 PyObject *t = PyTuple_New(size);
557 data = _unpackBlobConstants(tstate, &PyTuple_GET_ITEM(t, 0), data, size);
560 insertToDictCacheForcedHash(tuple_cache, &t, (hashfunc)our_tuple_hash, (richcmpfunc)our_tuple_tp_richcompare);
568 int size = (int)_unpackVariableLength(&data);
570 PyObject *l = PyList_New(size);
573 data = _unpackBlobConstants(tstate, &PyList_GET_ITEM(l, 0), data, size);
576 insertToDictCacheForcedHash(list_cache, &l, (hashfunc)our_list_hash, (richcmpfunc)our_list_tp_richcompare);
584 int size = (int)_unpackVariableLength(&data);
586 PyObject *d = _PyDict_NewPresized(size);
589 NUITKA_DYNAMIC_ARRAY_DECL(keys, PyObject *, size);
590 NUITKA_DYNAMIC_ARRAY_DECL(values, PyObject *, size);
592 data = _unpackBlobConstants(tstate, &keys[0], data, size);
593 data = _unpackBlobConstants(tstate, &values[0], data, size);
595 for (
int i = 0; i < size; i++) {
596 PyDict_SetItem(d, keys[i], values[i]);
600 insertToDictCacheForcedHash(dict_cache, &d, (hashfunc)our_dict_hash, (richcmpfunc)our_dict_tp_richcompare);
609 int size = (int)_unpackVariableLength(&data);
619 static PyObject *empty_frozenset = NULL;
621 if (empty_frozenset == NULL) {
622 empty_frozenset = CALL_FUNCTION_WITH_SINGLE_ARG(tstate, (PyObject *)&PyFrozenSet_Type,
623 Nuitka_Bytes_FromStringAndSize(
"", 0));
628 s = PyFrozenSet_New(NULL);
633 NUITKA_DYNAMIC_ARRAY_DECL(values, PyObject *, size);
635 data = _unpackBlobConstants(tstate, &values[0], data, size);
637 for (
int i = 0; i < size; i++) {
638 PySet_Add(s, values[i]);
644 insertToDictCacheForcedHash(set_cache, &s, (hashfunc)our_set_hash, (richcmpfunc)our_set_tp_richcompare);
646 insertToDictCacheForcedHash(frozenset_cache, &s, (hashfunc)our_set_hash,
647 (richcmpfunc)our_set_tp_richcompare);
655#if PYTHON_VERSION < 0x300
658 long value = (long)_unpackVariableLength(&data);
663 PyObject *i = PyInt_FromLong(value);
665 insertToDictCache(int_cache, &i);
676 uint64_t value = _unpackVariableLength(&data);
678 PyObject *l = Nuitka_LongFromCLong((c ==
'l') ? ((
long)value) : (-(long)value));
682#if PYTHON_VERSION >= 0x300
683 if (value < NUITKA_STATIC_SMALLINT_VALUE_MIN || value >= NUITKA_STATIC_SMALLINT_VALUE_MAX)
686 insertToDictCache(long_cache, &l);
696 PyObject *result = Nuitka_PyLong_FromLong(0);
698 int size = (int)_unpackVariableLength(&data);
700 PyObject *shift = Nuitka_PyLong_FromLong(31);
702 for (
int i = 0; i < size; i++) {
703 result = PyNumber_InPlaceLshift(result, shift);
705 uint64_t value = _unpackVariableLength(&data);
706 PyObject *part = Nuitka_LongFromCLong((
long)value);
707 assert(part != NULL);
708 result = PyNumber_InPlaceAdd(result, part);
715 Nuitka_LongSetSignNegative(result);
718 insertToDictCache(long_cache, &result);
726 double value = unpackValueFloat(&data);
728 PyObject *f = PyFloat_FromDouble(value);
731 insertToDictCacheForcedHash(float_cache, &f, NULL, (richcmpfunc)our_float_tp_richcompare);
739 double real = unpackValueFloat(&data);
740 double imag = unpackValueFloat(&data);
742 *output = PyComplex_FromDoubles(real, imag);
751 data = _unpackBlobConstants(tstate, &parts[0], data, 2);
753 *output = BUILTIN_COMPLEX2(tstate, parts[0], parts[1]);
758#if PYTHON_VERSION < 0x300
762 size_t size = strlen((
const char *)data);
764 PyObject *s = PyString_FromStringAndSize((
const char *)data, size);
770 PyString_InternInPlace(&s);
781 size_t size = strlen((
const char *)data);
783 PyObject *b = Nuitka_Bytes_FromStringAndSize((
const char *)data, size);
790 insertToDictCache(bytes_cache, &b);
802#if PYTHON_VERSION < 0x300
803 PyObject *s = PyString_FromStringAndSize((
const char *)data, 1);
807 PyObject *b = Nuitka_Bytes_FromStringAndSize((
const char *)data, 1);
818 PyObject *u = _Nuitka_Unicode_ImmortalFromStringAndSize(tstate, (
const char *)data, 1,
true);
828 int size = (int)_unpackVariableLength(&data);
831 PyObject *b = Nuitka_Bytes_FromStringAndSize((
const char *)data, size);
836#if PYTHON_VERSION >= 0x300
837 insertToDictCache(bytes_cache, &b);
847 int size = (int)_unpackVariableLength(&data);
849 PyObject *b = PyByteArray_FromStringAndSize((
const char *)data, size);
857#if PYTHON_VERSION >= 0x300
861 size_t size = strlen((
const char *)data);
864 PyObject *u = _Nuitka_Unicode_ImmortalFromStringAndSize(tstate, (
const char *)data, size, c ==
'a');
873 int size = (int)_unpackVariableLength(&data);
876 PyObject *u = _Nuitka_Unicode_ImmortalFromStringAndSize(tstate, (
const char *)data, size,
false);
891 *output = _Nuitka_Unicode_ImmortalFromStringAndSize(tstate, (
const char *)data, 0,
true);
911 data = _unpackBlobConstants(tstate, &items[0], data, 3);
913 PyObject *s = MAKE_SLICE_OBJECT3(tstate, items[0], items[1], items[2]);
923 data = _unpackBlobConstants(tstate, &items[0], data, 3);
924#if PYTHON_VERSION < 0x300
925 assert(PyInt_CheckExact(items[0]));
926 assert(PyInt_CheckExact(items[1]));
927 assert(PyInt_CheckExact(items[2]));
929 long start = PyInt_AS_LONG(items[0]);
930 long stop = PyInt_AS_LONG(items[1]);
931 long step = PyInt_AS_LONG(items[2]);
933 PyObject *s = MAKE_XRANGE(tstate, start, stop, step);
935 PyObject *s = BUILTIN_XRANGE3(tstate, items[0], items[1], items[2]);
944 unsigned char anon_index = *data++;
946 *output = _unpackAnonValue(anon_index);
953 unsigned char special_index = *data++;
955 *output = _unpackSpecialValue(special_index);
963 char const *builtin_name = (
char const *)data;
964 data = _unpackValueCString(data);
966 *output = PyObject_GetAttrString((PyObject *)builtin_module, builtin_name);
974 char const *builtin_exception_name = (
char const *)data;
975 data = _unpackValueCString(data);
977 *output = PyObject_GetAttrString((PyObject *)builtin_module, builtin_exception_name);
983 unsigned char v = *data++;
989 static PyObject *_const_float_0_0 = NULL;
991 if (_const_float_0_0 == NULL) {
992 _const_float_0_0 = PyFloat_FromDouble(0.0);
994 z = _const_float_0_0;
999 static PyObject *_const_float_minus_0_0 = NULL;
1001 if (_const_float_minus_0_0 == NULL) {
1002 _const_float_minus_0_0 = PyFloat_FromDouble(0.0);
1005 PyFloat_SET_DOUBLE(_const_float_minus_0_0, copysign(PyFloat_AS_DOUBLE(_const_float_minus_0_0), -1.0));
1007 z = _const_float_minus_0_0;
1013 static PyObject *_const_float_plus_nan = NULL;
1015 if (_const_float_plus_nan == NULL) {
1016 _const_float_plus_nan = PyFloat_FromDouble(Py_NAN);
1019 PyFloat_SET_DOUBLE(_const_float_plus_nan, copysign(PyFloat_AS_DOUBLE(_const_float_plus_nan), 1.0));
1021 z = _const_float_plus_nan;
1026 static PyObject *_const_float_minus_nan = NULL;
1028 if (_const_float_minus_nan == NULL) {
1029 _const_float_minus_nan = PyFloat_FromDouble(Py_NAN);
1032 PyFloat_SET_DOUBLE(_const_float_minus_nan, copysign(PyFloat_AS_DOUBLE(_const_float_minus_nan), -1.0));
1034 z = _const_float_minus_nan;
1039 static PyObject *_const_float_plus_inf = NULL;
1041 if (_const_float_plus_inf == NULL) {
1042 _const_float_plus_inf = PyFloat_FromDouble(Py_HUGE_VAL);
1045 PyFloat_SET_DOUBLE(_const_float_plus_inf, copysign(PyFloat_AS_DOUBLE(_const_float_plus_inf), 1.0));
1047 z = _const_float_plus_inf;
1052 static PyObject *_const_float_minus_inf = NULL;
1054 if (_const_float_minus_inf == NULL) {
1055 _const_float_minus_inf = PyFloat_FromDouble(Py_HUGE_VAL);
1058 PyFloat_SET_DOUBLE(_const_float_minus_inf, copysign(PyFloat_AS_DOUBLE(_const_float_minus_inf), -1.0));
1060 z = _const_float_minus_inf;
1065 PRINT_FORMAT(
"Missing decoding for %d\n", (
int)c);
1066 NUITKA_CANNOT_GET_HERE(
"Corrupt constants blob");
1071 insertToDictCacheForcedHash(float_cache, &z, NULL, (richcmpfunc)our_float_tp_richcompare);
1080 uint64_t size = _unpackVariableLength(&data);
1082 *output = (PyObject *)data;
1089#if PYTHON_VERSION >= 0x390
1093 data = _unpackBlobConstants(tstate, &items[0], data, 2);
1095 PyObject *g = Py_GenericAlias(items[0], items[1]);
1104#if PYTHON_VERSION >= 0x3a0
1108 data = _unpackBlobConstants(tstate, &args, data, 1);
1110 PyObject *union_type = MAKE_UNION_TYPE(args);
1113 *output = union_type;
1127 uint64_t flags = _unpackVariableLength(&data);
1131 uint64_t flag_base = 1;
1138 PyObject *function_name;
1139 data = _unpackBlobConstant(tstate, &function_name, data);
1143 int line_number = (int)_unpackVariableLength(&data) + 1;
1148 PyObject *arg_names;
1149 data = _unpackBlobConstant(tstate, &arg_names, data);
1154 int arg_count = (int)_unpackVariableLength(&data);
1158#if PYTHON_VERSION >= 0x3b0
1159 PyObject *function_qualname;
1161 if (flags & flag_base) {
1162 data = _unpackBlobConstant(tstate, &function_qualname, data);
1164 function_qualname = function_name;
1170 PyObject *free_vars = NULL;
1172 if (flags & flag_base) {
1173 data = _unpackBlobConstant(tstate, &free_vars, data);
1177#if PYTHON_VERSION >= 0x300
1178 int kw_only_count = 0;
1179 if (flags & flag_base) {
1180 kw_only_count = (int)_unpackVariableLength(&data) + 1;
1183 assert(kw_only_count >= 0);
1186#if PYTHON_VERSION >= 0x380
1187 int pos_only_count = 0;
1188 if (flags & flag_base) {
1189 pos_only_count = (int)_unpackVariableLength(&data) + 1;
1192 assert(pos_only_count >= 0);
1197#if PYTHON_VERSION >= 0x360
1198 if ((flags & (flag_base * 3)) == (flag_base * 3)) {
1199 co_flags += CO_ASYNC_GENERATOR;
1202#if PYTHON_VERSION >= 0x350
1203 if ((flags & (flag_base * 2)) == (flag_base * 2)) {
1204 co_flags += CO_COROUTINE;
1207 if (flags & flag_base) {
1208 co_flags += CO_GENERATOR;
1213 if (flags & flag_base) {
1214 co_flags += CO_OPTIMIZED;
1218 if (flags & flag_base) {
1219 co_flags += CO_NEWLOCALS;
1223 if (flags & flag_base) {
1224 co_flags += CO_VARARGS;
1228 if (flags & flag_base) {
1229 co_flags += CO_VARKEYWORDS;
1233#if PYTHON_VERSION < 0x300
1234 if (flags & flag_base) {
1235 co_flags += CO_FUTURE_DIVISION;
1240 if (flags & flag_base) {
1241 co_flags += CO_FUTURE_UNICODE_LITERALS;
1245#if PYTHON_VERSION < 0x300
1246 if (flags & flag_base) {
1247 co_flags += CO_FUTURE_PRINT_FUNCTION;
1252#if PYTHON_VERSION < 0x300
1253 if (flags & flag_base) {
1254 co_flags += CO_FUTURE_ABSOLUTE_IMPORT;
1259#if PYTHON_VERSION >= 0x350 && PYTHON_VERSION < 0x370
1260 if (flags & flag_base) {
1261 co_flags += CO_FUTURE_GENERATOR_STOP;
1266#if PYTHON_VERSION >= 0x370
1267 if (flags & flag_base) {
1268 co_flags += CO_FUTURE_ANNOTATIONS;
1273#if PYTHON_VERSION >= 0x300
1274 if (flags & flag_base) {
1275 co_flags += CO_FUTURE_BARRY_AS_BDFL;
1281 *output = (PyObject *)MAKE_CODE_OBJECT(Py_None, line_number, co_flags, function_name, function_qualname,
1282 arg_names, free_vars, arg_count, kw_only_count, pos_only_count);
1284 CHECK_OBJECT(*output);
1290 PRINT_STRING(
"Missing blob values\n");
1291 NUITKA_CANNOT_GET_HERE(
"Corrupt constants blob");
1294 PRINT_FORMAT(
"Missing decoding for %d\n", (
int)c);
1295 NUITKA_CANNOT_GET_HERE(
"Corrupt constants blob");
1298#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1299 printf(
"Size for %c was %d\n", c, data - data_old);
1305 if (is_object ==
true) {
1306 CHECK_OBJECT(*output);
1308#if PYTHON_VERSION < 0x3c0
1312 Py_SET_REFCNT_IMMORTAL(*output);
1319static unsigned char const *_unpackBlobConstants(PyThreadState *tstate, PyObject **output,
unsigned char const *data,
1321 for (
int _i = 0; _i < count; _i++) {
1322 data = _unpackBlobConstant(tstate, output, data);
1330static void unpackBlobConstants(PyThreadState *tstate, PyObject **output,
unsigned char const *data) {
1331 int count = (int)unpackValueUint16(&data);
1333#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1334 printf(
"unpackBlobConstants count %d\n", count);
1336 _unpackBlobConstants(tstate, output, data, count);
1339#if _NUITKA_CONSTANTS_FROM_MACOS_SECTION
1341#include <mach-o/getsect.h>
1342#include <mach-o/ldsyms.h>
1344#if !_NUITKA_EXE_MODE
1345static int findMacOSDllImageId(
void) {
1347 int res = dladdr((
void *)findMacOSDllImageId, &where);
1350 char const *dll_filename = where.dli_fname;
1352 unsigned long image_count = _dyld_image_count();
1354 for (
int i = 0; i < image_count; i++) {
1356 struct mach_header const *header = _dyld_get_image_header(i);
1357 if (header == NULL) {
1361 if (strcmp(dll_filename, _dyld_get_image_name(i)) == 0) {
1371#define mach_header_arch mach_header_64
1373#define mach_header_arch mach_header
1376unsigned char *findMacOSBinarySection(
void) {
1378 const struct mach_header_arch *header = &_mh_execute_header;
1380 int image_id = findMacOSDllImageId();
1381 assert(image_id != -1);
1383 const struct mach_header_arch *header = (
const struct mach_header_arch *)_dyld_get_image_header(image_id);
1387 return getsectiondata(header,
"constants",
"constants", &size);
1392void loadConstantsBlob(PyThreadState *tstate, PyObject **output,
char const *name) {
1393 static bool init_done =
false;
1395 if (init_done ==
false) {
1396 NUITKA_PRINT_TIMING(
"loadConstantsBlob(): One time init.");
1398#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1399 printf(
"loadConstantsBlob '%s' one time init\n", name);
1402#if defined(_NUITKA_CONSTANTS_FROM_INCBIN)
1403 constant_bin = getConstantsBlobData();
1404#elif defined(_NUITKA_CONSTANTS_FROM_RESOURCE)
1407 HMODULE handle = NULL;
1409 HMODULE handle = getDllModuleHandle();
1412 constant_bin = (
const unsigned char *)LockResource(
1413 LoadResource(handle, FindResource(handle, MAKEINTRESOURCE(3), RT_RCDATA)));
1415 assert(constant_bin);
1416#elif _NUITKA_CONSTANTS_FROM_MACOS_SECTION
1417 constant_bin = findMacOSBinarySection();
1419 assert(constant_bin);
1421 NUITKA_PRINT_TIMING(
"loadConstantsBlob(): Found blob, decoding now.");
1422 DECODE(constant_bin);
1424 NUITKA_PRINT_TIMING(
"loadConstantsBlob(): CRC32 that blob for correctness.");
1425 uint32_t hash = unpackValueUint32(&constant_bin);
1426 uint32_t size = unpackValueUint32(&constant_bin);
1428#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1429 printf(
"loadConstantsBlob '%u' hash value\n", hash);
1430 printf(
"loadConstantsBlob '%u' size value\n", size);
1432 if (calcCRC32(constant_bin, size) != hash) {
1433 puts(
"Error, corrupted constants object");
1437#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1438 printf(
"Checked CRC32 to match hash %u size %u\n", hash, size);
1441 NUITKA_PRINT_TIMING(
"loadConstantsBlob(): One time init complete.");
1446#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1447 printf(
"Loading blob named '%s'\n", name);
1450 if (strcmp(name,
".bytecode") != 0) {
1454 unsigned char const *w = constant_bin;
1457 int match = strcmp(name, (
char const *)w);
1458 w += strlen((
char const *)w) + 1;
1460#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1461 printf(
"offset of blob size %d\n", w - constant_bin);
1464 uint32_t size = unpackValueUint32(&w);
1467#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1468 printf(
"Loading blob named '%s' with size %d\n", name, size);
1477 unpackBlobConstants(tstate, output, w);