8000 gh-78465: Fix error message for cls.__new__(cls, ...) where cls is not instantiable by serhiy-storchaka · Pull Request #135981 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-78465: Fix error message for cls.__new__(cls, ...) where cls is not instantiable #135981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
1 change: 1 addition & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2333,6 +2333,7 @@ def check_disallow_instantiation(testcase, tp, *args, **kwds):
qualname = f"{name}"
msg = f"cannot create '{re.escape(qualname)}' instances"
testcase.assertRaisesRegex(TypeError, msg, tp, *args, **kwds)
testcase.assertRaisesRegex(TypeError, msg, tp.__new__, tp, *args, **kwds)

def get_recursion_depth():
"""Get the recursion depth of the caller function.
Expand Down
7 changes: 1 addition & 6 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,12 +869,7 @@ def test_sys_flags(self):
def assert_raise_on_new_sys_type(self, sys_attr):
# Users are intentionally prevented from creating new instances of
# sys.flags, sys.version_info, and sys.getwindowsversion.
arg = sys_attr
attr_type = type(sys_attr)
with self.assertRaises(TypeError):
attr_type(arg)
with self.assertRaises(TypeError):
attr_type.__new__(attr_type, arg)
support.check_disallow_instantiation(self, type(sys_attr), sys_attr)

def test_sys_flags_no_instantiation(self):
self.assert_raise_on_new_sys_type(sys.flags)
Expand Down
5 changes: 2 additions & 3 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from test.support import (
run_with_locale, cpython_only, no_rerun,
MISSING_C_DOCSTRINGS, EqualToForwardRef,
MISSING_C_DOCSTRINGS, EqualToForwardRef, check_disallow_instantiation,
)
from test.support.script_helper import assert_python_ok
from test.support.import_helper import import_fresh_module
Expand Down Expand Up @@ -1148,8 +1148,7 @@ def test_or_type_operator_reference_cycle(self):
msg='Check for union reference leak.')

def test_instantiation(self):
with self.assertRaises(TypeError):
types.UnionType()
check_disallow_instantiation(self, types.UnionType)
self.assertIs(int, types.UnionType[int])
self.assertIs(int, types.UnionType[int, int])
self.assertEqual(int | str, types.UnionType[int, str])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix error message for ``cls.__new__(cls, ...)`` where ``cls`` is not
instantiable builtin or extension type (with ``tp_new`` set to ``NULL``).
5 changes: 5 additions & 0 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -10020,6 +10020,11 @@ tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
/* If staticbase is NULL now, it is a really weird type.
In the spirit of backwards compatibility (?), just shut up. */
if (staticbase && staticbase->tp_new != type->tp_new) {
if (staticbase->tp_new == NULL) {
PyErr_Format(PyExc_TypeError,
"cannot create '%s' instances", subtype->tp_name);
return NULL;
}
PyErr_Format(PyExc_TypeError,
"%s.__new__(%s) is not safe, use %s.__new__()",
type->tp_name,
Expand Down
Loading
0