From f572b5991e341275eef39869a92f543bc5010020 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Tue, 11 Mar 2025 19:06:34 +0400 Subject: [PATCH 01/16] Add CLI docs for `pickle` --- Doc/library/cmdline.rst | 2 +- Doc/library/pickle.rst | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Doc/library/cmdline.rst b/Doc/library/cmdline.rst index 78fe95a014ff7c..b977ab88600556 100644 --- a/Doc/library/cmdline.rst +++ b/Doc/library/cmdline.rst @@ -26,7 +26,7 @@ The following modules have a command-line interface. * :ref:`json ` * :mod:`mimetypes` * :mod:`pdb` -* :mod:`pickle` +* :ref:`pickle ` * :ref:`pickletools ` * :mod:`platform` * :mod:`poplib` diff --git a/Doc/library/pickle.rst b/Doc/library/pickle.rst index 66aa51ceb84683..a8b0433e617733 100644 --- a/Doc/library/pickle.rst +++ b/Doc/library/pickle.rst @@ -511,6 +511,30 @@ 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`. + +.. code-block:: bash + + python -m pickle [pickle_file ...] + +The following options are accepted: + +.. program:: pickle + +.. option:: pickle_file + + The pickle file(s). + + .. _pickle-picklable: What can be pickled and unpickled? From 923af7bbf7ceba9a5705d078a2cf0ce6e760ddab Mon Sep 17 00:00:00 2001 From: donBarbos Date: Tue, 11 Mar 2025 19:47:41 +0400 Subject: [PATCH 02/16] Correct ref preview --- Doc/library/pickle.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/pickle.rst b/Doc/library/pickle.rst index a8b0433e617733..e1d14bccf946d9 100644 --- a/Doc/library/pickle.rst +++ b/Doc/library/pickle.rst @@ -520,7 +520,7 @@ 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`. +:ref:`pickletools CLI usage `. .. code-block:: bash From 5909db4bb44ca0e7235557d04d7211d2087bf54e Mon Sep 17 00:00:00 2001 From: donBarbos Date: Wed, 12 Mar 2025 15:55:27 +0000 Subject: [PATCH 03/16] Update Doc/library/pickle.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Doc/library/pickle.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/pickle.rst b/Doc/library/pickle.rst index e1d14bccf946d9..9c392c645cfdbf 100644 --- a/Doc/library/pickle.rst +++ b/Doc/library/pickle.rst @@ -514,7 +514,7 @@ The :mod:`pickle` module exports three classes, :class:`Pickler`, .. _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 From 291f6bff6b9ee7757f13097ec095b679768204c3 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Thu, 13 Mar 2025 17:26:53 +0400 Subject: [PATCH 04/16] Add suggestions --- Doc/library/pickle.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/pickle.rst b/Doc/library/pickle.rst index 9c392c645cfdbf..8a4febf548ab6c 100644 --- a/Doc/library/pickle.rst +++ b/Doc/library/pickle.rst @@ -526,13 +526,13 @@ is a safer option because it does not execute pickle bytecode, see python -m pickle [pickle_file ...] -The following options are accepted: +The following options is accepted: .. program:: pickle .. option:: pickle_file - The pickle file(s). + A pickle file. .. _pickle-picklable: From 215afa979f999db9c3d020b8b50b5d5f4127be8e Mon Sep 17 00:00:00 2001 From: donBarbos Date: Thu, 13 Mar 2025 17:36:52 +0400 Subject: [PATCH 05/16] Fix typo --- Doc/library/pickle.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/pickle.rst b/Doc/library/pickle.rst index 8a4febf548ab6c..0aef9dc7334d5a 100644 --- a/Doc/library/pickle.rst +++ b/Doc/library/pickle.rst @@ -526,7 +526,7 @@ is a safer option because it does not execute pickle bytecode, see python -m pickle [pickle_file ...] -The following options is accepted: +The following option is accepted: .. program:: pickle From 3b8efaed08823331adaf080f1d61683903ca7291 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Thu, 13 Mar 2025 19:21:15 +0400 Subject: [PATCH 06/16] Update doc --- Doc/library/pickle.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/pickle.rst b/Doc/library/pickle.rst index 0aef9dc7334d5a..1b71f1d8bd647c 100644 --- a/Doc/library/pickle.rst +++ b/Doc/library/pickle.rst @@ -524,7 +524,7 @@ is a safer option because it does not execute pickle bytecode, see .. code-block:: bash - python -m pickle [pickle_file ...] + python -m pickle pickle_file The following option is accepted: From 40a326536c05c93c30c2aea876c8d69cf17bf28e Mon Sep 17 00:00:00 2001 From: donBarbos Date: Thu, 13 Mar 2025 19:27:15 +0400 Subject: [PATCH 07/16] Fix pickle parser --- Doc/library/pickle.rst | 2 +- Lib/pickle.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/pickle.rst b/Doc/library/pickle.rst index 1b71f1d8bd647c..ddc5cefb43bd1a 100644 --- a/Doc/library/pickle.rst +++ b/Doc/library/pickle.rst @@ -524,7 +524,7 @@ is a safer option because it does not execute pickle bytecode, see .. code-block:: bash - python -m pickle pickle_file + python -m pickle pickle_file [pickle_file ...] The following option is accepted: diff --git a/Lib/pickle.py b/Lib/pickle.py index 8f7406d2534f2c..bcb4768c388f6b 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -1913,7 +1913,7 @@ def _loads(s, /, *, fix_imports=True, encoding="ASCII", errors="strict", 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() From c93b70f77413b1a3ac0df0923219ba2625511d52 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Thu, 13 Mar 2025 20:23:14 +0400 Subject: [PATCH 08/16] Add feature --- Doc/library/pickle.rst | 7 ++++++- Lib/pickle.py | 19 ++++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Doc/library/pickle.rst b/Doc/library/pickle.rst index ddc5cefb43bd1a..4c4622633994bd 100644 --- a/Doc/library/pickle.rst +++ b/Doc/library/pickle.rst @@ -532,7 +532,12 @@ The following option is accepted: .. option:: pickle_file - A 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: diff --git a/Lib/pickle.py b/Lib/pickle.py index bcb4768c388f6b..efcdcbec718166 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -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') 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) From cefd8a4affc192e4d04c510eb32131a42244598e Mon Sep 17 00:00:00 2001 From: donBarbos Date: Thu, 13 Mar 2025 16:35:26 +0000 Subject: [PATCH 09/16] Update Doc/library/pickle.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Doc/library/pickle.rst | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Doc/library/pickle.rst b/Doc/library/pickle.rst index 4c4622633994bd..7b61bfa4b80c7f 100644 --- a/Doc/library/pickle.rst +++ b/Doc/library/pickle.rst @@ -532,12 +532,7 @@ The following option is accepted: .. 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 - + A pickle file to read or ``-`` to indicate reading from standard input. .. _pickle-picklable: From 4bc3253bacafb505dfde55001eef5ab90d425ec7 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Thu, 13 Mar 2025 23:03:52 +0400 Subject: [PATCH 10/16] Move cli section --- Doc/library/pickle.rst | 48 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/Doc/library/pickle.rst b/Doc/library/pickle.rst index 7b61bfa4b80c7f..91a154fa48fac6 100644 --- a/Doc/library/pickle.rst +++ b/Doc/library/pickle.rst @@ -511,30 +511,6 @@ 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 `. - -.. 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. - - .. _pickle-picklable: What can be pickled and unpickled? @@ -1234,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 `. + +.. 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` From 29d13606cd43e31106f940e8112717d0f84795a3 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Fri, 14 Mar 2025 09:23:06 +0000 Subject: [PATCH 11/16] Update Doc/library/pickle.rst Co-authored-by: Victor Stinner --- Doc/library/pickle.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/pickle.rst b/Doc/library/pickle.rst index 91a154fa48fac6..007c9fe1b950cf 100644 --- a/Doc/library/pickle.rst +++ b/Doc/library/pickle.rst @@ -1231,7 +1231,7 @@ The following option is accepted: .. option:: pickle_file - A pickle file to read or ``-`` to indicate reading from standard input. + A pickle file to read, or ``-`` to indicate reading from standard input. .. seealso:: From 3dd66f2a172c9aa3042722bf61b2275ed5ff8296 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Fri, 14 Mar 2025 10:15:38 +0000 Subject: [PATCH 12/16] Update Lib/pickle.py Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- Lib/pickle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/pickle.py b/Lib/pickle.py index efcdcbec718166..eee7694071647f 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -1922,4 +1922,4 @@ def _loads(s, /, *, fix_imports=True, encoding="ASCII", errors="strict", else: with open(fn, 'rb') as f: obj = load(f) - pprint.pprint(obj) + pprint.pp(obj) From ff22e977d00bb94264c7e2a79bc4d2a74eec3f85 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Fri, 14 Mar 2025 14:20:32 +0400 Subject: [PATCH 13/16] back sort_dicts=True --- Lib/pickle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/pickle.py b/Lib/pickle.py index eee7694071647f..4006f4f9f572a8 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -1922,4 +1922,4 @@ def _loads(s, /, *, fix_imports=True, encoding="ASCII", errors="strict", else: with open(fn, 'rb') as f: obj = load(f) - pprint.pp(obj) + pprint.pp(obj, sort_dicts=True) From dbedc68bf8ad3856bbe17df114103d5da1fbbefe Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Fri, 14 Mar 2025 12:44:17 +0200 Subject: [PATCH 14/16] pprint.pp(obj) --- Lib/pickle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/pickle.py b/Lib/pickle.py index 4006f4f9f572a8..eee7694071647f 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -1922,4 +1922,4 @@ def _loads(s, /, *, fix_imports=True, encoding="ASCII", errors="strict", else: with open(fn, 'rb') as f: obj = load(f) - pprint.pp(obj, sort_dicts=True) + pprint.pp(obj) From 593fd7baa4335a846889c8949c2c12fcef12780b Mon Sep 17 00:00:00 2001 From: donBarbos Date: Fri, 14 Mar 2025 16:30:04 +0400 Subject: [PATCH 15/16] Revert using pp --- Lib/pickle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/pickle.py b/Lib/pickle.py index eee7694071647f..efcdcbec718166 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -1922,4 +1922,4 @@ def _loads(s, /, *, fix_imports=True, encoding="ASCII", errors="strict", else: with open(fn, 'rb') as f: obj = load(f) - pprint.pp(obj) + pprint.pprint(obj) From 8f1af6e25bcafeb99cc2e5c582ba049f6614c0bb Mon Sep 17 00:00:00 2001 From: donBarbos Date: Fri, 14 Mar 2025 12:35:30 +0000 Subject: [PATCH 16/16] Update Lib/pickle.py Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>