]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-93096: Update and document `pickle` CLI (#131097)
authordonBarbos <donbarbos@proton.me>
Fri, 14 Mar 2025 13:15:35 +0000 (17:15 +0400)
committerGitHub <noreply@github.com>
Fri, 14 Mar 2025 13:15:35 +0000 (14:15 +0100)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Doc/library/cmdline.rst
Doc/library/pickle.rst
Lib/pickle.py

index a000eb21845bacae4277d04f4e410064ae7527b3..25abee81e72a730a0ee6eaeeaa2b01efec97e17e 100644 (file)
@@ -25,7 +25,7 @@ The following modules have a command-line interface.
 * :ref:`json <json-commandline>`
 * :ref:`mimetypes <mimetypes-cli>`
 * :mod:`pdb`
-* :mod:`pickle`
+* :ref:`pickle <pickle-cli>`
 * :ref:`pickletools <pickletools-cli>`
 * :mod:`platform`
 * :mod:`poplib`
index 66aa51ceb84683aeafc29ca5acd5b54e5a93958f..007c9fe1b950cf2b02ecf0b96a3f6c654babdc82 100644 (file)
@@ -1210,6 +1210,30 @@ The following example reads the resulting pickled data. ::
 .. pickletools.optimize() or the gzip module).
 
 
+.. _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 to read, or ``-`` to indicate reading from standard input.
+
+
 .. seealso::
 
    Module :mod:`copyreg`
index 8f7406d2534f2c3e785d6932327456aec8513d4c..efcdcbec718166437cba9a7de741dc7fddcf6f5a 100644 (file)
@@ -1909,20 +1909,17 @@ except ImportError:
 
 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)