smore-inc/tinys3 — pull request #31 by andrewsg (2014-08-05 21:34:00+00:00)
Title: Use EAFP instead of hasattr() to check o.fileno

This will allow files being extracted via the tarfile module to be uploaded directly, without wrapping them in another file-like object.

Here's an example of the problem:

Code (abbreviated):

with tarfile.open(fileobj=tarball_fd) as archive:
for frame_number in range(1, 13):
with archive.extractfile("frame{0:02d}.png".format(frame_number)) as fd: # using extractfile here protects the filesystem from any maliciously named files -- we only get a file descriptor, it doesn't get written to the filesystem
key = "{base}_frame{frame_number}.png".format(base=tarball_hash, frame_number=frame_number)
conn.upload(key, fd, settings.AWS_STORAGE_BUCKET_NAME, public=True)

Traceback:

File "/home/andrewsg/src/gb/lib/python3.4/site-packages/tinys3/util.py", line 68, in __len__
return os.fstat(o.fileno()).st_size
AttributeError: '_FileInFile' object has no attribute 'fileno'

pdb inspection:

> /home/andrewsg/src/gb/lib/python3.4/site-packages/tinys3/util.py(69)__len__()
-> return os.fstat(o.fileno()).st_size
(Pdb) o

(Pdb) hasattr(o, 'fileno')
True
(Pdb) o.fileno

(Pdb) o.fileno()
*** AttributeError: '_FileInFile' object has no attribute 'fileno'