Skip to content

Commit 5414b97

Browse files
gh-123309: Remove check for redefined memo entry in pickletools.dis() (GH-123374)
Such pickles are supported by the Unpickler even if the Pickler does not produce them.
1 parent fc897fc commit 5414b97

File tree

3 files changed

+11
-24
lines changed

3 files changed

+11
-24
lines changed

Lib/pickletools.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -2429,8 +2429,6 @@ def dis(pickle, out=None, memo=None, indentlevel=4, annotate=0):
24292429
+ A memo entry isn't referenced before it's defined.
24302430
24312431
+ The markobject isn't stored in the memo.
2432-
2433-
+ A memo entry isn't redefined.
24342432
"""
24352433

24362434
# Most of the hair here is for sanity checks, but most of it is needed
@@ -2484,7 +2482,7 @@ def dis(pickle, out=None, memo=None, indentlevel=4, annotate=0):
24842482
assert opcode.name == "POP"
24852483
numtopop = 0
24862484
else:
2487-
errormsg = markmsg = "no MARK exists on stack"
2485+
errormsg = "no MARK exists on stack"
24882486

24892487
# Check for correct memo usage.
24902488
if opcode.name in ("PUT", "BINPUT", "LONG_BINPUT", "MEMOIZE"):
@@ -2494,9 +2492,7 @@ def dis(pickle, out=None, memo=None, indentlevel=4, annotate=0):
24942492
else:
24952493
assert arg is not None
24962494
memo_idx = arg
2497-
if memo_idx in memo:
2498-
errormsg = "memo key %r already defined" % arg
2499-
elif not stack:
2495+
if not stack:
25002496
errormsg = "stack is empty -- can't store into memo"
25012497
elif stack[-1] is markobject:
25022498
errormsg = "can't store markobject in the memo"

Lib/test/test_pickletools.py

+8-18
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def test_mark_without_pos(self):
206206
def test_no_mark(self):
207207
self.check_dis_error(b'Nt.', '''\
208208
0: N NONE
209-
1: t TUPLE no MARK exists on stack
209+
1: t TUPLE
210210
''', 'no MARK exists on stack')
211211

212212
def test_put(self):
@@ -221,26 +221,16 @@ def test_put(self):
221221
''')
222222

223223
def test_put_redefined(self):
224-
self.check_dis_error(b'Np1\np1\n.', '''\
224+
self.check_dis(b'Np1\np1\nq\x01r\x01\x00\x00\x00\x94.', '''\
225225
0: N NONE
226226
1: p PUT 1
227227
4: p PUT 1
228-
''', 'memo key 1 already defined')
229-
self.check_dis_error(b'Np1\nq\x01.', '''\
230-
0: N NONE
231-
1: p PUT 1
232-
4: q BINPUT 1
233-
''', 'memo key 1 already defined')
234-
self.check_dis_error(b'Np1\nr\x01\x00\x00\x00.', '''\
235-
0: N NONE
236-
1: p PUT 1
237-
4: r LONG_BINPUT 1
238-
''', 'memo key 1 already defined')
239-
self.check_dis_error(b'Np1\n\x94.', '''\
240-
0: N NONE
241-
1: p PUT 1
242-
4: \\x94 MEMOIZE (as 1)
243-
''', 'memo key None already defined')
228+
7: q BINPUT 1
229+
9: r LONG_BINPUT 1
230+
14: \\x94 MEMOIZE (as 1)
231+
15: . STOP
232+
highest protocol among opcodes = 4
233+
''')
244234

245235
def test_put_empty_stack(self):
246236
self.check_dis_error(b'p0\n', '''\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove check for redefined memo entry in :func:`pickletools.dis`.

0 commit comments

Comments
 (0)