By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

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

P: n/a
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:
test = file("testfile","w",1024)
test.write("a"*1025);


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
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Wed, Feb 04, 2004 at 03:14:23PM -0800, Darby Wong wrote:
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

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.