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
2 changes: 1 addition & 1 deletion Doc/using/mac.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Current installers provide a
`universal2 binary <https://en.wikipedia.org/wiki/Universal_binary>`_ build
of Python which runs natively on all Macs (Apple Silicon and Intel) that are
supported by a wide range of macOS versions,
currently typically from at least **macOS 10.13 High Sierra** on.
currently typically from at least **macOS 10.15 Catalina** on.

The downloaded file is a standard macOS installer package file (``.pkg``).
File integrity information (checksum, size, sigstore signature, etc) for each file is included
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
32 changes: 23 additions & 9 deletions Tools/build/check_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,29 @@
import sys
from collections import defaultdict
from pathlib import Path
from typing import NamedTuple
from typing import NamedTuple, TypedDict


class IgnoreRule(NamedTuple):
file_path: str
count: int
count: int # type: ignore[assignment]
ignore_all: bool = False
is_directory: bool = False


class CompileWarning(TypedDict):
file: str
line: str
column: str
message: str
option: str


def parse_warning_ignore_file(file_path: str) -> set[IgnoreRule]:
"""
Parses the warning ignore file and returns a set of IgnoreRules
"""
files_with_expected_warnings = set()
files_with_expected_warnings: set[IgnoreRule] = set()
with Path(file_path).open(encoding="UTF-8") as ignore_rules_file:
files_with_expected_warnings = set()
for i, line in enumerate(ignore_rules_file):
Expand All @@ -46,7 +54,7 @@ def parse_warning_ignore_file(file_path: str) -> set[IgnoreRule]:
)
sys.exit(1)
if ignore_all:
count = 0
count = "0"

files_with_expected_warnings.add(
IgnoreRule(
Expand All @@ -61,7 +69,7 @@ def extract_warnings_from_compiler_output(
compiler_output: str,
compiler_output_type: str,
path_prefix: str = "",
) -> list[dict]:
) -> list[CompileWarning]:
"""
Extracts warnings from the compiler output based on compiler
output type. Removes path prefix from file paths if provided.
Expand All @@ -78,8 +86,12 @@ def extract_warnings_from_compiler_output(
r"(?P<file>.*):(?P<line>\d+):(?P<column>\d+): warning: "
r"(?P<message>.*) (?P<option>\[-[^\]]+\])$"
)
else:
raise RuntimeError(
f"Unsupported compiler output type: {compiler_output_type}",
)
compiled_regex = re.compile(regex_pattern)
compiler_warnings = []
compiler_warnings: list[CompileWarning] = []
for i, line in enumerate(compiler_output.splitlines(), start=1):
if match := compiled_regex.match(line):
try:
Expand All @@ -100,7 +112,9 @@ def extract_warnings_from_compiler_output(
return compiler_warnings


def get_warnings_by_file(warnings: list[dict]) -> dict[str, list[dict]]:
def get_warnings_by_file(
warnings: list[CompileWarning],
) -> dict[str, list[CompileWarning]]:
"""
Returns a dictionary where the key is the file and the data is the
warnings in that file. Does not include duplicate warnings for a
Expand Down Expand Up @@ -138,7 +152,7 @@ def is_file_ignored(

def get_unexpected_warnings(
ignore_rules: set[IgnoreRule],
files_with_warnings: set[IgnoreRule],
files_with_warnings: dict[str, list[CompileWarning]],
) -> int:
"""
Returns failure status if warnings discovered in list of warnings
Expand Down Expand Up @@ -180,7 +194,7 @@ def get_unexpected_warnings(

def get_unexpected_improvements(
ignore_rules: set[IgnoreRule],
files_with_warnings: set[IgnoreRule],
files_with_warnings: dict[str, list[CompileWarning]],
) -> int:
"""
Returns failure status if the number of warnings for a file is greater
Expand Down
Loading
Loading