Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ on:
- "Lib/test/libregrtest/**"
- "Lib/tomllib/**"
- "Misc/mypy/**"
- "Tools/build/mypy.ini"
- "Tools/build/check_extension_modules.py"
- "Tools/build/check_warnings.py"
- "Tools/build/compute-changes.py"
- "Tools/build/deepfreeze.py"
- "Tools/build/generate-build-details.py"
- "Tools/build/generate_sbom.py"
- "Tools/build/generate_stdlib_module_names.py"
- "Tools/build/generate-build-details.py"
- "Tools/build/verify_ensurepip_wheels.py"
- "Tools/build/update_file.py"
- "Tools/build/mypy.ini"
- "Tools/build/umarshal.py"
- "Tools/build/update_file.py"
- "Tools/build/verify_ensurepip_wheels.py"
- "Tools/cases_generator/**"
- "Tools/clinic/**"
- "Tools/jit/**"
Expand Down
4 changes: 2 additions & 2 deletions Doc/bugs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ tracker <https://github.com/python/python-docs-theme>`_.
`Helping with Documentation <https://devguide.python.org/docquality/#helping-with-documentation>`_
Comprehensive guide for individuals that are interested in contributing to Python documentation.

`Documentation Translations <https://devguide.python.org/documentation/translating/>`_
A list of GitHub pages for documentation translation and their primary contacts.
`Documentation Translations <https://devguide.python.org/documentation/translations/translating/#translation-coordinators>`_
A list of GitHub pages for documentation translation and their coordination teams.


.. _using-the-tracker:
Expand Down
2 changes: 1 addition & 1 deletion Doc/tutorial/appendix.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ basic interpreter is supported on all platforms with minimal line
control capabilities.

On Windows, or Unix-like systems with :mod:`curses` support,
a new interactive shell is used by default.
a new interactive shell is used by default since Python 3.13.
This one supports color, multiline editing, history browsing, and
paste mode. To disable color, see :ref:`using-on-controlling-color` for
details. Function keys provide some additional functionality.
Expand Down
7 changes: 4 additions & 3 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ A new flag has been added, :data:`~sys.flags.context_aware_warnings`. This
flag defaults to true for the free-threaded build and false for the GIL-enabled
build. If the flag is true then the :class:`warnings.catch_warnings` context
manager uses a context variable for warning filters. This makes the context
manager behave predicably when used with multiple threads or asynchronous
manager behave predictably when used with multiple threads or asynchronous
tasks.

A new flag has been added, :data:`~sys.flags.thread_inherit_context`. This flag
Expand Down Expand Up @@ -1648,7 +1648,7 @@ io
:exc:`BlockingIOError` if the operation cannot immediately return bytes.
(Contributed by Giovanni Siragusa in :gh:`109523`.)

* Add protocols :class:`io.Reader` and :class:`io.Writer` as a simpler
* Add protocols :class:`io.Reader` and :class:`io.Writer` as simpler
alternatives to the pseudo-protocols :class:`typing.IO`,
:class:`typing.TextIO`, and :class:`typing.BinaryIO`.
(Contributed by Sebastian Rittau in :gh:`127648`.)
Expand Down Expand Up @@ -2148,6 +2148,8 @@ typing

(Contributed by Jelle Zijlstra in :gh:`105499`.)

* :class:`typing.TypeAliasType` now supports star unpacking.


unicodedata
-----------
Expand Down Expand Up @@ -2731,7 +2733,6 @@ typing
* Remove :class:`!typing.ByteString`. It had previously raised a
:exc:`DeprecationWarning` since Python 3.12.

* :class:`typing.TypeAliasType` now supports star unpacking.

urllib
------
Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ Other language changes
controlled by :ref:`environment variables <using-on-controlling-color>`.
(Contributed by Peter Bierma in :gh:`134170`.)

* The :meth:`~object.__repr__` of :class:`ImportError` and :class:`ModuleNotFoundError`
now shows "name" and "path" as ``name=<name>`` and ``path=<path>`` if they were given
as keyword arguments at construction time.
(Contributed by Serhiy Storchaka, Oleg Iarygin, and Yoav Nir in :gh:`74185`.)

New modules
===========
Expand Down
9 changes: 6 additions & 3 deletions Lib/_py_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,12 @@ def warn(message, category=None, stacklevel=1, source=None,
# Check category argument
if category is None:
category = UserWarning
if not (isinstance(category, type) and issubclass(category, Warning)):
raise TypeError("category must be a Warning subclass, "
"not '{:s}'".format(type(category).__name__))
elif not isinstance(category, type):
raise TypeError(f"category must be a Warning subclass, not "
f"'{type(category).__name__}'")
elif not issubclass(category, Warning):
raise TypeError(f"category must be a Warning subclass, not "
f"class '{category.__name__}'")
if not isinstance(skip_file_prefixes, tuple):
# The C version demands a tuple for implementation performance.
raise TypeError('skip_file_prefixes must be a tuple of strs.')
Expand Down
44 changes: 44 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2079,6 +2079,50 @@ def test_copy_pickle(self):
self.assertEqual(exc.name, orig.name)
self.assertEqual(exc.path, orig.path)

def test_repr(self):
exc = ImportError()
self.assertEqual(repr(exc), "ImportError()")

exc = ImportError('test')
self.assertEqual(repr(exc), "ImportError('test')")

exc = ImportError('test', 'case')
self.assertEqual(repr(exc), "ImportError('test', 'case')")

exc = ImportError(name='somemodule')
self.assertEqual(repr(exc), "ImportError(name='somemodule')")

exc = ImportError('test', name='somemodule')
self.assertEqual(repr(exc), "ImportError('test', name='somemodule')")

exc = ImportError(path='somepath')
self.assertEqual(repr(exc), "ImportError(path='somepath')")

exc = ImportError('test', path='somepath')
self.assertEqual(repr(exc), "ImportError('test', path='somepath')")

exc = ImportError(name='somename', path='somepath')
self.assertEqual(repr(exc),
"ImportError(name='somename', path='somepath')")

exc = ImportError('test', name='somename', path='somepath')
self.assertEqual(repr(exc),
"ImportError('test', name='somename', path='somepath')")

exc = ModuleNotFoundError('test', name='somename', path='somepath')
self.assertEqual(repr(exc),
"ModuleNotFoundError('test', name='somename', path='somepath')")

def test_ModuleNotFoundError_repr_with_failed_import(self):
with self.assertRaises(ModuleNotFoundError) as cm:
import does_not_exist # type: ignore[import] # noqa: F401

self.assertEqual(cm.exception.name, "does_not_exist")
self.assertIsNone(cm.exception.path)

self.assertEqual(repr(cm.exception),
"ModuleNotFoundError(\"No module named 'does_not_exist'\", name='does_not_exist')")


def run_script(source):
if isinstance(source, str):
Expand Down
20 changes: 7 additions & 13 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,25 +596,19 @@ def test_warning_classes(self):
class MyWarningClass(Warning):
pass

class NonWarningSubclass:
pass

# passing a non-subclass of Warning should raise a TypeError
with self.assertRaises(TypeError) as cm:
expected = "category must be a Warning subclass, not 'str'"
with self.assertRaisesRegex(TypeError, expected):
self.module.warn('bad warning category', '')
self.assertIn('category must be a Warning subclass, not ',
str(cm.exception))

with self.assertRaises(TypeError) as cm:
self.module.warn('bad warning category', NonWarningSubclass)
self.assertIn('category must be a Warning subclass, not ',
str(cm.exception))
expected = "category must be a Warning subclass, not class 'int'"
with self.assertRaisesRegex(TypeError, expected):
self.module.warn('bad warning category', int)

# check that warning instances also raise a TypeError
with self.assertRaises(TypeError) as cm:
expected = "category must be a Warning subclass, not '.*MyWarningClass'"
with self.assertRaisesRegex(TypeError, expected):
self.module.warn('bad warning category', MyWarningClass())
self.assertIn('category must be a Warning subclass, not ',
str(cm.exception))

with self.module.catch_warnings():
self.module.resetwarnings()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The :meth:`~object.__repr__` of :class:`ImportError` and :class:`ModuleNotFoundError`
now shows "name" and "path" as ``name=<name>`` and ``path=<path>`` if they were given
as keyword arguments at construction time.
Patch by Serhiy Storchaka, Oleg Iarygin, and Yoav Nir
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve error messages for invalid category in :func:`warnings.warn`.
82 changes: 76 additions & 6 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,62 @@ ImportError_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
return res;
}

