Skip to content

Commit 82da888

Browse files
committed
Issue python#15767: Revert 3a50025f1900 for ModuleNotFoundError
1 parent 45091c0 commit 82da888

File tree

10 files changed

+807
-842
lines changed

10 files changed

+807
-842
lines changed

Doc/c-api/exceptions.rst

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,6 @@ in various ways. There is a separate error indicator for each thread.
292292
293293
.. versionadded:: 3.3
294294
295-
.. c:function:: PyObject* PyErr_SetImportErrorSubclass(PyObject *msg, PyObject *name, PyObject *path)
296-
297-
Much like :c:func:`PyErr_SetImportError` but this function allows for
298-
specifying a subclass of :exc:`ImportError` to raise.
299-
300-
.. versionadded:: 3.4
301-
302295
303296
.. c:function:: void PyErr_SyntaxLocationEx(char *filename, int lineno, int col_offset)
304297

Doc/library/exceptions.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,7 @@ The following exceptions are the exceptions that are usually raised.
185185
A subclass of :exc:`ImportError` which is raised by :keyword:`import` when a
186186
module could not be located. This includes ``from ... import`` statements as
187187
the specific attribute being requested cannot be known a priori to be a module
188-
or some other type of object. It is also raised when ``None`` is found in
189-
:data:`sys.modules`.
188+
or some other type of object.
190189

191190
.. versionadded:: 3.4
192191

Doc/reference/import.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use the standard import system.
3737

