|
| 1 | + |
| 2 | +#include "nuitka/prelude.h" |
| 3 | +#include "structseq.h" |
| 4 | + |
| 5 | +// Global constants storage |
| 6 | +PyObject *global_constants[87]; |
| 7 | + |
| 8 | +// Sentinel PyObject to be used for all our call iterator endings. It will |
| 9 | +// become a PyCObject pointing to NULL. It's address is unique, and that's |
| 10 | +// enough for us to use it as sentinel value. |
| 11 | +PyObject *_sentinel_value = NULL; |
| 12 | + |
| 13 | +PyObject *Nuitka_dunder_compiled_value = NULL; |
| 14 | + |
| 15 | + |
| 16 | +#ifdef _NUITKA_STANDALONE |
| 17 | +extern PyObject *getStandaloneSysExecutablePath(PyObject *basename); |
| 18 | +#endif |
| 19 | + |
| 20 | +extern void setDistributionsMetadata(PyObject *metadata_values); |
| 21 | + |
| 22 | +// We provide the sys.version info shortcut as a global value here for ease of use. |
| 23 | +PyObject *Py_SysVersionInfo = NULL; |
| 24 | + |
| 25 | +static void _createGlobalConstants(void) { |
| 26 | + // We provide the sys.version info shortcut as a global value here for ease of use. |
| 27 | + Py_SysVersionInfo = Nuitka_SysGetObject("version_info"); |
| 28 | + |
| 29 | + // The empty name means global. |
| 30 | + loadConstantsBlob(&global_constants[0], ""); |
| 31 | + |
| 32 | +#if _NUITKA_EXE |
| 33 | + /* Set the "sys.executable" path to the original CPython executable or point to inside the |
| 34 | + distribution for standalone. */ |
| 35 | + Nuitka_SysSetObject( |
| 36 | + "executable", |
| 37 | +#ifndef _NUITKA_STANDALONE |
| 38 | + global_constants[86] |
| 39 | +#else |
| 40 | + getStandaloneSysExecutablePath(global_constants[86]) |
| 41 | +#endif |
| 42 | + ); |
| 43 | + |
| 44 | +#ifndef _NUITKA_STANDALONE |
| 45 | + /* Set the "sys.prefix" path to the original one. */ |
| 46 | + Nuitka_SysSetObject( |
| 47 | + "prefix", |
| 48 | + None |
| 49 | + ); |
| 50 | + |
| 51 | + /* Set the "sys.prefix" path to the original one. */ |
| 52 | + Nuitka_SysSetObject( |
| 53 | + "exec_prefix", |
| 54 | + None |
| 55 | + ); |
| 56 | + |
| 57 | + |
| 58 | +#if PYTHON_VERSION >= 0x300 |
| 59 | + /* Set the "sys.base_prefix" path to the original one. */ |
| 60 | + Nuitka_SysSetObject( |
| 61 | + "base_prefix", |
| 62 | + None |
| 63 | + ); |
| 64 | + |
| 65 | + /* Set the "sys.exec_base_prefix" path to the original one. */ |
| 66 | + Nuitka_SysSetObject( |
| 67 | + "base_exec_prefix", |
| 68 | + None |
| 69 | + ); |
| 70 | + |
| 71 | +#endif |
| 72 | +#endif |
| 73 | +#endif |
| 74 | + |
| 75 | + static PyTypeObject Nuitka_VersionInfoType; |
| 76 | + |
| 77 | + // Same fields as "sys.version_info" except no serial number. |
| 78 | + static PyStructSequence_Field Nuitka_VersionInfoFields[] = { |
| 79 | + {(char *)"major", (char *)"Major release number"}, |
| 80 | + {(char *)"minor", (char *)"Minor release number"}, |
| 81 | + {(char *)"micro", (char *)"Micro release number"}, |
| 82 | + {(char *)"releaselevel", (char *)"'alpha', 'beta', 'candidate', or 'release'"}, |
| 83 | + {(char *)"standalone", (char *)"boolean indicating standalone mode usage"}, |
| 84 | + {(char *)"onefile", (char *)"boolean indicating standalone mode usage"}, |
| 85 | + {0} |
| 86 | + }; |
| 87 | + |
| 88 | + static PyStructSequence_Desc Nuitka_VersionInfoDesc = { |
| 89 | + (char *)"__nuitka_version__", /* name */ |
| 90 | + (char *)"__compiled__\\n\\nVersion information as a named tuple.", /* doc */ |
| 91 | + Nuitka_VersionInfoFields, /* fields */ |
| 92 | + 6 |
| 93 | + }; |
| 94 | + |
| 95 | + PyStructSequence_InitType(&Nuitka_VersionInfoType, &Nuitka_VersionInfoDesc); |
| 96 | + |
| 97 | + Nuitka_dunder_compiled_value = PyStructSequence_New(&Nuitka_VersionInfoType); |
| 98 | + assert(Nuitka_dunder_compiled_value != NULL); |
| 99 | + |
| 100 | + PyStructSequence_SET_ITEM(Nuitka_dunder_compiled_value, 0, PyInt_FromLong(1)); |
| 101 | + PyStructSequence_SET_ITEM(Nuitka_dunder_compiled_value, 1, PyInt_FromLong(6)); |
| 102 | + PyStructSequence_SET_ITEM(Nuitka_dunder_compiled_value, 2, PyInt_FromLong(3)); |
| 103 | + |
| 104 | + PyStructSequence_SET_ITEM(Nuitka_dunder_compiled_value, 3, Nuitka_String_FromString("release")); |
| 105 | + |
| 106 | +#ifdef _NUITKA_STANDALONE |
| 107 | + PyObject *is_standalone_mode = Py_True; |
| 108 | +#else |
| 109 | + PyObject *is_standalone_mode = Py_False; |
| 110 | +#endif |
| 111 | +#ifdef _NUITKA_ONEFILE_MODE |
| 112 | + PyObject *is_onefile_mode = Py_True; |
| 113 | +#else |
| 114 | + PyObject *is_onefile_mode = Py_False; |
| 115 | +#endif |
| 116 | + |
| 117 | + PyStructSequence_SET_ITEM(Nuitka_dunder_compiled_value, 4, is_standalone_mode); |
| 118 | + PyStructSequence_SET_ITEM(Nuitka_dunder_compiled_value, 5, is_onefile_mode); |
| 119 | + |
| 120 | + // Prevent users from creating the Nuitka version type object. |
| 121 | + Nuitka_VersionInfoType.tp_init = NULL; |
| 122 | + Nuitka_VersionInfoType.tp_new = NULL; |
| 123 | + |
| 124 | + setDistributionsMetadata(global_constants[1]); |
| 125 | +} |
| 126 | + |
| 127 | +// In debug mode we can check that the constants were not tampered with in any |
| 128 | +// given moment. We typically do it at program exit, but we can add extra calls |
| 129 | +// for sanity. |
| 130 | +#ifndef __NUITKA_NO_ASSERT__ |
| 131 | +void checkGlobalConstants(void) { |
| 132 | +// TODO: Ask constant code to check values. |
| 133 | + |
| 134 | +} |
| 135 | +#endif |
| 136 | + |
| 137 | +void createGlobalConstants(void) { |
| 138 | + if (_sentinel_value == NULL) { |
| 139 | +#if PYTHON_VERSION < 0x300 |
| 140 | + _sentinel_value = PyCObject_FromVoidPtr(NULL, NULL); |
| 141 | +#else |
| 142 | + // The NULL value is not allowed for a capsule, so use something else. |
| 143 | + _sentinel_value = PyCapsule_New((void *)27, "sentinel", NULL); |
| 144 | +#endif |
| 145 | + assert(_sentinel_value); |
| 146 | + |
| 147 | + _createGlobalConstants(); |
| 148 | + } |
| 149 | +} |
0 commit comments