Skip to content

Commit f9d0531

Browse files
donBarbospicnixzvstinnerhugovk
authored
gh-93096: Update and document pickle CLI (#131097)
Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Victor Stinner <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent 67af96c commit f9d0531

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

Doc/library/cmdline.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The following modules have a command-line interface.
2525
* :ref:`json <json-commandline>`
2626
* :ref:`mimetypes <mimetypes-cli>`
2727
* :mod:`pdb`
28-
* :mod:`pickle`
28+
* :ref:`pickle <pickle-cli>`
2929
* :ref:`pickletools <pickletools-cli>`
3030
* :mod:`platform`
3131
* :mod:`poplib`

Doc/library/pickle.rst

+24
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,30 @@ The following example reads the resulting pickled data. ::
12101210
.. pickletools.optimize() or the gzip module).
12111211
12121212
1213+
.. _pickle-cli:
1214+
1215+
Command-line interface
1216+
----------------------
1217+
1218+
The :mod:`pickle` module can be invoked as a script from the command line,
1219+
it will display contents of the pickle files. However, when the pickle file
1220+
that you want to examine comes from an untrusted source, ``-m pickletools``
1221+
is a safer option because it does not execute pickle bytecode, see
1222+
:ref:`pickletools CLI usage <pickletools-cli>`.
1223+
1224+
.. code-block:: bash
1225+
1226+
python -m pickle pickle_file [pickle_file ...]
1227+
1228+
The following option is accepted:
1229+
1230+
.. program:: pickle
1231+
1232+
.. option:: pickle_file
1233+
1234+
A pickle file to read, or ``-`` to indicate reading from standard input.
1235+
1236+
12131237
.. seealso::
12141238

12151239
Module :mod:`copyreg`

Lib/pickle.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -1909,20 +1909,17 @@ def _loads(s, /, *, fix_imports=True, encoding="ASCII", errors="strict",
19091909

19101910
if __name__ == "__main__":
19111911
import argparse
1912+
import pprint
19121913
parser = argparse.ArgumentParser(
19131914
description='display contents of the pickle files')
19141915
parser.add_argument(
19151916
'pickle_file',
1916-
nargs='*', help='the pickle file')
1917+
nargs='+', help='the pickle file')
19171918
args = parser.parse_args()
1918-
if not args.pickle_file:
1919-
parser.print_help()
1920-
else:
1921-
import pprint
1922-
for fn in args.pickle_file:
1923-
if fn == '-':
1924-
obj = load(sys.stdin.buffer)
1925-
else:
1926-
with open(fn, 'rb') as f:
1927-
obj = load(f)
1928-
pprint.pprint(obj)
1919+
for fn in args.pickle_file:
1920+
if fn == '-':
1921+
obj = load(sys.stdin.buffer)
1922+
else:
1923+
with open(fn, 'rb') as f:
1924+
obj = load(f)
1925+
pprint.pprint(obj)

0 commit comments

Comments
 (0)