Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 60c762b

Browse files
committed
Issue #9094: Make python -m pickletools disassemble pickles given in
the command line.
1 parent df022da commit 60c762b

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

Lib/pickletools.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2330,4 +2330,43 @@ def _test():
23302330
return doctest.testmod()
23312331

23322332
if __name__ == "__main__":
2333-
_test()
2333+
import sys, argparse
2334+
parser = argparse.ArgumentParser(
2335+
description='disassemble one or more pickle files')
2336+
parser.add_argument(
2337+
'pickle_file', type=argparse.FileType('br'),
2338+
nargs='*', help='the pickle file')
2339+
parser.add_argument(
2340+
'-o', '--output', default=sys.stdout, type=argparse.FileType('w'),
2341+
help='the file where the output should be written')
2342+
parser.add_argument(
2343+
'-m', '--memo', action='store_true',
2344+
help='preserve memo between disassemblies')
2345+
parser.add_argument(
2346+
'-l', '--indentlevel', default=4, type=int,
2347+
help='the number of blanks by which to indent a new MARK level')
2348+
parser.add_argument(
2349+
'-p', '--preamble', default="==> {name} <==",
2350+
help='if more than one pickle file is specified, print this before'
2351+
' each disassembly')
2352+
parser.add_argument(
2353+
'-t', '--test', action='store_true',
2354+
help='run self-test suite')
2355+
parser.add_argument(
2356+
'-v', action='store_true',
2357+
help='run verbosely; only affects self-test run')
2358+
args = parser.parse_args()
2359+
if args.test:
2360+
_test()
2361+
else:
2362+
if not args.pickle_file:
2363+
parser.print_help()
2364+
elif len(args.pickle_file) == 1:
2365+
dis(args.pickle_file[0], args.output,
2366+
indentlevel=args.indentlevel)
2367+
else:
2368+
memo = {} if args.memo else None
2369+
for f in args.pickle_file:
2370+
preamble = args.preamble.format(name=f.name)
2371+
args.output.write(preamble + '\n')
2372+
dis(f, args.output, memo, args.indentlevel)

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,10 @@ C-API
468468
Library
469469
-------
470470

471+
- Issue #9094: python -m pickletools will now disassemble pickle files
472+
listed in the command line arguments. See output of python -m
473+
pickletools -h for more details.
474+
471475
- Issue #5468: urlencode to handle bytes type and other encodings in its query
472476
parameter. Patch by Dan Mahn.
473477

0 commit comments

Comments
 (0)