@@ -409,7 +409,7 @@ PyImport_GetMagicTag(void)
409
409
modules. A copy of the module's dictionary is stored by calling
410
410
_PyImport_FixupExtensionObject() immediately after the module initialization
411
411
function succeeds. A copy can be retrieved from there by calling
412
- _PyImport_FindExtensionObject ().
412
+ import_find_extension ().
413
413
414
414
Modules which do support multiple initialization set their m_size
415
415
field to a non-negative number (indicating the size of the
@@ -522,10 +522,14 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
522
522
if (mod == NULL )
523
523
return NULL ;
524
524
mdict = PyModule_GetDict (mod );
525
- if (mdict == NULL )
525
+ if (mdict == NULL ) {
526
+ Py_DECREF (mod );
526
527
return NULL ;
527
- if (PyDict_Update (mdict , def -> m_base .m_copy ))
528
+ }
529
+ if (PyDict_Update (mdict , def -> m_base .m_copy )) {
530
+ Py_DECREF (mod );
528
531
return NULL ;
532
+ }
529
533
}
530
534
else {
531
535
if (def -> m_base .m_init == NULL )
@@ -537,10 +541,10 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
537
541
Py_DECREF (mod );
538
542
return NULL ;
539
543
}
540
- Py_DECREF (mod );
541
544
}
542
545
if (_PyState_AddModule (tstate , mod , def ) < 0 ) {
543
546
PyMapping_DelItem (modules , name );
547
+ Py_DECREF (mod );
544
548
return NULL ;
545
549
}
546
550
@@ -552,31 +556,10 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
552
556
return mod ;
553
557
}
554
558
555
- PyObject *
556
- _PyImport_FindExtensionObject (PyObject * name , PyObject * filename )
557
- {
558
- PyThreadState * tstate = _PyThreadState_GET ();
559
- return import_find_extension (tstate , name , filename );
560
- }
561
-
562
-
563
- PyObject *
564
- _PyImport_FindBuiltin (PyThreadState * tstate , const char * name )
565
- {
566
- PyObject * res , * nameobj ;
567
- nameobj = PyUnicode_InternFromString (name );
568
- if (nameobj == NULL )
569
- return NULL ;
570
- res = import_find_extension (tstate , nameobj , nameobj );
571
- Py_DECREF (nameobj );
572
- return res ;
573
- }
574
559
575
560
/* Get the module object corresponding to a module name.
576
561
First check the modules dictionary if there's one there,
577
- if not, create a new one and insert it in the modules dictionary.
578
- Because the former action is most common, THIS DOES NOT RETURN A
579
- 'NEW' REFERENCE! */
562
+ if not, create a new one and insert it in the modules dictionary. */
580
563
581
564
static PyObject *
582
565
import_add_module (PyThreadState * tstate , PyObject * name )
@@ -591,6 +574,7 @@ import_add_module(PyThreadState *tstate, PyObject *name)
591
574
PyObject * m ;
592
575
if (PyDict_CheckExact (modules )) {
593
576
m = PyDict_GetItemWithError (modules , name );
577
+ Py_XINCREF (m );
594
578
}
595
579
else {
596
580
m = PyObject_GetItem (modules , name );
@@ -606,14 +590,14 @@ import_add_module(PyThreadState *tstate, PyObject *name)
606
590
if (m != NULL && PyModule_Check (m )) {
607
591
return m ;
608
592
}
593
+ Py_XDECREF (m );
609
594
m = PyModule_NewObject (name );
610
595
if (m == NULL )
611
596
return NULL ;
612
597
if (PyObject_SetItem (modules , name , m ) != 0 ) {
613
598
Py_DECREF (m );
614
599
return NULL ;
615
600
}
616
- Py_DECREF (m ); /* Yes, it still exists, in modules! */
617
601
618
602
return m ;
619
603
}
@@ -622,7 +606,17 @@ PyObject *
622
606
PyImport_AddModuleObject (PyObject * name )
623
607
{
624
608
PyThreadState * tstate = _PyThreadState_GET ();
625
- return import_add_module (tstate , name );
609
+ PyObject * mod = import_add_module (tstate , name );
610
+ if (mod ) {
611
+ PyObject * ref = PyWeakref_NewRef (mod , NULL );
612
+ Py_DECREF (mod );
613
+ if (ref == NULL ) {
614
+ return NULL ;
615
+ }
616
+ mod = PyWeakref_GetObject (ref );
617
+ Py_DECREF (ref );
618
+ }
619
+ return mod ; /* borrowed reference */
626
620
}
627
621
628
622
@@ -747,7 +741,7 @@ static PyObject *
747
741
module_dict_for_exec (PyThreadState * tstate , PyObject * name )
748
742
{
749
743
_Py_IDENTIFIER (__builtins__ );
750
- PyObject * m , * d = NULL ;
744
+ PyObject * m , * d ;
751
745
752
746
m = import_add_module (tstate , name );
753
747
if (m == NULL )
@@ -762,10 +756,13 @@ module_dict_for_exec(PyThreadState *tstate, PyObject *name)
762
756
}
763
757
if (r < 0 ) {
764
758
remove_module (tstate , name );
759
+ Py_DECREF (m );
765
760
return NULL ;
766
761
}
767
762
768
- return d ; /* Return a borrowed reference. */
763
+ Py_INCREF (d );
764
+ Py_DECREF (m );
765
+ return d ;
769
766
}
770
767
771
768
static PyObject *
@@ -809,8 +806,10 @@ PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
809
806
}
810
807
external = PyObject_GetAttrString (tstate -> interp -> importlib ,
811
808
"_bootstrap_external" );
812
- if (external == NULL )
809
+ if (external == NULL ) {
810
+ Py_DECREF (d );
813
811
return NULL ;
812
+ }
814
813
res = _PyObject_CallMethodIdObjArgs (external ,
815
814
& PyId__fix_up_module ,
816
815
d , name , pathname , cpathname , NULL );
@@ -819,6 +818,7 @@ PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
819
818
Py_DECREF (res );
820
819
res = exec_code_in_module (tstate , name , d , co );
821
820
}
821
+ Py_DECREF (d );
822
822
return res ;
823
823
}
824
824
@@ -912,8 +912,7 @@ is_builtin(PyObject *name)
912
912
that can handle the path item. Return None if no hook could;
913
913
this tells our caller that the path based finder could not find
914
914
a finder for this path item. Cache the result in
915
- path_importer_cache.
916
- Returns a borrowed reference. */
915
+ path_importer_cache. */
917
916
918
917
static PyObject *
919
918
get_path_importer (PyThreadState * tstate , PyObject * path_importer_cache ,
@@ -931,8 +930,10 @@ get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache,
931
930
return NULL ; /* Shouldn't happen */
932
931
933
932
importer = PyDict_GetItemWithError (path_importer_cache , p );
934
- if (importer != NULL || _PyErr_Occurred (tstate ))
933
+ if (importer != NULL || _PyErr_Occurred (tstate )) {
934
+ Py_XINCREF (importer );
935
935
return importer ;
936
+ }
936
937
937
938
/* set path_importer_cache[p] to None to avoid recursion */
938
939
if (PyDict_SetItem (path_importer_cache , p , Py_None ) != 0 )
@@ -952,13 +953,11 @@ get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache,
952
953
_PyErr_Clear (tstate );
953
954
}
954
955
if (importer == NULL ) {
955
- return Py_None ;
956
+ Py_RETURN_NONE ;
956
957
}
957
- if (importer != NULL ) {
958
- int err = PyDict_SetItem (path_importer_cache , p , importer );
958
+ if (PyDict_SetItem (path_importer_cache , p , importer ) < 0 ) {
959
959
Py_DECREF (importer );
960
- if (err != 0 )
961
- return NULL ;
960
+ return NULL ;
962
961
}
963
962
return importer ;
964
963
}
@@ -967,24 +966,19 @@ PyObject *
967
966
PyImport_GetImporter (PyObject * path )
968
967
{
969
968
PyThreadState * tstate = _PyThreadState_GET ();
970
- PyObject * importer = NULL , * path_importer_cache = NULL , * path_hooks = NULL ;
971
-
972
- path_importer_cache = PySys_GetObject ("path_importer_cache" );
973
- path_hooks = PySys_GetObject ("path_hooks" );
974
- if (path_importer_cache != NULL && path_hooks != NULL ) {
975
- importer = get_path_importer (tstate , path_importer_cache ,
976
- path_hooks , path );
969
+ PyObject * path_importer_cache = PySys_GetObject ("path_importer_cache" );
970
+ PyObject * path_hooks = PySys_GetObject ("path_hooks" );
971
+ if (path_importer_cache == NULL || path_hooks == NULL ) {
972
+ return NULL ;
977
973
}
978
- Py_XINCREF (importer ); /* get_path_importer returns a borrowed reference */
979
- return importer ;
974
+ return get_path_importer (tstate , path_importer_cache , path_hooks , path );
980
975
}
981
976
982
977
static PyObject *
983
978
create_builtin (PyThreadState * tstate , PyObject * name , PyObject * spec )
984
979
{
985
- PyObject * mod = _PyImport_FindExtensionObject ( name , name );
980
+ PyObject * mod = import_find_extension ( tstate , name , name );
986
981
if (mod || _PyErr_Occurred (tstate )) {
987
- Py_XINCREF (mod );
988
982
return mod ;
989
983
}
990
984
@@ -1165,10 +1159,12 @@ PyImport_ImportFrozenModuleObject(PyObject *name)
1165
1159
d = PyModule_GetDict (m );
1166
1160
l = PyList_New (0 );
1167
1161
if (l == NULL ) {
1162
+ Py_DECREF (m );
1168
1163
goto err_return ;
1169
1164
}
1170
1165
err = PyDict_SetItemString (d , "__path__" , l );
1171
1166
Py_DECREF (l );
1167
+ Py_DECREF (m );
1172
1168
if (err != 0 )
1173
1169
goto err_return ;
1174
1170
}
@@ -1177,6 +1173,7 @@ PyImport_ImportFrozenModuleObject(PyObject *name)
1177
1173
goto err_return ;
1178
1174
}
1179
1175
m = exec_code_in_module (tstate , name , d , co );
1176
+ Py_DECREF (d );
1180
1177
if (m == NULL ) {
1181
1178
goto err_return ;
1182
1179
}
@@ -1875,17 +1872,14 @@ _imp_init_frozen_impl(PyObject *module, PyObject *name)
1875
1872
{
1876
1873
PyThreadState * tstate = _PyThreadState_GET ();
1877
1874
int ret ;
1878
- PyObject * m ;
1879
1875
1880
1876
ret = PyImport_ImportFrozenModuleObject (name );
1881
1877
if (ret < 0 )
1882
1878
return NULL ;
1883
1879
if (ret == 0 ) {
1884
1880
Py_RETURN_NONE ;
1885
1881
}
1886
- m = import_add_module (tstate , name );
1887
- Py_XINCREF (m );
1888
- return m ;
1882
+ return import_add_module (tstate , name );
1889
1883
}
1890
1884
1891
1885
/*[clinic input]
@@ -2009,11 +2003,11 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
2009
2003
return NULL ;
2010
2004
}
2011
2005
2012
- mod = _PyImport_FindExtensionObject (name , path );
2006
+ PyThreadState * tstate = _PyThreadState_GET ();
2007
+ mod = import_find_extension (tstate , name , path );
2013
2008
if (mod != NULL || PyErr_Occurred ()) {
2014
2009
Py_DECREF (name );
2015
2010
Py_DECREF (path );
2016
- Py_XINCREF (mod );
2017
2011
return mod ;
2018
2012
}
2019
2013
0 commit comments