summaryrefslogtreecommitdiffstats
path: root/Lib/pickletools.py
diff options
context:
space:
mode:
authorAlexander Belopolsky <alexander.belopolsky@gmail.com>2010-07-03 20:35:53 (GMT)
committerAlexander Belopolsky <alexander.belopolsky@gmail.com>2010-07-03 20:35:53 (GMT)
commit60c762b77a87438691c1a788f0f427eb666e106b (patch)
tree4123b3908a72d369a074b468b78048b2fdee9670 /Lib/pickletools.py
parentdf022da3d87311e44213ea2e5f95cc6667d44d83 (diff)
downloadcpython-60c762b77a87438691c1a788f0f427eb666e106b.zip
cpython-60c762b77a87438691c1a788f0f427eb666e106b.tar.gz
cpython-60c762b77a87438691c1a788f0f427eb666e106b.tar.bz2
Issue #9094: Make python -m pickletools disassemble pickles given in
the command line.
Diffstat (limited to 'Lib/pickletools.py')
-rw-r--r--Lib/pickletools.py41
1 files changed, 40 insertions, 1 deletions
diff --git a/Lib/pickletools.py b/Lib/pickletools.py
index 6ab75c7..7c4f9f9 100644
--- a/Lib/pickletools.py
+++ b/Lib/pickletools.py
@@ -2330,4 +2330,43 @@ def _test():
return doctest.testmod()
if __name__ == "__main__":
- _test()
+ import sys, argparse
+ parser = argparse.ArgumentParser(
+ description='disassemble one or more pickle files')
+ parser.add_argument(
+ 'pickle_file', type=argparse.FileType('br'),
+ nargs='*', help='the pickle file')
+ parser.add_argument(
+ '-o', '--output', default=sys.stdout, type=argparse.FileType('w'),
+ help='the file where the output should be written')
+ parser.add_argument(
+ '-m', '--memo', action='store_true',
+ help='preserve memo between disassemblies')
+ parser.add_argument(
+ '-l', '--indentlevel', default=4, type=int,
+ help='the number of blanks by which to indent a new MARK level')
+ parser.add_argument(
+ '-p', '--preamble', default="==> {name} <==",
+ help='if more than one pickle file is specified, print this before'
+ ' each disassembly')
+ parser.add_argument(
+ '-t', '--test', action='store_true',
+ help='run self-test suite')
+ parser.add_argument(
+ '-v', action='store_true',
+ help='run verbosely; only affects self-test run')
+ args = parser.parse_args()
+ if args.test:
+ _test()
+ else:
+ if not args.pickle_file:
+ parser.print_help()
+ elif len(args.pickle_file) == 1:
+ dis(args.pickle_file[0], args.output,
+ indentlevel=args.indentlevel)
+ else:
+ memo = {} if args.memo else None
+ for f in args.pickle_file:
+ preamble = args.preamble.format(name=f.name)
+ args.output.write(preamble + '\n')
+ dis(f, args.output, memo, args.indentlevel)