3838
When a module is first imported, Python searches for the module and if found,
3939
it creates a module object [#fnmo]_, initializing it. If the named module
40-
cannot be found, an :exc:`ModuleNotFoundError` is raised. Python implements various
40+
cannot be found, an :exc:`ImportError` is raised. Python implements various
4141
strategies to search for the named module when the import machinery is
4242
invoked. These strategies can be modified and extended by using various hooks
4343
described in the sections below.
@@ -168,7 +168,7 @@ arguments to the :keyword:`import` statement, or from the parameters to the
168168
This name will be used in various phases of the import search, and it may be
169169
the dotted path to a submodule, e.g. ``foo.bar.baz``. In this case, Python
170170
first tries to import ``foo``, then ``foo.bar``, and finally ``foo.bar.baz``.
171-
If any of the intermediate imports fail, an :exc:`ModuleNotFoundError` is raised.
171+
If any of the intermediate imports fail, an :exc:`ImportError` is raised.
172172

173173

174174
The module cache
@@ -187,15 +187,15 @@ object.
187187
During import, the module name is looked up in :data:`sys.modules` and if
188188
present, the associated value is the module satisfying the import, and the
189189
process completes. However, if the value is ``None``, then an
190-
:exc:`ModuleNotFoundError` is raised. If the module name is missing, Python will
190+
:exc:`ImportError` is raised. If the module name is missing, Python will
191191
continue searching for the module.
192192

193193
:data:`sys.modules` is writable. Deleting a key may not destroy the
194194
associated module (as other modules may hold references to it),
195195
but it will invalidate the cache entry for the named module, causing
196196
Python to search anew for the named module upon its next
197197
import. The key can also be assigned to ``None``, forcing the next import
198-
of the module to result in an :exc:`ModuleNotFoundError`.
198+
of the module to result in an :exc:`ImportError`.
199199

200200
Beware though, as if you keep a reference to the module object,
201201
invalidate its cache entry in :data:`sys.modules`, and then re-import the
@@ -284,7 +284,7 @@ handle the named module or not.
284284
If the meta path finder knows how to handle the named module, it returns a
285285
loader object. If it cannot handle the named module, it returns ``None``. If
286286
:data:`sys.meta_path` processing reaches the end of its list without returning
287-
a loader, then an :exc:`ModuleNotFoundError` is raised. Any other exceptions raised
287+
a loader, then an :exc:`ImportError` is raised. Any other exceptions raised
288288
are simply propagated up, aborting the import process.
289289

290290
The :meth:`find_module()` method of meta path finders is called with two
@@ -647,7 +647,7 @@ import statements within that module.
647647

648648
To selectively prevent import of some modules from a hook early on the
649649
meta path (rather than disabling the standard import system entirely),
650-
it is sufficient to raise :exc:`ModuleNotFoundError` directly from
650+
it is sufficient to raise :exc:`ImportError` directly from
651651
:meth:`find_module` instead of returning ``None``. The latter indicates
652652
that the meta path search should continue. while raising an exception
653653
terminates it immediately.

Doc/whatsnew/3.4.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ that may require changes to your code.
312312
using ``hasattr(module, '__path__')``.
313313

314314
* :c:func:`PyErr_SetImportError` now sets :exc:`TypeError` when its **msg**
315-
argument is not set. Previously only ``NULL`` was returned.
315+
argument is not set. Previously only ``NULL`` was returned with no exception
316+
set.
316317

317318
* :func:`py_compile.compile` now raises :exc:`FileExistsError` if the file path
318319
it would write to is a symlink or a non-regular file. This is to act as a

Include/pyerrors.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,6 @@ PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErr(PyObject *, int);
268268

269269
PyAPI_FUNC(PyObject *) PyErr_SetExcWithArgsKwargs(PyObject *, PyObject *,
270270
PyObject *);
271-
272-
PyAPI_FUNC(PyObject *) PyErr_SetImportErrorSubclass(PyObject *, PyObject *,
273-
PyObject *, PyObject *);
274271
PyAPI_FUNC(PyObject *) PyErr_SetImportError(PyObject *, PyObject *,
275272
PyObject *);
276273

Lib/importlib/_bootstrap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1617,7 +1617,7 @@ def _gcd_import(name, package=None, level=0):
16171617
_imp.release_lock()
16181618
message = ("import of {} halted; "
16191619
"None in sys.modules".format(name))
1620-
raise ModuleNotFoundError(message, name=name)
1620+
raise ImportError(message, name=name)
16211621
_lock_unlock_module(name)
16221622
return module
16231623

Misc/NEWS

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,6 @@ Documentation
539539
C-API
540540
-----
541541

542-
- Issue #15767: Added PyErr_SetImportErrorSubclass().
543-
544-
- PyErr_SetImportError() now sets TypeError when its msg argument is set.
545-
546542
- Issue #9369: The types of `char*` arguments of PyObject_CallFunction() and
547543
PyObject_CallMethod() now changed to `const char*`. Based on patches by
548544
Jörg Müller and Lars Buitinck.

Python/errors.c

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -619,25 +619,12 @@ PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
619619
#endif /* MS_WINDOWS */
620620

621621
PyObject *
622-
PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
623-
PyObject *name, PyObject *path)
622+
PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
624623
{
625-
int issubclass;
626624
PyObject *args, *kwargs, *error;
627625

628-
issubclass = PyObject_IsSubclass(exception, PyExc_ImportError);
629-
if (issubclass < 0) {
630-
return NULL;
631-
}
632-
else if (!issubclass) {
633-
PyErr_SetString(PyExc_TypeError, "expected a subclass of ImportError");
634-
return NULL;
635-
}
636-
637-
if (msg == NULL) {
638-
PyErr_SetString(PyExc_TypeError, "expected a message argument");
626+
if (msg == NULL)
639627
return NULL;
640-
}
641628

642629
args = PyTuple_New(1);
643630
if (args == NULL)
@@ -662,7 +649,7 @@ PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
662649
PyDict_SetItemString(kwargs, "name", name);
663650
PyDict_SetItemString(kwargs, "path", path);
664651

665-
error = PyObject_Call(exception, args, kwargs);
652+
error = PyObject_Call(PyExc_ImportError, args, kwargs);
666653
if (error != NULL) {
667654
PyErr_SetObject((PyObject *)Py_TYPE(error), error);
668655
Py_DECREF(error);
@@ -674,12 +661,6 @@ PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
674661
return NULL;
675662
}
676663

677-
PyObject *
678-
PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
679-
{
680-
return PyErr_SetImportErrorSubclass(PyExc_ImportError, msg, name, path);
681-
}
682-
683664
void
684665
_PyErr_BadInternalCall(const char *filename, int lineno)
685666
{

Python/import.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,8 +1428,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
14281428
PyObject *msg = PyUnicode_FromFormat("import of %R halted; "
14291429
"None in sys.modules", abs_name);
14301430
if (msg != NULL) {
1431-
PyErr_SetImportErrorSubclass(PyExc_ModuleNotFoundError, msg,
1432-
abs_name, NULL);
1431+
PyErr_SetImportError(msg, abs_name, NULL);
14331432
Py_DECREF(msg);
14341433
}
14351434
mod = NULL;

0 commit comments

Comments
 (0)