[ python-Bugs-1461855 ] fdopen() not guaranteed to have Python semantics

SourceForge.net noreply at sourceforge.net
Fri Mar 31 22:15:19 CEST 2006


Bugs item #1461855, was opened at 2006-03-31 04:37
Message generated for change (Comment added) made by movement
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1461855&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.4
Status: Closed
Resolution: Fixed
Priority: 5
Submitted By: John Levon (movement)
Assigned to: Nobody/Anonymous (nobody)
Summary: fdopen() not guaranteed to have Python semantics

Initial Comment:
The specification for seek() says:

 seek(  	offset[, whence])
    Note that if the file is opened for appending (mode
'a' or 'a+'), any seek() operations will be undone at
the next write.

Consider operating on an fdopen()ed file. The Python
source simply calls into the OS-provided fdopen():

http://pxr.openlook.org/pxr/source/Modules/posixmodule.c#3530

However, the POSIX standard

http://www.opengroup.org/onlinepubs/009695399/functions/fdopen.html

says:

"Although not explicitly required by this volume of
IEEE Std 1003.1-2001, a good implementation of append
(a) mode would cause the O_APPEND flag to be set."

Thus, to ensure Python semantics, Python's fdopen()
must perform an fcntl() to ensure O_APPEND is set.

For example, on Solaris, this optional O_APPEND
behaviour is not applied:

http://cvs.opensolaris.org/source/xref/on/usr/src/lib/libc/port/stdio/fdopen.c?r=1.22#97

This has recently caused serious problems with the
Mercurial SCM.

----------------------------------------------------------------------

>Comment By: John Levon (movement)
Date: 2006-03-31 20:15

Message:
Logged In: YES 
user_id=53034

Shouldn't the documentation now state this change in
behaviour? i.e. if fdopen() fails, O_APPEND may still have
become set.

This behaviour seems a little odd to me in fact. Can't we set
fcntl after a successful fdopen()[1]? Also wouldn't it be
useful to point to the standards sentence I quote above?

[1] not sure what the ALLOW_THREAD thing does?

----------------------------------------------------------------------

Comment By: Georg Brandl (gbrandl)
Date: 2006-03-31 20:00

Message:
Logged In: YES 
user_id=849994

Applied patch in rev. 43501, 43502.

----------------------------------------------------------------------

Comment By: Ralf Schmitt (titty)
Date: 2006-03-31 17:02

Message:
Logged In: YES 
user_id=17929

patch here:
http://sourceforge.net/tracker/index.php?func=detail&aid=1462227&group_id=5470&atid=305470

----------------------------------------------------------------------

Comment By: Ralf Schmitt (titty)
Date: 2006-03-31 16:20

Message:
Logged In: YES 
user_id=17929

freebsd 4.11 shows the same behaviour.

import fcntl
import os

def test_fdopen_append():
    
    def is_append(fd):
        return bool(fcntl.fcntl(fd, fcntl.F_GETFL) &
os.O_APPEND)


    fd = os.open("foo.txt", os.O_RDWR | os.O_CREAT)
    assert fd != -1
    
    print "is_append:", is_append(fd)
    
    f=os.fdopen(fd, 'a')
    print "after fdopen is_append:", is_append(fd)

    assert is_append(fd)

test_fdopen_append()


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1461855&group_id=5470


More information about the Python-bugs-list mailing list