Skip to content

Commit 481bf5d

Browse files
Factor out _Py_ext_module_loader_info_init().
1 parent 5d951b3 commit 481bf5d

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

Include/internal/pycore_importdl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ struct _Py_ext_module_loader_info {
2929
};
3030
extern void _Py_ext_module_loader_info_clear(
3131
struct _Py_ext_module_loader_info *info);
32+
extern int _Py_ext_module_loader_info_init(
33+
struct _Py_ext_module_loader_info *info,
34+
PyObject *name,
35+
PyObject *filename);
3236
extern int _Py_ext_module_loader_info_init_from_spec(
3337
struct _Py_ext_module_loader_info *info,
3438
PyObject *spec);

Python/importdl.c

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,19 @@ _Py_ext_module_loader_info_clear(struct _Py_ext_module_loader_info *info)
105105
}
106106

107107
int
108-
_Py_ext_module_loader_info_init_from_spec(
109-
struct _Py_ext_module_loader_info *p_info,
110-
PyObject *spec)
108+
_Py_ext_module_loader_info_init(struct _Py_ext_module_loader_info *p_info,
109+
PyObject *name, PyObject *filename)
111110
{
112111
struct _Py_ext_module_loader_info info = {0};
113112

114-
info.name = PyObject_GetAttrString(spec, "name");
115-
if (info.name == NULL) {
116-
return -1;
117-
}
118-
if (!PyUnicode_Check(info.name)) {
113+
assert(name != NULL);
114+
if (!PyUnicode_Check(name)) {
119115
PyErr_SetString(PyExc_TypeError,
120-
"spec.name must be a string");
116+
"module name must be a string");
121117
_Py_ext_module_loader_info_clear(&info);
122118
return -1;
123119
}
120+
info.name = Py_NewRef(name);
124121

125122
info.name_encoded = get_encoded_name(info.name, &info.hook_prefix);
126123
if (info.name_encoded == NULL) {
@@ -134,24 +131,45 @@ _Py_ext_module_loader_info_init_from_spec(
134131
return -1;
135132
}
136133

137-
info.filename = PyObject_GetAttrString(spec, "origin");
138-
if (info.filename == NULL) {
139-
_Py_ext_module_loader_info_clear(&info);
140-
return -1;
141-
}
134+
if (filename != NULL) {
135+
if (!PyUnicode_Check(filename)) {
136+
PyErr_SetString(PyExc_TypeError,
137+
"module filename must be a string");
138+
_Py_ext_module_loader_info_clear(&info);
139+
return -1;
140+
}
141+
info.filename = Py_NewRef(filename);
142142

143143
#ifndef MS_WINDOWS
144-
info.filename_encoded = PyUnicode_EncodeFSDefault(info.filename);
145-
if (info.filename_encoded == NULL) {
146-
_Py_ext_module_loader_info_clear(&info);
147-
return -1;
148-
}
144+
info.filename_encoded = PyUnicode_EncodeFSDefault(info.filename);
145+
if (info.filename_encoded == NULL) {
146+
_Py_ext_module_loader_info_clear(&info);
147+
return -1;
148+
}
149149
#endif
150+
}
150151

151152
*p_info = info;
152153
return 0;
153154
}
154155

156+
int
157+
_Py_ext_module_loader_info_init_from_spec(
158+
struct _Py_ext_module_loader_info *p_info,
159+
PyObject *spec)
160+
{
161+
PyObject *name = PyObject_GetAttrString(spec, "name");
162+
if (name == NULL) {
163+
return -1;
164+
}
165+
PyObject *filename = PyObject_GetAttrString(spec, "origin");
166+
if (filename == NULL) {
167+
return -1;
168+
}
169+
return _Py_ext_module_loader_info_init(p_info, name, filename);
170+
}
171+
172+
155173
PyObject *
156174
_PyImport_LoadDynamicModuleWithSpec(struct _Py_ext_module_loader_info *info,
157175
PyObject *spec, FILE *fp)

0 commit comments

Comments
 (0)