@@ -105,22 +105,19 @@ _Py_ext_module_loader_info_clear(struct _Py_ext_module_loader_info *info)
105105}
106106
107107int
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+
155173PyObject *
156174_PyImport_LoadDynamicModuleWithSpec (struct _Py_ext_module_loader_info * info ,
157175 PyObject * spec , FILE * fp )
0 commit comments