Sophie

Sophie

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

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>28.8. atexit — Exit handlers &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="28. Python Runtime Services" href="python.html" />
    <link rel="next" title="28.9. traceback — Print or retrieve a stack traceback" href="traceback.html" />
    <link rel="prev" title="28.7. abc — Abstract Base Classes" href="abc.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="traceback.html" title="28.9. traceback — Print or retrieve a stack traceback"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="abc.html" title="28.7. abc — Abstract Base Classes"
             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="python.html" accesskey="U">28. Python Runtime Services</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-atexit">
<h1>28.8. <tt class="xref docutils literal"><span class="pre">atexit</span></tt> &#8212; Exit handlers<a class="headerlink" href="#module-atexit" title="Permalink to this headline">¶</a></h1>
<p class="versionadded">
<span class="versionmodified">New in version 2.0.</span></p>
<p>The <tt class="xref docutils literal"><span class="pre">atexit</span></tt> module defines a single function to register cleanup
functions.  Functions thus registered are automatically executed upon normal
interpreter termination.</p>
<p>Note: the functions registered via this module are not called when the program
is killed by a signal, when a Python fatal internal error is detected, or when
<a title="os._exit" class="reference external" href="os.html#os._exit"><tt class="xref docutils literal"><span class="pre">os._exit()</span></tt></a> is called.</p>
<p id="index-217">This is an alternate interface to the functionality provided by the
<tt class="docutils literal"><span class="pre">sys.exitfunc</span></tt> variable.</p>
<p>Note: This module is unlikely to work correctly when used with other code that
sets <tt class="docutils literal"><span class="pre">sys.exitfunc</span></tt>.  In particular, other core Python modules are free to use
<tt class="xref docutils literal"><span class="pre">atexit</span></tt> without the programmer&#8217;s knowledge.  Authors who use
<tt class="docutils literal"><span class="pre">sys.exitfunc</span></tt> should convert their code to use <tt class="xref docutils literal"><span class="pre">atexit</span></tt> instead.  The
simplest way to convert code that sets <tt class="docutils literal"><span class="pre">sys.exitfunc</span></tt> is to import
<tt class="xref docutils literal"><span class="pre">atexit</span></tt> and register the function that had been bound to <tt class="docutils literal"><span class="pre">sys.exitfunc</span></tt>.</p>
<dl class="function">
<dt id="atexit.register">
<tt class="descclassname">atexit.</tt><tt class="descname">register</tt><big>(</big><em>func</em><span class="optional">[</span>, <em>*args</em><span class="optional">[</span>, <em>**kargs</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#atexit.register" title="Permalink to this definition">¶</a></dt>
<dd><p>Register <em>func</em> as a function to be executed at termination.  Any optional
arguments that are to be passed to <em>func</em> must be passed as arguments to
<a title="atexit.register" class="reference internal" href="#atexit.register"><tt class="xref docutils literal"><span class="pre">register()</span></tt></a>.</p>
<p>At normal program termination (for instance, if <a title="sys.exit" class="reference external" href="sys.html#sys.exit"><tt class="xref docutils literal"><span class="pre">sys.exit()</span></tt></a> is called or
the main module&#8217;s execution completes), all functions registered are called in
last in, first out order.  The assumption is that lower level modules will
normally be imported before higher level modules and thus must be cleaned up
later.</p>
<p>If an exception is raised during execution of the exit handlers, a traceback is
printed (unless <a title="exceptions.SystemExit" class="reference external" href="exceptions.html#exceptions.SystemExit"><tt class="xref docutils literal"><span class="pre">SystemExit</span></tt></a> is raised) and the exception information is
saved.  After all exit handlers have had a chance to run the last exception to
be raised is re-raised.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 2.6: </span>This function now returns <em>func</em> which makes it possible to use it as a
decorator without binding the original name to <tt class="xref docutils literal"><span class="pre">None</span></tt>.</p>
</dd></dl>

<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt>Module <a title="(Unix) GNU readline support for Python." class="reference external" href="readline.html#module-readline"><tt class="xref docutils literal"><span class="pre">readline</span></tt></a></dt>
<dd>Useful example of <tt class="xref docutils literal"><span class="pre">atexit</span></tt> to read and write <a title="(Unix) GNU readline support for Python." class="reference external" href="readline.html#module-readline"><tt class="xref docutils literal"><span class="pre">readline</span></tt></a> history files.</dd>
</dl>
</div>
<div class="section" id="atexit-example">
<span id="id1"></span><h2>28.8.1. <tt class="xref docutils literal"><span class="pre">atexit</span></tt> Example<a class="headerlink" href="#atexit-example" title="Permalink to this headline">¶</a></h2>
<p>The following simple example demonstrates how a module can initialize a counter
from a file when it is imported and save the counter&#8217;s updated value
automatically when the program terminates without relying on the application
making an explicit call into this module at termination.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">try</span><span class="p">:</span>
    <span class="n">_count</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s">&quot;/tmp/counter&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
    <span class="n">_count</span> <span class="o">=</span> <span class="mi">0</span>

<span class="k">def</span> <span class="nf">incrcounter</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
    <span class="k">global</span> <span class="n">_count</span>
    <span class="n">_count</span> <span class="o">=</span> <span class="n">_count</span> <span class="o">+</span> <span class="n">n</span>

<span class="k">def</span> <span class="nf">savecounter</span><span class="p">():</span>
    <span class="nb">open</span><span class="p">(</span><span class="s">&quot;/tmp/counter&quot;</span><span class="p">,</span> <span class="s">&quot;w&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%d</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">_count</span><span class="p">)</span>

<span class="kn">import</span> <span class="nn">atexit</span>
<span class="n">atexit</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">savecounter</span><span class="p">)</span>
</pre></div>
</div>
<p>Positional and keyword arguments may also be passed to <a title="atexit.register" class="reference internal" href="#atexit.register"><tt class="xref docutils literal"><span class="pre">register()</span></tt></a> to be
passed along to the registered function when it is called:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">goodbye</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">adjective</span><span class="p">):</span>
    <span class="k">print</span> <span class="s">&#39;Goodbye, </span><span class="si">%s</span><span class="s">, it was </span><span class="si">%s</span><span class="s"> to meet you.&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">adjective</span><span class="p">)</span>

<span class="kn">import</span> <span class="nn">atexit</span>
<span class="n">atexit</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">goodbye</span><span class="p">,</span> <span class="s">&#39;Donny&#39;</span><span class="p">,</span> <span class="s">&#39;nice&#39;</span><span class="p">)</span>

<span class="c"># or:</span>
<span class="n">atexit</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">goodbye</span><span class="p">,</span> <span class="n">adjective</span><span class="o">=</span><span class="s">&#39;nice&#39;</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">&#39;Donny&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Usage as a <a class="reference external" href="../glossary.html#term-decorator"><em class="xref">decorator</em></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">atexit</span>

<span class="nd">@atexit.register</span>
<span class="k">def</span> <span class="nf">goodbye</span><span class="p">():</span>
    <span class="k">print</span> <span class="s">&quot;You are now leaving the Python sector.&quot;</span>
</pre></div>
</div>
<p>This obviously only works with functions that don&#8217;t take arguments.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="#">28.8. <tt class="docutils literal"><span class="pre">atexit</span></tt> &#8212; Exit handlers</a><ul>
<li><a class="reference external" href="#atexit-example">28.8.1. <tt class="docutils literal"><span class="pre">atexit</span></tt> Example</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="abc.html"
                                  title="previous chapter">28.7. <tt class="docutils literal docutils literal"><span class="pre">abc</span></tt> &#8212; Abstract Base Classes</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="traceback.html"
                                  title="next chapter">28.9. <tt class="docutils literal docutils literal"><span class="pre">traceback</span></tt> &#8212; Print or retrieve a stack traceback</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/atexit.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="traceback.html" title="28.9. traceback — Print or retrieve a stack traceback"
             >next</a> |</li>
        <li class="right" >
          <a href="abc.html" title="28.7. abc — Abstract Base Classes"
             >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="python.html" >28. Python Runtime 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>