1import sys
2from typing import IO, Any, Callable, ClassVar, Iterable, Iterator, Mapping, Optional, Tuple, Type, Union
3
4HIGHEST_PROTOCOL: int
5DEFAULT_PROTOCOL: int
6
7bytes_types: Tuple[Type[Any], ...]  # undocumented
8
9if sys.version_info >= (3, 8):
10    # TODO: holistic design for buffer interface (typing.Buffer?)
11    class PickleBuffer:
12        # buffer must be a buffer-providing object
13        def __init__(self, buffer: Any) -> None: ...
14        def raw(self) -> memoryview: ...
15        def release(self) -> None: ...
16    _BufferCallback = Optional[Callable[[PickleBuffer], Any]]
17    def dump(
18        obj: Any,
19        file: IO[bytes],
20        protocol: Optional[int] = ...,
21        *,
22        fix_imports: bool = ...,
23        buffer_callback: _BufferCallback = ...,
24    ) -> None: ...
25    def dumps(
26        obj: Any, protocol: Optional[int] = ..., *, fix_imports: bool = ..., buffer_callback: _BufferCallback = ...
27    ) -> bytes: ...
28    def load(
29        file: IO[bytes],
30        *,
31        fix_imports: bool = ...,
32        encoding: str = ...,
33        errors: str = ...,
34        buffers: Optional[Iterable[Any]] = ...,
35    ) -> Any: ...
36    def loads(
37        __data: bytes, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ..., buffers: Optional[Iterable[Any]] = ...
38    ) -> Any: ...
39
40else:
41    def dump(obj: Any, file: IO[bytes], protocol: Optional[int] = ..., *, fix_imports: bool = ...) -> None: ...
42    def dumps(obj: Any, protocol: Optional[int] = ..., *, fix_imports: bool = ...) -> bytes: ...
43    def load(file: IO[bytes], *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> Any: ...
44    def loads(data: bytes, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> Any: ...
45
46class PickleError(Exception): ...
47class PicklingError(PickleError): ...
48class UnpicklingError(PickleError): ...
49
50_reducedtype = Union[
51    str,
52    Tuple[Callable[..., Any], Tuple[Any, ...]],
53    Tuple[Callable[..., Any], Tuple[Any, ...], Any],
54    Tuple[Callable[..., Any], Tuple[Any, ...], Any, Optional[Iterator[Any]]],
55    Tuple[Callable[..., Any], Tuple[Any, ...], Any, Optional[Iterator[Any]], Optional[Iterator[Any]]],
56]
57
58class Pickler:
59    fast: bool
60    dispatch_table: Mapping[type, Callable[[Any], _reducedtype]]
61    bin: bool  # undocumented
62    dispatch: ClassVar[dict[type, Callable[[Unpickler, Any], None]]]  # undocumented, _Pickler only
63
64    if sys.version_info >= (3, 8):
65        def __init__(
66            self,
67            file: IO[bytes],
68            protocol: Optional[int] = ...,
69            *,
70            fix_imports: bool = ...,
71            buffer_callback: _BufferCallback = ...,
72        ) -> None: ...
73        def reducer_override(self, obj: Any) -> Any: ...
74    else:
75        def __init__(self, file: IO[bytes], protocol: Optional[int] = ..., *, fix_imports: bool = ...) -> None: ...
76    def dump(self, __obj: Any) -> None: ...
77    def clear_memo(self) -> None: ...
78    def persistent_id(self, obj: Any) -> Any: ...
79
80class Unpickler:
81    dispatch: ClassVar[dict[int, Callable[[Unpickler], None]]]  # undocumented, _Unpickler only
82
83    if sys.version_info >= (3, 8):
84        def __init__(
85            self,
86            file: IO[bytes],
87            *,
88            fix_imports: bool = ...,
89            encoding: str = ...,
90            errors: str = ...,
91            buffers: Optional[Iterable[Any]] = ...,
92        ) -> None: ...
93    else:
94        def __init__(self, file: IO[bytes], *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> None: ...
95    def load(self) -> Any: ...
96    def find_class(self, __module_name: str, __global_name: str) -> Any: ...
97    def persistent_load(self, pid: Any) -> Any: ...
98
99MARK: bytes
100STOP: bytes
101POP: bytes
102POP_MARK: bytes
103DUP: bytes
104FLOAT: bytes
105INT: bytes
106BININT: bytes
107BININT1: bytes
108LONG: bytes
109BININT2: bytes
110NONE: bytes
111PERSID: bytes
112BINPERSID: bytes
113REDUCE: bytes
114STRING: bytes
115BINSTRING: bytes
116SHORT_BINSTRING: bytes
117UNICODE: bytes
118BINUNICODE: bytes
119APPEND: bytes
120BUILD: bytes
121GLOBAL: bytes
122DICT: bytes
123EMPTY_DICT: bytes
124APPENDS: bytes
125GET: bytes
126BINGET: bytes
127INST: bytes
128LONG_BINGET: bytes
129LIST: bytes
130EMPTY_LIST: bytes
131OBJ: bytes
132PUT: bytes
133BINPUT: bytes
134LONG_BINPUT: bytes
135SETITEM: bytes
136TUPLE: bytes
137EMPTY_TUPLE: bytes
138SETITEMS: bytes
139BINFLOAT: bytes
140
141TRUE: bytes
142FALSE: bytes
143
144# protocol 2
145PROTO: bytes
146NEWOBJ: bytes
147EXT1: bytes
148EXT2: bytes
149EXT4: bytes
150TUPLE1: bytes
151TUPLE2: bytes
152TUPLE3: bytes
153NEWTRUE: bytes
154NEWFALSE: bytes
155LONG1: bytes
156LONG4: bytes
157
158# protocol 3
159BINBYTES: bytes
160SHORT_BINBYTES: bytes
161
162# protocol 4
163SHORT_BINUNICODE: bytes
164BINUNICODE8: bytes
165BINBYTES8: bytes
166EMPTY_SET: bytes
167ADDITEMS: bytes
168FROZENSET: bytes
169NEWOBJ_EX: bytes
170STACK_GLOBAL: bytes
171MEMOIZE: bytes
172FRAME: bytes
173
174if sys.version_info >= (3, 8):
175    # Protocol 5
176    BYTEARRAY8: bytes
177    NEXT_BUFFER: bytes
178    READONLY_BUFFER: bytes
179
180def encode_long(x: int) -> bytes: ...  # undocumented
181def decode_long(data: bytes) -> int: ...  # undocumented
182
183# pure-Python implementations
184_Pickler = Pickler  # undocumented
185_Unpickler = Unpickler  # undocumented
186