cpython: f7e1e39ccddd (original) (raw)
Mercurial > cpython
changeset 104610:f7e1e39ccddd 3.6
Issue #28214: Improved exception reporting for problematic __set_name__ attributes. [#28214]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Fri, 21 Oct 2016 17:13:31 +0300 |
parents | 969c8bfe8872 |
children | 7c3ec24f4582 b33c7055220e |
files | Lib/test/test_subclassinit.py Misc/NEWS Objects/typeobject.c |
diffstat | 3 files changed, 28 insertions(+), 8 deletions(-)[+] [-] Lib/test/test_subclassinit.py 26 Misc/NEWS 3 Objects/typeobject.c 7 |
line wrap: on
line diff
--- a/Lib/test/test_subclassinit.py +++ b/Lib/test/test_subclassinit.py @@ -133,20 +133,32 @@ class Test(unittest.TestCase): def test_set_name_error(self): class Descriptor: def set_name(self, owner, name):
raise RuntimeError[](#l1.7)
1/0[](#l1.8)
with self.assertRaises(RuntimeError) as cm:[](#l1.10)
class NotGoingToWork:[](#l1.11)
attr = Descriptor()[](#l1.12)
with self.assertRaises(RuntimeError):[](#l1.14)
class A:[](#l1.15)
d = Descriptor()[](#l1.16)
exc = cm.exception[](#l1.17)
self.assertRegex(str(exc), r'\bNotGoingToWork\b')[](#l1.18)
self.assertRegex(str(exc), r'\battr\b')[](#l1.19)
self.assertRegex(str(exc), r'\bDescriptor\b')[](#l1.20)
self.assertIsInstance(exc.__cause__, ZeroDivisionError)[](#l1.21)
def test_set_name_wrong(self): class Descriptor: def set_name(self): pass
with self.assertRaises(TypeError):[](#l1.28)
class A:[](#l1.29)
d = Descriptor()[](#l1.30)
with self.assertRaises(RuntimeError) as cm:[](#l1.31)
class NotGoingToWork:[](#l1.32)
attr = Descriptor()[](#l1.33)
exc = cm.exception[](#l1.35)
self.assertRegex(str(exc), r'\bNotGoingToWork\b')[](#l1.36)
self.assertRegex(str(exc), r'\battr\b')[](#l1.37)
self.assertRegex(str(exc), r'\bDescriptor\b')[](#l1.38)
self.assertIsInstance(exc.__cause__, TypeError)[](#l1.39)
def test_set_name_lookup(self): resolved = []
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.6.0 beta 3 Core and Builtins ----------------- +- Issue #28214: Improved exception reporting for problematic set_name
- Issue #23782: Fixed possible memory leak in _PyTraceback_Add() and exception loss in PyTraceBack_Here().
--- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -7022,8 +7022,13 @@ set_names(PyTypeObject *type) if (set_name != NULL) { tmp = PyObject_CallFunctionObjArgs(set_name, type, key, NULL); Py_DECREF(set_name);
if (tmp == NULL)[](#l3.7)
if (tmp == NULL) {[](#l3.8)
_PyErr_FormatFromCause(PyExc_RuntimeError,[](#l3.9)
"Error calling __set_name__ on '%.100s' instance %R "[](#l3.10)
"in '%.100s'",[](#l3.11)
value->ob_type->tp_name, key, type->tp_name);[](#l3.12) return -1;[](#l3.13)
}[](#l3.14) else[](#l3.15) Py_DECREF(tmp);[](#l3.16) }[](#l3.17)