Re: fcntl example

Jaap Vermeulen (jaap@sequent.com)
Thu, 5 May 94 22:18:31 GMT

In <9405030716.AA11277=guido@voorn.cwi.nl> Guido.van.Rossum@cwi.nl writes:

>File locking is not in the standard file object because it's not the
>same across different UNIX platforms. Now that we have the configure
>script, I suppose someone could figure out how to do it on SYSV and
>BSD and add some ifdef'ed code that implements simple file-level
>locking using a consistent interface to Python users. (Could a BSD
>version be done in Python?)

I propose an interface as implemented in my posixfile module. It
allows arbitrary file locks, including sections of a file. How this
exactly translates to BSD I forgot, but I'm sure it can be made
compatible.

To refresh you memory, here's the introduction to the module:

#
# Extended file operations
#
# f = posixfile.open(filename, mode)
#
# f.file()
# will return the original builtin file object

*** This used to be necessary for marshal.dump() et al., but
I'm not sure it's still necessary...

#
# f.dup()
# will return a new file object based on a new filedescriptor
#
# f.dup2(fd)
# will return a new file object based on the given filedescriptor
#
# f.flags(mode)
# will turn on the associated flag (merge)
# mode can contain the following characters:
#
# (character representing a flag)
# a append only flag
# c close on exec flag
# n no delay flag
# s synchronization flag
# (modifiers)
# ! turn flags 'off' instead of default 'on'
# = copy flags 'as is' instead of default 'merge'
# ? return a string in which the characters represent the flags
# that are set
#
# note: - the '!' and '=' modifiers are mutually exclusive.
# - the '?' modifier will return the status of the flags after they
# have been changed by other characters in the mode string
#
# f.lock(mode [, len [, start [, whence]]])
# will (un)lock a region
# mode can contain the following characters:
#
# (character representing type of lock)
# u unlock
# r read lock
# w write lock
# (modifiers)
# | wait until the lock can be granted
# ? return the first lock conflicting with the requested lock
# or 'None' if there is no conflict. The lock returned is in the
# format (mode, len, start, whence, pid) where mode is a
# character representing the type of lock ('r' or 'w')
#
# note: - the '?' modifier prevents a region from being locked; it is
# query only
#

-Jaap-

-- 
Jaap Vermeulen					+--------------------------+
						| Sequent Computer Systems |
	Internet : jaap@sequent.com		| Beaverton, Oregon	   |
	Uucp	 : ...uunet!sequent!jaap	+--------------------------+