This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author alter-bug-tracer
Recipients alter-bug-tracer
Date 2019-05-21.07:13:28
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1558422809.19.0.381089325981.issue36986@roundup.psfhosted.org>
In-reply-to
Content
The following code creates a new directory 'output' and extracts the tar archive given as argv in it. When a malformed archive (attached) is given as argv, a IsADirectoryError is thrown, as opposed to extracting the file contained in the archive to the 'output' directory.

Code:
import tarfile
import sys
import os

def extract(archive_path, destination_path):
  if not tarfile.is_tarfile(archive_path):
    print ('Not a tar file')
    return
  tar = tarfile.open(archive_path, 'r')
  for info in tar.getmembers():
    tar.extractall(destination_path, members=[tar.getmember(info.name)])

destination_path = 'output'
os.mkdir(destination_path)
extract(sys.argv[1], destination_path)

Result:
Traceback (most recent call last):
  File "code.py", line 15, in <module>
    extract(sys.argv[1], destination_path)
  File "code.py", line 11, in extract
    tar.extractall(destination_path, members=[tar.getmember(info.name)])
  File "/usr/lib/python3.6/tarfile.py", line 2010, in extractall
    numeric_owner=numeric_owner)
  File "/usr/lib/python3.6/tarfile.py", line 2052, in extract
    numeric_owner=numeric_owner)
  File "/usr/lib/python3.6/tarfile.py", line 2122, in _extract_member
    self.makefile(tarinfo, targetpath)
  File "/usr/lib/python3.6/tarfile.py", line 2163, in makefile
    with bltn_open(targetpath, "wb") as target:
IsADirectoryError: [Errno 21] Is a directory: 'output'
History
Date User Action Args
2019-05-21 07:13:29alter-bug-tracersetrecipients: + alter-bug-tracer
2019-05-21 07:13:29alter-bug-tracersetmessageid: <1558422809.19.0.381089325981.issue36986@roundup.psfhosted.org>
2019-05-21 07:13:29alter-bug-tracerlinkissue36986 messages
2019-05-21 07:13:28alter-bug-tracercreate