[Python-Dev] fileobj.read(float): warning or error?

Leif Walsh leif.walsh at gmail.com
Wed Jul 23 03:19:49 CEST 2008

On Tue, Jul 22, 2008 at 3:46 PM, Cameron Simpson <cs at zip.com.au> wrote:
> [ Don't you mean "min()"? Unimportant. ]

Haha, that's what I get for not actually _running_ the code example.

> I see the convenience here, but doubt I'd ever do that myself.
> I'd write the above like this:
>  while bytes_left > 0:
>    buf = f.read(max(bytes_left, page_size))
>    if buf == 0:
>      break
>    process(buf)  # updates bytes_left
> I'm kind of picky about doing things exactly as often as required and no
> more. Especially things that call another facility.

I do the same, but I know lots of people that prefer the example I
sent earlier.  Also, if we ever adopt the "while ... as ..." syntax
(here's not hoping) currently being discussed in another thread,
having read(0) return None or an empty buffer will cause that idiom to
short circuit as well.

> read(0) itself must internally have a check for size == 0 anyway, so
> it's not like the overall system is less complex. If we're unlucky it
> could trickle all the way down to an OS system call to read(2) (UNIX,
> substitute as suitable elsewhere) and for a no-op that would be overkill
> by far. The only way the read() implementation would avoid that is by
> doing the test on size anyway. But since read() is opaque IMO it is
> better to avoid it at the upper level if we know it will produce
> nothing.

If we are going to make read(0) a no-op, we should definitely do it
before it hits the underlying implementation, for portability's sake.

On Tue, Jul 22, 2008 at 4:43 PM, Victor Stinner
<victor.stinner at haypocalc.com> wrote:
> Would it possible to create an option "strict mode" which disallow dangerous
> cast? Especially in PyArg_Parse*() functions.

Ack!  We're not writing Perl here, guys.  Can we please not start
having multiple subsets of the language that are separately valid?


More information about the Python-Dev mailing list