MCPcopy Index your code
hub / github.com/python/cpython / extractfile

Method extractfile

Lib/tarfile.py:2569–2599  ·  view source on GitHub ↗

Extract a member from the archive as a file object. 'member' may be a filename or a TarInfo object. If 'member' is a regular file or a link, an io.BufferedReader object is returned. For all other existing members, None is returned. If 'member' does not appear

(self, member)

Source from the content-addressed store, hash-verified

2567 self._dbg(1, "tarfile: %s %s" % (type(e).__name__, e))
2568
2569 def extractfile(self, member):
2570 """Extract a member from the archive as a file object. 'member' may be
2571 a filename or a TarInfo object. If 'member' is a regular file or
2572 a link, an io.BufferedReader object is returned. For all other
2573 existing members, None is returned. If 'member' does not appear
2574 in the archive, KeyError is raised.
2575 """
2576 self._check("r")
2577
2578 if isinstance(member, str):
2579 tarinfo = self.getmember(member)
2580 else:
2581 tarinfo = member
2582
2583 if tarinfo.isreg() or tarinfo.type not in SUPPORTED_TYPES:
2584 # Members with unknown types are treated as regular files.
2585 return self.fileobject(self, tarinfo)
2586
2587 elif tarinfo.islnk() or tarinfo.issym():
2588 if isinstance(self.fileobj, _Stream):
2589 # A small but ugly workaround for the case that someone tries
2590 # to extract a (sym)link as a file-object from a non-seekable
2591 # stream of tar blocks.
2592 raise StreamError("cannot extract (sym)link as file object")
2593 else:
2594 # A (sym)link's file object is its target's file object.
2595 return self.extractfile(self._find_link_target(tarinfo))
2596 else:
2597 # If there's no data associated with the member (directory, chrdev,
2598 # blkdev, etc.), return None instead of a file object.
2599 return None
2600
2601 def _extract_member(self, tarinfo, targetpath, set_attrs=True,
2602 numeric_owner=False, *, filter_function=None,

Callers 15

test_fileobj_iterMethod · 0.80
test_fileobj_seekMethod · 0.80
test_fileobj_textMethod · 0.80
_test_fileobj_linkMethod · 0.80
test_read_throughMethod · 0.80

Calls 7

_checkMethod · 0.95
getmemberMethod · 0.95
_find_link_targetMethod · 0.95
StreamErrorClass · 0.85
isregMethod · 0.80
islnkMethod · 0.80
issymMethod · 0.80

Tested by 15

test_fileobj_iterMethod · 0.64
test_fileobj_seekMethod · 0.64
test_fileobj_textMethod · 0.64
_test_fileobj_linkMethod · 0.64
test_read_throughMethod · 0.64