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>
213#if PYTHON_VERSION < 0x300
233#define likely(x) HEDLEY_LIKELY(x)
234#define unlikely(x) HEDLEY_UNLIKELY(x)
240#define NUITKA_NO_RETURN HEDLEY_NO_RETURN
243#ifndef __NUITKA_NO_ASSERT__
244#define NUITKA_CANNOT_GET_HERE(NAME) \
245 PRINT_FORMAT("%s : %s\n", __FUNCTION__, #NAME); \
248#define NUITKA_CANNOT_GET_HERE(NAME) abort();
251#define NUITKA_ERROR_EXIT(NAME) \
252 PRINT_FORMAT("%s : %s\n", __FUNCTION__, #NAME); \
260#define NUITKA_DYNAMIC_ARRAY_DECL(VARIABLE_NAME, ELEMENT_TYPE, COUNT) \
261 ELEMENT_TYPE *VARIABLE_NAME = (ELEMENT_TYPE *)_alloca(sizeof(ELEMENT_TYPE) * (COUNT));
263#define NUITKA_DYNAMIC_ARRAY_DECL(VARIABLE_NAME, ELEMENT_TYPE, COUNT) ELEMENT_TYPE VARIABLE_NAME[COUNT];
269#if PYTHON_VERSION >= 0x300
270#define PyInt_AsLong PyLong_AsLong
271#define PyInt_FromSsize_t PyLong_FromSsize_t
273#define PyNumber_Int PyNumber_Long
275#define PyObject_Unicode PyObject_Str
282#if PYTHON_VERSION < 0x300
283#define PyUnicode_GET_LENGTH(x) (PyUnicode_GET_SIZE(x))
284#define Nuitka_String_AsString PyString_AsString
285#define Nuitka_String_AsString_Unchecked PyString_AS_STRING
286#define Nuitka_String_Check PyString_Check
287#define Nuitka_String_CheckExact PyString_CheckExact
288NUITKA_MAY_BE_UNUSED
static inline bool Nuitka_StringOrUnicode_CheckExact(PyObject *value) {
289 return PyString_CheckExact(value) || PyUnicode_CheckExact(value);
291#define Nuitka_StringObject PyStringObject
292#define Nuitka_String_FromString PyString_FromString
293#define Nuitka_String_FromStringAndSize PyString_FromStringAndSize
294#define Nuitka_String_FromFormat PyString_FromFormat
295#define PyUnicode_CHECK_INTERNED (0)
296NUITKA_MAY_BE_UNUSED
static Py_UNICODE *Nuitka_UnicodeAsWideString(PyObject *str, Py_ssize_t *size) {
299 if (!PyUnicode_Check(str)) {
302 unicode = PyObject_Unicode(str);
308 *size = (Py_ssize_t)PyUnicode_GET_LENGTH(unicode);
311 return PyUnicode_AsUnicode(unicode);
314#define Nuitka_String_AsString _PyUnicode_AsString
318#define _PyUnicode_UTF8(op) (((PyCompactUnicodeObject *)(op))->utf8)
319#define PyUnicode_UTF8(op) \
320 (assert(PyUnicode_IS_READY(op)), \
321 PyUnicode_IS_COMPACT_ASCII(op) ? ((char *)((PyASCIIObject *)(op) + 1)) : _PyUnicode_UTF8(op))
322#ifdef __NUITKA_NO_ASSERT__
323#define Nuitka_String_AsString_Unchecked PyUnicode_UTF8
325NUITKA_MAY_BE_UNUSED
static char const *Nuitka_String_AsString_Unchecked(PyObject *
object) {
326 char const *result = PyUnicode_UTF8(
object);
327 assert(result != NULL);
331#define Nuitka_String_Check PyUnicode_Check
332#define Nuitka_String_CheckExact PyUnicode_CheckExact
333#define Nuitka_StringOrUnicode_CheckExact PyUnicode_CheckExact
334#define Nuitka_StringObject PyUnicodeObject
335#define Nuitka_String_FromString PyUnicode_FromString
336#define Nuitka_String_FromStringAndSize PyUnicode_FromStringAndSize
337#define Nuitka_String_FromFormat PyUnicode_FromFormat
338#define Nuitka_UnicodeAsWideString PyUnicode_AsWideCharString
344#ifdef __NUITKA_NO_ASSERT__
345#define Nuitka_TypeLookup(x, y) _PyType_Lookup(x, y)
347NUITKA_MAY_BE_UNUSED
static PyObject *Nuitka_TypeLookup(PyTypeObject *type, PyObject *name) {
348 return _PyType_Lookup(type, name);
358#if PYTHON_VERSION < 0x300
359#define NUITKA_MODULE_ENTRY_FUNCTION void
361#define NUITKA_MODULE_ENTRY_FUNCTION PyObject *
364#if PYTHON_VERSION < 0x300
365typedef long Py_hash_t;
371#define NUITKA_CROSS_MODULE
372#define NUITKA_LOCAL_MODULE static
382#if (defined(_WIN32) || defined(__MSYS__)) && PYTHON_VERSION < 0x380
383#define Nuitka_GC_Track PyObject_GC_Track
384#define Nuitka_GC_UnTrack PyObject_GC_UnTrack
385#undef _PyObject_GC_TRACK
386#undef _PyObject_GC_UNTRACK
387#elif PYTHON_VERSION == 0x370
388#define Nuitka_GC_Track PyObject_GC_Track
389#define Nuitka_GC_UnTrack PyObject_GC_UnTrack
390#undef _PyObject_GC_TRACK
391#undef _PyObject_GC_UNTRACK
392#elif _NUITKA_MODULE_MODE && PYTHON_VERSION >= 0x370 && PYTHON_VERSION < 0x380
393#define Nuitka_GC_Track PyObject_GC_Track
394#define Nuitka_GC_UnTrack PyObject_GC_UnTrack
395#undef _PyObject_GC_TRACK
396#undef _PyObject_GC_UNTRACK
397#undef PyThreadState_GET
398#define PyThreadState_GET PyThreadState_Get
400#define Nuitka_GC_Track _PyObject_GC_TRACK
401#define Nuitka_GC_UnTrack _PyObject_GC_UNTRACK
404#if _NUITKA_EXPERIMENTAL_FAST_THREAD_GET && PYTHON_VERSION >= 0x300 && PYTHON_VERSION < 0x370
408#undef PyThreadState_GET
409extern PyThreadState *_PyThreadState_Current;
410#define PyThreadState_GET() (_PyThreadState_Current)
413#ifndef _NUITKA_FULL_COMPAT
417#undef Py_EnterRecursiveCall
418#define Py_EnterRecursiveCall(arg) (0)
419#undef Py_LeaveRecursiveCall
420#define Py_LeaveRecursiveCall()
423#if PYTHON_VERSION < 0x300
424#define TP_RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) ? (t)->tp_richcompare : NULL)
426#define TP_RICHCOMPARE(t) ((t)->tp_richcompare)
431#define Py_ABS(x) ((x) < 0 ? -(x) : (x))
435#define Py_MIN(x, y) (((x) > (y)) ? (y) : (x))
439#define Py_MAX(x, y) (((x) > (y)) ? (x) : (y))
443#define Py_SET_SIZE(op, size) ((PyVarObject *)(op))->ob_size = size
446#ifndef PyFloat_SET_DOUBLE
447#define PyFloat_SET_DOUBLE(op, value) ((PyFloatObject *)(op))->ob_fval = value
451static inline PyObject *_Py_NewRef(PyObject *obj) {
456static inline PyObject *_Py_XNewRef(PyObject *obj) {
461#define Py_NewRef(obj) _Py_NewRef((PyObject *)(obj))
462#define Py_XNewRef(obj) _Py_XNewRef((PyObject *)(obj))
466#if PYTHON_VERSION < 0x300
467#define NuitkaType_HasFeatureClass(descr) (PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_HAVE_CLASS))
469#define NuitkaType_HasFeatureClass(descr) (1)
474#if PYTHON_VERSION < 0x3d0
475#define FT_ATOMIC_LOAD_PTR(value) value
476#define FT_ATOMIC_STORE_PTR(value, new_value) value = new_value
477#define FT_ATOMIC_LOAD_SSIZE(value) value
478#define FT_ATOMIC_LOAD_SSIZE_ACQUIRE(value) value
479#define FT_ATOMIC_LOAD_SSIZE_RELAXED(value) value
480#define FT_ATOMIC_STORE_PTR(value, new_value) value = new_value
481#define FT_ATOMIC_LOAD_PTR_ACQUIRE(value) value
482#define FT_ATOMIC_LOAD_UINTPTR_ACQUIRE(value) value
483#define FT_ATOMIC_LOAD_PTR_RELAXED(value) value
484#define FT_ATOMIC_LOAD_UINT8(value) value
485#define FT_ATOMIC_STORE_UINT8(value, new_value) value = new_value
486#define FT_ATOMIC_LOAD_UINT8_RELAXED(value) value
487#define FT_ATOMIC_LOAD_UINT16_RELAXED(value) value
488#define FT_ATOMIC_LOAD_UINT32_RELAXED(value) value
489#define FT_ATOMIC_LOAD_ULONG_RELAXED(value) value
490#define FT_ATOMIC_STORE_PTR_RELAXED(value, new_value) value = new_value
491#define FT_ATOMIC_STORE_PTR_RELEASE(value, new_value) value = new_value
492#define FT_ATOMIC_STORE_UINTPTR_RELEASE(value, new_value) value = new_value
493#define FT_ATOMIC_STORE_SSIZE_RELAXED(value, new_value) value = new_value
494#define FT_ATOMIC_STORE_UINT8_RELAXED(value, new_value) value = new_value
495#define FT_ATOMIC_STORE_UINT16_RELAXED(value, new_value) value = new_value
496#define FT_ATOMIC_STORE_UINT32_RELAXED(value, new_value) value = new_value
498#define Py_BEGIN_CRITICAL_SECTION(mut) {
499#define Py_BEGIN_CRITICAL_SECTION2(m1, m2) {
500#define Py_BEGIN_CRITICAL_SECTION_MUT(mut) {
501#define Py_BEGIN_CRITICAL_SECTION2_MUT(m1, m2) {
502#define Py_END_CRITICAL_SECTION() }
504#define Py_BEGIN_CRITICAL_SECTION_SEQUENCE_FAST(original) {
505#define Py_END_CRITICAL_SECTION_SEQUENCE_FAST() }
506#define _Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED(mutex)
507#define _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op)
512extern bool Nuitka_Type_IsSubtype(PyTypeObject *a, PyTypeObject *b);
514#include "nuitka/allocator.h"
515#include "nuitka/exceptions.h"
518#if PYTHON_VERSION < 0x300
519#include <longintrepr.h>
521#if PYTHON_VERSION < 0x270
523typedef signed int sdigit;
528typedef long nuitka_digit;
530#include "nuitka/helpers.h"
532#include "nuitka/compiled_frame.h"
534#include "nuitka/compiled_cell.h"
536#include "nuitka/compiled_function.h"
539extern PyObject *Nuitka_sentinel_value;
542extern PyObject *Nuitka_dunder_compiled_value;
544#include "nuitka/compiled_generator.h"
546#include "nuitka/compiled_method.h"
548#if PYTHON_VERSION >= 0x350
549#include "nuitka/compiled_coroutine.h"
552#if PYTHON_VERSION >= 0x360
553#include "nuitka/compiled_asyncgen.h"
556#include "nuitka/filesystem_paths.h"
557#include "nuitka/safe_string_ops.h"
559#include "nuitka/jit_sources.h"
561#if _NUITKA_EXPERIMENTAL_WRITEABLE_CONSTANTS
562#include "nuitka_data_decoder.h"
564#define DECODE(x) assert(x)
565#define UN_TRANSLATE(x) (x)
568#if _NUITKA_EXPERIMENTAL_FILE_TRACING
569#include "nuitka_file_tracer.h"
571#if PYTHON_VERSION < 0x300
572#define TRACE_FILE_OPEN(tstate, x, y, z, r) (false)
574#define TRACE_FILE_OPEN(tstate, x, y, z, a, b, c, d, e, r) (false)
576#define TRACE_FILE_READ(tstate, x, y) (false)
578#define TRACE_FILE_EXISTS(tstate, x, y) (false)
579#define TRACE_FILE_ISFILE(tstate, x, y) (false)
580#define TRACE_FILE_ISDIR(tstate, x, y) (false)
582#define TRACE_FILE_LISTDIR(tstate, x, y) (false)
584#define TRACE_FILE_STAT(tstate, x, y, z, r) (false)
588#if _NUITKA_EXPERIMENTAL_INIT_PROGRAM
589#include "nuitka_init_program.h"
591#define NUITKA_INIT_PROGRAM_EARLY(argc, argv)
592#define NUITKA_INIT_PROGRAM_LATE(module_name)
595#if _NUITKA_EXPERIMENTAL_EXIT_PROGRAM
596#include "nuitka_exit_program.h"
598#define NUITKA_FINALIZE_PROGRAM(tstate)
602#ifndef PyCFunction_CheckExact
603#define PyCFunction_CheckExact PyCFunction_Check
606#ifdef _NUITKA_EXPERIMENTAL_DUMP_C_TRACEBACKS
607extern void INIT_C_BACKTRACES(
void);
608extern void DUMP_C_BACKTRACE(
void);