3#ifndef __NUITKA_PRELUDE_H__
4#define __NUITKA_PRELUDE_H__
6#ifdef __NUITKA_NO_ASSERT__
11#include "nuitka/debug_settings.h"
21#include <patchlevel.h>
24#if PY_MICRO_VERSION < 16
25#define PYTHON_VERSION (PY_MAJOR_VERSION * 256 + PY_MINOR_VERSION * 16 + PY_MICRO_VERSION)
27#define PYTHON_VERSION (PY_MAJOR_VERSION * 256 + PY_MINOR_VERSION * 16 + 15)
34#define initproc python_init_proc
35#define initfunc python_init_func
36#define initstate python_initstate
41#pragma warning(disable : 4200)
42#pragma warning(disable : 4244)
47#include <frameobject.h>
49#include <methodobject.h>
53#if PYTHON_VERSION < 0x3a0
62#define NUITKA_MAY_BE_UNUSED __attribute__((__unused__))
64#define NUITKA_MAY_BE_UNUSED
69#if PYTHON_VERSION >= 0x3a0 && PYTHON_VERSION < 0x3c0
71#define Py_REFCNT(ob) (_PyObject_CAST(ob)->ob_refcnt)
76#define Py_SET_REFCNT(ob, refcnt) Py_REFCNT(ob) = refcnt
81#define MIN_PYCORE_PYTHON_VERSION 0x380
83#define MIN_PYCORE_PYTHON_VERSION 0x371
86#if PYTHON_VERSION >= MIN_PYCORE_PYTHON_VERSION
87#define NUITKA_USE_PYCORE_THREAD_STATE
90#ifdef NUITKA_USE_PYCORE_THREAD_STATE
95#if PYTHON_VERSION < 0x380
98#undef Py_INTERNAL_PYSTATE_H
99#include <internal/pystate.h>
103extern _PyRuntimeState _PyRuntime;
106#if PYTHON_VERSION >= 0x3c0
107#include <internal/pycore_runtime.h>
108#include <internal/pycore_typevarobject.h>
110static inline size_t Nuitka_static_builtin_index_get(PyTypeObject *self) {
return (
size_t)self->tp_subclasses - 1; }
113#if PYTHON_VERSION < 0x3d0
114#define managed_static_type_state static_builtin_state
116static inline managed_static_type_state *Nuitka_static_builtin_state_get(PyInterpreterState *interp,
117 PyTypeObject *self) {
118 return &(interp->types.builtins[Nuitka_static_builtin_index_get(self)]);
121static inline managed_static_type_state *Nuitka_static_builtin_state_get(PyInterpreterState *interp,
122 PyTypeObject *self) {
123 return &(interp->types.builtins.initialized[Nuitka_static_builtin_index_get(self)]);
127NUITKA_MAY_BE_UNUSED
static inline managed_static_type_state *Nuitka_PyStaticType_GetState(PyInterpreterState *interp,
128 PyTypeObject *self) {
129 assert(self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN);
130 return Nuitka_static_builtin_state_get(interp, self);
133#define _PyStaticType_GetState(interp, self) Nuitka_PyStaticType_GetState(interp, self)
136#include <internal/pycore_pystate.h>
139#if PYTHON_VERSION >= 0x390
140#include <internal/pycore_ceval.h>
141#include <internal/pycore_interp.h>
142#include <internal/pycore_runtime.h>
145#if PYTHON_VERSION >= 0x380
146#include <cpython/initconfig.h>
147#include <internal/pycore_initconfig.h>
148#include <internal/pycore_pathconfig.h>
149#include <internal/pycore_pyerrors.h>
152#if PYTHON_VERSION >= 0x3a0
153#include <internal/pycore_long.h>
154#include <internal/pycore_unionobject.h>
157#if PYTHON_VERSION >= 0x3b0
158#include <internal/pycore_dict.h>
159#include <internal/pycore_frame.h>
160#include <internal/pycore_gc.h>
164#if PYTHON_VERSION >= 0x3b0
165#if PYTHON_VERSION >= 0x3d0
166#include <internal/pycore_opcode_utils.h>
167#include <opcode_ids.h>
169#include <internal/pycore_opcode.h>
175#if PYTHON_VERSION >= 0x3c0
176#include <cpython/code.h>
179#if PYTHON_VERSION < 0x3c0
181#undef PyThreadState_GET
182#define _PyThreadState_Current _PyRuntime.gilstate.tstate_current
183#define PyThreadState_GET() ((PyThreadState *)_Py_atomic_load_relaxed(&_PyThreadState_Current))
186#if PYTHON_VERSION >= 0x380
187#undef _PyObject_LookupSpecial
188#include <internal/pycore_object.h>
193#if PYTHON_VERSION >= 0x3d0
194#include <internal/pycore_critical_section.h>
195#include <internal/pycore_freelist.h>
196#include <internal/pycore_intrinsics.h>
197#include <internal/pycore_modsupport.h>
198#include <internal/pycore_parking_lot.h>
199#include <internal/pycore_pyatomic_ft_wrappers.h>
200#include <internal/pycore_setobject.h>
201#include <internal/pycore_time.h>
204#if PYTHON_VERSION >= 0x3e0
205#include <internal/pycore_interpframe.h>
206#include <internal/pycore_tuple.h>
207#include <internal/pycore_typedefs.h>
208#include <internal/pycore_unicodeobject.h>
220#if PYTHON_VERSION < 0x300
240#define likely(x) HEDLEY_LIKELY(x)
241#define unlikely(x) HEDLEY_UNLIKELY(x)
247#define NUITKA_NO_RETURN HEDLEY_NO_RETURN
250#ifndef __NUITKA_NO_ASSERT__
251#define NUITKA_CANNOT_GET_HERE(NAME) \
252 PRINT_FORMAT("%s : %s\n", __FUNCTION__, #NAME); \
255#define NUITKA_CANNOT_GET_HERE(NAME) abort();
258#define NUITKA_ERROR_EXIT(NAME) \
259 PRINT_FORMAT("%s : %s\n", __FUNCTION__, #NAME); \
267#define NUITKA_DYNAMIC_ARRAY_DECL(VARIABLE_NAME, ELEMENT_TYPE, COUNT) \
268 ELEMENT_TYPE *VARIABLE_NAME = (ELEMENT_TYPE *)_alloca(sizeof(ELEMENT_TYPE) * (COUNT));
270#define NUITKA_DYNAMIC_ARRAY_DECL(VARIABLE_NAME, ELEMENT_TYPE, COUNT) ELEMENT_TYPE VARIABLE_NAME[COUNT];
276#if PYTHON_VERSION >= 0x300
277#define PyInt_AsLong PyLong_AsLong
278#define PyInt_FromSsize_t PyLong_FromSsize_t
280#define PyNumber_Int PyNumber_Long
282#define PyObject_Unicode PyObject_Str
289#if PYTHON_VERSION < 0x300
290#define PyUnicode_GET_LENGTH(x) (PyUnicode_GET_SIZE(x))
291#define Nuitka_String_AsString PyString_AsString
292#define Nuitka_String_AsString_Unchecked PyString_AS_STRING
293#define Nuitka_String_Check PyString_Check
294#define Nuitka_String_CheckExact PyString_CheckExact
295NUITKA_MAY_BE_UNUSED
static inline bool Nuitka_StringOrUnicode_CheckExact(PyObject *value) {
296 return PyString_CheckExact(value) || PyUnicode_CheckExact(value);
298#define Nuitka_StringObject PyStringObject
299#define Nuitka_String_FromString PyString_FromString
300#define Nuitka_String_FromStringAndSize PyString_FromStringAndSize
301#define Nuitka_String_FromFormat PyString_FromFormat
302#define PyUnicode_CHECK_INTERNED (0)
303NUITKA_MAY_BE_UNUSED
static Py_UNICODE *Nuitka_UnicodeAsWideString(PyObject *str, Py_ssize_t *size) {
306 if (!PyUnicode_Check(str)) {
309 unicode = PyObject_Unicode(str);
315 *size = (Py_ssize_t)PyUnicode_GET_LENGTH(unicode);
318 return PyUnicode_AsUnicode(unicode);
321#define Nuitka_String_AsString _PyUnicode_AsString
325#define _PyUnicode_UTF8(op) (((PyCompactUnicodeObject *)(op))->utf8)
326#define PyUnicode_UTF8(op) \
327 (assert(PyUnicode_IS_READY(op)), \
328 PyUnicode_IS_COMPACT_ASCII(op) ? ((char *)((PyASCIIObject *)(op) + 1)) : _PyUnicode_UTF8(op))
329#ifdef __NUITKA_NO_ASSERT__
330#define Nuitka_String_AsString_Unchecked PyUnicode_UTF8
332NUITKA_MAY_BE_UNUSED
static char const *Nuitka_String_AsString_Unchecked(PyObject *
object) {
333 char const *result = PyUnicode_UTF8(
object);
334 assert(result != NULL);
338#define Nuitka_String_Check PyUnicode_Check
339#define Nuitka_String_CheckExact PyUnicode_CheckExact
340#define Nuitka_StringOrUnicode_CheckExact PyUnicode_CheckExact
341#define Nuitka_StringObject PyUnicodeObject
342#define Nuitka_String_FromString PyUnicode_FromString
343#define Nuitka_String_FromStringAndSize PyUnicode_FromStringAndSize
344#define Nuitka_String_FromFormat PyUnicode_FromFormat
345#define Nuitka_UnicodeAsWideString PyUnicode_AsWideCharString
351#ifdef __NUITKA_NO_ASSERT__
352#define Nuitka_TypeLookup(x, y) _PyType_Lookup(x, y)
354NUITKA_MAY_BE_UNUSED
static PyObject *Nuitka_TypeLookup(PyTypeObject *type, PyObject *name) {
355 return _PyType_Lookup(type, name);
365#if PYTHON_VERSION < 0x300
366#define NUITKA_MODULE_ENTRY_FUNCTION void
368#define NUITKA_MODULE_ENTRY_FUNCTION PyObject *
371#if PYTHON_VERSION < 0x300
372typedef long Py_hash_t;
378#define NUITKA_CROSS_MODULE
379#define NUITKA_LOCAL_MODULE static
389#if (defined(_WIN32) || defined(__MSYS__)) && PYTHON_VERSION < 0x380
390#define Nuitka_GC_Track PyObject_GC_Track
391#define Nuitka_GC_UnTrack PyObject_GC_UnTrack
392#undef _PyObject_GC_TRACK
393#undef _PyObject_GC_UNTRACK
394#elif PYTHON_VERSION == 0x370
395#define Nuitka_GC_Track PyObject_GC_Track
396#define Nuitka_GC_UnTrack PyObject_GC_UnTrack
397#undef _PyObject_GC_TRACK
398#undef _PyObject_GC_UNTRACK
399#elif _NUITKA_MODULE_MODE && PYTHON_VERSION >= 0x370 && PYTHON_VERSION < 0x380
400#define Nuitka_GC_Track PyObject_GC_Track
401#define Nuitka_GC_UnTrack PyObject_GC_UnTrack
402#undef _PyObject_GC_TRACK
403#undef _PyObject_GC_UNTRACK
404#undef PyThreadState_GET
405#define PyThreadState_GET PyThreadState_Get
407#define Nuitka_GC_Track _PyObject_GC_TRACK
408#define Nuitka_GC_UnTrack _PyObject_GC_UNTRACK
411#if _NUITKA_EXPERIMENTAL_FAST_THREAD_GET && PYTHON_VERSION >= 0x300 && PYTHON_VERSION < 0x370
415#undef PyThreadState_GET
416extern PyThreadState *_PyThreadState_Current;
417#define PyThreadState_GET() (_PyThreadState_Current)
420#ifndef _NUITKA_FULL_COMPAT
424#undef Py_EnterRecursiveCall
425#define Py_EnterRecursiveCall(arg) (0)
426#undef Py_LeaveRecursiveCall
427#define Py_LeaveRecursiveCall()
430#if PYTHON_VERSION < 0x300
431#define TP_RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) ? (t)->tp_richcompare : NULL)
433#define TP_RICHCOMPARE(t) ((t)->tp_richcompare)
438#define Py_ABS(x) ((x) < 0 ? -(x) : (x))
442#define Py_MIN(x, y) (((x) > (y)) ? (y) : (x))
446#define Py_MAX(x, y) (((x) > (y)) ? (x) : (y))
450#define Py_SET_SIZE(op, size) ((PyVarObject *)(op))->ob_size = size
453#ifndef PyFloat_SET_DOUBLE
454#define PyFloat_SET_DOUBLE(op, value) ((PyFloatObject *)(op))->ob_fval = value
458static inline PyObject *_Py_NewRef(PyObject *obj) {
463static inline PyObject *_Py_XNewRef(PyObject *obj) {
468#define Py_NewRef(obj) _Py_NewRef((PyObject *)(obj))
469#define Py_XNewRef(obj) _Py_XNewRef((PyObject *)(obj))
473#if PYTHON_VERSION < 0x300
474#define NuitkaType_HasFeatureClass(descr) (PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_HAVE_CLASS))
476#define NuitkaType_HasFeatureClass(descr) (1)
481#if PYTHON_VERSION < 0x3d0
482#define FT_ATOMIC_LOAD_PTR(value) value
483#define FT_ATOMIC_STORE_PTR(value, new_value) value = new_value
484#define FT_ATOMIC_LOAD_SSIZE(value) value
485#define FT_ATOMIC_LOAD_SSIZE_ACQUIRE(value) value
486#define FT_ATOMIC_LOAD_SSIZE_RELAXED(value) value
487#define FT_ATOMIC_STORE_PTR(value, new_value) value = new_value
488#define FT_ATOMIC_LOAD_PTR_ACQUIRE(value) value
489#define FT_ATOMIC_LOAD_UINTPTR_ACQUIRE(value) value
490#define FT_ATOMIC_LOAD_PTR_RELAXED(value) value
491#define FT_ATOMIC_LOAD_UINT8(value) value
492#define FT_ATOMIC_STORE_UINT8(value, new_value) value = new_value
493#define FT_ATOMIC_LOAD_UINT8_RELAXED(value) value
494#define FT_ATOMIC_LOAD_UINT16_RELAXED(value) value
495#define FT_ATOMIC_LOAD_UINT32_RELAXED(value) value
496#define FT_ATOMIC_LOAD_ULONG_RELAXED(value) value
497#define FT_ATOMIC_STORE_PTR_RELAXED(value, new_value) value = new_value
498#define FT_ATOMIC_STORE_PTR_RELEASE(value, new_value) value = new_value
499#define FT_ATOMIC_STORE_UINTPTR_RELEASE(value, new_value) value = new_value
500#define FT_ATOMIC_STORE_SSIZE_RELAXED(value, new_value) value = new_value
501#define FT_ATOMIC_STORE_UINT8_RELAXED(value, new_value) value = new_value
502#define FT_ATOMIC_STORE_UINT16_RELAXED(value, new_value) value = new_value
503#define FT_ATOMIC_STORE_UINT32_RELAXED(value, new_value) value = new_value
505#define Py_BEGIN_CRITICAL_SECTION(mut) {
506#define Py_BEGIN_CRITICAL_SECTION2(m1, m2) {
507#define Py_BEGIN_CRITICAL_SECTION_MUT(mut) {
508#define Py_BEGIN_CRITICAL_SECTION2_MUT(m1, m2) {
509#define Py_END_CRITICAL_SECTION() }
511#define Py_BEGIN_CRITICAL_SECTION_SEQUENCE_FAST(original) {
512#define Py_END_CRITICAL_SECTION_SEQUENCE_FAST() }
513#define _Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED(mutex)
514#define _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op)
519extern bool Nuitka_Type_IsSubtype(PyTypeObject *a, PyTypeObject *b);
521#include "nuitka/allocator.h"
522#include "nuitka/exceptions.h"
525#if PYTHON_VERSION < 0x300
526#include <longintrepr.h>
528#if PYTHON_VERSION < 0x270
530typedef signed int sdigit;
535typedef long nuitka_digit;
537#include "nuitka/helpers.h"
539#include "nuitka/compiled_frame.h"
541#include "nuitka/compiled_cell.h"
543#include "nuitka/compiled_function.h"
546extern PyObject *Nuitka_sentinel_value;
549extern PyObject *Nuitka_dunder_compiled_value;
551#include "nuitka/compiled_generator.h"
553#include "nuitka/compiled_method.h"
555#if PYTHON_VERSION >= 0x350
556#include "nuitka/compiled_coroutine.h"
559#if PYTHON_VERSION >= 0x360
560#include "nuitka/compiled_asyncgen.h"
563#include "nuitka/filesystem_paths.h"
564#include "nuitka/safe_string_ops.h"
566#include "nuitka/jit_sources.h"
568#if _NUITKA_EXPERIMENTAL_WRITEABLE_CONSTANTS
569#include "nuitka_data_decoder.h"
571#define DECODE(x) assert(x)
572#define UN_TRANSLATE(x) (x)
575#if _NUITKA_EXPERIMENTAL_FILE_TRACING
576#include "nuitka_file_tracer.h"
578#if PYTHON_VERSION < 0x300
579#define TRACE_FILE_OPEN(tstate, x, y, z, r) (false)
581#define TRACE_FILE_OPEN(tstate, x, y, z, a, b, c, d, e, r) (false)
583#define TRACE_FILE_READ(tstate, x, y) (false)
585#define TRACE_FILE_EXISTS(tstate, x, y) (false)
586#define TRACE_FILE_ISFILE(tstate, x, y) (false)
587#define TRACE_FILE_ISDIR(tstate, x, y) (false)
589#define TRACE_FILE_LISTDIR(tstate, x, y) (false)
591#define TRACE_FILE_STAT(tstate, x, y, z, r) (false)
595#if _NUITKA_EXPERIMENTAL_INIT_PROGRAM
596#include "nuitka_init_program.h"
598#define NUITKA_INIT_PROGRAM_EARLY(argc, argv)
599#define NUITKA_INIT_PROGRAM_LATE(module_name)
602#if _NUITKA_EXPERIMENTAL_EXIT_PROGRAM
603#include "nuitka_exit_program.h"
605#define NUITKA_FINALIZE_PROGRAM(tstate)
609#ifndef PyCFunction_CheckExact
610#define PyCFunction_CheckExact PyCFunction_Check
613#ifdef _NUITKA_EXPERIMENTAL_DUMP_C_TRACEBACKS
614extern void INIT_C_BACKTRACES(
void);
615extern void DUMP_C_BACKTRACE(
void);
618#if _NUITKA_PLUGIN_THEMIDA_ENABLED
619#include "nuitka_themida.h"