Skip to content

Commit 4eef2e8

Browse files
Fix check_pickle to seek to file start and use correct opcode count API (#253)
check_pickle() had three bugs: - No file.seek(0), so results depended on file position left by prior calls (_is_zipfile, tarfile.is_tarfile) - Used .opcodes() which is a @Property, not a method — calling it always raised TypeError, forcing every call through the StackedPickle fallback - StackedPickle path returned True unconditionally without min_length check Fix by seeking to start, using len(Pickled.load(file)) instead of len(Pickled.load(file).opcodes()), and validating StackedPickle results. Also fix test_legacy_pickle_properties to test the actual legacy pickle file instead of duplicating test_v1_3_properties. This test would fail without the check_pickle fix. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 61d9daf commit 4eef2e8

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

fickling/polyglot.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,15 @@ def check_numpy(file): # returns isNumpy,isNumpyPickle
143143

144144
def check_pickle(file, min_length=0):
145145
"""Checks if a file can be pickled; this does not directly determine the file is a pickle"""
146+
file.seek(0)
146147
try:
147-
opcodes = Pickled.load(file).opcodes()
148-
return len(opcodes) > min_length
148+
p = Pickled.load(file)
149+
return len(p) > min_length
149150
except Exception: # noqa
151+
file.seek(0)
150152
try:
151-
StackedPickle.load(file)
152-
return True
153+
sp = StackedPickle.load(file)
154+
return any(len(p) > min_length for p in sp.pickled)
153155
except Exception: # noqa
154156
return False
155157

test/test_polyglot.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,8 @@ def test_v1_3_properties(self):
196196
self.assertEqual(properties, proper_result)
197197

198198
def test_legacy_pickle_properties(self):
199-
properties = polyglot.find_file_properties(self.filename_v1_3)
200-
proper_result = _make_properties(
201-
is_torch_zip=True, is_standard_zip=True, has_data_pkl=True, has_version=True
202-
)
199+
properties = polyglot.find_file_properties(self.filename_legacy_pickle)
200+
proper_result = _make_properties(is_valid_pickle=True)
203201
self.assertEqual(properties, proper_result)
204202

205203
@unittest.skipIf(_lacks_torch_jit_support, "PyTorch 2.9.1 JIT broken with Python 3.14+")

0 commit comments

Comments
 (0)