Is this a bug in the windows tempfile.py?

J

Jonathan Wright

import tempfile
import types
print isinstance(tempfile.TemporaryFile(), types.FileType)


Prints False on Windows and True on linux or any other posix system.
The reason for the difference is on posix systems TemporaryFile returns
an actual file (the result of os.fdopen()) and on windows,
TemporaryFile returns a file wrapper which forwards getattr calls to
the underlying file.

The wrapper used on windows provides an alternative close method that
deletes the temporary file.
Should the wrapper inherit types.FileType?

Jonathan.
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Jonathan said:
Should the wrapper inherit types.FileType?

No. Code assuming that the result from tempfile.TemporaryFile
is a file object is broken - you are only guaranteed a
file-like object. Applications should not use isinstance to
determine whether something is a file-like object.

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
Martindap

Latest Threads

Top