Debian Codegraph @

pypy/module/_pickle/app_pickle.py

raw symbols SCIP
1 bytes_types = (bytes, bytearray)
2
3 class PickleError(Exception):
4 """A common base class for the other pickling exceptions."""
5 pass
6
7 class PicklingError(PickleError):
8 """This exception is raised when an unpicklable object is passed to the
9 dump() method.
10
11 """
12 pass
13
14 class UnpicklingError(PickleError):
15 """This exception is raised when there is a problem unpickling an object,
16 such as a security violation.
17
18 Note that other exceptions may also be raised during unpickling, including
19 (but not necessarily limited to) AttributeError, EOFError, ImportError,
20 and IndexError.
21
22 """
23 pass
24
25 def dump(obj, file, protocol=None, *, fix_imports=True, buffer_callback=None):
26 """
27 Write a pickled representation of obj to the open file object file.
28
29 This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
30 be more efficient.
31
32 The optional *protocol* argument tells the pickler to use the given
33 protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default
34 protocol is 4. It was introduced in Python 3.4, and is incompatible
35 with previous versions.
36
37 Specifying a negative protocol version selects the highest protocol
38 version supported. The higher the protocol used, the more recent the
39 version of Python needed to read the pickle produced.
40
41 The *file* argument must have a write() method that accepts a single
42 bytes argument. It can thus be a file object opened for binary
43 writing, an io.BytesIO instance, or any other custom object that meets
44 this interface.
45
46 If *fix_imports* is True and protocol is less than 3, pickle will try
47 to map the new Python 3 names to the old module names used in Python
48 2, so that the pickle data stream is readable with Python 2.
49
50 If *buffer_callback* is None (the default), buffer views are serialized
51 into *file* as part of the pickle stream. It is an error if
52 *buffer_callback* is not None and *protocol* is None or smaller than 5.
53 """
54
55 from _pickle import Pickler
56 Pickler(file, protocol, fix_imports=fix_imports,
57 buffer_callback=buffer_callback).dump(obj)
58
59 def dumps(obj, protocol=None, *, fix_imports=True, buffer_callback=None):
60 """
61 Return the pickled representation of the object as a bytes object.
62
63 The optional *protocol* argument tells the pickler to use the given
64 protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default
65 protocol is 4. It was introduced in Python 3.4, and is incompatible
66 with previous versions.
67
68 Specifying a negative protocol version selects the highest protocol
69 version supported. The higher the protocol used, the more recent the
70 version of Python needed to read the pickle produced.
71
72 If *fix_imports* is True and *protocol* is less than 3, pickle will
73 try to map the new Python 3 names to the old module names used in
74 Python 2, so that the pickle data stream is readable with Python 2.
75
76 If *buffer_callback* is None (the default), buffer views are serialized
77 into *file* as part of the pickle stream. It is an error if
78 *buffer_callback* is not None and *protocol* is None or smaller than 5.
79 """
80
81 from _pickle import Pickler
82 import _io
83 f = _io.BytesIO()
84 if protocol is None:
85 protocol = 4
86 Pickler(f, protocol, fix_imports=fix_imports, buffer_callback=buffer_callback).dump(obj)
87 res = f.getvalue()
88 assert isinstance(res, bytes_types)
89 return res
90
91 def load(file, *, fix_imports=True, encoding="ASCII", errors="strict",
92 buffers=None):
93 """
94 Read and return an object from the pickle data stored in a file.
95
96 This is equivalent to ``Unpickler(file).load()``, but may be more
97 efficient.
98
99 The protocol version of the pickle is detected automatically, so no
100 protocol argument is needed. Bytes past the pickled object's
101 representation are ignored.
102
103 The argument *file* must have two methods, a read() method that takes
104 an integer argument, and a readline() method that requires no
105 arguments. Both methods should return bytes. Thus *file* can be a
106 binary file object opened for reading, an io.BytesIO object, or any
107 other custom object that meets this interface.
108
109 Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
110 which are used to control compatibility support for pickle stream
111 generated by Python 2. If *fix_imports* is True, pickle will try to
112 map the old Python 2 names to the new names used in Python 3. The
113 *encoding* and *errors* tell pickle how to decode 8-bit string
114 instances pickled by Python 2; these default to 'ASCII' and 'strict',
115 respectively. The *encoding* can be 'bytes' to read these 8-bit
116 string instances as bytes objects.
117 """
118
119 from _pickle import Unpickler
120 return Unpickler(file, fix_imports=fix_imports, buffers=buffers,
121 encoding=encoding, errors=errors).load()
122
123 def loads(s, /, *, fix_imports=True, encoding="ASCII", errors="strict",
124 buffers=None):
125 """
126 Read and return an object from the given pickle data.
127
128 The protocol version of the pickle is detected automatically, so no
129 protocol argument is needed. Bytes past the pickled object's
130 representation are ignored.
131
132 Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
133 which are used to control compatibility support for pickle stream
134 generated by Python 2. If *fix_imports* is True, pickle will try to
135 map the old Python 2 names to the new names used in Python 3. The
136 *encoding* and *errors* tell pickle how to decode 8-bit string
137 instances pickled by Python 2; these default to 'ASCII' and 'strict',
138 respectively. The *encoding* can be 'bytes' to read these 8-bit
139 string instances as bytes objects.
140 """
141 from _pickle import Unpickler
142 import _io
143 if isinstance(s, str):
144 raise TypeError("Can't load pickle from unicode string")
145 file = _io.BytesIO(s)
146 return Unpickler(file, fix_imports=fix_imports, buffers=buffers,
147 encoding=encoding, errors=errors).load()
148