Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: where is a true built-in file object required?

From the python docs on urllib.urlopen(), talking about the file-like object the function returns on success:

(It is not a built-in file object, however, so it can’t be used at those few places where a true built-in file object is required.)

What are those few places where a true built-in file object is required?

NB: This is purely out of curiosity... no practical problem to be solved here.

like image 417
Claudiu Avatar asked May 15 '11 23:05

Claudiu


1 Answers

As the other answers have noted, there isn't really anywhere that specifically needs a file object, but there are interfaces that need real OS level file descriptors, which file-like objects like StringIO can't provide.

The os module has several methods that operate directly on file descriptors, as do the select and mmap modules. Some higher level modules rely on those under the hood, so may exhibit some limitations when working with file-like objects that don't support the fileno() method.

I'm not aware of any consistent documentation of these limitations, though (aside from the obvious one of APIs that accept numeric file descriptors rather than objects). It's more a matter of "try it and see if it works". If things don't work, then this is something to keep in the back of your mind to check as a possible culprit (especially if phrases like "no attribute named 'fileno'" or "invalid file descriptor" appear in any relevant error messages).

like image 155
ncoghlan Avatar answered Sep 25 '22 03:09

ncoghlan