Patch #1504073: Fix tarfile.open() for mode "r" with a fileobj argument.
Backport from rev. 53161.
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 4a41d9f..4c7dadb 100644
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1133,9 +1133,13 @@
# Find out which *open() is appropriate for opening the file.
for comptype in cls.OPEN_METH:
func = getattr(cls, cls.OPEN_METH[comptype])
+ if fileobj is not None:
+ saved_pos = fileobj.tell()
try:
return func(name, "r", fileobj)
except (ReadError, CompressionError):
+ if fileobj is not None:
+ fileobj.seek(saved_pos)
continue
raise ReadError("file could not be opened successfully")