anyconfig.backend.pickle.stdlib

A backend module to load and dump pickle files.

  • Format to support: Pickle

  • Requirements: It should be available always.

  • Development Status :: 4 - Beta

  • Limitations: The parser cannot load some primitive data such like ‘’ (empty string), ‘ ‘ (white space) and [] (empty list) as these are because of the implementation of anyconfig.backend.base.load_with_fn().

  • Special options: All options of pickle.{load{s,},dump{s,}} should work.

Changelog:

Changed in version 0.9.7:

  • Add support of loading primitives other than mapping objects.

Added in version 0.8.3.

class anyconfig.backend.pickle.stdlib.Parser

Bases: StringStreamFnParser

Parser for Pickle files.

_cid: typing.ClassVar[str] = 'pickle.stdlib'
_type: typing.ClassVar[str] = 'pickle'
_extensions: tuple[str, ...] = ('pkl', 'pickle')
_open_read_mode: typing.ClassVar[str] = 'rb'
_open_write_mode: typing.ClassVar[str] = 'wb'
_load_opts: tuple[str, ...] = ('fix_imports', 'encoding', 'errors')
_dump_opts: tuple[str, ...] = ('protocol', 'fix_imports')
_allow_primitives: typing.ClassVar[bool] = True
_load_from_string_fn(*, fix_imports=True, encoding='ASCII', errors='strict', buffers=())

Read and return an object from the given pickle data.

The protocol version of the pickle is detected automatically, so no protocol argument is needed. Bytes past the pickled object’s representation are ignored.

Optional keyword arguments are fix_imports, encoding and errors, which are used to control compatibility support for pickle stream generated by Python 2. If fix_imports is True, pickle will try to map the old Python 2 names to the new names used in Python 3. The encoding and errors tell pickle how to decode 8-bit string instances pickled by Python 2; these default to ‘ASCII’ and ‘strict’, respectively. The encoding can be ‘bytes’ to read these 8-bit string instances as bytes objects.

_load_from_stream_fn(*, fix_imports=True, encoding='ASCII', errors='strict', buffers=())

Read and return an object from the pickle data stored in a file.

This is equivalent to Unpickler(file).load(), but may be more efficient.

The protocol version of the pickle is detected automatically, so no protocol argument is needed. Bytes past the pickled object’s representation are ignored.

The argument file must have two methods, a read() method that takes an integer argument, and a readline() method that requires no arguments. Both methods should return bytes. Thus file can be a binary file object opened for reading, an io.BytesIO object, or any other custom object that meets this interface.

Optional keyword arguments are fix_imports, encoding and errors, which are used to control compatibility support for pickle stream generated by Python 2. If fix_imports is True, pickle will try to map the old Python 2 names to the new names used in Python 3. The encoding and errors tell pickle how to decode 8-bit string instances pickled by Python 2; these default to ‘ASCII’ and ‘strict’, respectively. The encoding can be ‘bytes’ to read these 8-bit string instances as bytes objects.

_dump_to_string_fn(protocol=None, *, fix_imports=True, buffer_callback=None)

Return the pickled representation of the object as a bytes object.

The optional protocol argument tells the pickler to use the given protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default protocol is 4. It was introduced in Python 3.4, and is incompatible with previous versions.

Specifying a negative protocol version selects the highest protocol version supported. The higher the protocol used, the more recent the version of Python needed to read the pickle produced.

If fix_imports is True and protocol is less than 3, pickle will try to map the new Python 3 names to the old module names used in Python 2, so that the pickle data stream is readable with Python 2.

If buffer_callback is None (the default), buffer views are serialized into file as part of the pickle stream. It is an error if buffer_callback is not None and protocol is None or smaller than 5.

_dump_to_stream_fn(file, protocol=None, *, fix_imports=True, buffer_callback=None)

Write a pickled representation of obj to the open file object file.

This is equivalent to Pickler(file, protocol).dump(obj), but may be more efficient.

The optional protocol argument tells the pickler to use the given protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default protocol is 4. It was introduced in Python 3.4, and is incompatible with previous versions.

Specifying a negative protocol version selects the highest protocol version supported. The higher the protocol used, the more recent the version of Python needed to read the pickle produced.

The file argument must have a write() method that accepts a single bytes argument. It can thus be a file object opened for binary writing, an io.BytesIO instance, or any other custom object that meets this interface.

If fix_imports is True and protocol is less than 3, pickle will try to map the new Python 3 names to the old module names used in Python 2, so that the pickle data stream is readable with Python 2.

If buffer_callback is None (the default), buffer views are serialized into file as part of the pickle stream. It is an error if buffer_callback is not None and protocol is None or smaller than 5.

__annotations__ = {'_allow_primitives': 'typing.ClassVar[bool]', '_cid': 'typing.ClassVar[str]', '_dict_opts': 'tuple[str, ...]', '_dump_opts': 'tuple[str, ...]', '_dump_to_stream_fn': 'typing.Optional[DumpFnT]', '_dump_to_string_fn': 'typing.Optional[DumpFnT]', '_extensions': 'tuple[str, ...]', '_load_from_stream_fn': 'typing.Optional[LoadFnT]', '_load_from_string_fn': 'typing.Optional[LoadFnT]', '_load_opts': 'tuple[str, ...]', '_open_read_mode': 'typing.ClassVar[str]', '_open_write_mode': 'typing.ClassVar[str]', '_ordered': 'typing.ClassVar[bool]', '_priority': 'typing.ClassVar[int]', '_type': 'typing.ClassVar[str]'}
__module__ = 'anyconfig.backend.pickle.stdlib'