gh-93096: Update and document pickletools CLI (#131273)

This commit is contained in:
Semyon Moroz 2025-03-17 13:36:30 +04:00 committed by GitHub
parent a1aeec61c4
commit 85c04f80fd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 27 deletions

View file

@ -19,7 +19,7 @@ ordinary users of the :mod:`pickle` module probably won't find the
.. _pickletools-cli: .. _pickletools-cli:
Command line usage Command-line usage
------------------ ------------------
.. versionadded:: 3.2 .. versionadded:: 3.2
@ -48,7 +48,7 @@ For example, with a tuple ``(1, 2)`` pickled in file ``x.pickle``:
9: . STOP 9: . STOP
highest protocol among opcodes = 2 highest protocol among opcodes = 2
Command line options Command-line options
^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
.. program:: pickletools .. program:: pickletools
@ -72,12 +72,16 @@ Command line options
.. option:: -p, --preamble=<preamble> .. option:: -p, --preamble=<preamble>
When more than one pickle file are specified, print given preamble When more than one pickle file is specified, print given preamble
before each disassembly. before each disassembly.
.. option:: pickle_file
A pickle file to read, or ``-`` to indicate reading from standard input.
Programmatic Interface
Programmatic interface
---------------------- ----------------------

View file

@ -2845,7 +2845,7 @@ if __name__ == "__main__":
description='disassemble one or more pickle files') description='disassemble one or more pickle files')
parser.add_argument( parser.add_argument(
'pickle_file', 'pickle_file',
nargs='*', help='the pickle file') nargs='+', help='the pickle file')
parser.add_argument( parser.add_argument(
'-o', '--output', '-o', '--output',
help='the file where the output should be written') help='the file where the output should be written')
@ -2863,26 +2863,23 @@ if __name__ == "__main__":
help='if more than one pickle file is specified, print this before' help='if more than one pickle file is specified, print this before'
' each disassembly') ' each disassembly')
args = parser.parse_args() args = parser.parse_args()
if not args.pickle_file: annotate = 30 if args.annotate else 0
parser.print_help() memo = {} if args.memo else None
if args.output is None:
output = sys.stdout
else: else:
annotate = 30 if args.annotate else 0 output = open(args.output, 'w')
memo = {} if args.memo else None try:
if args.output is None: for arg in args.pickle_file:
output = sys.stdout if len(args.pickle_file) > 1:
else: name = '<stdin>' if arg == '-' else arg
output = open(args.output, 'w') preamble = args.preamble.format(name=name)
try: output.write(preamble + '\n')
for arg in args.pickle_file: if arg == '-':
if len(args.pickle_file) > 1: dis(sys.stdin.buffer, output, memo, args.indentlevel, annotate)
name = '<stdin>' if arg == '-' else arg else:
preamble = args.preamble.format(name=name) with open(arg, 'rb') as f:
output.write(preamble + '\n') dis(f, output, memo, args.indentlevel, annotate)
if arg == '-': finally:
dis(sys.stdin.buffer, output, memo, args.indentlevel, annotate) if output is not sys.stdout:
else: output.close()
with open(arg, 'rb') as f:
dis(f, output, memo, args.indentlevel, annotate)
finally:
if output is not sys.stdout:
output.close()