mirror of
https://github.com/python/cpython.git
synced 2025-10-19 16:03:42 +00:00
[3.13] gh-78465: Fix error message for cls.__new__(cls, ...) where cls is not instantiable (GH-135981) (GH-136031)
Previous error message suggested to use cls.__new__(), which
obviously does not work. Now the error message is the same as for
cls(...).
(cherry picked from commit c45f4f3ebe
)
This commit is contained in:
parent
ea25f4a8ec
commit
3576e1a954
4 changed files with 9 additions and 6 deletions
|
@ -2269,6 +2269,7 @@ def check_disallow_instantiation(testcase, tp, *args, **kwds):
|
||||||
qualname = f"{name}"
|
qualname = f"{name}"
|
||||||
msg = f"cannot create '{re.escape(qualname)}' instances"
|
msg = f"cannot create '{re.escape(qualname)}' instances"
|
||||||
testcase.assertRaisesRegex(TypeError, msg, tp, *args, **kwds)
|
testcase.assertRaisesRegex(TypeError, msg, tp, *args, **kwds)
|
||||||
|
testcase.assertRaisesRegex(TypeError, msg, tp.__new__, tp, *args, **kwds)
|
||||||
|
|
||||||
def get_recursion_depth():
|
def get_recursion_depth():
|
||||||
"""Get the recursion depth of the caller function.
|
"""Get the recursion depth of the caller function.
|
||||||
|
|
|
@ -894,12 +894,7 @@ def test_sys_flags(self):
|
||||||
def assert_raise_on_new_sys_type(self, sys_attr):
|
def assert_raise_on_new_sys_type(self, sys_attr):
|
||||||
# Users are intentionally prevented from creating new instances of
|
# Users are intentionally prevented from creating new instances of
|
||||||
# sys.flags, sys.version_info, and sys.getwindowsversion.
|
# sys.flags, sys.version_info, and sys.getwindowsversion.
|
||||||
arg = sys_attr
|
support.check_disallow_instantiation(self, type(sys_attr), sys_attr)
|
||||||
attr_type = type(sys_attr)
|
|
||||||
with self.assertRaises(TypeError):
|
|
||||||
attr_type(arg)
|
|
||||||
with self.assertRaises(TypeError):
|
|
||||||
attr_type.__new__(attr_type, arg)
|
|
||||||
|
|
||||||
def test_sys_flags_no_instantiation(self):
|
def test_sys_flags_no_instantiation(self):
|
||||||
self.assert_raise_on_new_sys_type(sys.flags)
|
self.assert_raise_on_new_sys_type(sys.flags)
|
||||||
|
|
|
@ -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``).
|
|
@ -9087,6 +9087,11 @@ tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
|
||||||
/* If staticbase is NULL now, it is a really weird type.
|
/* If staticbase is NULL now, it is a really weird type.
|
||||||
In the spirit of backwards compatibility (?), just shut up. */
|
In the spirit of backwards compatibility (?), just shut up. */
|
||||||
if (staticbase && staticbase->tp_new != type->tp_new) {
|
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,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"%s.__new__(%s) is not safe, use %s.__new__()",
|
"%s.__new__(%s) is not safe, use %s.__new__()",
|
||||||
type->tp_name,
|
type->tp_name,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue