Re: fcntl example

Guido.van.Rossum@cwi.nl
Tue, 03 May 1994 13:47:18 +0200

On re-reading lockfile.py for the third time I found another nit :-)

> if fcntl.fcntl(fd, fcntlh.F_SETLKW, flock) != -1:
> return 1
> else:
> return 0

It's not particularly useful nor necessary to return a value nor to
check the return value from fcntl.fcntl() -- if an error is returned
by the C fcntl(), the Python version raises IOError so the return
value of fcntl.fcntl will never be -1. On success the Python
interface does return the return value of the C interface, since for
certain fcntl codes this is used -- but the lock calls don't have a
meaningful return value apart from the error so the lockfile interface
can throw this return value away. I would just write

> dummy = fcntl.fcntl(fd, fcntlh.F_SETLKW, flock)

(to avoid the value to be printed). The caller can then just say

lockfile(f)

instead of

dummy = lockfile(f)

or, if you want to catch the error

try:
lockfile(f)
except IOError, msg:
print "Can't lock it:", msg

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
URL: <http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>