https://github.com/python/cpython/commit/a3076c734d310d2b860c6da5e10a67fc9c29d9b6
commit: a3076c734d310d2b860c6da5e10a67fc9c29d9b6
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-07-29T12:27:34+03:00
summary:

[3.12] gh-122311: Fix some error messages in pickle (GH-122386) (GH-122388)

(cherry picked from commit 3b034d26eb8480f8d12ae11f42d038d24cf8498a)

Co-authored-by: Serhiy Storchaka <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-07-29-10-24-48.gh-issue-122311.xChV1b.rst
M Lib/pickle.py
M Lib/test/pickletester.py
M Modules/_pickle.c

diff --git a/Lib/pickle.py b/Lib/pickle.py
index 8bf9c19237178f..c4d6e658216f17 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -314,16 +314,17 @@ def load_frame(self, frame_size):
 # Tools used for pickling.
 
 def _getattribute(obj, name):
+    top = obj
     for subpath in name.split('.'):
         if subpath == '<locals>':
             raise AttributeError("Can't get local attribute {!r} on {!r}"
-                                 .format(name, obj))
+                                 .format(name, top))
         try:
             parent = obj
             obj = getattr(obj, subpath)
         except AttributeError:
             raise AttributeError("Can't get attribute {!r} on {!r}"
-                                 .format(name, obj)) from None
+                                 .format(name, top)) from None
     return obj, parent
 
 def whichmodule(obj, name):
@@ -830,7 +831,7 @@ def save_bytearray(self, obj):
     if _HAVE_PICKLE_BUFFER:
         def save_picklebuffer(self, obj):
             if self.proto < 5:
-                raise PicklingError("PickleBuffer can only pickled with "
+                raise PicklingError("PickleBuffer can only be pickled with "
                                     "protocol >= 5")
             with obj.raw() as m:
                 if not m.contiguous:
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py
index 308fee4f8c9784..733ee8f50b6444 100644
--- a/Lib/test/pickletester.py
+++ b/Lib/test/pickletester.py
@@ -1984,8 +1984,10 @@ def test_picklebuffer_error(self):
         pb = pickle.PickleBuffer(b"foobar")
         for proto in range(0, 5):
             with self.subTest(proto=proto):
-                with self.assertRaises(pickle.PickleError):
+                with self.assertRaises(pickle.PickleError) as cm:
                     self.dumps(pb, proto)
+                self.assertEqual(str(cm.exception),
+                    'PickleBuffer can only be pickled with protocol >= 5')
 
     def test_non_continuous_buffer(self):
         if self.pickler is pickle._Pickler:
diff --git 
a/Misc/NEWS.d/next/Library/2024-07-29-10-24-48.gh-issue-122311.xChV1b.rst 
b/Misc/NEWS.d/next/Library/2024-07-29-10-24-48.gh-issue-122311.xChV1b.rst
new file mode 100644
index 00000000000000..8d70c610a8dad6
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-07-29-10-24-48.gh-issue-122311.xChV1b.rst
@@ -0,0 +1 @@
+Fix some error messages in :mod:`pickle`.
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index bb9d7764744563..4986809957204e 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -1876,10 +1876,10 @@ get_dotted_path(PyObject *obj, PyObject *name)
         if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
             if (obj == NULL)
                 PyErr_Format(PyExc_AttributeError,
-                             "Can't pickle local object %R", name);
+                             "Can't get local object %R", name);
             else
                 PyErr_Format(PyExc_AttributeError,
-                             "Can't pickle local attribute %R on %R", name, 
obj);
+                             "Can't get local attribute %R on %R", name, obj);
             Py_DECREF(dotted_path);
             return NULL;
         }
@@ -2566,7 +2566,7 @@ save_picklebuffer(PickleState *st, PicklerObject *self, 
PyObject *obj)
 {
     if (self->proto < 5) {
         PyErr_SetString(st->PicklingError,
-                        "PickleBuffer can only pickled with protocol >= 5");
+                        "PickleBuffer can only be pickled with protocol >= 5");
         return -1;
     }
     const Py_buffer* view = PyPickleBuffer_GetBuffer(obj);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to