You forget that __builtin__.open() is an interface to stdio's
fopen(), not to the POSIX open() system call. The same problem exists
in C. The same solution also works: use fd = posix.open(filename,
mode) followed by fp = posix.fdopen(fd). (I recommend using posix
instead of os here to point out that this won't necessarily work on
non-UNIX systems.)
The bufsize argument was added and documented in 1.0.2 as far as I
recollect. I first played with a separate setbuf() method, as in the
C stdio library, but found that the documented restriction that you
can't call setbuf() once a file has been read or written is quite
serious -- on most UNIX versions this causes a core dump, which is a
no-no for Python built-in operations. There is no portable way to
test of a stdio FILE* has been read/written before, so to report a
Python exception instead of dumping core would have required setting a
flag on the first read/write, which would require adding flag-setting
calls to every file method... It seemed much more elegant to pack the
buffer size in the open call.
Cheers,
--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
<URL:http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>