@@ -27,9 +27,6 @@ extern "C" {
27
27
/* Forward references */
28
28
static PyObject * import_add_module (PyThreadState * tstate , PyObject * name );
29
29
30
- /* See _PyImport_FixupExtensionObject() below */
31
- static PyObject * extensions = NULL ;
32
-
33
30
/* This table is defined in config.c: */
34
31
extern struct _inittab _PyImport_Inittab [];
35
32
@@ -221,10 +218,12 @@ _imp_release_lock_impl(PyObject *module)
221
218
Py_RETURN_NONE ;
222
219
}
223
220
221
+ static inline void _extensions_cache_clear (void );
222
+
224
223
void
225
224
_PyImport_Fini (void )
226
225
{
227
- Py_CLEAR ( extensions );
226
+ _extensions_cache_clear ( );
228
227
if (import_lock != NULL ) {
229
228
PyThread_free_lock (import_lock );
230
229
import_lock = NULL ;
@@ -398,6 +397,51 @@ PyImport_GetMagicTag(void)
398
397
dictionary, to avoid loading shared libraries twice.
399
398
*/
400
399
400
+ static PyModuleDef *
401
+ _extensions_cache_get (PyObject * filename , PyObject * name )
402
+ {
403
+ PyObject * extensions = _PyRuntime .imports .extensions ;
404
+ if (extensions == NULL ) {
405
+ return NULL ;
406
+ }
407
+ PyObject * key = PyTuple_Pack (2 , filename , name );
408
+ if (key == NULL ) {
409
+ return NULL ;
410
+ }
411
+ PyModuleDef * def = (PyModuleDef * )PyDict_GetItemWithError (extensions , key );
412
+ Py_DECREF (key );
413
+ return def ;
414
+ }
415
+
416
+ static int
417
+ _extensions_cache_set (PyObject * filename , PyObject * name , PyModuleDef * def )
418
+ {
419
+ PyObject * extensions = _PyRuntime .imports .extensions ;
420
+ if (extensions == NULL ) {
421
+ extensions = PyDict_New ();
422
+ if (extensions == NULL ) {
423
+ return -1 ;
424
+ }
425
+ _PyRuntime .imports .extensions = extensions ;
426
+ }
427
+ PyObject * key = PyTuple_Pack (2 , filename , name );
428
+ if (key == NULL ) {
429
+ return -1 ;
430
+ }
431
+ int res = PyDict_SetItem (extensions , key , (PyObject * )def );
432
+ Py_DECREF (key );
433
+ if (res < 0 ) {
434
+ return -1 ;
435
+ }
436
+ return 0 ;
437
+ }
438
+
439
+ static void
440
+ _extensions_cache_clear (void )
441
+ {
442
+ Py_CLEAR (_PyRuntime .imports .extensions );
443
+ }
444
+
401
445
int
402
446
_PyImport_FixupExtensionObject (PyObject * mod , PyObject * name ,
403
447
PyObject * filename , PyObject * modules )
@@ -442,20 +486,7 @@ _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
442
486
}
443
487
}
444
488
445
- if (extensions == NULL ) {
446
- extensions = PyDict_New ();
447
- if (extensions == NULL ) {
448
- return -1 ;
449
- }
450
- }
451
-
452
- PyObject * key = PyTuple_Pack (2 , filename , name );
453
- if (key == NULL ) {
454
- return -1 ;
455
- }
456
- int res = PyDict_SetItem (extensions , key , (PyObject * )def );
457
- Py_DECREF (key );
458
- if (res < 0 ) {
489
+ if (_extensions_cache_set (filename , name , def ) < 0 ) {
459
490
return -1 ;
460
491
}
461
492
}
@@ -480,16 +511,7 @@ static PyObject *
480
511
import_find_extension (PyThreadState * tstate , PyObject * name ,
481
512
PyObject * filename )
482
513
{
483
- if (extensions == NULL ) {
484
- return NULL ;
485
- }
486
-
487
- PyObject * key = PyTuple_Pack (2 , filename , name );
488
- if (key == NULL ) {
489
- return NULL ;
490
- }
491
- PyModuleDef * def = (PyModuleDef * )PyDict_GetItemWithError (extensions , key );
492
- Py_DECREF (key );
514
+ PyModuleDef * def = _extensions_cache_get (filename , name );
493
515
if (def == NULL ) {
494
516
return NULL ;
495
517
}
0 commit comments