Question about the built-in file() and buffer size...

D

Darby Wong

The documentation for file() says that the buffer size will be:

system default if bufsize == -1,
unbuffered if bufsize == 0,
line buffered if bufsize == 1,
or else it is just bufsize.

I can't get this last one to work... if I write something like:

the file "testfile" is not written to... it only gets written to when
the buffer size reaches 4096 bytes, which is the system default from
what I can ascertain. It doesn't work if I set bufsize to be larger
than 4096 either, it will get written when it reaches 4096 bytes no
matter what.

Am I doing something wrong? Is this option not implemented all the
way (the -1,0,1 seem to work fine... except that 1 is also 4096 byte
buffered, in addition to being line buffered)?

Thanks :)
Darby
 
J

Jeff Epler

The documentation for file() says that the buffer size will be:
[...]

The buffer size argument is merely passed along to a call to setvbuf
as the "size" argument, like so:
+setvbuf(0x0815e780, NULL, 0, 1024) = 0
So it's beyond the control of Python. Apparently your C library
disregards the actual size parameter given and choose another size
(perhaps related to the OS page size or the filesystem block size).

This behavior is different between 2.2.2 from redhat9 and python CVS.
In CVS, the buffer is allocated by Python:
setvbuf(0x082089d0, 0x08183338, 0, 1024) = 0
I don't know if 2.3 behaves like 2.2 or like this CVS release. This
might give the behavior you're interested in, but it's still safer to
use .flush() at the right time, because either the C standard doesn't
guarantee much, or C libraries aren't very good.

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top