Message223318
It looks like if you pass a âfileobjâ argument to âgettarinfoâ, it assumes it can use the ânameâ as a text string.
>>> import tarfile
>>> with tarfile.open("/dev/null", "w") as tar, open("/bin/sh", "rb") as file: tar.gettarinfo(fileobj=file)
...
<TarInfo 'bin/sh' at 0x7f13cc937f20>
>>> with tarfile.open("/dev/null", "w") as tar, open(b"/bin/sh", "rb") as file: tar.gettarinfo(fileobj=file)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/media/disk/home/proj/python/cpython/Lib/tarfile.py", line 1767, in gettarinfo
arcname = arcname.replace(os.sep, "/")
TypeError: expected bytes, bytearray or buffer compatible object
>>> with tarfile.open("/dev/null", "w") as tar, open(0, "rb", closefd=False) as file: tar.gettarinfo(fileobj=file)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/media/disk/home/proj/python/cpython/Lib/tarfile.py", line 1766, in gettarinfo
drv, arcname = os.path.splitdrive(arcname)
File "Lib/posixpath.py", line 133, in splitdrive
return p[:0], p
TypeError: 'int' object is not subscriptable
In my case, my code always sets the final TarInfo.name attribute later on, so the initial name does not matter. Perhaps at least the documentation should say that âfileobj.nameâ must be a real unencoded file name string unless âarcnameâ is also given. My workaround was to add a dummy arcname argument, a bit like this:
# Explicit dummy name to avoid using file name of bytes
tarinfo = self.tar.gettarinfo(fileobj=file, arcname="")
# . . .
tarinfo.name = "{}/{}".format(self.pkgname, name) |
|
| Date |
User |
Action |
Args |
| 2014-07-17 07:52:08 | martin.panter | set | recipients:
+ martin.panter |
| 2014-07-17 07:52:08 | martin.panter | set | messageid: <1405583528.37.0.553699872308.issue21996@psf.upfronthosting.co.za> |
| 2014-07-17 07:52:08 | martin.panter | link | issue21996 messages |
| 2014-07-17 07:52:07 | martin.panter | create | |
|