Nuitka
The Python compiler
Loading...
Searching...
No Matches
importing.h
1// Copyright 2026, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
2
3#ifndef __NUITKA_IMPORTING_H__
4#define __NUITKA_IMPORTING_H__
5
6/* These are for the built-in import.
7 *
8 * They call the real thing with varying amount of arguments. For keyword
9 * calls using default values, the _KW helper is used.
10 *
11 */
12extern PyObject *IMPORT_MODULE1(PyThreadState *tstate, PyObject *module_name);
13extern PyObject *IMPORT_MODULE2(PyThreadState *tstate, PyObject *module_name, PyObject *globals);
14extern PyObject *IMPORT_MODULE3(PyThreadState *tstate, PyObject *module_name, PyObject *globals, PyObject *locals);
15extern PyObject *IMPORT_MODULE4(PyThreadState *tstate, PyObject *module_name, PyObject *globals, PyObject *locals,
16 PyObject *import_items);
17extern PyObject *IMPORT_MODULE5(PyThreadState *tstate, PyObject *module_name, PyObject *globals, PyObject *locals,
18 PyObject *import_items, PyObject *level);
19
20extern PyObject *IMPORT_MODULE_KW(PyThreadState *tstate, PyObject *module_name, PyObject *globals, PyObject *locals,
21 PyObject *import_items, PyObject *level);
22
23extern bool IMPORT_MODULE_STAR(PyThreadState *tstate, PyObject *target, bool is_module, PyObject *module);
24
25// Fixed import name to be imported and used by value name.
26extern PyObject *IMPORT_MODULE_FIXED(PyThreadState *tstate, PyObject *module_name, PyObject *value_name);
27
28// Import an embedded module directly.
29extern PyObject *IMPORT_EMBEDDED_MODULE(PyThreadState *tstate, char const *name);
30
31// Execute a module, the module object is prepared empty, but with __name__.
32extern PyObject *EXECUTE_EMBEDDED_MODULE(PyThreadState *tstate, PyObject *module);
33
34// Import a name from a module.
35extern PyObject *IMPORT_NAME_FROM_MODULE(PyThreadState *tstate, PyObject *module, PyObject *import_name);
36
37// import a name from a module, potentially making an import of it if necessary.
38#if PYTHON_VERSION >= 0x350
39extern PyObject *IMPORT_NAME_OR_MODULE(PyThreadState *tstate, PyObject *module, PyObject *globals,
40 PyObject *import_name, PyObject *level);
41#endif
42
43#if PYTHON_VERSION >= 0x300
44extern PyObject *getImportLibBootstrapModule(void);
45#endif
46
47// Replacement for "PyImport_GetModuleDict"
48NUITKA_MAY_BE_UNUSED static PyObject *Nuitka_GetSysModules(void) {
49#if PYTHON_VERSION < 0x390
50 return PyThreadState_GET()->interp->modules;
51#elif PYTHON_VERSION < 0x3c0
52 return _PyInterpreterState_GET()->modules;
53#else
54 return Nuitka_PyInterpreterState_GetImportsState(_PyInterpreterState_GET())->modules;
55#endif
56}
57
58// Check if a module is in "sys.modules"
59NUITKA_MAY_BE_UNUSED static bool Nuitka_HasModule(PyThreadState *tstate, PyObject *module_name) {
60 return DICT_HAS_ITEM(tstate, Nuitka_GetSysModules(), module_name) == 1;
61}
62
63// Replacement for "PyImport_GetModule" working across all versions and less checks.
64NUITKA_MAY_BE_UNUSED static PyObject *Nuitka_GetModule(PyThreadState *tstate, PyObject *module_name) {
65 return DICT_GET_ITEM1(tstate, Nuitka_GetSysModules(), module_name);
66}
67
68// Replacement for PyImport_GetModule working across all versions and less checks.
69NUITKA_MAY_BE_UNUSED static PyObject *Nuitka_GetModuleString(PyThreadState *tstate, char const *module_name) {
70 PyObject *module_name_object = Nuitka_String_FromString(module_name);
71 PyObject *result = Nuitka_GetModule(tstate, module_name_object);
72 Py_DECREF(module_name_object);
73
74 return result;
75}
76
77// Add a module to the modules dictionary from name object
78NUITKA_MAY_BE_UNUSED static bool Nuitka_SetModule(PyObject *module_name, PyObject *module) {
79 CHECK_OBJECT(module_name);
80 CHECK_OBJECT(module);
81
82 return DICT_SET_ITEM(Nuitka_GetSysModules(), module_name, module);
83}
84
85// Add a module to the modules dictionary from name C string
86NUITKA_MAY_BE_UNUSED static bool Nuitka_SetModuleString(char const *module_name, PyObject *module) {
87 PyObject *module_name_object = Nuitka_String_FromString(module_name);
88 bool result = Nuitka_SetModule(module_name_object, module);
89 Py_DECREF(module_name_object);
90
91 return result;
92}
93
94// Remove a module to the modules dictionary from name object
95NUITKA_MAY_BE_UNUSED static bool Nuitka_DelModule(PyThreadState *tstate, PyObject *module_name) {
96 CHECK_OBJECT(module_name);
97
98 struct Nuitka_ExceptionPreservationItem saved_exception_state;
99 FETCH_ERROR_OCCURRED_STATE(tstate, &saved_exception_state);
100
101 bool result = DICT_REMOVE_ITEM(PyImport_GetModuleDict(), module_name);
102
103 RESTORE_ERROR_OCCURRED_STATE(tstate, &saved_exception_state);
104
105 return result;
106}
107
108// Remove a module to the modules dictionary from name C string
109NUITKA_MAY_BE_UNUSED static bool Nuitka_DelModuleString(PyThreadState *tstate, char const *module_name) {
110 PyObject *module_name_object = Nuitka_String_FromString(module_name);
111 bool result = Nuitka_DelModule(tstate, module_name_object);
112 Py_DECREF(module_name_object);
113
114 return result;
115}
116
117// Wrapper for PyModule_GetFilenameObject that has no error.
118NUITKA_MAY_BE_UNUSED static PyObject *Nuitka_GetFilenameObject(PyThreadState *tstate, PyObject *module) {
119#if PYTHON_VERSION < 0x300
120 PyObject *filename = LOOKUP_ATTRIBUTE(tstate, module, const_str_plain___file__);
121#else
122 PyObject *filename = PyModule_GetFilenameObject(module);
123#endif
124
125 if (unlikely(filename == NULL)) {
126 CLEAR_ERROR_OCCURRED(tstate);
127 filename = PyUnicode_FromString("unknown location");
128 }
129
130 return filename;
131}
132
133#endif
134
135// Part of "Nuitka", an optimizing Python compiler that is compatible and
136// integrates with CPython, but also works on its own.
137//
138// Licensed under the GNU Affero General Public License, Version 3 (the "License");
139// you may not use this file except in compliance with the License.
140// You may obtain a copy of the License at
141//
142// http://www.gnu.org/licenses/agpl.txt
143//
144// Unless required by applicable law or agreed to in writing, software
145// distributed under the License is distributed on an "AS IS" BASIS,
146// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
147// See the License for the specific language governing permissions and
148// limitations under the License.
Definition exceptions.h:712