[Python-Dev] file(file)

Brett Cannon brett at python.org
Sat Jan 13 01:37:26 CET 2007


On 1/12/07, A.M. Kuchling <amk at amk.ca> wrote:
> Forwarded for discussion from http://www.python.org/sf/1633665.
>
> --amk
>
> [forwarded from http://bugs.debian.org/327060]
>
> Many types in Python are idempotent, so that int(1) works
> as expected, float(2.34)==2.34, ''.join('hello')=='hello'
> et cetera.
>
> Why not file()? Currently, file(open(something, 'r')) fails
> with "TypeError: coercing to Unicode: need string or buffer, file found."
>
> Semantically, file(fd) should be equivalent to os.fdopen(fd.fileno())
> or the proposed file.fromfd() (Jp Calderone, Python-dev, 2003).
> You should get another independent
> file object that accesses the same file.
>

For security reasons I might be asking for file's constructor to be
removed from the type for Python source code at some point (it can be
relocated to an extension module if desired).  By forcing people to go
through open() to create a file object you can more easily control
read/write access to the file system (assuming the proper importation
of extension modules has been blocked).  Not removing the constructor
allows any code that has been explicitly given a file object but not
open() to just get the class and call the constructor to open a new
file.

Don't know when I am gonig to get around to asking for it, though
(still have to finish my security work to have something to present to
python-dev).

-Brett


More information about the Python-Dev mailing list