Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
37c22206
Commit
37c22206
authored
Sep 11, 2019
by
Joannah Nanjekye
Committed by
Brett Cannon
Sep 11, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-35943: Prevent PyImport_GetModule() from returning a partially-initialized module (GH-15057)
parent
60bba83b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
23 deletions
+48
-23
Misc/NEWS.d/next/Library/2019-07-31-15-52-51.bpo-35943.-KswoB.rst
...S.d/next/Library/2019-07-31-15-52-51.bpo-35943.-KswoB.rst
+2
-0
Python/import.c
Python/import.c
+46
-23
No files found.
Misc/NEWS.d/next/Library/2019-07-31-15-52-51.bpo-35943.-KswoB.rst
0 → 100644
View file @
37c22206
The function :c:func:`PyImport_GetModule` now ensures any module it returns is fully initialized.
Patch by Joannah Nanjekye.
Python/import.c
View file @
37c22206
...
...
@@ -387,11 +387,33 @@ import_get_module(PyThreadState *tstate, PyObject *name)
}
PyObject
*
PyImport_GetModule
(
PyObject
*
name
)
static
int
import_ensure_initialized
(
PyThreadState
*
tstate
,
PyObject
*
mod
,
PyObject
*
name
)
{
PyThreadState
*
tstate
=
_PyThreadState_GET
();
return
import_get_module
(
tstate
,
name
);
PyInterpreterState
*
interp
=
tstate
->
interp
;
PyObject
*
spec
;
_Py_IDENTIFIER
(
__spec__
);
_Py_IDENTIFIER
(
_lock_unlock_module
);
/* Optimization: only call _bootstrap._lock_unlock_module() if
__spec__._initializing is true.
NOTE: because of this, initializing must be set *before*
stuffing the new module in sys.modules.
*/
spec
=
_PyObject_GetAttrId
(
mod
,
&
PyId___spec__
);
int
busy
=
_PyModuleSpec_IsInitializing
(
spec
);
Py_XDECREF
(
spec
);
if
(
busy
)
{
/* Wait until module is done importing. */
PyObject
*
value
=
_PyObject_CallMethodIdOneArg
(
interp
->
importlib
,
&
PyId__lock_unlock_module
,
name
);
if
(
value
==
NULL
)
{
return
-
1
;
}
Py_DECREF
(
value
);
}
return
0
;
}
...
...
@@ -1461,6 +1483,7 @@ PyImport_ImportModule(const char *name)
return
result
;
}
/* Import a module without blocking
*
* At first it tries to fetch the module from sys.modules. If the module was
...
...
@@ -1762,6 +1785,23 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name)
return
mod
;
}
PyObject
*
PyImport_GetModule
(
PyObject
*
name
)
{
PyThreadState
*
tstate
=
_PyThreadState_GET
();
PyObject
*
mod
;
mod
=
import_get_module
(
tstate
,
name
);
if
(
mod
!=
NULL
&&
mod
!=
Py_None
)
{
if
(
import_ensure_initialized
(
tstate
,
mod
,
name
)
<
0
)
{
Py_DECREF
(
mod
);
remove_importlib_frames
(
tstate
);
return
NULL
;
}
}
return
mod
;
}
PyObject
*
PyImport_ImportModuleLevelObject
(
PyObject
*
name
,
PyObject
*
globals
,
PyObject
*
locals
,
PyObject
*
fromlist
,
...
...
@@ -1817,26 +1857,9 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
}
if
(
mod
!=
NULL
&&
mod
!=
Py_None
)
{
_Py_IDENTIFIER
(
__spec__
);
_Py_IDENTIFIER
(
_lock_unlock_module
);
PyObject
*
spec
;
/* Optimization: only call _bootstrap._lock_unlock_module() if
__spec__._initializing is true.
NOTE: because of this, initializing must be set *before*
stuffing the new module in sys.modules.
*/
spec
=
_PyObject_GetAttrId
(
mod
,
&
PyId___spec__
);
if
(
_PyModuleSpec_IsInitializing
(
spec
))
{
PyObject
*
value
=
_PyObject_CallMethodIdOneArg
(
interp
->
importlib
,
&
PyId__lock_unlock_module
,
abs_name
);
if
(
value
==
NULL
)
{
Py_DECREF
(
spec
);
goto
error
;
}
Py_DECREF
(
value
);
if
(
import_ensure_initialized
(
tstate
,
mod
,
name
)
<
0
)
{
goto
error
;
}
Py_XDECREF
(
spec
);
}
else
{
Py_XDECREF
(
mod
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment