[Python-Dev] fp vs. fd

Guido van Rossum guido@digicool.com
Wed, 07 Feb 2001 03:36:56 -0500


> There are a number of places in the Python library that require a
> numeric file descriptor, rather than a file object.  This complicates
> code slightly and (IMO) breaches the wrapper around the file-object
> abstraction (which Guido says is only supposed to depend on
> stdio-level stuff).
> 
> Are there design reasons for this, or is it historical accident?
> 
> If the latter, I'll go through and fix these to accept either an fd
> or an fp.  And fix the docs, too.

I don't see why this violates abstraction.  Take e.g. select.
Sometimes you have opened a low-level filedescriptor, e.g. with
os.open() or os.pipe().  So it clearly must take an integer fd.
Sometimes you have an object at hand that has a fileno() method,
e.g. a socket.  It would be a waste of time to have to maintain a
mapping from integer fd to object in the app, so it's useful to take
an object with a fileno() method.

There's no problem with knowing that on some (most) platforms,
standard files have an underlying implementation using integer fds,
and using this in some apps.  That's not to say that Python should
offer standar APIs that *require* having such an implementation.

--Guido van Rossum (home page: http://www.python.org/~guido/)