Skip to content

Commit a3076c7

Browse files
[3.12] gh-122311: Fix some error messages in pickle (GH-122386) (GH-122388)
(cherry picked from commit 3b034d2) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent ecc97cb commit a3076c7

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

Lib/pickle.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,16 +314,17 @@ def load_frame(self, frame_size):
314314
# Tools used for pickling.
315315

316316
def _getattribute(obj, name):
317+
top = obj
317318
for subpath in name.split('.'):
318319
if subpath == '<locals>':
319320
raise AttributeError("Can't get local attribute {!r} on {!r}"
320-
.format(name, obj))
321+
.format(name, top))
321322
try:
322323
parent = obj
323324
obj = getattr(obj, subpath)
324325
except AttributeError:
325326
raise AttributeError("Can't get attribute {!r} on {!r}"
326-
.format(name, obj)) from None
327+
.format(name, top)) from None
327328
return obj, parent
328329

329330
def whichmodule(obj, name):
@@ -830,7 +831,7 @@ def save_bytearray(self, obj):
830831
if _HAVE_PICKLE_BUFFER:
831832
def save_picklebuffer(self, obj):
832833
if self.proto < 5:
833-
raise PicklingError("PickleBuffer can only pickled with "
834+
raise PicklingError("PickleBuffer can only be pickled with "
834835
"protocol >= 5")
835836
with obj.raw() as m:
836837
if not m.contiguous:

Lib/test/pickletester.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1984,8 +1984,10 @@ def test_picklebuffer_error(self):
19841984
pb = pickle.PickleBuffer(b"foobar")
19851985
for proto in range(0, 5):
19861986
with self.subTest(proto=proto):
1987-
with self.assertRaises(pickle.PickleError):
1987+
with self.assertRaises(pickle.PickleError) as cm:
19881988
self.dumps(pb, proto)
1989+
self.assertEqual(str(cm.exception),
1990+
'PickleBuffer can only be pickled with protocol >= 5')
19891991

19901992
def test_non_continuous_buffer(self):
19911993
if self.pickler is pickle._Pickler:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix some error messages in :mod:`pickle`.

Modules/_pickle.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,10 +1876,10 @@ get_dotted_path(PyObject *obj, PyObject *name)
18761876
if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
18771877
if (obj == NULL)
18781878
PyErr_Format(PyExc_AttributeError,
1879-
"Can't pickle local object %R", name);
1879+
"Can't get local object %R", name);
18801880
else
18811881
PyErr_Format(PyExc_AttributeError,
1882-
"Can't pickle local attribute %R on %R", name, obj);
1882+
"Can't get local attribute %R on %R", name, obj);
18831883
Py_DECREF(dotted_path);
18841884
return NULL;
18851885
}
@@ -2566,7 +2566,7 @@ save_picklebuffer(PickleState *st, PicklerObject *self, PyObject *obj)
25662566
{
25672567
if (self->proto < 5) {
25682568
PyErr_SetString(st->PicklingError,
2569-
"PickleBuffer can only pickled with protocol >= 5");
2569+
"PickleBuffer can only be pickled with protocol >= 5");
25702570
return -1;
25712571
}
25722572
const Py_buffer* view = PyPickleBuffer_GetBuffer(obj);

0 commit comments

Comments
 (0)