Nuitka
The Python compiler
Loading...
Searching...
No Matches
builtins.h
1// Copyright 2025, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
2
3#ifndef __NUITKA_BUILTINS_H__
4#define __NUITKA_BUILTINS_H__
5
6extern PyModuleObject *builtin_module;
7extern PyDictObject *dict_builtin;
8
9#include "nuitka/calling.h"
10
11NUITKA_MAY_BE_UNUSED static PyObject *LOOKUP_BUILTIN(PyObject *name) {
12 CHECK_OBJECT(dict_builtin);
13 CHECK_OBJECT(name);
14 assert(Nuitka_String_CheckExact(name));
15
16 PyObject *result = GET_STRING_DICT_VALUE(dict_builtin, (Nuitka_StringObject *)name);
17
18 // This is assumed to not fail, abort if it does.
19 if (unlikely(result == NULL)) {
20 PyErr_PrintEx(0);
21 Py_Exit(1);
22 }
23
24 CHECK_OBJECT(result);
25
26 return result;
27}
28
29// Returns a reference.
30NUITKA_MAY_BE_UNUSED static PyObject *LOOKUP_BUILTIN_STR(char const *name) {
31 CHECK_OBJECT(dict_builtin);
32
33 PyObject *result = PyDict_GetItemString((PyObject *)dict_builtin, name);
34
35 // This is assumed to not fail, abort if it does.
36 if (unlikely(result == NULL)) {
37 PyErr_PrintEx(0);
38 Py_Exit(1);
39 }
40
41 CHECK_OBJECT(result);
42
43 Py_INCREF(result);
44 return result;
45}
46
47extern void _initBuiltinModule(void);
48
49#define NUITKA_DECLARE_BUILTIN(name) extern PyObject *_python_original_builtin_value_##name;
50#define NUITKA_DEFINE_BUILTIN(name) PyObject *_python_original_builtin_value_##name = NULL;
51#define NUITKA_ASSIGN_BUILTIN(name) \
52 if (_python_original_builtin_value_##name == NULL) \
53 _python_original_builtin_value_##name = LOOKUP_BUILTIN_STR(#name);
54#define NUITKA_UPDATE_BUILTIN(name, value) _python_original_builtin_value_##name = value;
55#define NUITKA_ACCESS_BUILTIN(name) (_python_original_builtin_value_##name)
56
57#if !_NUITKA_MODULE_MODE
58// Original builtin values, currently only used for assertions.
59NUITKA_DECLARE_BUILTIN(type);
60NUITKA_DECLARE_BUILTIN(len);
61NUITKA_DECLARE_BUILTIN(range);
62NUITKA_DECLARE_BUILTIN(repr);
63NUITKA_DECLARE_BUILTIN(int);
64NUITKA_DECLARE_BUILTIN(iter);
65#if PYTHON_VERSION < 0x300
66NUITKA_DECLARE_BUILTIN(long);
67#endif
68
69extern void _initBuiltinOriginalValues(void);
70#endif
71
72// Avoid the casts needed for older Python, as it's easily forgotten and
73// potentially have our own better implementation later. Gives no reference.
74// TODO: Can do it ourselves once DICT_GET_ITEM_WITH_ERROR becomes available.
75NUITKA_MAY_BE_UNUSED static PyObject *Nuitka_SysGetObject(char const *name) { return PySys_GetObject((char *)name); }
76
77NUITKA_MAY_BE_UNUSED static void Nuitka_SysSetObject(char const *name, PyObject *value) {
78 // TODO: Check error in debug mode at least.
79 PySys_SetObject((char *)name, value);
80}
81
82#endif
83
84// Part of "Nuitka", an optimizing Python compiler that is compatible and
85// integrates with CPython, but also works on its own.
86//
87// Licensed under the Apache License, Version 2.0 (the "License");
88// you may not use this file except in compliance with the License.
89// You may obtain a copy of the License at
90//
91// http://www.apache.org/licenses/LICENSE-2.0
92//
93// Unless required by applicable law or agreed to in writing, software
94// distributed under the License is distributed on an "AS IS" BASIS,
95// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
96// See the License for the specific language governing permissions and
97// limitations under the License.
Definition helpers.h:10