[Python-Dev] Fuzziness in io module specs

Steven D'Aprano steve at pearwood.info
Sun Sep 20 02:07:11 CEST 2009


On Sat, 19 Sep 2009 06:08:23 am James Y Knight wrote:
> On Sep 18, 2009, at 3:55 PM, MRAB wrote:
> > I think that this should be an invariant:
> >
> >    0 <= file pointer <= file size
> >
> > so the file pointer might sometimes have to be moved.
> >
> > As for the question of whether 'truncate' should be able to
> > lengthen a file, the method name suggests no; if the method name
> > were 'resize', for
> > example, then maybe yes, zeroing the new bytes for security.
>
> Why are you just making things up? 

Well, why not? The POSIX standard wasn't derived from the physical laws 
of the universe, somebody -- or more likely, some committee -- made 
them up. It's not like we are forced to accept MRAB's suggestions, but 
surely he's allowed to make them?

> There is a *vast* amount of 
> precedent for how file operations should work. Python should follow
> that precedent and do like POSIX unless there's a compelling reason
> not to. Quoting:
>
>         If  fildes  refers  to  a  regular  file,  the ftruncate()
> function shall cause the size of the file to be truncated to
>         length. If the size of the file previously exceeded length,
> the extra data shall no longer be available to reads on the
>         file.  If  the  file  previously  was smaller than this size,
> ftruncate() shall either increase the size of the file or
>         fail.   XSI-conformant systems shall increase the size of the
> file.  If the file size is increased, the  extended  area
>         shall appear as if it were zero-filled. The value of the seek
> pointer shall not be modified by a call to ftruncate().

Standard or not, it has a seriously misleading name. Truncate means "to 
cut off", and is a standard English word and mathematical term. In 
English and mathematics, truncating something can NEVER lead it to 
increase in size, the object being truncated can only decrease or 
remain the same size.

Using "truncate" to mean "increase in size" makes about as much sense as 
having a list method called "remove" used to insert items. I can't 
imagine what the committee who approved this were thinking.

Java's FileChannel object uses truncate to only shrink a file, never 
increase it. To increase it, you have to explicitly write to the file.

http://java.sun.com/j2se/1.4.2/docs/api/java/nio/channels/FileChannel.html#truncate(long)

In any case, some platforms don't allow the file pointer to exceed the 
size of the file, so it's not clear that the POSIX standard is 
appropriate for Python.



-- 
Steven D'Aprano


More information about the Python-Dev mailing list