Nuitka
The Python compiler
Loading...
Searching...
No Matches
importing.h
1// Copyright 2025, 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 _PyInterpreterState_GET()->imports.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 assert(PyModule_Check(module));
82
83 return DICT_SET_ITEM(Nuitka_GetSysModules(), module_name, module);
84}
85
86// Add a module to the modules dictionary from name C string
87NUITKA_MAY_BE_UNUSED static bool Nuitka_SetModuleString(char const *module_name, PyObject *module) {
88 PyObject *module_name_object = Nuitka_String_FromString(module_name);
89 bool result = Nuitka_SetModule(module_name_object, module);
90 Py_DECREF(module_name_object);
91
92 return result;
93}
94
95// Remove a module to the modules dictionary from name object
96NUITKA_MAY_BE_UNUSED static bool Nuitka_DelModule(PyThreadState *tstate, PyObject *module_name) {
97 CHECK_OBJECT(module_name);
98
99 struct Nuitka_ExceptionPreservationItem saved_exception_state;
100 FETCH_ERROR_OCCURRED_STATE(tstate, &saved_exception_state);
101
102 bool result = DICT_REMOVE_ITEM(PyImport_GetModuleDict(), module_name);
103
104 RESTORE_ERROR_OCCURRED_STATE(tstate, &saved_exception_state);
105
106 return result;
107}
108
109// Remove a module to the modules dictionary from name C string
110NUITKA_MAY_BE_UNUSED static bool Nuitka_DelModuleString(PyThreadState *tstate, char const *module_name) {
111 PyObject *module_name_object = Nuitka_String_FromString(module_name);
112 bool result = Nuitka_DelModule(tstate, module_name_object);
113 Py_DECREF(module_name_object);
114
115 return result;
116}
117
118// Wrapper for PyModule_GetFilenameObject that has no error.
119NUITKA_MAY_BE_UNUSED static PyObject *Nuitka_GetFilenameObject(PyThreadState *tstate, PyObject *module) {
120#if PYTHON_VERSION < 0x300
121 PyObject *filename = LOOKUP_ATTRIBUTE(tstate, module, const_str_plain___file__);
122#else
123 PyObject *filename = PyModule_GetFilenameObject(module);
124#endif
125
126 if (unlikely(filename == NULL)) {
127 CLEAR_ERROR_OCCURRED(tstate);
128 filename = PyUnicode_FromString("unknown location");
129 }
130
131 return filename;
132}
133
134#endif
135
136// Part of "Nuitka", an optimizing Python compiler that is compatible and
137// integrates with CPython, but also works on its own.
138//
139// Licensed under the Apache License, Version 2.0 (the "License");
140// you may not use this file except in compliance with the License.
141// You may obtain a copy of the License at
142//
143// http://www.apache.org/licenses/LICENSE-2.0
144//
145// Unless required by applicable law or agreed to in writing, software
146// distributed under the License is distributed on an "AS IS" BASIS,
147// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
148// See the License for the specific language governing permissions and
149// limitations under the License.
Definition exceptions.h:712