static PyObject *
ImportError_repr(PyObject *self)
{
int hasargs = PyTuple_GET_SIZE(((PyBaseExceptionObject *)self)->args) != 0;
PyImportErrorObject *exc = PyImportErrorObject_CAST(self);
if (exc->name == NULL && exc->path == NULL) {
return BaseException_repr(self);
}
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
if (writer == NULL) {
goto error;
}
PyObject *r = BaseException_repr(self);
if (r == NULL) {
goto error;
}
if (PyUnicodeWriter_WriteSubstring(
writer, r, 0, PyUnicode_GET_LENGTH(r) - 1) < 0)
{
Py_DECREF(r);
goto error;
}
Py_DECREF(r);
if (exc->name) {
if (hasargs) {
if (PyUnicodeWriter_WriteASCII(writer, ", ", 2) < 0) {
goto error;
}
}
if (PyUnicodeWriter_Format(writer, "name=%R", exc->name) < 0) {
goto error;
}
hasargs = 1;
}
if (exc->path) {
if (hasargs) {
if (PyUnicodeWriter_WriteASCII(writer, ", ", 2) < 0) {
goto error;
}
}
if (PyUnicodeWriter_Format(writer, "path=%R", exc->path) < 0) {
goto error;
}
}

if (PyUnicodeWriter_WriteChar(writer, ')') < 0) {
goto error;
}

return PyUnicodeWriter_Finish(writer);

error:
PyUnicodeWriter_Discard(writer);
return NULL;
}

static PyMemberDef ImportError_members[] = {
{"msg", _Py_T_OBJECT, offsetof(PyImportErrorObject, msg), 0,
PyDoc_STR("exception message")},
Expand All @@ -1881,12 +1937,26 @@ static PyMethodDef ImportError_methods[] = {
{NULL}
};

ComplexExtendsException(PyExc_Exception, ImportError,
ImportError, 0 /* new */,
ImportError_methods, ImportError_members,
0 /* getset */, ImportError_str,
"Import can't find module, or can't find name in "
"module.");
static PyTypeObject _PyExc_ImportError = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "ImportError",
.tp_basicsize = sizeof(PyImportErrorObject),
.tp_dealloc = ImportError_dealloc,
.tp_repr = ImportError_repr,
.tp_str = ImportError_str,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
.tp_doc = PyDoc_STR(
"Import can't find module, "
"or can't find name in module."),
.tp_traverse = ImportError_traverse,
.tp_clear = ImportError_clear,
.tp_methods = ImportError_methods,
.tp_members = ImportError_members,
.tp_base = &_PyExc_Exception,
.tp_dictoffset = offsetof(PyImportErrorObject, dict),
.tp_init = ImportError_init,
};
PyObject *PyExc_ImportError = (PyObject *)&_PyExc_ImportError;

/*
* ModuleNotFoundError extends ImportError
Expand Down
39 changes: 17 additions & 22 deletions Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -823,11 +823,7 @@ warn_explicit(PyThreadState *tstate, PyObject *category, PyObject *message,

/* Normalize message. */
Py_INCREF(message); /* DECREF'ed in cleanup. */
rc = PyObject_IsInstance(message, PyExc_Warning);
if (rc == -1) {
goto cleanup;
}
if (rc == 1) {
if (PyObject_TypeCheck(message, (PyTypeObject *)PyExc_Warning)) {
text = PyObject_Str(message);
if (text == NULL)
goto cleanup;
Expand Down Expand Up @@ -1124,26 +1120,25 @@ setup_context(Py_ssize_t stack_level,
static PyObject *
get_category(PyObject *message, PyObject *category)
{
int rc;

/* Get category. */
rc = PyObject_IsInstance(message, PyExc_Warning);
if (rc == -1)
return NULL;

if (rc == 1)
category = (PyObject*)Py_TYPE(message);
else if (category == NULL || category == Py_None)
category = PyExc_UserWarning;
if (PyObject_TypeCheck(message, (PyTypeObject *)PyExc_Warning)) {
/* Ignore the category argument. */
return (PyObject*)Py_TYPE(message);
}
if (category == NULL || category == Py_None) {
return PyExc_UserWarning;
}

/* Validate category. */
rc = PyObject_IsSubclass(category, PyExc_Warning);
/* category is not a subclass of PyExc_Warning or
PyObject_IsSubclass raised an error */
if (rc == -1 || rc == 0) {
if (!PyType_Check(category)) {
PyErr_Format(PyExc_TypeError,
"category must be a Warning subclass, not '%T'",
category);
return NULL;
}
if (!PyType_IsSubtype((PyTypeObject *)category, (PyTypeObject *)PyExc_Warning)) {
PyErr_Format(PyExc_TypeError,
"category must be a Warning subclass, not '%s'",
Py_TYPE(category)->tp_name);
"category must be a Warning subclass, not class '%N'",
(PyTypeObject *)category);
return NULL;
}

Expand Down
Loading
Loading