/Doc/library/posixfile.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 190 lines · 132 code · 58 blank · 0 comment · 0 complexity · 4e664770adb84d17ac62a2cb248caeba MD5 · raw file

  1. :mod:`posixfile` --- File-like objects with locking support
  2. ===========================================================
  3. .. module:: posixfile
  4. :platform: Unix
  5. :synopsis: A file-like object with support for locking.
  6. :deprecated:
  7. .. moduleauthor:: Jaap Vermeulen
  8. .. sectionauthor:: Jaap Vermeulen
  9. .. index:: pair: POSIX; file object
  10. .. deprecated:: 1.5
  11. The locking operation that this module provides is done better and more portably
  12. by the :func:`fcntl.lockf` call.
  13. .. index:: single: fcntl() (in module fcntl)
  14. This module implements some additional functionality over the built-in file
  15. objects. In particular, it implements file locking, control over the file
  16. flags, and an easy interface to duplicate the file object. The module defines a
  17. new file object, the posixfile object. It has all the standard file object
  18. methods and adds the methods described below. This module only works for
  19. certain flavors of Unix, since it uses :func:`fcntl.fcntl` for file locking.
  20. To instantiate a posixfile object, use the :func:`open` function in the
  21. :mod:`posixfile` module. The resulting object looks and feels roughly the same
  22. as a standard file object.
  23. The :mod:`posixfile` module defines the following constants:
  24. .. data:: SEEK_SET
  25. Offset is calculated from the start of the file.
  26. .. data:: SEEK_CUR
  27. Offset is calculated from the current position in the file.
  28. .. data:: SEEK_END
  29. Offset is calculated from the end of the file.
  30. The :mod:`posixfile` module defines the following functions:
  31. .. function:: open(filename[, mode[, bufsize]])
  32. Create a new posixfile object with the given filename and mode. The *filename*,
  33. *mode* and *bufsize* arguments are interpreted the same way as by the built-in
  34. :func:`open` function.
  35. .. function:: fileopen(fileobject)
  36. Create a new posixfile object with the given standard file object. The resulting
  37. object has the same filename and mode as the original file object.
  38. The posixfile object defines the following additional methods:
  39. .. method:: posixfile.lock(fmt, [len[, start[, whence]]])
  40. Lock the specified section of the file that the file object is referring to.
  41. The format is explained below in a table. The *len* argument specifies the
  42. length of the section that should be locked. The default is ``0``. *start*
  43. specifies the starting offset of the section, where the default is ``0``. The
  44. *whence* argument specifies where the offset is relative to. It accepts one of
  45. the constants :const:`SEEK_SET`, :const:`SEEK_CUR` or :const:`SEEK_END`. The
  46. default is :const:`SEEK_SET`. For more information about the arguments refer to
  47. the :manpage:`fcntl(2)` manual page on your system.
  48. .. method:: posixfile.flags([flags])
  49. Set the specified flags for the file that the file object is referring to. The
  50. new flags are ORed with the old flags, unless specified otherwise. The format
  51. is explained below in a table. Without the *flags* argument a string indicating
  52. the current flags is returned (this is the same as the ``?`` modifier). For
  53. more information about the flags refer to the :manpage:`fcntl(2)` manual page on
  54. your system.
  55. .. method:: posixfile.dup()
  56. Duplicate the file object and the underlying file pointer and file descriptor.
  57. The resulting object behaves as if it were newly opened.
  58. .. method:: posixfile.dup2(fd)
  59. Duplicate the file object and the underlying file pointer and file descriptor.
  60. The new object will have the given file descriptor. Otherwise the resulting
  61. object behaves as if it were newly opened.
  62. .. method:: posixfile.file()
  63. Return the standard file object that the posixfile object is based on. This is
  64. sometimes necessary for functions that insist on a standard file object.
  65. All methods raise :exc:`IOError` when the request fails.
  66. Format characters for the :meth:`lock` method have the following meaning:
  67. +--------+-----------------------------------------------+
  68. | Format | Meaning |
  69. +========+===============================================+
  70. | ``u`` | unlock the specified region |
  71. +--------+-----------------------------------------------+
  72. | ``r`` | request a read lock for the specified section |
  73. +--------+-----------------------------------------------+
  74. | ``w`` | request a write lock for the specified |
  75. | | section |
  76. +--------+-----------------------------------------------+
  77. In addition the following modifiers can be added to the format:
  78. +----------+--------------------------------+-------+
  79. | Modifier | Meaning | Notes |
  80. +==========+================================+=======+
  81. | ``|`` | wait until the lock has been | |
  82. | | granted | |
  83. +----------+--------------------------------+-------+
  84. | ``?`` | return the first lock | \(1) |
  85. | | conflicting with the requested | |
  86. | | lock, or ``None`` if there is | |
  87. | | no conflict. | |
  88. +----------+--------------------------------+-------+
  89. Note:
  90. (1)
  91. The lock returned is in the format ``(mode, len, start, whence, pid)`` where
  92. *mode* is a character representing the type of lock ('r' or 'w'). This modifier
  93. prevents a request from being granted; it is for query purposes only.
  94. Format characters for the :meth:`flags` method have the following meanings:
  95. +--------+-----------------------------------------------+
  96. | Format | Meaning |
  97. +========+===============================================+
  98. | ``a`` | append only flag |
  99. +--------+-----------------------------------------------+
  100. | ``c`` | close on exec flag |
  101. +--------+-----------------------------------------------+
  102. | ``n`` | no delay flag (also called non-blocking flag) |
  103. +--------+-----------------------------------------------+
  104. | ``s`` | synchronization flag |
  105. +--------+-----------------------------------------------+
  106. In addition the following modifiers can be added to the format:
  107. +----------+---------------------------------+-------+
  108. | Modifier | Meaning | Notes |
  109. +==========+=================================+=======+
  110. | ``!`` | turn the specified flags 'off', | \(1) |
  111. | | instead of the default 'on' | |
  112. +----------+---------------------------------+-------+
  113. | ``=`` | replace the flags, instead of | \(1) |
  114. | | the default 'OR' operation | |
  115. +----------+---------------------------------+-------+
  116. | ``?`` | return a string in which the | \(2) |
  117. | | characters represent the flags | |
  118. | | that are set. | |
  119. +----------+---------------------------------+-------+
  120. Notes:
  121. (1)
  122. The ``!`` and ``=`` modifiers are mutually exclusive.
  123. (2)
  124. This string represents the flags after they may have been altered by the same
  125. call.
  126. Examples::
  127. import posixfile
  128. file = posixfile.open('/tmp/test', 'w')
  129. file.lock('w|')
  130. ...
  131. file.lock('u')
  132. file.close()