[Python-checkins] bpo-44655: Include the name of the type in unset __slots__ attribute errors (GH-27199) (GH-27201)
pablogsal
webhook-mailer at python.org
Fri Jul 16 20:02:19 EDT 2021
https://github.com/python/cpython/commit/efda9054b9fc91e69ecb34eae84fdd2ca8e6feef
commit: efda9054b9fc91e69ecb34eae84fdd2ca8e6feef
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: pablogsal <Pablogsal at gmail.com>
date: 2021-07-17T01:01:53+01:00
summary:
bpo-44655: Include the name of the type in unset __slots__ attribute errors (GH-27199) (GH-27201)
(cherry picked from commit f783428a2313a729ca8b539c5a86ff114b9ff375)
Co-authored-by: Pablo Galindo Salgado <Pablogsal at gmail.com>
Co-authored-by: Pablo Galindo Salgado <Pablogsal at gmail.com>
files:
A Misc/NEWS.d/next/Core and Builtins/2021-07-16-21-35-14.bpo-44655.95I7M6.rst
M Lib/test/test_descr.py
M Python/structmember.c
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index 3cb923ed0520a..c67911b8cdf71 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -1303,6 +1303,12 @@ class X(object):
with self.assertRaises(AttributeError):
del X().a
+ # Inherit from object on purpose to check some backwards compatibility paths
+ class X(object):
+ __slots__ = "a"
+ with self.assertRaisesRegex(AttributeError, "'X' object has no attribute 'a'"):
+ X().a
+
def test_slots_special(self):
# Testing __dict__ and __weakref__ in __slots__...
class D(object):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-07-16-21-35-14.bpo-44655.95I7M6.rst b/Misc/NEWS.d/next/Core and Builtins/2021-07-16-21-35-14.bpo-44655.95I7M6.rst
new file mode 100644
index 0000000000000..17733b3619a8f
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-07-16-21-35-14.bpo-44655.95I7M6.rst
@@ -0,0 +1,2 @@
+Include the name of the type in unset __slots__ attribute errors. Patch by
+Pablo Galindo
diff --git a/Python/structmember.c b/Python/structmember.c
index ba88e15f93869..c7e318811d82b 100644
--- a/Python/structmember.c
+++ b/Python/structmember.c
@@ -5,11 +5,11 @@
#include "structmember.h" // PyMemberDef
PyObject *
-PyMember_GetOne(const char *addr, PyMemberDef *l)
+PyMember_GetOne(const char *obj_addr, PyMemberDef *l)
{
PyObject *v;
- addr += l->offset;
+ const char* addr = obj_addr + l->offset;
switch (l->type) {
case T_BOOL:
v = PyBool_FromLong(*(char*)addr);
@@ -69,8 +69,13 @@ PyMember_GetOne(const char *addr, PyMemberDef *l)
break;
case T_OBJECT_EX:
v = *(PyObject **)addr;
- if (v == NULL)
- PyErr_SetString(PyExc_AttributeError, l->name);
+ if (v == NULL) {
+ PyObject *obj = (PyObject *)obj_addr;
+ PyTypeObject *tp = Py_TYPE(obj);
+ PyErr_Format(PyExc_AttributeError,
+ "'%.200s' object has no attribute '%s'",
+ tp->tp_name, l->name);
+ }
Py_XINCREF(v);
break;
case T_LONGLONG:
More information about the Python-checkins
mailing list