Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-117953: Refactor Import Machinery Code for Extension Modules #118116

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
cc67849
Factor out _PyImport_RunDynamicModule().
ericsnowcurrently Feb 8, 2024
2e5ecde
Drop _PyImport_LoadDynamicModuleWithSpec().
ericsnowcurrently Feb 8, 2024
b810cd8
Add an assert.
ericsnowcurrently Feb 8, 2024
8d0ef38
Factor out fix_up_extension_for_interpreter().
ericsnowcurrently Feb 8, 2024
7f930cc
Do _extensions_cache_set() first.
ericsnowcurrently Feb 8, 2024
1ffeaef
Calculate path_bytes ahead of time.
ericsnowcurrently Apr 16, 2024
7500285
Move the audit entry to _imp_create_dynamic_impl().
ericsnowcurrently Apr 16, 2024
3c0b170
_Py_ext_module_loader_info_from_spec() -> _Py_ext_module_loader_info_…
ericsnowcurrently Apr 16, 2024
02e24cc
Add info.newcontext.
ericsnowcurrently Apr 16, 2024
ba5ffa7
Split up _PyImport_RunDynamicModule() along cross-interpreter-safe bo…
ericsnowcurrently Apr 16, 2024
b895966
Drop _PyImport_RunDynamicModule().
ericsnowcurrently Apr 16, 2024
76fa273
Always close the file handle.
ericsnowcurrently Apr 16, 2024
1e936c5
Revert the fclose() change.
ericsnowcurrently Apr 16, 2024
6bb07b9
Factor out create_dynamic().
ericsnowcurrently Apr 16, 2024
2705e65
Drop _PyImport_FixupExtensionObject().
ericsnowcurrently Apr 16, 2024
98d8b4b
Move setting __file__ to fix_up_extension().
ericsnowcurrently Apr 17, 2024
ddf1135
Pass tstate through to fix_up_extension().
ericsnowcurrently Apr 17, 2024
694a763
Add a note about _PyImport_FixupBuiltin().
ericsnowcurrently Apr 17, 2024
72e0d73
Always expect a valid modules dict passed to fix_up_extension().
ericsnowcurrently Apr 17, 2024
3f14a67
Factor out reload_singlephase_extension().
ericsnowcurrently Apr 17, 2024
b5f5868
res.singlephase -> res.kind
ericsnowcurrently Apr 17, 2024
a5f7535
Factor out _extensions_cache_init().
ericsnowcurrently Apr 17, 2024
e8d674a
Factor out _extensions_cache_find_unlocked().
ericsnowcurrently Apr 17, 2024
27b365d
Use _PyImport_RunModInitFunc() in create_builtin().
ericsnowcurrently Apr 18, 2024
5128702
Clean up _PyImport_FixupBuiltin().
ericsnowcurrently Apr 18, 2024
aaec360
Add some asserts to _PyImport_RunModInitFunc().
ericsnowcurrently Apr 18, 2024
f97671e
Drop the replace arg to _extensions_cache_set().
ericsnowcurrently Apr 18, 2024
2366983
Check the module returned by import_find_extension() to ensure single…
ericsnowcurrently Apr 18, 2024
d5039ea
Allow mod->md_def to be NULL.
ericsnowcurrently Apr 19, 2024
67d217b
Call _PyImport_RunModInitFunc() in reload_singlephase_extension().
ericsnowcurrently Apr 19, 2024
9e40287
res->err is an array, not just a pointer.
ericsnowcurrently Apr 19, 2024
b8c2a1d
Factor out import_run_extension() (and drop create_dynamic()).
ericsnowcurrently Apr 18, 2024
967c649
Factor out update_extension_cache() and drop fix_up_extension_for_int…
ericsnowcurrently Apr 19, 2024
aecf56c
We already know the def is okay.
ericsnowcurrently Apr 19, 2024
0d03d23
Revive fix_up_extension_for_interpreter().
ericsnowcurrently Apr 19, 2024
cace102
Add a distinct _Py_ext_module_loader_info.path (and old path -> filen…
ericsnowcurrently Apr 19, 2024
3cf0db0
Do not call update_extensions_cache() in fix_up_extension().
ericsnowcurrently Apr 19, 2024
9b3088a
Let the modules arg to fix_up_extension_for_interpreter() be NULL.
ericsnowcurrently Apr 19, 2024
4ac1644
Let the caller of update_extensions_cache() decide if m_copy should b…
ericsnowcurrently Apr 19, 2024
393a177
Fix the update_extensions_cache() signature.
ericsnowcurrently Apr 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Include/internal/pycore_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ extern int _PyImport_SetModuleString(const char *name, PyObject* module);
extern void _PyImport_AcquireLock(PyInterpreterState *interp);
extern int _PyImport_ReleaseLock(PyInterpreterState *interp);

// This is used exclusively for the sys and builtins modules:
extern int _PyImport_FixupBuiltin(
PyThreadState *tstate,
PyObject *mod,
const char *name, /* UTF-8 encoded string */
PyObject *modules
);
extern int _PyImport_FixupExtensionObject(PyObject*, PyObject *,
PyObject *, PyObject *);

// Export for many shared extensions, like '_json'
PyAPI_FUNC(PyObject*) _PyImport_GetModuleAttr(PyObject *, PyObject *);
Expand All @@ -52,7 +52,7 @@ struct _import_runtime_state {
Only legacy (single-phase init) extension modules are added
and only if they support multiple initialization (m_size >- 0)
or are imported in the main interpreter.
This is initialized lazily in _PyImport_FixupExtensionObject().
This is initialized lazily in fix_up_extension() in import.c.
Modules are added there and looked up in _imp.find_extension(). */
_Py_hashtable_t *hashtable;
} extensions;
Expand Down
45 changes: 43 additions & 2 deletions Include/internal/pycore_importdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,51 @@ extern "C" {

extern const char *_PyImport_DynLoadFiletab[];

extern PyObject *_PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *);

typedef PyObject *(*PyModInitFunction)(void);

struct _Py_ext_module_loader_info {
PyObject *filename;
#ifndef MS_WINDOWS
PyObject *filename_encoded;
#endif
PyObject *name;
PyObject *name_encoded;
/* path is always a borrowed ref of name or filename,
* depending on if it's builtin or not. */
PyObject *path;
const char *hook_prefix;
const char *newcontext;
};
extern void _Py_ext_module_loader_info_clear(
struct _Py_ext_module_loader_info *info);
extern int _Py_ext_module_loader_info_init_for_builtin(
struct _Py_ext_module_loader_info *p_info,
PyObject *name);
extern int _Py_ext_module_loader_info_init_from_spec(
struct _Py_ext_module_loader_info *info,
PyObject *spec);

struct _Py_ext_module_loader_result {
PyModuleDef *def;
PyObject *module;
enum _Py_ext_module_loader_result_kind {
_Py_ext_module_loader_result_UNKNOWN = 0,
_Py_ext_module_loader_result_SINGLEPHASE = 1,
_Py_ext_module_loader_result_MULTIPHASE = 2,
_Py_ext_module_loader_result_INVALID = 3,
} kind;
char err[200];
};
extern void _Py_ext_module_loader_result_apply_error(
struct _Py_ext_module_loader_result *res);
extern PyModInitFunction _PyImport_GetModInitFunc(
struct _Py_ext_module_loader_info *info,
FILE *fp);
extern int _PyImport_RunModInitFunc(
PyModInitFunction p0,
struct _Py_ext_module_loader_info *info,
struct _Py_ext_module_loader_result *p_res);

/* Max length of module suffix searched for -- accommodates "module.slb" */
#define MAXSUFFIXSIZE 12

Expand Down
2 changes: 1 addition & 1 deletion Include/moduleobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ typedef struct PyModuleDef_Base {
/* A copy of the module's __dict__ after the first time it was loaded.
This is only set/used for legacy modules that do not support
multiple initializations.
It is set by _PyImport_FixupExtensionObject(). */
It is set by fix_up_extension() in import.c. */
PyObject* m_copy;
} PyModuleDef_Base;

Expand Down
Loading
Loading