@@ -672,7 +672,7 @@ PyImport_GetMagicTag(void)
672
672
modules. A copy of the module's dictionary is stored by calling
673
673
_PyImport_FixupExtensionObject() immediately after the module initialization
674
674
function succeeds. A copy can be retrieved from there by calling
675
- _PyImport_FindExtensionObject ().
675
+ import_find_extension ().
676
676
677
677
Modules which do support multiple initialization set their m_size
678
678
field to a non-negative number (indicating the size of the
@@ -785,10 +785,14 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
785
785
if (mod == NULL )
786
786
return NULL ;
787
787
mdict = PyModule_GetDict (mod );
788
- if (mdict == NULL )
788
+ if (mdict == NULL ) {
789
+ Py_DECREF (mod );
789
790
return NULL ;
790
- if (PyDict_Update (mdict , def -> m_base .m_copy ))
791
+ }
792
+ if (PyDict_Update (mdict , def -> m_base .m_copy )) {
793
+ Py_DECREF (mod );
791
794
return NULL ;
795
+ }
792
796
}
793
797
else {
794
798
if (def -> m_base .m_init == NULL )
@@ -800,10 +804,10 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
800
804
Py_DECREF (mod );
801
805
return NULL ;
802
806
}
803
- Py_DECREF (mod );
804
807
}
805
808
if (_PyState_AddModule (tstate , mod , def ) < 0 ) {
806
809
PyMapping_DelItem (modules , name );
810
+ Py_DECREF (mod );
807
811
return NULL ;
808
812
}
809
813
@@ -815,31 +819,10 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
815
819
return mod ;
816
820
}
817
821
818
- PyObject *
819
- _PyImport_FindExtensionObject (PyObject * name , PyObject * filename )
820
- {
821
- PyThreadState * tstate = _PyThreadState_GET ();
822
- return import_find_extension (tstate , name , filename );
823
- }
824
-
825
-
826
- PyObject *
827
- _PyImport_FindBuiltin (PyThreadState * tstate , const char * name )
828
- {
829
- PyObject * res , * nameobj ;
830
- nameobj = PyUnicode_InternFromString (name );
831
- if (nameobj == NULL )
832
- return NULL ;
833
- res = import_find_extension (tstate , nameobj , nameobj );
834
- Py_DECREF (nameobj );
835
- return res ;
836
- }
837
822
838
823
/* Get the module object corresponding to a module name.
839
824
First check the modules dictionary if there's one there,
840
- if not, create a new one and insert it in the modules dictionary.
841
- Because the former action is most common, THIS DOES NOT RETURN A
842
- 'NEW' REFERENCE! */
825
+ if not, create a new one and insert it in the modules dictionary. */
843
826
844
827
static PyObject *
845
828
import_add_module (PyThreadState * tstate , PyObject * name )
@@ -854,6 +837,7 @@ import_add_module(PyThreadState *tstate, PyObject *name)
854
837
PyObject * m ;
855
838
if (PyDict_CheckExact (modules )) {
856
839
m = PyDict_GetItemWithError (modules , name );
840
+ Py_XINCREF (m );
857
841
}
858
842
else {
859
843
m = PyObject_GetItem (modules , name );
@@ -869,14 +853,14 @@ import_add_module(PyThreadState *tstate, PyObject *name)
869
853
if (m != NULL && PyModule_Check (m )) {
870
854
return m ;
871
855
}
856
+ Py_XDECREF (m );
872
857
m = PyModule_NewObject (name );
873
858
if (m == NULL )
874
859
return NULL ;
875
860
if (PyObject_SetItem (modules , name , m ) != 0 ) {
876
861
Py_DECREF (m );
877
862
return NULL ;
878
863
}
879
- Py_DECREF (m ); /* Yes, it still exists, in modules! */
880
864
881
865
return m ;
882
866
}
@@ -885,7 +869,25 @@ PyObject *
885
869
PyImport_AddModuleObject (PyObject * name )
886
870
{
887
871
PyThreadState * tstate = _PyThreadState_GET ();
888
- return import_add_module (tstate , name );
872
+ PyObject * mod = import_add_module (tstate , name );
873
+ if (mod ) {
874
+ if (Py_REFCNT (mod ) == 1 ) {
875
+ /* This check does not preven an undefined behavior in the
876
+ * following code, because the module can have references
877
+ * to itself. */
878
+ PyErr_SetString (PyExc_RuntimeError , "Unexpected zero reference count" );
879
+ Py_DECREF (mod );
880
+ return NULL ;
881
+ }
882
+ Py_DECREF (mod );
883
+ if (Py_REFCNT (mod ) == 0 ) {
884
+ /* Strictly speaking, this is an undefined behafior, and
885
+ * the above check can crash. */
886
+ PyErr_SetString (PyExc_RuntimeError , "Unexpected zero reference count" );
887
+ return NULL ;
888
+ }
889
+ }
890
+ return mod ; /* borrowed reference */
889
891
}
890
892
891
893
@@ -1007,7 +1009,7 @@ static PyObject *
1007
1009
module_dict_for_exec (PyThreadState * tstate , PyObject * name )
1008
1010
{
1009
1011
_Py_IDENTIFIER (__builtins__ );
1010
- PyObject * m , * d = NULL ;
1012
+ PyObject * m , * d ;
1011
1013
1012
1014
m = import_add_module (tstate , name );
1013
1015
if (m == NULL )
@@ -1021,11 +1023,14 @@ module_dict_for_exec(PyThreadState *tstate, PyObject *name)
1021
1023
PyEval_GetBuiltins ()) != 0 )
1022
1024
{
1023
1025
remove_module (tstate , name );
1026
+ Py_DECREF (m );
1024
1027
return NULL ;
1025
1028
}
1026
1029
}
1027
1030
1028
- return d ; /* Return a borrowed reference. */
1031
+ Py_INCREF (d );
1032
+ Py_DECREF (m );
1033
+ return d ;
1029
1034
}
1030
1035
1031
1036
static PyObject *
@@ -1069,8 +1074,10 @@ PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
1069
1074
}
1070
1075
external = PyObject_GetAttrString (tstate -> interp -> importlib ,
1071
1076
"_bootstrap_external" );
1072
- if (external == NULL )
1077
+ if (external == NULL ) {
1078
+ Py_DECREF (d );
1073
1079
return NULL ;
1080
+ }
1074
1081
res = _PyObject_CallMethodIdObjArgs (external ,
1075
1082
& PyId__fix_up_module ,
1076
1083
d , name , pathname , cpathname , NULL );
@@ -1079,6 +1086,7 @@ PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
1079
1086
Py_DECREF (res );
1080
1087
res = exec_code_in_module (tstate , name , d , co );
1081
1088
}
1089
+ Py_DECREF (d );
1082
1090
return res ;
1083
1091
}
1084
1092
@@ -1263,10 +1271,9 @@ _imp_create_builtin(PyObject *module, PyObject *spec)
1263
1271
return NULL ;
1264
1272
}
1265
1273
1266
- mod = _PyImport_FindExtensionObject ( name , name );
1274
+ mod = import_find_extension ( tstate , name , name );
1267
1275
if (mod || _PyErr_Occurred (tstate )) {
1268
1276
Py_DECREF (name );
1269
- Py_XINCREF (mod );
1270
1277
return mod ;
1271
1278
}
1272
1279
@@ -1429,10 +1436,12 @@ PyImport_ImportFrozenModuleObject(PyObject *name)
1429
1436
d = PyModule_GetDict (m );
1430
1437
l = PyList_New (0 );
1431
1438
if (l == NULL ) {
1439
+ Py_DECREF (m );
1432
1440
goto err_return ;
1433
1441
}
1434
1442
err = PyDict_SetItemString (d , "__path__" , l );
1435
1443
Py_DECREF (l );
1444
+ Py_DECREF (m );
1436
1445
if (err != 0 )
1437
1446
goto err_return ;
1438
1447
}
@@ -1442,10 +1451,12 @@ PyImport_ImportFrozenModuleObject(PyObject *name)
1442
1451
}
1443
1452
m = exec_code_in_module (tstate , name , d , co );
1444
1453
if (m == NULL ) {
1454
+ Py_DECREF (d );
1445
1455
goto err_return ;
1446
1456
}
1447
1457
Py_DECREF (co );
1448
1458
Py_DECREF (m );
1459
+ Py_DECREF (d );
1449
1460
return 1 ;
1450
1461
1451
1462
err_return :
@@ -2135,17 +2146,14 @@ _imp_init_frozen_impl(PyObject *module, PyObject *name)
2135
2146
{
2136
2147
PyThreadState * tstate = _PyThreadState_GET ();
2137
2148
int ret ;
2138
- PyObject * m ;
2139
2149
2140
2150
ret = PyImport_ImportFrozenModuleObject (name );
2141
2151
if (ret < 0 )
2142
2152
return NULL ;
2143
2153
if (ret == 0 ) {
2144
2154
Py_RETURN_NONE ;
2145
2155
}
2146
- m = import_add_module (tstate , name );
2147
- Py_XINCREF (m );
2148
- return m ;
2156
+ return import_add_module (tstate , name );
2149
2157
}
2150
2158
2151
2159
/*[clinic input]
@@ -2269,11 +2277,11 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
2269
2277
return NULL ;
2270
2278
}
2271
2279
2272
- mod = _PyImport_FindExtensionObject (name , path );
2280
+ PyThreadState * tstate = _PyThreadState_GET ();
2281
+ mod = import_find_extension (tstate , name , path );
2273
2282
if (mod != NULL || PyErr_Occurred ()) {
2274
2283
Py_DECREF (name );
2275
2284
Py_DECREF (path );
2276
- Py_XINCREF (mod );
2277
2285
return mod ;
2278
2286
}
2279
2287
0 commit comments