Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-19 09:50:47

0001 #ifndef Py_LIMITED_API
0002 #ifndef Py_INTERNAL_IMPORT_H
0003 #define Py_INTERNAL_IMPORT_H
0004 #ifdef __cplusplus
0005 extern "C" {
0006 #endif
0007 
0008 #ifndef Py_BUILD_CORE
0009 #  error "this header requires Py_BUILD_CORE define"
0010 #endif
0011 
0012 #include "pycore_lock.h"          // PyMutex
0013 #include "pycore_hashtable.h"     // _Py_hashtable_t
0014 
0015 extern int _PyImport_IsInitialized(PyInterpreterState *);
0016 
0017 // Export for 'pyexpat' shared extension
0018 PyAPI_FUNC(int) _PyImport_SetModule(PyObject *name, PyObject *module);
0019 
0020 extern int _PyImport_SetModuleString(const char *name, PyObject* module);
0021 
0022 extern void _PyImport_AcquireLock(PyInterpreterState *interp);
0023 extern void _PyImport_ReleaseLock(PyInterpreterState *interp);
0024 extern void _PyImport_ReInitLock(PyInterpreterState *interp);
0025 
0026 // This is used exclusively for the sys and builtins modules:
0027 extern int _PyImport_FixupBuiltin(
0028     PyThreadState *tstate,
0029     PyObject *mod,
0030     const char *name,            /* UTF-8 encoded string */
0031     PyObject *modules
0032     );
0033 
0034 // Export for many shared extensions, like '_json'
0035 PyAPI_FUNC(PyObject*) _PyImport_GetModuleAttr(PyObject *, PyObject *);
0036 
0037 // Export for many shared extensions, like '_datetime'
0038 PyAPI_FUNC(PyObject*) _PyImport_GetModuleAttrString(const char *, const char *);
0039 
0040 
0041 struct _import_runtime_state {
0042     /* The builtin modules (defined in config.c). */
0043     struct _inittab *inittab;
0044     /* The most recent value assigned to a PyModuleDef.m_base.m_index.
0045        This is incremented each time PyModuleDef_Init() is called,
0046        which is just about every time an extension module is imported.
0047        See PyInterpreterState.modules_by_index for more info. */
0048     Py_ssize_t last_module_index;
0049     struct {
0050         /* A lock to guard the cache. */
0051         PyMutex mutex;
0052         /* The actual cache of (filename, name, PyModuleDef) for modules.
0053            Only legacy (single-phase init) extension modules are added
0054            and only if they support multiple initialization (m_size >- 0)
0055            or are imported in the main interpreter.
0056            This is initialized lazily in fix_up_extension() in import.c.
0057            Modules are added there and looked up in _imp.find_extension(). */
0058         _Py_hashtable_t *hashtable;
0059     } extensions;
0060     /* Package context -- the full module name for package imports */
0061     const char * pkgcontext;
0062 };
0063 
0064 struct _import_state {
0065     /* cached sys.modules dictionary */
0066     PyObject *modules;
0067     /* This is the list of module objects for all legacy (single-phase init)
0068        extension modules ever loaded in this process (i.e. imported
0069        in this interpreter or in any other).  Py_None stands in for
0070        modules that haven't actually been imported in this interpreter.
0071 
0072        A module's index (PyModuleDef.m_base.m_index) is used to look up
0073        the corresponding module object for this interpreter, if any.
0074        (See PyState_FindModule().)  When any extension module
0075        is initialized during import, its moduledef gets initialized by
0076        PyModuleDef_Init(), and the first time that happens for each
0077        PyModuleDef, its index gets set to the current value of
0078        a global counter (see _PyRuntimeState.imports.last_module_index).
0079        The entry for that index in this interpreter remains unset until
0080        the module is actually imported here.  (Py_None is used as
0081        a placeholder.)  Note that multi-phase init modules always get
0082        an index for which there will never be a module set.
0083 
0084        This is initialized lazily in PyState_AddModule(), which is also
0085        where modules get added. */
0086     PyObject *modules_by_index;
0087     /* importlib module._bootstrap */
0088     PyObject *importlib;
0089     /* override for config->use_frozen_modules (for tests)
0090        (-1: "off", 1: "on", 0: no override) */
0091     int override_frozen_modules;
0092     int override_multi_interp_extensions_check;
0093 #ifdef HAVE_DLOPEN
0094     int dlopenflags;
0095 #endif
0096     PyObject *import_func;
0097     /* The global import lock. */
0098     _PyRecursiveMutex lock;
0099     /* diagnostic info in PyImport_ImportModuleLevelObject() */
0100     struct {
0101         int import_level;
0102         PyTime_t accumulated;
0103         int header;
0104     } find_and_load;
0105 };
0106 
0107 #ifdef HAVE_DLOPEN
0108 #  include <dlfcn.h>              // RTLD_NOW, RTLD_LAZY
0109 #  if HAVE_DECL_RTLD_NOW
0110 #    define _Py_DLOPEN_FLAGS RTLD_NOW
0111 #  else
0112 #    define _Py_DLOPEN_FLAGS RTLD_LAZY
0113 #  endif
0114 #  define DLOPENFLAGS_INIT .dlopenflags = _Py_DLOPEN_FLAGS,
0115 #else
0116 #  define _Py_DLOPEN_FLAGS 0
0117 #  define DLOPENFLAGS_INIT
0118 #endif
0119 
0120 #define IMPORTS_INIT \
0121     { \
0122         DLOPENFLAGS_INIT \
0123         .find_and_load = { \
0124             .header = 1, \
0125         }, \
0126     }
0127 
0128 extern void _PyImport_ClearCore(PyInterpreterState *interp);
0129 
0130 extern Py_ssize_t _PyImport_GetNextModuleIndex(void);
0131 extern const char * _PyImport_ResolveNameWithPackageContext(const char *name);
0132 extern const char * _PyImport_SwapPackageContext(const char *newcontext);
0133 
0134 extern int _PyImport_GetDLOpenFlags(PyInterpreterState *interp);
0135 extern void _PyImport_SetDLOpenFlags(PyInterpreterState *interp, int new_val);
0136 
0137 extern PyObject * _PyImport_InitModules(PyInterpreterState *interp);
0138 extern PyObject * _PyImport_GetModules(PyInterpreterState *interp);
0139 extern void _PyImport_ClearModules(PyInterpreterState *interp);
0140 
0141 extern void _PyImport_ClearModulesByIndex(PyInterpreterState *interp);
0142 
0143 extern int _PyImport_InitDefaultImportFunc(PyInterpreterState *interp);
0144 extern int _PyImport_IsDefaultImportFunc(
0145         PyInterpreterState *interp,
0146         PyObject *func);
0147 
0148 extern PyObject * _PyImport_GetImportlibLoader(
0149         PyInterpreterState *interp,
0150         const char *loader_name);
0151 extern PyObject * _PyImport_GetImportlibExternalLoader(
0152         PyInterpreterState *interp,
0153         const char *loader_name);
0154 extern PyObject * _PyImport_BlessMyLoader(
0155         PyInterpreterState *interp,
0156         PyObject *module_globals);
0157 extern PyObject * _PyImport_ImportlibModuleRepr(
0158         PyInterpreterState *interp,
0159         PyObject *module);
0160 
0161 
0162 extern PyStatus _PyImport_Init(void);
0163 extern void _PyImport_Fini(void);
0164 extern void _PyImport_Fini2(void);
0165 
0166 extern PyStatus _PyImport_InitCore(
0167         PyThreadState *tstate,
0168         PyObject *sysmod,
0169         int importlib);
0170 extern PyStatus _PyImport_InitExternal(PyThreadState *tstate);
0171 extern void _PyImport_FiniCore(PyInterpreterState *interp);
0172 extern void _PyImport_FiniExternal(PyInterpreterState *interp);
0173 
0174 
0175 extern PyObject* _PyImport_GetBuiltinModuleNames(void);
0176 
0177 struct _module_alias {
0178     const char *name;                 /* ASCII encoded string */
0179     const char *orig;                 /* ASCII encoded string */
0180 };
0181 
0182 // Export these 3 symbols for test_ctypes
0183 PyAPI_DATA(const struct _frozen*) _PyImport_FrozenBootstrap;
0184 PyAPI_DATA(const struct _frozen*) _PyImport_FrozenStdlib;
0185 PyAPI_DATA(const struct _frozen*) _PyImport_FrozenTest;
0186 
0187 extern const struct _module_alias * _PyImport_FrozenAliases;
0188 
0189 extern int _PyImport_CheckSubinterpIncompatibleExtensionAllowed(
0190     const char *name);
0191 
0192 
0193 // Export for '_testinternalcapi' shared extension
0194 PyAPI_FUNC(int) _PyImport_ClearExtension(PyObject *name, PyObject *filename);
0195 
0196 #ifdef Py_GIL_DISABLED
0197 // Assuming that the GIL is enabled from a call to
0198 // _PyEval_EnableGILTransient(), resolve the transient request depending on the
0199 // state of the module argument:
0200 // - If module is NULL or a PyModuleObject with md_gil == Py_MOD_GIL_NOT_USED,
0201 //   call _PyEval_DisableGIL().
0202 // - Otherwise, call _PyEval_EnableGILPermanent(). If the GIL was not already
0203 //   enabled permanently, issue a warning referencing the module's name.
0204 //
0205 // This function may raise an exception.
0206 extern int _PyImport_CheckGILForModule(PyObject *module, PyObject *module_name);
0207 #endif
0208 
0209 #ifdef __cplusplus
0210 }
0211 #endif
0212 #endif /* !Py_INTERNAL_IMPORT_H */
0213 #endif /* !Py_LIMITED_API */