Question: posix fcntl module

Sheila King sheila at spamcop.net
Tue Aug 28 21:58:20 EDT 2001


On Tue, 28 Aug 2001 20:02:51 -0400 (EDT), Ignacio Vazquez-Abrams
<ignacio at openservices.net> wrote in comp.lang.python in article
<mailman.999043423.30546.python-list at python.org>:

:Oh hell. fcntlmodule.so munges the parameter passed to it so that LOCK_UN
:looks like F_UNLCK, LOCK_SH like F_RDLCK, and LOCK_EX like F_WRLCK. 

I was suspecting that might be what was going on.

:But I think I have found the real reason this time. Passing no mode argument
:to open() tells Python to open the file read-only. Guess what? By rejecting
:LOCK_EX Linux is telling you "Write lock? Why the hell does a read-only file
:need a write lock?". Same thing with 'w' and LOCK_SH. 'w+' and 'r+' can take
:both.

Great. Thanks. I will test more thoroughly, later tonight. But
preliminary testing seems to indicate this has solved the problem.

:So it seems that you need to take both a filename _and_ a mode in __init__(),
:and then raise an exception in the appropriate locking methods based on
:f.mode. You may also want to take a third parameter for initial lock mode,
:just in case.

Not at all. Based on whether the flag is LOCK_SH or LOCK_EX, I know
whether to open the file with 'r' or with 'w'.

:Also, you should make it so that the file is immediately opened in __init__(),

Why? I don't see any persuasive reason for that. The methods that I'm
using under windows don't allow for me to separate the action of opening
the file and obtaining the lock (there is a single command which does
both), and I wanted to keep the functions between the windows and the
posix versions as close to parallel as possible. In any case, I can't
see any compelling reason for the file opening to occur in the init
function.

:then close the file both in a close() method and in __del__(). In both methods
:you should probably check f.closed to make sure that the file hasn't already
:been closed.

Thanks. I probably do need to make a __del__ method. I hadn't thought of
that.

:Hopefully your problem has now been solved. Third (or fourth or whatever :) )
:time's the charm, right?

I really appreciate the help. The Python community is great!

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/




More information about the Python-list mailing list