Re: While coding a little script

Guido.van.Rossum@cwi.nl
Wed, 24 Aug 1994 23:32:04 +0200

> I came across the fact that the open() builtin allows an undocumented
> third argument, the bufsize. However, it lacks the ability to set the
> permissions of a newly created file. I assume that you have to dink
> around with os.umask() to get things happen, rather clumsy. I actually
> expected the third argument to open() to be the mode of the file (as in
> dbm.open()). Is anybody using the bufsize functionality? Can we add,
> or could you point out, an easier way of specifying the permissions of
> newly created files?

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>