Skip to content

Commit

Permalink
remove spec._initializing + related locking check
Browse files Browse the repository at this point in the history
spec._initializing is only there for a single check + optimization
within import.c that ends up calling _lock_unlock_module. Since we are
ignoring locks for the time being, this check can also be ignored.
  • Loading branch information
ahgamut committed May 15, 2022
1 parent a0c31d2 commit fccb0f1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 48 deletions.
18 changes: 5 additions & 13 deletions third_party/python/Lib/importlib/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,22 +300,14 @@ def __enter__(self):
# This must be done before putting the module in sys.modules
# (otherwise an optimization shortcut in import.c becomes
# wrong)
self._spec._initializing = True
sys.modules[self._spec.name] = self._module

def __exit__(self, *args):
try:
spec = self._spec
if any(arg is not None for arg in args):
try:
del sys.modules[spec.name]
except KeyError:
pass
else:
_verbose_message('import {!r} # {!r}', spec.name, spec.loader)
finally:
self._spec._initializing = False

spec = self._spec
if args and any(arg is not None for arg in args):
sys.modules.pop(spec.name, None)
else:
_verbose_message('import {!r} # {!r}', spec.name, spec.loader)

class ModuleSpec:
"""The specification for a module, used for loading.
Expand Down
37 changes: 2 additions & 35 deletions third_party/python/Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -1588,40 +1588,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,

mod = PyDict_GetItem(interp->modules, abs_name);
if (mod != NULL && mod != Py_None) {
_Py_IDENTIFIER(__spec__);
_Py_IDENTIFIER(_initializing);
_Py_IDENTIFIER(_lock_unlock_module);
PyObject *value = NULL;
PyObject *spec;
int initializing = 0;

Py_INCREF(mod);
/* 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 (spec != NULL) {
value = _PyObject_GetAttrId(spec, &PyId__initializing);
Py_DECREF(spec);
}
if (value == NULL)
PyErr_Clear();
else {
initializing = PyObject_IsTrue(value);
Py_DECREF(value);
if (initializing == -1)
PyErr_Clear();
if (0 && initializing > 0) {
value = _PyObject_CallMethodIdObjArgs(interp->importlib,
&PyId__lock_unlock_module, abs_name,
NULL);
if (value == NULL)
goto error;
Py_DECREF(value);
}
}
}
else {
mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
Expand Down Expand Up @@ -2462,8 +2429,7 @@ static PyObject *SFLObject_get_code(SourcelessFileLoader *self, PyObject *arg) {
// bytes_data = _validate_bytecode_header(data, name=fullname, path=path)
headerlen = fread(bytecode_header, sizeof(char), sizeof(bytecode_header), fp);

if (headerlen < 4 || (magic = READ16LE(bytecode_header)) != 3390 ||
bytecode_header[2] != '\r' || bytecode_header[3] != '\n') {
if (headerlen < 4 || (magic = READ32LE(bytecode_header)) != PyImport_GetMagicNumber()) {
PyErr_Format(PyExc_ImportError, "bad magic number in %s: %d\n", name,
magic);
goto exit;
Expand Down Expand Up @@ -2636,6 +2602,7 @@ static PyTypeObject SourcelessFileLoaderType = {
* to be portable to Windows without using C++. */
PyVarObject_HEAD_INIT(NULL, 0).tp_name =
"_imp.SourcelessFileLoader", /*tp_name*/
.tp_base = &PyBaseObject_Type, /*tp_base*/
.tp_basicsize = sizeof(SourcelessFileLoader), /*tp_basicsize*/
.tp_dealloc = (destructor)SFLObject_dealloc, /*tp_dealloc*/
.tp_hash = (hashfunc)SFLObject_hash, /*tp_hash*/
Expand Down

0 comments on commit fccb0f1

Please sign in to comment.