Skip to content

Commit 85c04f8

Browse files
authored
gh-93096: Update and document pickletools CLI (#131273)
1 parent a1aeec6 commit 85c04f8

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

Doc/library/pickletools.rst

+8-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ordinary users of the :mod:`pickle` module probably won't find the
1919

2020
.. _pickletools-cli:
2121

22-
Command line usage
22+
Command-line usage
2323
------------------
2424

2525
.. versionadded:: 3.2
@@ -48,7 +48,7 @@ For example, with a tuple ``(1, 2)`` pickled in file ``x.pickle``:
4848
9: . STOP
4949
highest protocol among opcodes = 2
5050
51-
Command line options
51+
Command-line options
5252
^^^^^^^^^^^^^^^^^^^^
5353

5454
.. program:: pickletools
@@ -72,12 +72,16 @@ Command line options
7272

7373
.. option:: -p, --preamble=<preamble>
7474

75-
When more than one pickle file are specified, print given preamble
75+
When more than one pickle file is specified, print given preamble
7676
before each disassembly.
7777

78+
.. option:: pickle_file
7879

80+
A pickle file to read, or ``-`` to indicate reading from standard input.
7981

80-
Programmatic Interface
82+
83+
84+
Programmatic interface
8185
----------------------
8286

8387

Lib/pickletools.py

+20-23
Original file line numberDiff line numberDiff line change
@@ -2845,7 +2845,7 @@ def __init__(self, value):
28452845
description='disassemble one or more pickle files')
28462846
parser.add_argument(
28472847
'pickle_file',
2848-
nargs='*', help='the pickle file')
2848+
nargs='+', help='the pickle file')
28492849
parser.add_argument(
28502850
'-o', '--output',
28512851
help='the file where the output should be written')
@@ -2863,26 +2863,23 @@ def __init__(self, value):
28632863
help='if more than one pickle file is specified, print this before'
28642864
' each disassembly')
28652865
args = parser.parse_args()
2866-
if not args.pickle_file:
2867-
parser.print_help()
2866+
annotate = 30 if args.annotate else 0
2867+
memo = {} if args.memo else None
2868+
if args.output is None:
2869+
output = sys.stdout
28682870
else:
2869-
annotate = 30 if args.annotate else 0
2870-
memo = {} if args.memo else None
2871-
if args.output is None:
2872-
output = sys.stdout
2873-
else:
2874-
output = open(args.output, 'w')
2875-
try:
2876-
for arg in args.pickle_file:
2877-
if len(args.pickle_file) > 1:
2878-
name = '<stdin>' if arg == '-' else arg
2879-
preamble = args.preamble.format(name=name)
2880-
output.write(preamble + '\n')
2881-
if arg == '-':
2882-
dis(sys.stdin.buffer, output, memo, args.indentlevel, annotate)
2883-
else:
2884-
with open(arg, 'rb') as f:
2885-
dis(f, output, memo, args.indentlevel, annotate)
2886-
finally:
2887-
if output is not sys.stdout:
2888-
output.close()
2871+
output = open(args.output, 'w')
2872+
try:
2873+
for arg in args.pickle_file:
2874+
if len(args.pickle_file) > 1:
2875+
name = '<stdin>' if arg == '-' else arg
2876+
preamble = args.preamble.format(name=name)
2877+
output.write(preamble + '\n')
2878+
if arg == '-':
2879+
dis(sys.stdin.buffer, output, memo, args.indentlevel, annotate)
2880+
else:
2881+
with open(arg, 'rb') as f:
2882+
dis(f, output, memo, args.indentlevel, annotate)
2883+
finally:
2884+
if output is not sys.stdout:
2885+
output.close()

0 commit comments

Comments
 (0)