]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-122311: Fix some error messages in pickle (GH-122386)
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 29 Jul 2024 08:49:13 +0000 (11:49 +0300)
committerGitHub <noreply@github.com>
Mon, 29 Jul 2024 08:49:13 +0000 (11:49 +0300)
Lib/pickle.py
Lib/test/pickletester.py
Misc/NEWS.d/next/Library/2024-07-29-10-24-48.gh-issue-122311.xChV1b.rst [new file with mode: 0644]
Modules/_pickle.c

index c6c151b2065f4d333f8269f044fb01e9ea7ecfb8..299c9e0e5e5641c16a8bc162cde20375128241a3 100644 (file)
@@ -314,16 +314,17 @@ class _Unframer:
 # 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):
@@ -832,7 +833,7 @@ class _Pickler:
     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:
index 174f4ff6d021b20bd34d0b28401991d2adbc346e..a2b49e6c92a7b3d4799be82eeec46059e49304e7 100644 (file)
@@ -1982,8 +1982,10 @@ class AbstractPicklingErrorTests:
         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 (file)
index 0000000..8d70c61
--- /dev/null
@@ -0,0 +1 @@
+Fix some error messages in :mod:`pickle`.
index 861363b68c20c5fe79bedc26c0d0d0a2c31b9e3e..452b4aff0237cadac8248d7cbeee47b8a31bac67 100644 (file)
@@ -1817,10 +1817,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;
         }
@@ -2507,7 +2507,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);