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.

classification
Title: tarfile: unexpected IsADirectoryError on extraction
Type: behavior Stage:
Components: Versions: Python 3.8, Python 3.7, Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: alter-bug-tracer
Priority: normal Keywords:

Created on 2019-05-21 07:13 by alter-bug-tracer, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
input0.tar alter-bug-tracer, 2019-05-21 07:13
Messages (2)
msg342983 - (view) Author: alter-bug-tracer (alter-bug-tracer) * Date: 2019-05-21 07:13
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'
msg343005 - (view) Author: Michele Angrisano (mangrisano) * Date: 2019-05-21 10:47
It looks like it has the same behavior of issue8958.
History
Date User Action Args
2022-04-11 14:59:15adminsetgithub: 81167
2019-06-02 22:04:23mangrisanosetnosy: - mangrisano
2019-05-21 10:47:21mangrisanosetnosy: + mangrisano
messages: + msg343005
2019-05-21 07:13:29alter-bug-tracercreate