Skip to content

gh-93096: Update and document pickle CLI #131097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doc/library/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The following modules have a command-line interface.
* :ref:`json <json-commandline>`
* :mod:`mimetypes`
* :mod:`pdb`
* :mod:`pickle`
* :ref:`pickle <pickle-cli>`
* :ref:`pickletools <pickletools-cli>`
* :mod:`platform`
* :mod:`poplib`
Expand Down
29 changes: 29 additions & 0 deletions Doc/library/pickle.rst
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,35 @@ The :mod:`pickle` module exports three classes, :class:`Pickler`,
Release the underlying buffer exposed by the PickleBuffer object.


.. _pickle-cli:

Command-line interface
----------------------

The :mod:`pickle` module can be invoked as a script from the command line,
it will display contents of the pickle files. However, when the pickle file
that you want to examine comes from an untrusted source, ``-m pickletools``
is a safer option because it does not execute pickle bytecode, see
:ref:`pickletools CLI usage <pickletools-cli>`.

.. code-block:: bash

python -m pickle pickle_file [pickle_file ...]

The following option is accepted:

.. program:: pickle

.. option:: pickle_file

A pickle file, which can be a regular filename or a filepath.
Additionally, you can use ``-`` to read from standard input (stdin).
For example, you can pipe a pickle file to the script using a command like:

.. code-block:: bash
echo ... | python -m pickle -


.. _pickle-picklable:

What can be pickled and unpickled?
Expand Down
21 changes: 9 additions & 12 deletions Lib/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -1909,20 +1909,17 @@ def _loads(s, /, *, fix_imports=True, encoding="ASCII", errors="strict",

if __name__ == "__main__":
import argparse
import pprint
parser = argparse.ArgumentParser(
description='display contents of the pickle files')
parser.add_argument(
'pickle_file',
nargs='*', help='the pickle file')
nargs='+', help='the pickle file')
args = parser.parse_args()
if not args.pickle_file:
parser.print_help()
else:
import pprint
for fn in args.pickle_file:
if fn == '-':
obj = load(sys.stdin.buffer)
else:
with open(fn, 'rb') as f:
obj = load(f)
pprint.pprint(obj)
for fn in args.pickle_file:
if fn == '-':
obj = load(sys.stdin.buffer)
else:
with open(fn, 'rb') as f:
obj = load(f)
pprint.pprint(obj)
Loading