Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8f1462e52e1797a02c97073eed0b7f92 > files > 846

python-docs-2.6.5-2.5mdv2010.2.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>36.12. posixfile — File-like objects with locking support &mdash; Python v2.6.5 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.5',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.5 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.5 documentation" href="../index.html" />
    <link rel="up" title="36. Unix Specific Services" href="unix.html" />
    <link rel="next" title="36.13. resource — Resource usage information" href="resource.html" />
    <link rel="prev" title="36.11. pipes — Interface to shell pipelines" href="pipes.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="resource.html" title="36.13. resource — Resource usage information"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="pipes.html" title="36.11. pipes — Interface to shell pipelines"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.5 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="unix.html" accesskey="U">36. Unix Specific Services</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-posixfile">
<h1>36.12. <tt class="xref docutils literal"><span class="pre">posixfile</span></tt> &#8212; File-like objects with locking support<a class="headerlink" href="#module-posixfile" title="Permalink to this headline">¶</a></h1>
<p><em>Platforms: </em>Unix</p>
<p class="deprecated" id="index-502">
<span class="versionmodified">Deprecated since version 1.5: </span>The locking operation that this module provides is done better and more portably
by the <a title="fcntl.lockf" class="reference external" href="fcntl.html#fcntl.lockf"><tt class="xref docutils literal"><span class="pre">fcntl.lockf()</span></tt></a> call.</p>
<p id="index-503">This module implements some additional functionality over the built-in file
objects.  In particular, it implements file locking, control over the file
flags, and an easy interface to duplicate the file object. The module defines a
new file object, the posixfile object.  It has all the standard file object
methods and adds the methods described below.  This module only works for
certain flavors of Unix, since it uses <a title="fcntl.fcntl" class="reference external" href="fcntl.html#fcntl.fcntl"><tt class="xref docutils literal"><span class="pre">fcntl.fcntl()</span></tt></a> for file locking.</p>
<p>To instantiate a posixfile object, use the <a title="posixfile.open" class="reference internal" href="#posixfile.open"><tt class="xref docutils literal"><span class="pre">posixfile.open()</span></tt></a> function.  The
resulting object looks and feels roughly the same as a standard file object.</p>
<p>The <tt class="xref docutils literal"><span class="pre">posixfile</span></tt> module defines the following constants:</p>
<dl class="data">
<dt id="posixfile.SEEK_SET">
<tt class="descclassname">posixfile.</tt><tt class="descname">SEEK_SET</tt><a class="headerlink" href="#posixfile.SEEK_SET" title="Permalink to this definition">¶</a></dt>
<dd>Offset is calculated from the start of the file.</dd></dl>

<dl class="data">
<dt id="posixfile.SEEK_CUR">
<tt class="descclassname">posixfile.</tt><tt class="descname">SEEK_CUR</tt><a class="headerlink" href="#posixfile.SEEK_CUR" title="Permalink to this definition">¶</a></dt>
<dd>Offset is calculated from the current position in the file.</dd></dl>

<dl class="data">
<dt id="posixfile.SEEK_END">
<tt class="descclassname">posixfile.</tt><tt class="descname">SEEK_END</tt><a class="headerlink" href="#posixfile.SEEK_END" title="Permalink to this definition">¶</a></dt>
<dd>Offset is calculated from the end of the file.</dd></dl>

