[Python-checkins] cpython (3.6): Issue #28214: Improved exception reporting for problematic __set_name__
serhiy.storchaka
python-checkins at python.org
Fri Oct 21 10:15:47 EDT 2016
https://hg.python.org/cpython/rev/f7e1e39ccddd
changeset: 104610:f7e1e39ccddd
branch: 3.6
parent: 104608:969c8bfe8872
user: Serhiy Storchaka <storchaka at gmail.com>
date: Fri Oct 21 17:13:31 2016 +0300
summary:
Issue #28214: Improved exception reporting for problematic __set_name__
attributes.
files:
Lib/test/test_subclassinit.py | 26 ++++++++++++++++------
Misc/NEWS | 3 ++
Objects/typeobject.c | 7 +++++-
3 files changed, 28 insertions(+), 8 deletions(-)
diff --git a/Lib/test/test_subclassinit.py b/Lib/test/test_subclassinit.py
--- a/Lib/test/test_subclassinit.py
+++ b/Lib/test/test_subclassinit.py
@@ -133,20 +133,32 @@
def test_set_name_error(self):
class Descriptor:
def __set_name__(self, owner, name):
- raise RuntimeError
+ 1/0
- with self.assertRaises(RuntimeError):
- class A:
- d = Descriptor()
+ with self.assertRaises(RuntimeError) as cm:
+ class NotGoingToWork:
+ attr = Descriptor()
+
+ exc = cm.exception
+ self.assertRegex(str(exc), r'\bNotGoingToWork\b')
+ self.assertRegex(str(exc), r'\battr\b')
+ self.assertRegex(str(exc), r'\bDescriptor\b')
+ self.assertIsInstance(exc.__cause__, ZeroDivisionError)
def test_set_name_wrong(self):
class Descriptor:
def __set_name__(self):
pass
- with self.assertRaises(TypeError):
- class A:
- d = Descriptor()
+ with self.assertRaises(RuntimeError) as cm:
+ class NotGoingToWork:
+ attr = Descriptor()
+
+ exc = cm.exception
+ self.assertRegex(str(exc), r'\bNotGoingToWork\b')
+ self.assertRegex(str(exc), r'\battr\b')
+ self.assertRegex(str(exc), r'\bDescriptor\b')
+ self.assertIsInstance(exc.__cause__, TypeError)
def test_set_name_lookup(self):
resolved = []
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
Core and Builtins
-----------------
+- Issue #28214: Improved exception reporting for problematic __set_name__
+ attributes.
+
- Issue #23782: Fixed possible memory leak in _PyTraceback_Add() and exception
loss in PyTraceBack_Here().
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -7022,8 +7022,13 @@
if (set_name != NULL) {
tmp = PyObject_CallFunctionObjArgs(set_name, type, key, NULL);
Py_DECREF(set_name);
- if (tmp == NULL)
+ if (tmp == NULL) {
+ _PyErr_FormatFromCause(PyExc_RuntimeError,
+ "Error calling __set_name__ on '%.100s' instance %R "
+ "in '%.100s'",
+ value->ob_type->tp_name, key, type->tp_name);
return -1;
+ }
else
Py_DECREF(tmp);
}
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list