LinuxCommandLibrary

pickle

Serialize Python objects into a byte stream

TLDR

Install a specific PHP extension

$ pickle install [extension_name]
copy

Convert an existing PECL extension configuration to a Pickle configuration file
$ pickle convert [path/to/directory]
copy

Validate a PECL extension
$ pickle validate [path/to/directory]
copy

Package a PECL extension for release
$ pickle release [path/to/directory]
copy

SYNOPSIS

As pickle is a Python module and not a standalone executable, there is no direct "pickle" command to run in a Linux shell. Its functionality is accessed through Python scripts or the Python interpreter.

To use pickle, you typically invoke the Python interpreter:
python [options] script.py [arguments]
Or for direct interaction:
python
Inside the Python environment, you would import and use the pickle module's functions, such as pickle.dump(), pickle.load(), pickle.dumps(), or pickle.loads().

PARAMETERS

pickle.dump(obj, file, protocol=None, *, fix_imports=True, buffer_callback=None)
    Writes the pickled representation of obj to the open file object.

pickle.load(file, *, fix_imports=True, encoding='ASCII', errors='strict', buffers=None)
    Reads a pickled object representation from the open file object and reconstructs it.

pickle.dumps(obj, protocol=None, *, fix_imports=True, buffer_callback=None)
    Returns the pickled representation of obj as a bytes object.

pickle.loads(bytes_object, *, fix_imports=True, encoding='ASCII', errors='strict', buffers=None)
    Reads a pickled object representation from bytes_object and reconstructs it.

protocol
    An integer, specifying the pickle protocol version to use (e.g., 0-5). Higher protocols are generally more efficient and newer.

DESCRIPTION

The pickle module is a fundamental component of the Python standard library, used for serializing and deserializing Python object structures. It converts a Python object hierarchy into a byte stream, a process known as "pickling" or "serialization". Conversely, it can reconstruct the original Python object hierarchy from that byte stream, a process called "unpickling" or "deserialization".

While not a standalone Linux command, pickle is extensively used within Python scripts executed on Linux systems. Its primary purpose is to allow Python objects to be stored on disk, transferred across a network, or otherwise persisted. Unlike JSON or XML, pickle is Python-specific, meaning it can serialize complex Python data types that these more universal formats might not support directly, such as custom class instances, functions, or sets. However, this specificity also comes with a significant security caveat: deserializing untrusted pickle data can execute arbitrary code.

CAVEATS

  • Security Vulnerability: Unpickling data from an untrusted source is a significant security risk. Maliciously crafted pickle data can lead to arbitrary code execution on the system. Never unpickle data unless you are absolutely certain of its origin and integrity.
  • Python Version Dependency: Pickle protocols can evolve. Data pickled with a newer Python version's protocol might not be compatible with older Python versions, and vice-versa. Best practice is to use a consistent Python version for pickling and unpickling.
  • Not Human-Readable: Pickled data is a binary format and is not designed to be human-readable or easily edited, unlike formats such as JSON or YAML.

PICKLE PROTOCOLS

The pickle module supports several protocol versions (0 through 5 as of Python 3.8+). Newer protocols generally offer better performance and smaller output sizes. Protocol 0 is the original human-readable (text-based) protocol, while higher protocols are binary. The latest recommended protocol is typically the highest available, specified by pickle.DEFAULT_PROTOCOL or pickle.HIGHEST_PROTOCOL for maximum compatibility/efficiency.

MODULE VS. COMMAND

It is crucial to understand that pickle is a Python module, not a standalone shell command. You cannot type pickle <filename> directly in a Linux terminal. Its functionality is accessed by writing Python code that imports and uses the pickle module.

HISTORY

The pickle module has been a core part of the Python standard library since its early versions (first introduced in Python 1.5.2). Its development has primarily focused on introducing new, more efficient, and robust protocol versions while maintaining backward compatibility where possible. Each new protocol version aims to improve performance, reduce the size of the pickled data, or handle more complex data structures. The module's design has remained consistent, providing the primary functions for serialization and deserialization, serving as a reliable mechanism for persisting Python objects.

SEE ALSO

python(1), json(1), marshal (Python module), struct (Python module)

Copied to clipboard