15#include "nuitka/prelude.h"
19#define _NUITKA_CONSTANTS_FROM_COFF_OBJ 1
20#elif defined(__APPLE__)
21#define _NUITKA_CONSTANTS_FROM_MACOS_SECTION 1
23#define _NUITKA_CONSTANTS_FROM_CODE 1
28#if _NUITKA_EXPERIMENTAL_WRITEABLE_CONSTANTS
29#define CONSTANT_BIN_CONSTANT
31#define CONSTANT_BIN_CONSTANT const
34#if defined(_NUITKA_CONSTANTS_FROM_LINKER) || defined(_NUITKA_CONSTANTS_FROM_COFF_OBJ) || \
35 defined(_NUITKA_CONSTANTS_FROM_CODE) || defined(_NUITKA_CONSTANTS_FROM_INCBIN) || \
36 defined(_NUITKA_CONSTANTS_FROM_C23_EMBED) || defined(_NUITKA_CONSTANTS_FROM_RESOURCE) || \
37 defined(_NUITKA_CONSTANTS_FROM_MACOS_SECTION)
38NUITKA_DECLARE_CONSTANT_BLOB(constant_bin, ConstantBlob, CONSTANT_BIN_CONSTANT, 3);
41#include "nuitka/constants_blob_spec.h"
44unsigned char const *constant_bin = NULL;
46#if PYTHON_VERSION < 0x300
47static PyObject *int_cache = NULL;
50static PyObject *long_cache = NULL;
52static PyObject *float_cache = NULL;
54#if PYTHON_VERSION >= 0x300
55static PyObject *bytes_cache = NULL;
58#if PYTHON_VERSION < 0x300
59static PyObject *unicode_cache = NULL;
62static PyObject *tuple_cache = NULL;
64static PyObject *list_cache = NULL;
66static PyObject *dict_cache = NULL;
68static PyObject *set_cache = NULL;
70static PyObject *frozenset_cache = NULL;
74static Py_hash_t Nuitka_FastHashBytes(
const void *value, Py_ssize_t size) {
75 if (unlikely(size == 0)) {
79 unsigned char *w = (
unsigned char *)value;
83 x = (1000003 * x) ^ *w++;
96static Py_hash_t our_list_hash(PyListObject *list) {
97 return Nuitka_FastHashBytes(&list->ob_item[0], Py_SIZE(list) *
sizeof(PyObject *));
100static PyObject *our_list_tp_richcompare(PyListObject *list1, PyListObject *list2,
int op) {
105 if (list1 == list2) {
107 }
else if (Py_SIZE(list1) != Py_SIZE(list2)) {
109 }
else if (memcmp(&list1->ob_item[0], &list2->ob_item[0], Py_SIZE(list1) *
sizeof(PyObject *)) == 0) {
115 Py_INCREF_IMMORTAL(result);
119static Py_hash_t our_tuple_hash(PyTupleObject *tuple) {
120 return Nuitka_FastHashBytes(&tuple->ob_item[0], Py_SIZE(tuple) *
sizeof(PyObject *));
123static PyObject *our_tuple_tp_richcompare(PyTupleObject *tuple1, PyTupleObject *tuple2,
int op) {
128 if (tuple1 == tuple2) {
130 }
else if (Py_SIZE(tuple1) != Py_SIZE(tuple2)) {
132 }
else if (memcmp(&tuple1->ob_item[0], &tuple2->ob_item[0], Py_SIZE(tuple1) *
sizeof(PyObject *)) == 0) {
138 Py_INCREF_IMMORTAL(result);
142static Py_hash_t our_set_hash(PyObject *set) {
143 Py_hash_t result = 0;
147#if PYTHON_VERSION < 0x300
150 while (_PySet_Next(set, &pos, &key)) {
152 result ^= Nuitka_FastHashBytes(&key,
sizeof(PyObject *));
157 while (_PySet_NextEntry(set, &pos, &key, &unused)) {
159 result ^= Nuitka_FastHashBytes(&key,
sizeof(PyObject *));
166static PyObject *our_set_tp_richcompare(PyObject *set1, PyObject *set2,
int op) {
171 Py_ssize_t pos1 = 0, pos2 = 0;
172 PyObject *key1, *key2;
174 if (Py_SIZE(set1) != Py_SIZE(set2)) {
179#if PYTHON_VERSION < 0x300
182 while (_PySet_Next(set1, &pos1, &key1)) {
184 NUITKA_MAY_BE_UNUSED
int res = _PySet_Next(set2, &pos2, &key2);
194 Py_hash_t unused1, unused2;
198 while (_PySet_NextEntry(set1, &pos1, &key1, &unused1)) {
200 NUITKA_MAY_BE_UNUSED
int res = _PySet_NextEntry(set2, &pos2, &key2, &unused2);
212 Py_INCREF_IMMORTAL(result);
216static PyObject *our_float_tp_richcompare(PyFloatObject *a, PyFloatObject *b,
int op) {
222 if (memcmp(&a->ob_fval, &b->ob_fval,
sizeof(b->ob_fval)) == 0) {
228 Py_INCREF_IMMORTAL(result);
232static Py_hash_t our_dict_hash(PyObject *dict) {
233 Py_hash_t result = 0;
236 PyObject *key, *value;
238 while (Nuitka_DictNext(dict, &pos, &key, &value)) {
240 result ^= Nuitka_FastHashBytes(&key,
sizeof(PyObject *));
242 result ^= Nuitka_FastHashBytes(&value,
sizeof(PyObject *));
248static PyObject *our_dict_tp_richcompare(PyObject *a, PyObject *b,
int op) {
251 if (Py_SIZE(a) != Py_SIZE(b)) {
256 Py_ssize_t pos1 = 0, pos2 = 0;
257 PyObject *key1, *value1;
258 PyObject *key2, *value2;
263 while (Nuitka_DictNext(a, &pos1, &key1, &value1)) {
265 NUITKA_MAY_BE_UNUSED
int res = Nuitka_DictNext(b, &pos2, &key2, &value2);
269 if (key1 != key2 || value1 != value2) {
276 Py_INCREF_IMMORTAL(result);
281#if PYTHON_VERSION < 0x3b0
282#if PYTHON_VERSION >= 0x390
283PyObject **Nuitka_Long_SmallValues;
284#elif PYTHON_VERSION >= 0x300
285PyObject *Nuitka_Long_SmallValues[NUITKA_STATIC_SMALLINT_VALUE_MAX - NUITKA_STATIC_SMALLINT_VALUE_MIN + 1];
289static void initCaches(
void) {
290 static bool init_done =
false;
291 if (init_done ==
true) {
295#if PYTHON_VERSION < 0x300
296 int_cache = PyDict_New();
299 long_cache = PyDict_New();
301 float_cache = PyDict_New();
303#if PYTHON_VERSION >= 0x300
304 bytes_cache = PyDict_New();
307#if PYTHON_VERSION < 0x300
308 unicode_cache = PyDict_New();
311 tuple_cache = PyDict_New();
313 list_cache = PyDict_New();
315 dict_cache = PyDict_New();
317 set_cache = PyDict_New();
319 frozenset_cache = PyDict_New();
321#if PYTHON_VERSION < 0x3b0
322#if PYTHON_VERSION >= 0x390
324 Nuitka_Long_SmallValues = (PyObject **)_PyInterpreterState_GET()->small_ints;
325#elif PYTHON_VERSION >= 0x300
326 for (
long i = NUITKA_STATIC_SMALLINT_VALUE_MIN; i < NUITKA_STATIC_SMALLINT_VALUE_MAX; i++) {
329 PyObject *value = PyLong_FromLong(i);
330 Nuitka_Long_SmallValues[NUITKA_TO_SMALL_VALUE_OFFSET(i)] = value;
338static void insertToDictCache(PyObject *dict, PyObject **value) {
339 PyObject *item = PyDict_GetItem(dict, *value);
344 PyDict_SetItem(dict, *value, *value);
348static void insertToDictCacheForcedHash(PyObject *dict, PyObject **value, hashfunc tp_hash,
349 richcmpfunc tp_richcompare) {
350 hashfunc old_hash = Py_TYPE(*value)->tp_hash;
351 richcmpfunc old_richcmpfunc = Py_TYPE(*value)->tp_richcompare;
355 if (tp_hash != NULL) {
356 Py_TYPE(*value)->tp_hash = tp_hash;
358 Py_TYPE(*value)->tp_richcompare = tp_richcompare;
360 insertToDictCache(dict, value);
362 Py_TYPE(*value)->tp_hash = old_hash;
363 Py_TYPE(*value)->tp_richcompare = old_richcmpfunc;
366static uint16_t unpackValueUint16(
unsigned char const **data) {
369 memcpy(&value, *data,
sizeof(value));
371 assert(
sizeof(value) == 2);
373 *data +=
sizeof(value);
378static uint32_t unpackValueUint32(
unsigned char const **data) {
381 memcpy(&value, *data,
sizeof(value));
383 assert(
sizeof(value) == 4);
385 *data +=
sizeof(value);
390static double unpackValueFloat(
unsigned char const **data) {
393 memcpy(&size, *data,
sizeof(size));
394 *data +=
sizeof(size);
399static unsigned char const *_unpackValueCString(
unsigned char const *data) {
400 while (*(data++) != 0) {
407static uint64_t _unpackVariableLength(
unsigned char const **data) {
412 unsigned char value = **data;
415 result += (value & 127) * factor;
427static PyObject *_unpackAnonValue(
unsigned char anon_index) {
428 switch (anon_index) {
430 return (PyObject *)Py_TYPE(Py_None);
432 return (PyObject *)&PyEllipsis_Type;
434 return (PyObject *)Py_TYPE(Py_NotImplemented);
436 return (PyObject *)&PyFunction_Type;
438 return (PyObject *)&PyGen_Type;
440 return (PyObject *)&PyCFunction_Type;
442 return (PyObject *)&PyCode_Type;
444 return (PyObject *)&PyModule_Type;
446#if PYTHON_VERSION < 0x300
448 return (PyObject *)&PyFile_Type;
450 return (PyObject *)&PyClass_Type;
452 return (PyObject *)&PyInstance_Type;
454 return (PyObject *)&PyMethod_Type;
457#if PYTHON_VERSION >= 0x3a0
459 return (PyObject *)Nuitka_PyUnion_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 || (PYTHON_VERSION >= 0x3c7 && _NUITKA_EXE_MODE)
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 assert(Nuitka_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 unsigned char c = *data++;
541#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
542 unsigned char const *data_old = data;
543 printf(
"Type %u:\n", (
unsigned int)c);
547 case NUITKA_CONSTANT_BLOB_TAG_PREVIOUS: {
548 *output = *(output - 1);
553 case NUITKA_CONSTANT_BLOB_TAG_TUPLE: {
554 int size = (int)_unpackVariableLength(&data);
556 PyObject *t = PyTuple_New(size);
560 data = _unpackBlobConstants(tstate, &PyTuple_GET_ITEM(t, 0), data, size);
562 CHECK_OBJECTS(&PyTuple_GET_ITEM(t, 0), size);
565 insertToDictCacheForcedHash(tuple_cache, &t, (hashfunc)our_tuple_hash, (richcmpfunc)our_tuple_tp_richcompare);
572 case NUITKA_CONSTANT_BLOB_TAG_LIST: {
573 int size = (int)_unpackVariableLength(&data);
575 PyObject *l = PyList_New(size);
579 data = _unpackBlobConstants(tstate, &PyList_GET_ITEM(l, 0), data, size);
581 CHECK_OBJECTS(&PyList_GET_ITEM(l, 0), size);
584 insertToDictCacheForcedHash(list_cache, &l, (hashfunc)our_list_hash, (richcmpfunc)our_list_tp_richcompare);
591 case NUITKA_CONSTANT_BLOB_TAG_DICT: {
592 int size = (int)_unpackVariableLength(&data);
594 PyObject *d = _PyDict_NewPresized(size);
598 NUITKA_DYNAMIC_ARRAY_DECL(keys, PyObject *, size);
599 NUITKA_DYNAMIC_ARRAY_DECL(values, PyObject *, size);
601 data = _unpackBlobConstants(tstate, &keys[0], data, size);
602 data = _unpackBlobConstants(tstate, &values[0], data, size);
604 CHECK_OBJECTS(&keys[0], size);
605 CHECK_OBJECTS(&values[0], size);
607 for (
int i = 0; i < size; i++) {
608 NUITKA_MAY_BE_UNUSED
int res = PyDict_SetItem(d, keys[i], values[i]);
613 insertToDictCacheForcedHash(dict_cache, &d, (hashfunc)our_dict_hash, (richcmpfunc)our_dict_tp_richcompare);
620 case NUITKA_CONSTANT_BLOB_TAG_FROZENSET:
621 case NUITKA_CONSTANT_BLOB_TAG_SET: {
622 int size = (int)_unpackVariableLength(&data);
626 if (c == NUITKA_CONSTANT_BLOB_TAG_SET) {
632 static PyObject *empty_frozenset = NULL;
634 if (empty_frozenset == NULL) {
635 empty_frozenset = CALL_FUNCTION_WITH_SINGLE_ARG(tstate, (PyObject *)&PyFrozenSet_Type,
636 Nuitka_Bytes_FromStringAndSize(
"", 0));
641 s = PyFrozenSet_New(NULL);
648 NUITKA_DYNAMIC_ARRAY_DECL(values, PyObject *, size);
650 data = _unpackBlobConstants(tstate, &values[0], data, size);
652 CHECK_OBJECTS(&values[0], size);
654 for (
int i = 0; i < size; i++) {
655 NUITKA_MAY_BE_UNUSED
int res = PySet_Add(s, values[i]);
661 if (c == NUITKA_CONSTANT_BLOB_TAG_SET) {
662 insertToDictCacheForcedHash(set_cache, &s, (hashfunc)our_set_hash, (richcmpfunc)our_set_tp_richcompare);
664 insertToDictCacheForcedHash(frozenset_cache, &s, (hashfunc)our_set_hash,
665 (richcmpfunc)our_set_tp_richcompare);
673#if PYTHON_VERSION < 0x300
674 case NUITKA_CONSTANT_BLOB_TAG_INT_NEGATIVE:
675 case NUITKA_CONSTANT_BLOB_TAG_INT_POSITIVE: {
676 long value = (long)_unpackVariableLength(&data);
677 if (c == NUITKA_CONSTANT_BLOB_TAG_INT_NEGATIVE) {
681 PyObject *i = PyInt_FromLong(value);
683 insertToDictCache(int_cache, &i);
691 case NUITKA_CONSTANT_BLOB_TAG_LONG_POSITIVE_SMALL:
692 case NUITKA_CONSTANT_BLOB_TAG_LONG_NEGATIVE_SMALL: {
694 uint64_t value = _unpackVariableLength(&data);
697 Nuitka_LongFromCLong((c == NUITKA_CONSTANT_BLOB_TAG_LONG_POSITIVE_SMALL) ? ((
long)value) : (-(long)value));
701#if PYTHON_VERSION >= 0x300
702 long check_value = (c == NUITKA_CONSTANT_BLOB_TAG_LONG_POSITIVE_SMALL) ? (
long)value : -(long)value;
703 if (check_value < NUITKA_STATIC_SMALLINT_VALUE_MIN || check_value >= NUITKA_STATIC_SMALLINT_VALUE_MAX)
706 insertToDictCache(long_cache, &l);
714 case NUITKA_CONSTANT_BLOB_TAG_LONG_NEGATIVE_LARGE:
715 case NUITKA_CONSTANT_BLOB_TAG_LONG_POSITIVE_LARGE: {
716 PyObject *result = Nuitka_PyLong_FromLong(0);
718 int size = (int)_unpackVariableLength(&data);
720 PyObject *shift = Nuitka_PyLong_FromLong(31);
722 for (
int i = 0; i < size; i++) {
723 result = PyNumber_InPlaceLshift(result, shift);
725 uint64_t value = _unpackVariableLength(&data);
726 PyObject *part = Nuitka_LongFromCLong((
long)value);
727 assert(part != NULL);
728 result = PyNumber_InPlaceAdd(result, part);
734 if (c == NUITKA_CONSTANT_BLOB_TAG_LONG_NEGATIVE_LARGE) {
735 Nuitka_LongSetSignNegative(result);
738 insertToDictCache(long_cache, &result);
745 case NUITKA_CONSTANT_BLOB_TAG_FLOAT: {
746 double value = unpackValueFloat(&data);
748 PyObject *f = PyFloat_FromDouble(value);
751 insertToDictCacheForcedHash(float_cache, &f, NULL, (richcmpfunc)our_float_tp_richcompare);
758 case NUITKA_CONSTANT_BLOB_TAG_COMPLEX: {
759 double real = unpackValueFloat(&data);
760 double imag = unpackValueFloat(&data);
762 *output = PyComplex_FromDoubles(real, imag);
767 case NUITKA_CONSTANT_BLOB_TAG_COMPLEX_SPECIAL: {
771 data = _unpackBlobConstants(tstate, &parts[0], data, 2);
773 *output = BUILTIN_COMPLEX2(tstate, parts[0], parts[1]);
778#if PYTHON_VERSION < 0x300
779 case NUITKA_CONSTANT_BLOB_TAG_ATTRIBUTE_NAME:
780 case NUITKA_CONSTANT_BLOB_TAG_BYTES_ZERO_TERMINATED: {
782 size_t size = strlen((
const char *)data);
784 PyObject *s = PyString_FromStringAndSize((
const char *)data, size);
789 if (c == NUITKA_CONSTANT_BLOB_TAG_ATTRIBUTE_NAME) {
790 PyString_InternInPlace(&s);
799 case NUITKA_CONSTANT_BLOB_TAG_BYTES_ZERO_TERMINATED: {
801 size_t size = strlen((
const char *)data);
803 PyObject *b = Nuitka_Bytes_FromStringAndSize((
const char *)data, size);
810 insertToDictCache(bytes_cache, &b);
819 case NUITKA_CONSTANT_BLOB_TAG_BYTES_SINGLE: {
822#if PYTHON_VERSION < 0x300
823 PyObject *s = PyString_FromStringAndSize((
const char *)data, 1);
827 PyObject *b = Nuitka_Bytes_FromStringAndSize((
const char *)data, 1);
836 case NUITKA_CONSTANT_BLOB_TAG_TEXT_SINGLE: {
838 PyObject *u = _Nuitka_Unicode_ImmortalFromStringAndSize(tstate, (
const char *)data, 1,
true);
846 case NUITKA_CONSTANT_BLOB_TAG_BYTES_LENGTH_PREFIXED: {
848 int size = (int)_unpackVariableLength(&data);
851 PyObject *b = Nuitka_Bytes_FromStringAndSize((
const char *)data, size);
856#if PYTHON_VERSION >= 0x300
857 insertToDictCache(bytes_cache, &b);
866 case NUITKA_CONSTANT_BLOB_TAG_BYTEARRAY: {
867 int size = (int)_unpackVariableLength(&data);
869 PyObject *b = PyByteArray_FromStringAndSize((
const char *)data, size);
877#if PYTHON_VERSION >= 0x300
878 case NUITKA_CONSTANT_BLOB_TAG_ATTRIBUTE_NAME:
880 case NUITKA_CONSTANT_BLOB_TAG_TEXT_UTF8_ZERO_TERMINATED: {
881 size_t size = strlen((
const char *)data);
884 PyObject *u = _Nuitka_Unicode_ImmortalFromStringAndSize(tstate, (
const char *)data, size,
885 c == NUITKA_CONSTANT_BLOB_TAG_ATTRIBUTE_NAME);
893 case NUITKA_CONSTANT_BLOB_TAG_TEXT_UTF8_LENGTH_PREFIXED: {
894 int size = (int)_unpackVariableLength(&data);
897 PyObject *u = _Nuitka_Unicode_ImmortalFromStringAndSize(tstate, (
const char *)data, size,
false);
905 case NUITKA_CONSTANT_BLOB_TAG_NONE: {
911 case NUITKA_CONSTANT_BLOB_TAG_TEXT_EMPTY: {
912 *output = _Nuitka_Unicode_ImmortalFromStringAndSize(tstate, (
const char *)data, 0,
true);
917 case NUITKA_CONSTANT_BLOB_TAG_TRUE: {
923 case NUITKA_CONSTANT_BLOB_TAG_FALSE: {
929 case NUITKA_CONSTANT_BLOB_TAG_SLICE: {
932 data = _unpackBlobConstants(tstate, &items[0], data, 3);
934 PyObject *s = MAKE_SLICE_OBJECT3(tstate, items[0], items[1], items[2]);
941 case NUITKA_CONSTANT_BLOB_TAG_RANGE: {
944 data = _unpackBlobConstants(tstate, &items[0], data, 3);
945#if PYTHON_VERSION < 0x300
946 assert(PyInt_CheckExact(items[0]));
947 assert(PyInt_CheckExact(items[1]));
948 assert(PyInt_CheckExact(items[2]));
950 long start = PyInt_AS_LONG(items[0]);
951 long stop = PyInt_AS_LONG(items[1]);
952 long step = PyInt_AS_LONG(items[2]);
954 PyObject *s = MAKE_XRANGE(tstate, start, stop, step);
956 PyObject *s = BUILTIN_XRANGE3(tstate, items[0], items[1], items[2]);
963 case NUITKA_CONSTANT_BLOB_TAG_BUILTIN_ANON: {
965 unsigned char anon_index = *data++;
967 *output = _unpackAnonValue(anon_index);
972 case NUITKA_CONSTANT_BLOB_TAG_BUILTIN_SPECIAL: {
974 unsigned char special_index = *data++;
976 *output = _unpackSpecialValue(special_index);
981 case NUITKA_CONSTANT_BLOB_TAG_BUILTIN_NAMED: {
984 char const *builtin_name = (
char const *)data;
985 data = _unpackValueCString(data);
987 *output = PyObject_GetAttrString((PyObject *)builtin_module, builtin_name);
992 case NUITKA_CONSTANT_BLOB_TAG_BUILTIN_EXCEPTION: {
995 char const *builtin_exception_name = (
char const *)data;
996 data = _unpackValueCString(data);
998 *output = PyObject_GetAttrString((PyObject *)builtin_module, builtin_exception_name);
1003 case NUITKA_CONSTANT_BLOB_TAG_FLOAT_SPECIAL: {
1004 unsigned char v = *data++;
1009 case NUITKA_CONSTANT_BLOB_FLOAT_SPECIAL_POS_ZERO: {
1010 static PyObject *_const_float_0_0 = NULL;
1012 if (_const_float_0_0 == NULL) {
1013 _const_float_0_0 = PyFloat_FromDouble(0.0);
1015 z = _const_float_0_0;
1019 case NUITKA_CONSTANT_BLOB_FLOAT_SPECIAL_NEG_ZERO: {
1020 static PyObject *_const_float_minus_0_0 = NULL;
1022 if (_const_float_minus_0_0 == NULL) {
1023 _const_float_minus_0_0 = PyFloat_FromDouble(0.0);
1026 PyFloat_SET_DOUBLE(_const_float_minus_0_0, copysign(PyFloat_AS_DOUBLE(_const_float_minus_0_0), -1.0));
1028 z = _const_float_minus_0_0;
1033 case NUITKA_CONSTANT_BLOB_FLOAT_SPECIAL_POS_NAN: {
1034 static PyObject *_const_float_plus_nan = NULL;
1036 if (_const_float_plus_nan == NULL) {
1037 _const_float_plus_nan = PyFloat_FromDouble(Py_NAN);
1040 PyFloat_SET_DOUBLE(_const_float_plus_nan, copysign(PyFloat_AS_DOUBLE(_const_float_plus_nan), 1.0));
1042 z = _const_float_plus_nan;
1046 case NUITKA_CONSTANT_BLOB_FLOAT_SPECIAL_NEG_NAN: {
1047 static PyObject *_const_float_minus_nan = NULL;
1049 if (_const_float_minus_nan == NULL) {
1050 _const_float_minus_nan = PyFloat_FromDouble(Py_NAN);
1053 PyFloat_SET_DOUBLE(_const_float_minus_nan, copysign(PyFloat_AS_DOUBLE(_const_float_minus_nan), -1.0));
1055 z = _const_float_minus_nan;
1059 case NUITKA_CONSTANT_BLOB_FLOAT_SPECIAL_POS_INF: {
1060 static PyObject *_const_float_plus_inf = NULL;
1062 if (_const_float_plus_inf == NULL) {
1063 _const_float_plus_inf = PyFloat_FromDouble(Py_HUGE_VAL);
1066 PyFloat_SET_DOUBLE(_const_float_plus_inf, copysign(PyFloat_AS_DOUBLE(_const_float_plus_inf), 1.0));
1068 z = _const_float_plus_inf;
1072 case NUITKA_CONSTANT_BLOB_FLOAT_SPECIAL_NEG_INF: {
1073 static PyObject *_const_float_minus_inf = NULL;
1075 if (_const_float_minus_inf == NULL) {
1076 _const_float_minus_inf = PyFloat_FromDouble(Py_HUGE_VAL);
1079 PyFloat_SET_DOUBLE(_const_float_minus_inf, copysign(PyFloat_AS_DOUBLE(_const_float_minus_inf), -1.0));
1081 z = _const_float_minus_inf;
1086 PRINT_FORMAT(
"Missing decoding for %d\n", (
int)c);
1087 NUITKA_CANNOT_GET_HERE(
"Corrupt constants blob");
1092 insertToDictCacheForcedHash(float_cache, &z, NULL, (richcmpfunc)our_float_tp_richcompare);
1099 case NUITKA_CONSTANT_BLOB_TAG_BLOB_DATA: {
1101 uint64_t size = _unpackVariableLength(&data);
1103 *output = (PyObject *)data;
1110#if PYTHON_VERSION >= 0x390
1111 case NUITKA_CONSTANT_BLOB_TAG_GENERIC_ALIAS: {
1114 data = _unpackBlobConstants(tstate, &items[0], data, 2);
1116 PyObject *g = Py_GenericAlias(items[0], items[1]);
1125#if PYTHON_VERSION >= 0x3a0
1126 case NUITKA_CONSTANT_BLOB_TAG_UNION_TYPE: {
1129 data = _unpackBlobConstants(tstate, &args, data, 1);
1131 PyObject *union_type = MAKE_UNION_TYPE(args);
1134 *output = union_type;
1140 case NUITKA_CONSTANT_BLOB_TAG_CODE_OBJECT: {
1147 uint64_t flags = _unpackVariableLength(&data);
1154 PyObject *function_name;
1155 data = _unpackBlobConstant(tstate, &function_name, data);
1159 int line_number = (int)_unpackVariableLength(&data) + 1;
1164 PyObject *arg_names;
1165 data = _unpackBlobConstant(tstate, &arg_names, data);
1170 int arg_count = (int)_unpackVariableLength(&data);
1174#if PYTHON_VERSION >= 0x3b0
1175 PyObject *function_qualname;
1177 if (flags & NUITKA_CONSTANT_BLOB_CODE_FLAG_QUALNAME) {
1178 data = _unpackBlobConstant(tstate, &function_qualname, data);
1180 function_qualname = function_name;
1185 PyObject *free_vars = NULL;
1187 if (flags & NUITKA_CONSTANT_BLOB_CODE_FLAG_FREE_VARS) {
1188 data = _unpackBlobConstant(tstate, &free_vars, data);
1191#if PYTHON_VERSION >= 0x300
1192 int kw_only_count = 0;
1193 if (flags & NUITKA_CONSTANT_BLOB_CODE_FLAG_KW_ONLY) {
1194 kw_only_count = (int)_unpackVariableLength(&data) + 1;
1196 assert(kw_only_count >= 0);
1199#if PYTHON_VERSION >= 0x380
1200 int pos_only_count = 0;
1201 if (flags & NUITKA_CONSTANT_BLOB_CODE_FLAG_POS_ONLY) {
1202 pos_only_count = (int)_unpackVariableLength(&data) + 1;
1204 assert(pos_only_count >= 0);
1207 switch (flags & NUITKA_CONSTANT_BLOB_CODE_KIND_MASK) {
1208#if PYTHON_VERSION >= 0x360
1209 case NUITKA_CONSTANT_BLOB_CODE_KIND_ASYNCGEN:
1210 co_flags += CO_ASYNC_GENERATOR;
1213#if PYTHON_VERSION >= 0x350
1214 case NUITKA_CONSTANT_BLOB_CODE_KIND_COROUTINE:
1215 co_flags += CO_COROUTINE;
1218 case NUITKA_CONSTANT_BLOB_CODE_KIND_GENERATOR:
1219 co_flags += CO_GENERATOR;
1225 if (flags & NUITKA_CONSTANT_BLOB_CODE_FLAG_OPTIMIZED) {
1226 co_flags += CO_OPTIMIZED;
1229 if (flags & NUITKA_CONSTANT_BLOB_CODE_FLAG_NEWLOCALS) {
1230 co_flags += CO_NEWLOCALS;
1233 if (flags & NUITKA_CONSTANT_BLOB_CODE_FLAG_VARARGS) {
1234 co_flags += CO_VARARGS;
1237 if (flags & NUITKA_CONSTANT_BLOB_CODE_FLAG_VARKEYWORDS) {
1238 co_flags += CO_VARKEYWORDS;
1241#if PYTHON_VERSION >= 0x370
1242 if (flags & NUITKA_CONSTANT_BLOB_CODE_FLAG_FUTURE_ANNOTATIONS) {
1243 co_flags += CO_FUTURE_ANNOTATIONS;
1247#if PYTHON_VERSION < 0x300
1248 if (flags & NUITKA_CONSTANT_BLOB_CODE_FLAG_FUTURE_DIVISION) {
1249 co_flags += CO_FUTURE_DIVISION;
1253 if (flags & NUITKA_CONSTANT_BLOB_CODE_FLAG_FUTURE_UNICODE_LITERALS) {
1254 co_flags += CO_FUTURE_UNICODE_LITERALS;
1257#if PYTHON_VERSION < 0x300
1258 if (flags & NUITKA_CONSTANT_BLOB_CODE_FLAG_FUTURE_PRINT_FUNCTION) {
1259 co_flags += CO_FUTURE_PRINT_FUNCTION;
1263#if PYTHON_VERSION < 0x300
1264 if (flags & NUITKA_CONSTANT_BLOB_CODE_FLAG_FUTURE_ABSOLUTE_IMPORT) {
1265 co_flags += CO_FUTURE_ABSOLUTE_IMPORT;
1269#if PYTHON_VERSION >= 0x350 && PYTHON_VERSION < 0x370
1270 if (flags & NUITKA_CONSTANT_BLOB_CODE_FLAG_FUTURE_GENERATOR_STOP) {
1271 co_flags += CO_FUTURE_GENERATOR_STOP;
1275#if PYTHON_VERSION >= 0x300
1276 if (flags & NUITKA_CONSTANT_BLOB_CODE_FLAG_FUTURE_BARRY_AS_BDFL) {
1277 co_flags += CO_FUTURE_BARRY_AS_BDFL;
1282 *output = (PyObject *)MAKE_CODE_OBJECT(Py_None, line_number, co_flags, function_name, function_qualname,
1283 arg_names, free_vars, arg_count, kw_only_count, pos_only_count);
1285 CHECK_OBJECT(*output);
1290 case NUITKA_CONSTANT_BLOB_TAG_END: {
1291 PRINT_STRING(
"Missing blob values\n");
1292 NUITKA_CANNOT_GET_HERE(
"Corrupt constants blob");
1295 PRINT_FORMAT(
"Missing decoding for %d\n", (
int)c);
1296 NUITKA_CANNOT_GET_HERE(
"Corrupt constants blob");
1299#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1300 printf(
"Size for %u was %d\n", (
unsigned int)c, (
int)(data - data_old));
1306 if (is_object ==
true) {
1307 CHECK_OBJECT(*output);
1309#if PYTHON_VERSION < 0x3c0
1313 Py_SET_REFCNT_IMMORTAL(*output);
1320static unsigned char const *_unpackBlobConstants(PyThreadState *tstate, PyObject **output,
unsigned char const *data,
1322 for (
int _i = 0; _i < count; _i++) {
1323 data = _unpackBlobConstant(tstate, output, data);
1331static int unpackBlobConstants(PyThreadState *tstate, PyObject **output,
unsigned char const *data) {
1332 int count = (int)unpackValueUint16(&data);
1334#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1335 printf(
"unpackBlobConstants count %d\n", count);
1337 _unpackBlobConstants(tstate, output, data, count);
1342int loadConstantsBlob(PyThreadState *tstate, PyObject **output,
char const *name) {
1343 static bool init_done =
false;
1345 if (init_done ==
false) {
1346 NUITKA_PRINT_TIMING(
"loadConstantsBlob(): One time init.");
1348#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1349 printf(
"loadConstantsBlob '%s' one time init\n", name);
1352#if defined(_NUITKA_CONSTANTS_FROM_INCBIN) || defined(_NUITKA_CONSTANTS_FROM_LINKER) || \
1353 defined(_NUITKA_CONSTANTS_FROM_COFF_OBJ) || defined(_NUITKA_CONSTANTS_FROM_CODE) || \
1354 defined(_NUITKA_CONSTANTS_FROM_C23_EMBED) || defined(_NUITKA_CONSTANTS_FROM_RESOURCE) || \
1355 defined(_NUITKA_CONSTANTS_FROM_MACOS_SECTION)
1356 constant_bin = getConstantBlobData();
1358 NUITKA_PRINT_TIMING(
"loadConstantsBlob(): Found blob, decoding now.");
1359 DECODE(constant_bin);
1361 NUITKA_PRINT_TIMING(
"loadConstantsBlob(): One time init complete.");
1366#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1367 printf(
"Loading blob named '%s'\n", name);
1370 if (strcmp(name,
".bytecode") != 0) {
1374 unsigned char const *w = constant_bin;
1377 int match = strcmp(name, (
char const *)w);
1378 w += strlen((
char const *)w) + 1;
1380#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1381 printf(
"offset of blob size %d\n", w - constant_bin);
1384 uint32_t size = unpackValueUint32(&w);
1387#ifdef _NUITKA_EXPERIMENTAL_DEBUG_CONSTANTS
1388 printf(
"Loading blob named '%s' with size %d\n", name, size);
1397 return unpackBlobConstants(tstate, output, w);