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);
521#if PYTHON_VERSION >= 0x3c0 && PYTHON_VERSION < 0x3e0 && !defined(__NUITKA_NO_ASSERT__)
523 Py_SET_REFCNT_IMMORTAL(u);
525 _PyUnicode_CheckConsistency(u, 1);
531static unsigned char const *_unpackBlobConstants(PyThreadState *tstate, PyObject **output,
unsigned char const *data,
534static unsigned char const *_unpackBlobConstant(PyThreadState *tstate, PyObject **output,
unsigned char const *data) {
540 char c = *((
char const *)data++);
541#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
542 unsigned char const *data_old = data;
543 printf(
"Type %c:\n", c);
548 *output = *(output - 1);
554 int size = (int)_unpackVariableLength(&data);
556 PyObject *t = PyTuple_New(size);
559 data = _unpackBlobConstants(tstate, &PyTuple_GET_ITEM(t, 0), data, size);
562 insertToDictCacheForcedHash(tuple_cache, &t, (hashfunc)our_tuple_hash, (richcmpfunc)our_tuple_tp_richcompare);
570 int size = (int)_unpackVariableLength(&data);
572 PyObject *l = PyList_New(size);
575 data = _unpackBlobConstants(tstate, &PyList_GET_ITEM(l, 0), data, size);
578 insertToDictCacheForcedHash(list_cache, &l, (hashfunc)our_list_hash, (richcmpfunc)our_list_tp_richcompare);
586 int size = (int)_unpackVariableLength(&data);
588 PyObject *d = _PyDict_NewPresized(size);
591 NUITKA_DYNAMIC_ARRAY_DECL(keys, PyObject *, size);
592 NUITKA_DYNAMIC_ARRAY_DECL(values, PyObject *, size);
594 data = _unpackBlobConstants(tstate, &keys[0], data, size);
595 data = _unpackBlobConstants(tstate, &values[0], data, size);
597 for (
int i = 0; i < size; i++) {
598 PyDict_SetItem(d, keys[i], values[i]);
602 insertToDictCacheForcedHash(dict_cache, &d, (hashfunc)our_dict_hash, (richcmpfunc)our_dict_tp_richcompare);
611 int size = (int)_unpackVariableLength(&data);
621 static PyObject *empty_frozenset = NULL;
623 if (empty_frozenset == NULL) {
624 empty_frozenset = CALL_FUNCTION_WITH_SINGLE_ARG(tstate, (PyObject *)&PyFrozenSet_Type,
625 Nuitka_Bytes_FromStringAndSize(
"", 0));
630 s = PyFrozenSet_New(NULL);
635 NUITKA_DYNAMIC_ARRAY_DECL(values, PyObject *, size);
637 data = _unpackBlobConstants(tstate, &values[0], data, size);
639 for (
int i = 0; i < size; i++) {
640 PySet_Add(s, values[i]);
646 insertToDictCacheForcedHash(set_cache, &s, (hashfunc)our_set_hash, (richcmpfunc)our_set_tp_richcompare);
648 insertToDictCacheForcedHash(frozenset_cache, &s, (hashfunc)our_set_hash,
649 (richcmpfunc)our_set_tp_richcompare);
657#if PYTHON_VERSION < 0x300
660 long value = (long)_unpackVariableLength(&data);
665 PyObject *i = PyInt_FromLong(value);
667 insertToDictCache(int_cache, &i);
678 uint64_t value = _unpackVariableLength(&data);
680 PyObject *l = Nuitka_LongFromCLong((c ==
'l') ? ((
long)value) : (-(long)value));
684#if PYTHON_VERSION >= 0x300
685 if (value < NUITKA_STATIC_SMALLINT_VALUE_MIN || value >= NUITKA_STATIC_SMALLINT_VALUE_MAX)
688 insertToDictCache(long_cache, &l);
698 PyObject *result = Nuitka_PyLong_FromLong(0);
700 int size = (int)_unpackVariableLength(&data);
702 PyObject *shift = Nuitka_PyLong_FromLong(31);
704 for (
int i = 0; i < size; i++) {
705 result = PyNumber_InPlaceLshift(result, shift);
707 uint64_t value = _unpackVariableLength(&data);
708 PyObject *part = Nuitka_LongFromCLong((
long)value);
709 assert(part != NULL);
710 result = PyNumber_InPlaceAdd(result, part);
717 Nuitka_LongSetSignNegative(result);
720 insertToDictCache(long_cache, &result);
728 double value = unpackValueFloat(&data);
730 PyObject *f = PyFloat_FromDouble(value);
733 insertToDictCacheForcedHash(float_cache, &f, NULL, (richcmpfunc)our_float_tp_richcompare);
741 double real = unpackValueFloat(&data);
742 double imag = unpackValueFloat(&data);
744 *output = PyComplex_FromDoubles(real, imag);
753 data = _unpackBlobConstants(tstate, &parts[0], data, 2);
755 *output = BUILTIN_COMPLEX2(tstate, parts[0], parts[1]);
760#if PYTHON_VERSION < 0x300
764 size_t size = strlen((
const char *)data);
766 PyObject *s = PyString_FromStringAndSize((
const char *)data, size);
772 PyString_InternInPlace(&s);
783 size_t size = strlen((
const char *)data);
785 PyObject *b = Nuitka_Bytes_FromStringAndSize((
const char *)data, size);
792 insertToDictCache(bytes_cache, &b);
804#if PYTHON_VERSION < 0x300
805 PyObject *s = PyString_FromStringAndSize((
const char *)data, 1);
809 PyObject *b = Nuitka_Bytes_FromStringAndSize((
const char *)data, 1);
820 PyObject *u = _Nuitka_Unicode_ImmortalFromStringAndSize(tstate, (
const char *)data, 1,
true);
830 int size = (int)_unpackVariableLength(&data);
833 PyObject *b = Nuitka_Bytes_FromStringAndSize((
const char *)data, size);
838#if PYTHON_VERSION >= 0x300
839 insertToDictCache(bytes_cache, &b);
849 int size = (int)_unpackVariableLength(&data);
851 PyObject *b = PyByteArray_FromStringAndSize((
const char *)data, size);
859#if PYTHON_VERSION >= 0x300
863 size_t size = strlen((
const char *)data);
866 PyObject *u = _Nuitka_Unicode_ImmortalFromStringAndSize(tstate, (
const char *)data, size, c ==
'a');
875 int size = (int)_unpackVariableLength(&data);
878 PyObject *u = _Nuitka_Unicode_ImmortalFromStringAndSize(tstate, (
const char *)data, size,
false);
893 *output = _Nuitka_Unicode_ImmortalFromStringAndSize(tstate, (
const char *)data, 0,
true);
913 data = _unpackBlobConstants(tstate, &items[0], data, 3);
915 PyObject *s = MAKE_SLICE_OBJECT3(tstate, items[0], items[1], items[2]);
925 data = _unpackBlobConstants(tstate, &items[0], data, 3);
926#if PYTHON_VERSION < 0x300
927 assert(PyInt_CheckExact(items[0]));
928 assert(PyInt_CheckExact(items[1]));
929 assert(PyInt_CheckExact(items[2]));
931 long start = PyInt_AS_LONG(items[0]);
932 long stop = PyInt_AS_LONG(items[1]);
933 long step = PyInt_AS_LONG(items[2]);
935 PyObject *s = MAKE_XRANGE(tstate, start, stop, step);
937 PyObject *s = BUILTIN_XRANGE3(tstate, items[0], items[1], items[2]);
946 unsigned char anon_index = *data++;
948 *output = _unpackAnonValue(anon_index);
955 unsigned char special_index = *data++;
957 *output = _unpackSpecialValue(special_index);
965 char const *builtin_name = (
char const *)data;
966 data = _unpackValueCString(data);
968 *output = PyObject_GetAttrString((PyObject *)builtin_module, builtin_name);
976 char const *builtin_exception_name = (
char const *)data;
977 data = _unpackValueCString(data);
979 *output = PyObject_GetAttrString((PyObject *)builtin_module, builtin_exception_name);
985 unsigned char v = *data++;
991 static PyObject *_const_float_0_0 = NULL;
993 if (_const_float_0_0 == NULL) {
994 _const_float_0_0 = PyFloat_FromDouble(0.0);
996 z = _const_float_0_0;
1001 static PyObject *_const_float_minus_0_0 = NULL;
1003 if (_const_float_minus_0_0 == NULL) {
1004 _const_float_minus_0_0 = PyFloat_FromDouble(0.0);
1007 PyFloat_SET_DOUBLE(_const_float_minus_0_0, copysign(PyFloat_AS_DOUBLE(_const_float_minus_0_0), -1.0));
1009 z = _const_float_minus_0_0;
1015 static PyObject *_const_float_plus_nan = NULL;
1017 if (_const_float_plus_nan == NULL) {
1018 _const_float_plus_nan = PyFloat_FromDouble(Py_NAN);
1021 PyFloat_SET_DOUBLE(_const_float_plus_nan, copysign(PyFloat_AS_DOUBLE(_const_float_plus_nan), 1.0));
1023 z = _const_float_plus_nan;
1028 static PyObject *_const_float_minus_nan = NULL;
1030 if (_const_float_minus_nan == NULL) {
1031 _const_float_minus_nan = PyFloat_FromDouble(Py_NAN);
1034 PyFloat_SET_DOUBLE(_const_float_minus_nan, copysign(PyFloat_AS_DOUBLE(_const_float_minus_nan), -1.0));
1036 z = _const_float_minus_nan;
1041 static PyObject *_const_float_plus_inf = NULL;
1043 if (_const_float_plus_inf == NULL) {
1044 _const_float_plus_inf = PyFloat_FromDouble(Py_HUGE_VAL);
1047 PyFloat_SET_DOUBLE(_const_float_plus_inf, copysign(PyFloat_AS_DOUBLE(_const_float_plus_inf), 1.0));
1049 z = _const_float_plus_inf;
1054 static PyObject *_const_float_minus_inf = NULL;
1056 if (_const_float_minus_inf == NULL) {
1057 _const_float_minus_inf = PyFloat_FromDouble(Py_HUGE_VAL);
1060 PyFloat_SET_DOUBLE(_const_float_minus_inf, copysign(PyFloat_AS_DOUBLE(_const_float_minus_inf), -1.0));
1062 z = _const_float_minus_inf;
1067 PRINT_FORMAT(
"Missing decoding for %d\n", (
int)c);
1068 NUITKA_CANNOT_GET_HERE(
"Corrupt constants blob");
1073 insertToDictCacheForcedHash(float_cache, &z, NULL, (richcmpfunc)our_float_tp_richcompare);
1082 uint64_t size = _unpackVariableLength(&data);
1084 *output = (PyObject *)data;
1091#if PYTHON_VERSION >= 0x390
1095 data = _unpackBlobConstants(tstate, &items[0], data, 2);
1097 PyObject *g = Py_GenericAlias(items[0], items[1]);
1106#if PYTHON_VERSION >= 0x3a0
1110 data = _unpackBlobConstants(tstate, &args, data, 1);
1112 PyObject *union_type = MAKE_UNION_TYPE(args);
1115 *output = union_type;
1129 uint64_t flags = _unpackVariableLength(&data);
1133 uint64_t flag_base = 1;
1140 PyObject *function_name;
1141 data = _unpackBlobConstant(tstate, &function_name, data);
1145 int line_number = (int)_unpackVariableLength(&data) + 1;
1150 PyObject *arg_names;
1151 data = _unpackBlobConstant(tstate, &arg_names, data);
1156 int arg_count = (int)_unpackVariableLength(&data);
1160#if PYTHON_VERSION >= 0x3b0
1161 PyObject *function_qualname;
1163 if (flags & flag_base) {
1164 data = _unpackBlobConstant(tstate, &function_qualname, data);
1166 function_qualname = function_name;
1172 PyObject *free_vars = NULL;
1174 if (flags & flag_base) {
1175 data = _unpackBlobConstant(tstate, &free_vars, data);
1179#if PYTHON_VERSION >= 0x300
1180 int kw_only_count = 0;
1181 if (flags & flag_base) {
1182 kw_only_count = (int)_unpackVariableLength(&data) + 1;
1185 assert(kw_only_count >= 0);
1188#if PYTHON_VERSION >= 0x380
1189 int pos_only_count = 0;
1190 if (flags & flag_base) {
1191 pos_only_count = (int)_unpackVariableLength(&data) + 1;
1194 assert(pos_only_count >= 0);
1199#if PYTHON_VERSION >= 0x360
1200 if ((flags & (flag_base * 3)) == (flag_base * 3)) {
1201 co_flags += CO_ASYNC_GENERATOR;
1204#if PYTHON_VERSION >= 0x350
1205 if ((flags & (flag_base * 2)) == (flag_base * 2)) {
1206 co_flags += CO_COROUTINE;
1209 if (flags & flag_base) {
1210 co_flags += CO_GENERATOR;
1215 if (flags & flag_base) {
1216 co_flags += CO_OPTIMIZED;
1220 if (flags & flag_base) {
1221 co_flags += CO_NEWLOCALS;
1225 if (flags & flag_base) {
1226 co_flags += CO_VARARGS;
1230 if (flags & flag_base) {
1231 co_flags += CO_VARKEYWORDS;
1235#if PYTHON_VERSION < 0x300
1236 if (flags & flag_base) {
1237 co_flags += CO_FUTURE_DIVISION;
1242 if (flags & flag_base) {
1243 co_flags += CO_FUTURE_UNICODE_LITERALS;
1247#if PYTHON_VERSION < 0x300
1248 if (flags & flag_base) {
1249 co_flags += CO_FUTURE_PRINT_FUNCTION;
1254#if PYTHON_VERSION < 0x300
1255 if (flags & flag_base) {
1256 co_flags += CO_FUTURE_ABSOLUTE_IMPORT;
1261#if PYTHON_VERSION >= 0x350 && PYTHON_VERSION < 0x370
1262 if (flags & flag_base) {
1263 co_flags += CO_FUTURE_GENERATOR_STOP;
1268#if PYTHON_VERSION >= 0x370
1269 if (flags & flag_base) {
1270 co_flags += CO_FUTURE_ANNOTATIONS;
1275#if PYTHON_VERSION >= 0x300
1276 if (flags & flag_base) {
1277 co_flags += CO_FUTURE_BARRY_AS_BDFL;
1283 *output = (PyObject *)MAKE_CODE_OBJECT(Py_None, line_number, co_flags, function_name, function_qualname,
1284 arg_names, free_vars, arg_count, kw_only_count, pos_only_count);
1286 CHECK_OBJECT(*output);
1292 PRINT_STRING(
"Missing blob values\n");
1293 NUITKA_CANNOT_GET_HERE(
"Corrupt constants blob");
1296 PRINT_FORMAT(
"Missing decoding for %d\n", (
int)c);
1297 NUITKA_CANNOT_GET_HERE(
"Corrupt constants blob");
1300#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1301 printf(
"Size for %c was %d\n", c, data - data_old);
1307 if (is_object ==
true) {
1308 CHECK_OBJECT(*output);
1310#if PYTHON_VERSION < 0x3c0
1314 Py_SET_REFCNT_IMMORTAL(*output);
1321static unsigned char const *_unpackBlobConstants(PyThreadState *tstate, PyObject **output,
unsigned char const *data,
1323 for (
int _i = 0; _i < count; _i++) {
1324 data = _unpackBlobConstant(tstate, output, data);
1332static void unpackBlobConstants(PyThreadState *tstate, PyObject **output,
unsigned char const *data) {
1333 int count = (int)unpackValueUint16(&data);
1335#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1336 printf(
"unpackBlobConstants count %d\n", count);
1338 _unpackBlobConstants(tstate, output, data, count);
1341#if _NUITKA_CONSTANTS_FROM_MACOS_SECTION
1343#include <mach-o/getsect.h>
1344#include <mach-o/ldsyms.h>
1346#if !_NUITKA_EXE_MODE
1347static int findMacOSDllImageId(
void) {
1349 int res = dladdr((
void *)findMacOSDllImageId, &where);
1352 char const *dll_filename = where.dli_fname;
1354 unsigned long image_count = _dyld_image_count();
1356 for (
int i = 0; i < image_count; i++) {
1358 struct mach_header const *header = _dyld_get_image_header(i);
1359 if (header == NULL) {
1363 if (strcmp(dll_filename, _dyld_get_image_name(i)) == 0) {
1373#define mach_header_arch mach_header_64
1375#define mach_header_arch mach_header
1378unsigned char *findMacOSBinarySection(
void) {
1380 const struct mach_header_arch *header = &_mh_execute_header;
1382 int image_id = findMacOSDllImageId();
1383 assert(image_id != -1);
1385 const struct mach_header_arch *header = (
const struct mach_header_arch *)_dyld_get_image_header(image_id);
1389 return getsectiondata(header,
"constants",
"constants", &size);
1394void loadConstantsBlob(PyThreadState *tstate, PyObject **output,
char const *name) {
1395 static bool init_done =
false;
1397 if (init_done ==
false) {
1398 NUITKA_PRINT_TIMING(
"loadConstantsBlob(): One time init.");
1400#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1401 printf(
"loadConstantsBlob '%s' one time init\n", name);
1404#if defined(_NUITKA_CONSTANTS_FROM_INCBIN)
1405 constant_bin = getConstantsBlobData();
1406#elif defined(_NUITKA_CONSTANTS_FROM_RESOURCE)
1409 HMODULE handle = NULL;
1411 HMODULE handle = getDllModuleHandle();
1414 constant_bin = (
const unsigned char *)LockResource(
1415 LoadResource(handle, FindResource(handle, MAKEINTRESOURCE(3), RT_RCDATA)));
1417 assert(constant_bin);
1418#elif _NUITKA_CONSTANTS_FROM_MACOS_SECTION
1419 constant_bin = findMacOSBinarySection();
1421 assert(constant_bin);
1423 NUITKA_PRINT_TIMING(
"loadConstantsBlob(): Found blob, decoding now.");
1424 DECODE(constant_bin);
1426 NUITKA_PRINT_TIMING(
"loadConstantsBlob(): CRC32 that blob for correctness.");
1427 uint32_t hash = unpackValueUint32(&constant_bin);
1428 uint32_t size = unpackValueUint32(&constant_bin);
1430#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1431 printf(
"loadConstantsBlob '%u' hash value\n", hash);
1432 printf(
"loadConstantsBlob '%u' size value\n", size);
1434 if (calcCRC32(constant_bin, size) != hash) {
1435 puts(
"Error, corrupted constants object");
1439#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1440 printf(
"Checked CRC32 to match hash %u size %u\n", hash, size);
1443 NUITKA_PRINT_TIMING(
"loadConstantsBlob(): One time init complete.");
1448#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1449 printf(
"Loading blob named '%s'\n", name);
1452 if (strcmp(name,
".bytecode") != 0) {
1456 unsigned char const *w = constant_bin;
1459 int match = strcmp(name, (
char const *)w);
1460 w += strlen((
char const *)w) + 1;
1462#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1463 printf(
"offset of blob size %d\n", w - constant_bin);
1466 uint32_t size = unpackValueUint32(&w);
1469#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1470 printf(
"Loading blob named '%s' with size %d\n", name, size);
1479 unpackBlobConstants(tstate, output, w);