<p>The <tt class="xref docutils literal"><span class="pre">posixfile</span></tt> module defines the following functions:</p>
<dl class="function">
<dt id="posixfile.open">
<tt class="descclassname">posixfile.</tt><tt class="descname">open</tt><big>(</big><em>filename</em><span class="optional">[</span>, <em>mode</em><span class="optional">[</span>, <em>bufsize</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#posixfile.open" title="Permalink to this definition">¶</a></dt>
<dd>Create a new posixfile object with the given filename and mode.  The <em>filename</em>,
<em>mode</em> and <em>bufsize</em> arguments are interpreted the same way as by the built-in
<a title="open" class="reference external" href="functions.html#open"><tt class="xref docutils literal"><span class="pre">open()</span></tt></a> function.</dd></dl>

<dl class="function">
<dt id="posixfile.fileopen">
<tt class="descclassname">posixfile.</tt><tt class="descname">fileopen</tt><big>(</big><em>fileobject</em><big>)</big><a class="headerlink" href="#posixfile.fileopen" title="Permalink to this definition">¶</a></dt>
<dd>Create a new posixfile object with the given standard file object. The resulting
object has the same filename and mode as the original file object.</dd></dl>

<p>The posixfile object defines the following additional methods:</p>
<dl class="method">
<dt id="posixfile.posixfile.lock">
<tt class="descclassname">posixfile.</tt><tt class="descname">lock</tt><big>(</big><em>fmt</em><span class="optional">[</span>, <em>len</em><span class="optional">[</span>, <em>start</em><span class="optional">[</span>, <em>whence</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#posixfile.posixfile.lock" title="Permalink to this definition">¶</a></dt>
<dd>Lock the specified section of the file that the file object is referring to.
The format is explained below in a table.  The <em>len</em> argument specifies the
length of the section that should be locked. The default is <tt class="docutils literal"><span class="pre">0</span></tt>. <em>start</em>
specifies the starting offset of the section, where the default is <tt class="docutils literal"><span class="pre">0</span></tt>.  The
<em>whence</em> argument specifies where the offset is relative to. It accepts one of
the constants <a title="posixfile.SEEK_SET" class="reference internal" href="#posixfile.SEEK_SET"><tt class="xref docutils literal"><span class="pre">SEEK_SET</span></tt></a>, <a title="posixfile.SEEK_CUR" class="reference internal" href="#posixfile.SEEK_CUR"><tt class="xref docutils literal"><span class="pre">SEEK_CUR</span></tt></a> or <a title="posixfile.SEEK_END" class="reference internal" href="#posixfile.SEEK_END"><tt class="xref docutils literal"><span class="pre">SEEK_END</span></tt></a>.  The
default is <a title="posixfile.SEEK_SET" class="reference internal" href="#posixfile.SEEK_SET"><tt class="xref docutils literal"><span class="pre">SEEK_SET</span></tt></a>.  For more information about the arguments refer to
the <em>fcntl(2)</em> manual page on your system.</dd></dl>

<dl class="method">
<dt id="posixfile.posixfile.flags">
<tt class="descclassname">posixfile.</tt><tt class="descname">flags</tt><big>(</big><span class="optional">[</span><em>flags</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#posixfile.posixfile.flags" title="Permalink to this definition">¶</a></dt>
<dd>Set the specified flags for the file that the file object is referring to.  The
new flags are ORed with the old flags, unless specified otherwise.  The format
is explained below in a table.  Without the <em>flags</em> argument a string indicating
the current flags is returned (this is the same as the <tt class="docutils literal"><span class="pre">?</span></tt> modifier).  For
more information about the flags refer to the <em>fcntl(2)</em> manual page on
your system.</dd></dl>

<dl class="method">
<dt id="posixfile.posixfile.dup">
<tt class="descclassname">posixfile.</tt><tt class="descname">dup</tt><big>(</big><big>)</big><a class="headerlink" href="#posixfile.posixfile.dup" title="Permalink to this definition">¶</a></dt>
<dd>Duplicate the file object and the underlying file pointer and file descriptor.
The resulting object behaves as if it were newly opened.</dd></dl>

<dl class="method">
<dt id="posixfile.posixfile.dup2">
<tt class="descclassname">posixfile.</tt><tt class="descname">dup2</tt><big>(</big><em>fd</em><big>)</big><a class="headerlink" href="#posixfile.posixfile.dup2" title="Permalink to this definition">¶</a></dt>
<dd>Duplicate the file object and the underlying file pointer and file descriptor.
The new object will have the given file descriptor. Otherwise the resulting
object behaves as if it were newly opened.</dd></dl>

<dl class="method">
<dt id="posixfile.posixfile.file">
<tt class="descclassname">posixfile.</tt><tt class="descname">file</tt><big>(</big><big>)</big><a class="headerlink" href="#posixfile.posixfile.file" title="Permalink to this definition">¶</a></dt>
<dd>Return the standard file object that the posixfile object is based on.  This is
sometimes necessary for functions that insist on a standard file object.</dd></dl>

<p>All methods raise <a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a> when the request fails.</p>
<p>Format characters for the <tt class="xref docutils literal"><span class="pre">lock()</span></tt> method have the following meaning:</p>
<table border="1" class="docutils">
<colgroup>
<col width="15%" />
<col width="85%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Format</th>
<th class="head">Meaning</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">u</span></tt></td>
<td>unlock the specified region</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">r</span></tt></td>
<td>request a read lock for the specified section</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">w</span></tt></td>
<td>request a write lock for the specified
section</td>
</tr>
</tbody>
</table>
<p>In addition the following modifiers can be added to the format:</p>
<table border="1" class="docutils">
<colgroup>
<col width="20%" />
<col width="65%" />
<col width="14%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Modifier</th>
<th class="head">Meaning</th>
<th class="head">Notes</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">|</span></tt></td>
<td>wait until the lock has been
granted</td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">?</span></tt></td>
<td>return the first lock
conflicting with the requested
lock, or <tt class="xref docutils literal"><span class="pre">None</span></tt> if there is
no conflict.</td>
<td>(1)</td>
</tr>
</tbody>
</table>
<p>Note:</p>
<ol class="arabic simple">
<li>The lock returned is in the format <tt class="docutils literal"><span class="pre">(mode,</span> <span class="pre">len,</span> <span class="pre">start,</span> <span class="pre">whence,</span> <span class="pre">pid)</span></tt> where
<em>mode</em> is a character representing the type of lock (&#8216;r&#8217; or &#8216;w&#8217;).  This modifier
prevents a request from being granted; it is for query purposes only.</li>
</ol>
<p>Format characters for the <tt class="xref docutils literal"><span class="pre">flags()</span></tt> method have the following meanings:</p>
<table border="1" class="docutils">
<colgroup>
<col width="15%" />
<col width="85%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Format</th>
<th class="head">Meaning</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">a</span></tt></td>
<td>append only flag</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">c</span></tt></td>
<td>close on exec flag</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">n</span></tt></td>
<td>no delay flag (also called non-blocking flag)</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">s</span></tt></td>
<td>synchronization flag</td>
</tr>
</tbody>
</table>
<p>In addition the following modifiers can be added to the format:</p>
<table border="1" class="docutils">
<colgroup>
<col width="20%" />
<col width="66%" />
<col width="14%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Modifier</th>
<th class="head">Meaning</th>
<th class="head">Notes</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">!</span></tt></td>
<td>turn the specified flags &#8216;off&#8217;,
instead of the default &#8216;on&#8217;</td>
<td>(1)</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">=</span></tt></td>
<td>replace the flags, instead of
the default &#8216;OR&#8217; operation</td>
<td>(1)</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">?</span></tt></td>
<td>return a string in which the
characters represent the flags
that are set.</td>
<td>(2)</td>
</tr>
</tbody>
</table>
<p>Notes:</p>
<ol class="arabic simple">
<li>The <tt class="docutils literal"><span class="pre">!</span></tt> and <tt class="docutils literal"><span class="pre">=</span></tt> modifiers are mutually exclusive.</li>
<li>This string represents the flags after they may have been altered by the same
call.</li>
</ol>
<p>Examples:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">posixfile</span>

<span class="nb">file</span> <span class="o">=</span> <span class="n">posixfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">&#39;/tmp/test&#39;</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">)</span>
<span class="nb">file</span><span class="o">.</span><span class="n">lock</span><span class="p">(</span><span class="s">&#39;w|&#39;</span><span class="p">)</span>
<span class="o">...</span>
<span class="nb">file</span><span class="o">.</span><span class="n">lock</span><span class="p">(</span><span class="s">&#39;u&#39;</span><span class="p">)</span>
<span class="nb">file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h4>Previous topic</h4>
            <p class="topless"><a href="pipes.html"
                                  title="previous chapter">36.11. <tt class="docutils literal docutils literal docutils literal"><span class="pre">pipes</span></tt> &#8212; Interface to shell pipelines</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="resource.html"
                                  title="next chapter">36.13. <tt class="docutils literal docutils literal"><span class="pre">resource</span></tt> &#8212; Resource usage information</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/posixfile.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="resource.html" title="36.13. resource — Resource usage information"
             >next</a> |</li>
        <li class="right" >
          <a href="pipes.html" title="36.11. pipes — Interface to shell pipelines"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.5 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="unix.html" >36. Unix Specific Services</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2010, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Mar 19, 2010.
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.5.
    </div>

  </body>
</html>