Debian Codegraph @

jedi/third_party/typeshed/stdlib/tarfile.pyi

raw symbols SCIP
1 import bz2
2 import io
3 import sys
4 from _typeshed import ReadableBuffer, StrOrBytesPath, StrPath, SupportsRead, WriteableBuffer
5 from builtins import list as _list # aliases to avoid name clashes with fields named "type" or "list"
6 from collections.abc import Callable, Iterable, Iterator, Mapping
7 from gzip import _ReadableFileobj as _GzipReadableFileobj, _WritableFileobj as _GzipWritableFileobj
8 from types import TracebackType
9 from typing import IO, ClassVar, Final, Literal, Protocol, overload, type_check_only
10 from typing_extensions import Self, TypeAlias, deprecated
11
12 if sys.version_info >= (3, 14):
13 from compression.zstd import ZstdDict
14
15 __all__ = [
16 "TarFile",
17 "TarInfo",
18 "is_tarfile",
19 "TarError",
20 "ReadError",
21 "CompressionError",
22 "StreamError",
23 "ExtractError",
24 "HeaderError",
25 "ENCODING",
26 "USTAR_FORMAT",
27 "GNU_FORMAT",
28 "PAX_FORMAT",
29 "DEFAULT_FORMAT",
30 "open",
31 ]
32 if sys.version_info >= (3, 12):
33 __all__ += [
34 "fully_trusted_filter",
35 "data_filter",
36 "tar_filter",
37 "FilterError",
38 "AbsoluteLinkError",
39 "OutsideDestinationError",
40 "SpecialFileError",
41 "AbsolutePathError",
42 "LinkOutsideDestinationError",
43 ]
44 if sys.version_info >= (3, 13):
45 __all__ += ["LinkFallbackError"]
46
47 _FilterFunction: TypeAlias = Callable[[TarInfo, str], TarInfo | None]
48 _TarfileFilter: TypeAlias = Literal["fully_trusted", "tar", "data"] | _FilterFunction
49
50 @type_check_only
51 class _Fileobj(Protocol):
52 def read(self, size: int, /) -> bytes: ...
53 def write(self, b: bytes, /) -> object: ...
54 def tell(self) -> int: ...
55 def seek(self, pos: int, /) -> object: ...
56 def close(self) -> object: ...
57 # Optional fields:
58 # name: str | bytes
59 # mode: Literal["rb", "r+b", "wb", "xb"]
60
61 @type_check_only
62 class _Bz2ReadableFileobj(bz2._ReadableFileobj):
63 def close(self) -> object: ...
64
65 @type_check_only
66 class _Bz2WritableFileobj(bz2._WritableFileobj):
67 def close(self) -> object: ...
68
69 # tar constants
70 NUL: Final = b"\0"
71 BLOCKSIZE: Final = 512
72 RECORDSIZE: Final = 10240
73 GNU_MAGIC: Final = b"ustar \0"
74 POSIX_MAGIC: Final = b"ustar\x0000"
75
76 LENGTH_NAME: Final = 100
77 LENGTH_LINK: Final = 100
78 LENGTH_PREFIX: Final = 155
79
80 REGTYPE: Final = b"0"
81 AREGTYPE: Final = b"\0"
82 LNKTYPE: Final = b"1"
83 SYMTYPE: Final = b"2"
84 CHRTYPE: Final = b"3"
85 BLKTYPE: Final = b"4"
86 DIRTYPE: Final = b"5"
87 FIFOTYPE: Final = b"6"
88 CONTTYPE: Final = b"7"
89
90 GNUTYPE_LONGNAME: Final = b"L"
91 GNUTYPE_LONGLINK: Final = b"K"
92 GNUTYPE_SPARSE: Final = b"S"
93
94 XHDTYPE: Final = b"x"
95 XGLTYPE: Final = b"g"
96 SOLARIS_XHDTYPE: Final = b"X"
97
98 _TarFormat: TypeAlias = Literal[0, 1, 2] # does not exist at runtime
99 USTAR_FORMAT: Final = 0
100 GNU_FORMAT: Final = 1
101 PAX_FORMAT: Final = 2
102 DEFAULT_FORMAT: Final = PAX_FORMAT
103
104 # tarfile constants
105
106 SUPPORTED_TYPES: Final[tuple[bytes, ...]]
107 REGULAR_TYPES: Final[tuple[bytes, ...]]
108 GNU_TYPES: Final[tuple[bytes, ...]]
109 PAX_FIELDS: Final[tuple[str, ...]]
110 PAX_NUMBER_FIELDS: Final[dict[str, type]]
111 PAX_NAME_FIELDS: Final[set[str]]
112
113 ENCODING: Final[str]
114
115 class ExFileObject(io.BufferedReader): # undocumented
116 def __init__(self, tarfile: TarFile, tarinfo: TarInfo) -> None: ...
117
118 class TarFile:
119 OPEN_METH: ClassVar[Mapping[str, str]]
120 name: StrOrBytesPath | None
121 mode: Literal["r", "a", "w", "x"]
122 fileobj: _Fileobj
123 format: _TarFormat
124 tarinfo: type[TarInfo]
125 dereference: bool
126 ignore_zeros: bool
127 encoding: str
128 errors: str
129 fileobject: type[ExFileObject] # undocumented
130 pax_headers: Mapping[str, str]
131 debug: Literal[0, 1, 2, 3]
132 errorlevel: Literal[0, 1, 2]
133 offset: int # undocumented
134 extraction_filter: _FilterFunction | None
135 if sys.version_info >= (3, 13):
136 stream: bool
137 def __init__(
138 self,
139 name: StrOrBytesPath | None = None,
140 mode: Literal["r", "a", "w", "x"] = "r",
141 fileobj: _Fileobj | None = None,
142 format: int | None = None,
143 tarinfo: type[TarInfo] | None = None,
144 dereference: bool | None = None,
145 ignore_zeros: bool | None = None,
146 encoding: str | None = None,
147 errors: str = "surrogateescape",
148 pax_headers: Mapping[str, str] | None = None,
149 debug: Literal[0, 1, 2, 3] | None = None, # default 0
150 errorlevel: Literal[0, 1, 2] | None = None, # default 1
151 copybufsize: int | None = None, # undocumented
152 stream: bool = False,
153 ) -> None: ...
154 else:
155 def __init__(
156 self,
157 name: StrOrBytesPath | None = None,
158 mode: Literal["r", "a", "w", "x"] = "r",
159 fileobj: _Fileobj | None = None,
160 format: int | None = None,
161 tarinfo: type[TarInfo] | None = None,
162 dereference: bool | None = None,
163 ignore_zeros: bool | None = None,
164 encoding: str | None = None,
165 errors: str = "surrogateescape",
166 pax_headers: Mapping[str, str] | None = None,
167 debug: Literal[0, 1, 2, 3] | None = None, # default 0
168 errorlevel: Literal[0, 1, 2] | None = None, # default 1
169 copybufsize: int | None = None, # undocumented
170 ) -> None: ...
171
172 def __enter__(self) -> Self: ...
173 def __exit__(
174 self, type: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
175 ) -> None: ...
176 def __iter__(self) -> Iterator[TarInfo]: ...
177 @overload
178 @classmethod
179 def open(
180 cls,
181 name: StrOrBytesPath | None = None,
182 mode: Literal["r", "r:*", "r:", "r:gz", "r:bz2", "r:xz"] = "r",
183 fileobj: _Fileobj | None = None,
184 bufsize: int = 10240,
185 *,
186 format: int | None = ...,
187 tarinfo: type[TarInfo] | None = ...,
188 dereference: bool | None = ...,
189 ignore_zeros: bool | None = ...,
190 encoding: str | None = ...,
191 errors: str = ...,
192 pax_headers: Mapping[str, str] | None = ...,
193 debug: Literal[0, 1, 2, 3] | None = None, # default 0
194 errorlevel: Literal[0, 1, 2] | None = None, # default 1
195 ) -> Self: ...
196 if sys.version_info >= (3, 14):
197 @overload
198 @classmethod
199 def open(
200 cls,
201 name: StrOrBytesPath | None,
202 mode: Literal["r:zst"],
203 fileobj: _Fileobj | None = None,
204 bufsize: int = 10240,
205 *,
206 format: int | None = ...,
207 tarinfo: type[TarInfo] | None = ...,
208 dereference: bool | None = ...,
209 ignore_zeros: bool | None = ...,
210 encoding: str | None = ...,
211 errors: str = ...,
212 pax_headers: Mapping[str, str] | None = ...,
213 debug: Literal[0, 1, 2, 3] | None = None, # default 0
214 errorlevel: Literal[0, 1, 2] | None = None, # default 1
215 level: None = None,
216 options: Mapping[int, int] | None = None,
217 zstd_dict: ZstdDict | tuple[ZstdDict, int] | None = None,
218 ) -> Self: ...
219
220 @overload
221 @classmethod
222 def open(
223 cls,
224 name: StrOrBytesPath | None,
225 mode: Literal["x", "x:", "a", "a:", "w", "w:", "w:tar"],
226 fileobj: _Fileobj | None = None,
227 bufsize: int = 10240,
228 *,
229 format: int | None = ...,
230 tarinfo: type[TarInfo] | None = ...,
231 dereference: bool | None = ...,
232 ignore_zeros: bool | None = ...,
233 encoding: str | None = ...,
234 errors: str = ...,
235 pax_headers: Mapping[str, str] | None = ...,
236 debug: Literal[0, 1, 2, 3] | None = None, # default 0
237 errorlevel: Literal[0, 1, 2] | None = None, # default 1
238 ) -> Self: ...
239 @overload
240 @classmethod
241 def open(
242 cls,
243 name: StrOrBytesPath | None = None,
244 *,
245 mode: Literal["x", "x:", "a", "a:", "w", "w:", "w:tar"],
246 fileobj: _Fileobj | None = None,
247 bufsize: int = 10240,
248 format: int | None = ...,
249 tarinfo: type[TarInfo] | None = ...,
250 dereference: bool | None = ...,
251 ignore_zeros: bool | None = ...,
252 encoding: str | None = ...,
253 errors: str = ...,
254 pax_headers: Mapping[str, str] | None = ...,
255 debug: Literal[0, 1, 2, 3] | None = None, # default 0
256 errorlevel: Literal[0, 1, 2] | None = None, # default 1
257 ) -> Self: ...
258 @overload
259 @classmethod
260 def open(
261 cls,
262 name: StrOrBytesPath | None,
263 mode: Literal["x:gz", "x:bz2", "w:gz", "w:bz2"],
264 fileobj: _Fileobj | None = None,
265 bufsize: int = 10240,
266 *,
267 format: int | None = ...,
268 tarinfo: type[TarInfo] | None = ...,
269 dereference: bool | None = ...,
270 ignore_zeros: bool | None = ...,
271 encoding: str | None = ...,
272 errors: str = ...,
273 pax_headers: Mapping[str, str] | None = ...,
274 debug: Literal[0, 1, 2, 3] | None = None, # default 0
275 errorlevel: Literal[0, 1, 2] | None = None, # default 1
276 compresslevel: int = 9,
277 ) -> Self: ...
278 @overload
279 @classmethod
280 def open(
281 cls,
282 name: StrOrBytesPath | None = None,
283 *,
284 mode: Literal["x:gz", "x:bz2", "w:gz", "w:bz2"],
285 fileobj: _Fileobj | None = None,
286 bufsize: int = 10240,
287 format: int | None = ...,
288 tarinfo: type[TarInfo] | None = ...,
289 dereference: bool | None = ...,
290 ignore_zeros: bool | None = ...,
291 encoding: str | None = ...,
292 errors: str = ...,
293 pax_headers: Mapping[str, str] | None = ...,
294 debug: Literal[0, 1, 2, 3] | None = None, # default 0
295 errorlevel: Literal[0, 1, 2] | None = None, # default 1
296 compresslevel: int = 9,
297 ) -> Self: ...
298 @overload
299 @classmethod
300 def open(
301 cls,
302 name: StrOrBytesPath | None,
303 mode: Literal["x:xz", "w:xz"],
304 fileobj: _Fileobj | None = None,
305 bufsize: int = 10240,
306 *,
307 format: int | None = ...,
308 tarinfo: type[TarInfo] | None = ...,
309 dereference: bool | None = ...,
310 ignore_zeros: bool | None = ...,
311 encoding: str | None = ...,
312 errors: str = ...,
313 pax_headers: Mapping[str, str] | None = ...,
314 debug: Literal[0, 1, 2, 3] | None = None, # default 0
315 errorlevel: Literal[0, 1, 2] | None = None, # default 1
316 preset: Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | None = ...,
317 ) -> Self: ...
318 @overload
319 @classmethod
320 def open(
321 cls,
322 name: StrOrBytesPath | None = None,
323 *,
324 mode: Literal["x:xz", "w:xz"],
325 fileobj: _Fileobj | None = None,
326 bufsize: int = 10240,
327 format: int | None = ...,
328 tarinfo: type[TarInfo] | None = ...,
329 dereference: bool | None = ...,
330 ignore_zeros: bool | None = ...,
331 encoding: str | None = ...,
332 errors: str = ...,
333 pax_headers: Mapping[str, str] | None = ...,
334 debug: Literal[0, 1, 2, 3] | None = None, # default 0
335 errorlevel: Literal[0, 1, 2] | None = None, # default 1
336 preset: Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | None = ...,
337 ) -> Self: ...
338 if sys.version_info >= (3, 14):
339 @overload
340 @classmethod
341 def open(
342 cls,
343 name: StrOrBytesPath | None,
344 mode: Literal["x:zst", "w:zst"],
345 fileobj: _Fileobj | None = None,
346 bufsize: int = 10240,
347 *,
348 format: int | None = ...,
349 tarinfo: type[TarInfo] | None = ...,
350 dereference: bool | None = ...,
351 ignore_zeros: bool | None = ...,
352 encoding: str | None = ...,
353 errors: str = ...,
354 pax_headers: Mapping[str, str] | None = ...,
355 debug: Literal[0, 1, 2, 3] | None = None, # default 0
356 errorlevel: Literal[0, 1, 2] | None = None, # default 1
357 options: Mapping[int, int] | None = None,
358 zstd_dict: ZstdDict | tuple[ZstdDict, int] | None = None,
359 ) -> Self: ...
360 @overload
361 @classmethod
362 def open(
363 cls,
364 name: StrOrBytesPath | None = None,
365 *,
366 mode: Literal["x:zst", "w:zst"],
367 fileobj: _Fileobj | None = None,
368 bufsize: int = 10240,
369 format: int | None = ...,
370 tarinfo: type[TarInfo] | None = ...,
371 dereference: bool | None = ...,
372 ignore_zeros: bool | None = ...,
373 encoding: str | None = ...,
374 errors: str = ...,
375 pax_headers: Mapping[str, str] | None = ...,
376 debug: Literal[0, 1, 2, 3] | None = None, # default 0
377 errorlevel: Literal[0, 1, 2] | None = None, # default 1
378 options: Mapping[int, int] | None = None,
379 zstd_dict: ZstdDict | tuple[ZstdDict, int] | None = None,
380 ) -> Self: ...
381
382 @overload
383 @classmethod
384 def open(
385 cls,
386 name: StrOrBytesPath | ReadableBuffer | None,
387 mode: Literal["r|*", "r|", "r|gz", "r|bz2", "r|xz", "r|zst"],
388 fileobj: _Fileobj | None = None,
389 bufsize: int = 10240,
390 *,
391 format: int | None = ...,
392 tarinfo: type[TarInfo] | None = ...,
393 dereference: bool | None = ...,
394 ignore_zeros: bool | None = ...,
395 encoding: str | None = ...,
396 errors: str = ...,
397 pax_headers: Mapping[str, str] | None = ...,
398 debug: Literal[0, 1, 2, 3] | None = None, # default 0
399 errorlevel: Literal[0, 1, 2] | None = None, # default 1
400 ) -> Self: ...
401 @overload
402 @classmethod
403 def open(
404 cls,
405 name: StrOrBytesPath | ReadableBuffer | None = None,
406 *,
407 mode: Literal["r|*", "r|", "r|gz", "r|bz2", "r|xz", "r|zst"],
408 fileobj: _Fileobj | None = None,
409 bufsize: int = 10240,
410 format: int | None = ...,
411 tarinfo: type[TarInfo] | None = ...,
412 dereference: bool | None = ...,
413 ignore_zeros: bool | None = ...,
414 encoding: str | None = ...,
415 errors: str = ...,
416 pax_headers: Mapping[str, str] | None = ...,
417 debug: Literal[0, 1, 2, 3] | None = None, # default 0
418 errorlevel: Literal[0, 1, 2] | None = None, # default 1
419 ) -> Self: ...
420 @overload
421 @classmethod
422 def open(
423 cls,
424 name: StrOrBytesPath | WriteableBuffer | None,
425 mode: Literal["w|", "w|xz", "w|zst"],
426 fileobj: _Fileobj | None = None,
427 bufsize: int = 10240,
428 *,
429 format: int | None = ...,
430 tarinfo: type[TarInfo] | None = ...,
431 dereference: bool | None = ...,
432 ignore_zeros: bool | None = ...,
433 encoding: str | None = ...,
434 errors: str = ...,
435 pax_headers: Mapping[str, str] | None = ...,
436 debug: Literal[0, 1, 2, 3] | None = None, # default 0
437 errorlevel: Literal[0, 1, 2] | None = None, # default 1
438 ) -> Self: ...
439 @overload
440 @classmethod
441 def open(
442 cls,
443 name: StrOrBytesPath | WriteableBuffer | None = None,
444 *,
445 mode: Literal["w|", "w|xz", "w|zst"],
446 fileobj: _Fileobj | None = None,
447 bufsize: int = 10240,
448 format: int | None = ...,
449 tarinfo: type[TarInfo] | None = ...,
450 dereference: bool | None = ...,
451 ignore_zeros: bool | None = ...,
452 encoding: str | None = ...,
453 errors: str = ...,
454 pax_headers: Mapping[str, str] | None = ...,
455 debug: Literal[0, 1, 2, 3] | None = None, # default 0
456 errorlevel: Literal[0, 1, 2] | None = None, # default 1
457 ) -> Self: ...
458 @overload
459 @classmethod
460 def open(
461 cls,
462 name: StrOrBytesPath | WriteableBuffer | None,
463 mode: Literal["w|gz", "w|bz2"],
464 fileobj: _Fileobj | None = None,
465 bufsize: int = 10240,
466 *,
467 format: int | None = ...,
468 tarinfo: type[TarInfo] | None = ...,
469 dereference: bool | None = ...,
470 ignore_zeros: bool | None = ...,
471 encoding: str | None = ...,
472 errors: str = ...,
473 pax_headers: Mapping[str, str] | None = ...,
474 debug: Literal[0, 1, 2, 3] | None = None, # default 0
475 errorlevel: Literal[0, 1, 2] | None = None, # default 1
476 compresslevel: int = 9,
477 ) -> Self: ...
478 @overload
479 @classmethod
480 def open(
481 cls,
482 name: StrOrBytesPath | WriteableBuffer | None = None,
483 *,
484 mode: Literal["w|gz", "w|bz2"],
485 fileobj: _Fileobj | None = None,
486 bufsize: int = 10240,
487 format: int | None = ...,
488 tarinfo: type[TarInfo] | None = ...,
489 dereference: bool | None = ...,
490 ignore_zeros: bool | None = ...,
491 encoding: str | None = ...,
492 errors: str = ...,
493 pax_headers: Mapping[str, str] | None = ...,
494 debug: Literal[0, 1, 2, 3] | None = None, # default 0
495 errorlevel: Literal[0, 1, 2] | None = None, # default 1
496 compresslevel: int = 9,
497 ) -> Self: ...
498 @classmethod
499 def taropen(
500 cls,
501 name: StrOrBytesPath | None,
502 mode: Literal["r", "a", "w", "x"] = "r",
503 fileobj: _Fileobj | None = None,
504 *,
505 compresslevel: int = ...,
506 format: int | None = ...,
507 tarinfo: type[TarInfo] | None = ...,
508 dereference: bool | None = ...,
509 ignore_zeros: bool | None = ...,
510 encoding: str | None = ...,
511 pax_headers: Mapping[str, str] | None = ...,
512 debug: Literal[0, 1, 2, 3] | None = None, # default 0
513 errorlevel: Literal[0, 1, 2] | None = None, # default 1
514 ) -> Self: ...
515 @overload
516 @classmethod
517 def gzopen(
518 cls,
519 name: StrOrBytesPath | None,
520 mode: Literal["r"] = "r",
521 fileobj: _GzipReadableFileobj | None = None,
522 compresslevel: int = 9,
523 *,
524 format: int | None = ...,
525 tarinfo: type[TarInfo] | None = ...,
526 dereference: bool | None = ...,
527 ignore_zeros: bool | None = ...,
528 encoding: str | None = ...,
529 pax_headers: Mapping[str, str] | None = ...,
530 debug: Literal[0, 1, 2, 3] | None = None, # default 0
531 errorlevel: Literal[0, 1, 2] | None = None, # default 1
532 ) -> Self: ...
533 @overload
534 @classmethod
535 def gzopen(
536 cls,
537 name: StrOrBytesPath | None,
538 mode: Literal["w", "x"],
539 fileobj: _GzipWritableFileobj | None = None,
540 compresslevel: int = 9,
541 *,
542 format: int | None = ...,
543 tarinfo: type[TarInfo] | None = ...,
544 dereference: bool | None = ...,
545 ignore_zeros: bool | None = ...,
546 encoding: str | None = ...,
547 pax_headers: Mapping[str, str] | None = ...,
548 debug: Literal[0, 1, 2, 3] | None = None, # default 0
549 errorlevel: Literal[0, 1, 2] | None = None, # default 1
550 ) -> Self: ...
551 @overload
552 @classmethod
553 def bz2open(
554 cls,
555 name: StrOrBytesPath | None,
556 mode: Literal["w", "x"],
557 fileobj: _Bz2WritableFileobj | None = None,
558 compresslevel: int = 9,
559 *,
560 format: int | None = ...,
561 tarinfo: type[TarInfo] | None = ...,
562 dereference: bool | None = ...,
563 ignore_zeros: bool | None = ...,
564 encoding: str | None = ...,
565 pax_headers: Mapping[str, str] | None = ...,
566 debug: Literal[0, 1, 2, 3] | None = None, # default 0
567 errorlevel: Literal[0, 1, 2] | None = None, # default 1
568 ) -> Self: ...
569 @overload
570 @classmethod
571 def bz2open(
572 cls,
573 name: StrOrBytesPath | None,
574 mode: Literal["r"] = "r",
575 fileobj: _Bz2ReadableFileobj | None = None,
576 compresslevel: int = 9,
577 *,
578 format: int | None = ...,
579 tarinfo: type[TarInfo] | None = ...,
580 dereference: bool | None = ...,
581 ignore_zeros: bool | None = ...,
582 encoding: str | None = ...,
583 pax_headers: Mapping[str, str] | None = ...,
584 debug: Literal[0, 1, 2, 3] | None = None, # default 0
585 errorlevel: Literal[0, 1, 2] | None = None, # default 1
586 ) -> Self: ...
587 @classmethod
588 def xzopen(
589 cls,
590 name: StrOrBytesPath | None,
591 mode: Literal["r", "w", "x"] = "r",
592 fileobj: IO[bytes] | None = None,
593 preset: int | None = None,
594 *,
595 format: int | None = ...,
596 tarinfo: type[TarInfo] | None = ...,
597 dereference: bool | None = ...,
598 ignore_zeros: bool | None = ...,
599 encoding: str | None = ...,
600 pax_headers: Mapping[str, str] | None = ...,
601 debug: Literal[0, 1, 2, 3] | None = None, # default 0
602 errorlevel: Literal[0, 1, 2] | None = None, # default 1
603 ) -> Self: ...
604 if sys.version_info >= (3, 14):
605 @overload
606 @classmethod
607 def zstopen(
608 cls,
609 name: StrOrBytesPath | None,
610 mode: Literal["r"] = "r",
611 fileobj: IO[bytes] | None = None,
612 level: None = None,
613 options: Mapping[int, int] | None = None,
614 zstd_dict: ZstdDict | tuple[ZstdDict, int] | None = None,
615 *,
616 format: int | None = ...,
617 tarinfo: type[TarInfo] | None = ...,
618 dereference: bool | None = ...,
619 ignore_zeros: bool | None = ...,
620 encoding: str | None = ...,
621 pax_headers: Mapping[str, str] | None = ...,
622 debug: Literal[0, 1, 2, 3] | None = None, # default 0
623 errorlevel: Literal[0, 1, 2] | None = None, # default 1
624 ) -> Self: ...
625 @overload
626 @classmethod
627 def zstopen(
628 cls,
629 name: StrOrBytesPath | None,
630 mode: Literal["w", "x"],
631 fileobj: IO[bytes] | None = None,
632 level: int | None = None,
633 options: Mapping[int, int] | None = None,
634 zstd_dict: ZstdDict | tuple[ZstdDict, int] | None = None,
635 *,
636 format: int | None = ...,
637 tarinfo: type[TarInfo] | None = ...,
638 dereference: bool | None = ...,
639 ignore_zeros: bool | None = ...,
640 encoding: str | None = ...,
641 pax_headers: Mapping[str, str] | None = ...,
642 debug: Literal[0, 1, 2, 3] | None = None, # default 0
643 errorlevel: Literal[0, 1, 2] | None = None, # default 1
644 ) -> Self: ...
645
646 def getmember(self, name: str) -> TarInfo: ...
647 def getmembers(self) -> _list[TarInfo]: ...
648 def getnames(self) -> _list[str]: ...
649 def list(self, verbose: bool = True, *, members: Iterable[TarInfo] | None = None) -> None: ...
650 def next(self) -> TarInfo | None: ...
651 # Calling this method without `filter` is deprecated, but it may be set either on the class or in an
652 # individual call, so we can't mark it as @deprecated here.
653 def extractall(
654 self,
655 path: StrOrBytesPath = ".",
656 members: Iterable[TarInfo] | None = None,
657 *,
658 numeric_owner: bool = False,
659 filter: _TarfileFilter | None = None,
660 ) -> None: ...
661 # Same situation as for `extractall`.
662 def extract(
663 self,
664 member: str | TarInfo,
665 path: StrOrBytesPath = "",
666 set_attrs: bool = True,
667 *,
668 numeric_owner: bool = False,
669 filter: _TarfileFilter | None = None,
670 ) -> None: ...
671 def _extract_member(
672 self,
673 tarinfo: TarInfo,
674 targetpath: str,
675 set_attrs: bool = True,
676 numeric_owner: bool = False,
677 *,
678 filter_function: _FilterFunction | None = None,
679 extraction_root: str | None = None,
680 ) -> None: ... # undocumented
681 def extractfile(self, member: str | TarInfo) -> IO[bytes] | None: ...
682 def makedir(self, tarinfo: TarInfo, targetpath: StrOrBytesPath) -> None: ... # undocumented
683 def makefile(self, tarinfo: TarInfo, targetpath: StrOrBytesPath) -> None: ... # undocumented
684 def makeunknown(self, tarinfo: TarInfo, targetpath: StrOrBytesPath) -> None: ... # undocumented
685 def makefifo(self, tarinfo: TarInfo, targetpath: StrOrBytesPath) -> None: ... # undocumented
686 def makedev(self, tarinfo: TarInfo, targetpath: StrOrBytesPath) -> None: ... # undocumented
687 def makelink(self, tarinfo: TarInfo, targetpath: StrOrBytesPath) -> None: ... # undocumented
688 def makelink_with_filter(
689 self, tarinfo: TarInfo, targetpath: StrOrBytesPath, filter_function: _FilterFunction, extraction_root: str
690 ) -> None: ... # undocumented
691 def chown(self, tarinfo: TarInfo, targetpath: StrOrBytesPath, numeric_owner: bool) -> None: ... # undocumented
692 def chmod(self, tarinfo: TarInfo, targetpath: StrOrBytesPath) -> None: ... # undocumented
693 def utime(self, tarinfo: TarInfo, targetpath: StrOrBytesPath) -> None: ... # undocumented
694 def add(
695 self,
696 name: StrPath,
697 arcname: StrPath | None = None,
698 recursive: bool = True,
699 *,
700 filter: Callable[[TarInfo], TarInfo | None] | None = None,
701 ) -> None: ...
702 def addfile(self, tarinfo: TarInfo, fileobj: SupportsRead[bytes] | None = None) -> None: ...
703 def gettarinfo(
704 self, name: StrOrBytesPath | None = None, arcname: str | None = None, fileobj: IO[bytes] | None = None
705 ) -> TarInfo: ...
706 def close(self) -> None: ...
707
708 open = TarFile.open
709
710 def is_tarfile(name: StrOrBytesPath | IO[bytes]) -> bool: ...
711
712 class TarError(Exception): ...
713 class ReadError(TarError): ...
714 class CompressionError(TarError): ...
715 class StreamError(TarError): ...
716 class ExtractError(TarError): ...
717 class HeaderError(TarError): ...
718
719 class FilterError(TarError):
720 # This attribute is only set directly on the subclasses, but the documentation guarantees
721 # that it is always present on FilterError.
722 tarinfo: TarInfo
723
724 class AbsolutePathError(FilterError):
725 def __init__(self, tarinfo: TarInfo) -> None: ...
726
727 class OutsideDestinationError(FilterError):
728 def __init__(self, tarinfo: TarInfo, path: str) -> None: ...
729
730 class SpecialFileError(FilterError):
731 def __init__(self, tarinfo: TarInfo) -> None: ...
732
733 class AbsoluteLinkError(FilterError):
734 def __init__(self, tarinfo: TarInfo) -> None: ...
735
736 class LinkOutsideDestinationError(FilterError):
737 def __init__(self, tarinfo: TarInfo, path: str) -> None: ...
738
739 class LinkFallbackError(FilterError):
740 def __init__(self, tarinfo: TarInfo, path: str) -> None: ...
741
742 def fully_trusted_filter(member: TarInfo, dest_path: str) -> TarInfo: ...
743 def tar_filter(member: TarInfo, dest_path: str) -> TarInfo: ...
744 def data_filter(member: TarInfo, dest_path: str) -> TarInfo: ...
745
746 class TarInfo:
747 __slots__ = (
748 "name",
749 "mode",
750 "uid",
751 "gid",
752 "size",
753 "mtime",
754 "chksum",
755 "type",
756 "linkname",
757 "uname",
758 "gname",
759 "devmajor",
760 "devminor",
761 "offset",
762 "offset_data",
763 "pax_headers",
764 "sparse",
765 "_tarfile",
766 "_sparse_structs",
767 "_link_target",
768 )
769 name: str
770 path: str
771 size: int
772 mtime: int | float
773 chksum: int
774 devmajor: int
775 devminor: int
776 offset: int
777 offset_data: int
778 sparse: bytes | None
779 mode: int
780 type: bytes # usually one of the TYPE constants, but could be an arbitrary byte
781 linkname: str
782 uid: int
783 gid: int
784 uname: str
785 gname: str
786 pax_headers: Mapping[str, str]
787 def __init__(self, name: str = "") -> None: ...
788 if sys.version_info >= (3, 13):
789 @property
790 @deprecated("Deprecated since Python 3.13; will be removed in Python 3.16.")
791 def tarfile(self) -> TarFile | None: ...
792 @tarfile.setter
793 @deprecated("Deprecated since Python 3.13; will be removed in Python 3.16.")
794 def tarfile(self, tarfile: TarFile | None) -> None: ...
795 else:
796 tarfile: TarFile | None
797
798 @classmethod
799 def frombuf(cls, buf: bytes | bytearray, encoding: str, errors: str) -> Self: ...
800 @classmethod
801 def fromtarfile(cls, tarfile: TarFile) -> Self: ...
802 @property
803 def linkpath(self) -> str: ...
804 @linkpath.setter
805 def linkpath(self, linkname: str) -> None: ...
806 def replace(
807 self,
808 *,
809 name: str = ...,
810 mtime: float = ...,
811 mode: int = ...,
812 linkname: str = ...,
813 uid: int = ...,
814 gid: int = ...,
815 uname: str = ...,
816 gname: str = ...,
817 deep: bool = True,
818 ) -> Self: ...
819 def get_info(self) -> Mapping[str, str | int | bytes | Mapping[str, str]]: ...
820 def tobuf(self, format: _TarFormat | None = 2, encoding: str | None = "utf-8", errors: str = "surrogateescape") -> bytes: ...
821 def create_ustar_header(
822 self, info: Mapping[str, str | int | bytes | Mapping[str, str]], encoding: str, errors: str
823 ) -> bytes: ...
824 def create_gnu_header(
825 self, info: Mapping[str, str | int | bytes | Mapping[str, str]], encoding: str, errors: str
826 ) -> bytes: ...
827 def create_pax_header(self, info: Mapping[str, str | int | bytes | Mapping[str, str]], encoding: str) -> bytes: ...
828 @classmethod
829 def create_pax_global_header(cls, pax_headers: Mapping[str, str]) -> bytes: ...
830 def isfile(self) -> bool: ...
831 def isreg(self) -> bool: ...
832 def issparse(self) -> bool: ...
833 def isdir(self) -> bool: ...
834 def issym(self) -> bool: ...
835 def islnk(self) -> bool: ...
836 def ischr(self) -> bool: ...
837 def isblk(self) -> bool: ...
838 def isfifo(self) -> bool: ...
839 def isdev(self) -> bool: ...