Skip to content

Commit 3b034d2

Browse files
gh-122311: Fix some error messages in pickle (GH-122386)
1 parent 169e713 commit 3b034d2

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

Lib/pickle.py

+4-3
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):
@@ -832,7 +833,7 @@ def save_bytearray(self, obj):
832833
if _HAVE_PICKLE_BUFFER:
833834
def save_picklebuffer(self, obj):
834835
if self.proto < 5:
835-
raise PicklingError("PickleBuffer can only pickled with "
836+
raise PicklingError("PickleBuffer can only be pickled with "
836837
"protocol >= 5")
837838
with obj.raw() as m:
838839
if not m.contiguous:

Lib/test/pickletester.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1982,8 +1982,10 @@ def test_picklebuffer_error(self):
19821982
pb = pickle.PickleBuffer(b"foobar")
19831983
for proto in range(0, 5):
19841984
with self.subTest(proto=proto):
1985-
with self.assertRaises(pickle.PickleError):
1985+
with self.assertRaises(pickle.PickleError) as cm:
19861986
self.dumps(pb, proto)
1987+
self.assertEqual(str(cm.exception),
1988+
'PickleBuffer can only be pickled with protocol >= 5')
19871989

19881990
def test_non_continuous_buffer(self):
19891991
if self.pickler is pickle._Pickler:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix some error messages in :mod:`pickle`.

Modules/_pickle.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1817,10 +1817,10 @@ get_dotted_path(PyObject *obj, PyObject *name)
18171817
if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
18181818
if (obj == NULL)
18191819
PyErr_Format(PyExc_AttributeError,
1820-
"Can't pickle local object %R", name);
1820+
"Can't get local object %R", name);
18211821
else
18221822
PyErr_Format(PyExc_AttributeError,
1823-
"Can't pickle local attribute %R on %R", name, obj);
1823+
"Can't get local attribute %R on %R", name, obj);
18241824
Py_DECREF(dotted_path);
18251825
return NULL;
18261826
}
@@ -2507,7 +2507,7 @@ save_picklebuffer(PickleState *st, PicklerObject *self, PyObject *obj)
25072507
{
25082508
if (self->proto < 5) {
25092509
PyErr_SetString(st->PicklingError,
2510-
"PickleBuffer can only pickled with protocol >= 5");
2510+
"PickleBuffer can only be pickled with protocol >= 5");
25112511
return -1;
25122512
}
25132513
const Py_buffer* view = PyPickleBuffer_GetBuffer(obj);

0 commit comments

Comments
 (0)