Sophie

Sophie

distrib > Mageia > 3 > i586 > media > core-updates > by-pkgid > 50402eac2a16508b365658612a898528 > files > 482

python3-docs-3.3.0-4.3.mga3.noarch.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>Exception Handling &mdash; Python v3.3.0 documentation</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.3.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v3.3.0 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 v3.3.0 documentation" href="../index.html" />
    <link rel="up" title="Python/C API Reference Manual" href="index.html" />
    <link rel="next" title="Utilities" href="utilities.html" />
    <link rel="prev" title="Reference Counting" href="refcounting.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
 

  </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="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="utilities.html" title="Utilities"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="refcounting.html" title="Reference Counting"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li><a href="../index.html">3.3.0 Documentation</a> &raquo;</li>

          <li><a href="index.html" accesskey="U">Python/C API Reference Manual</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="exception-handling">
<span id="exceptionhandling"></span><h1>Exception Handling<a class="headerlink" href="#exception-handling" title="Permalink to this headline">¶</a></h1>
<p>The functions described in this chapter will let you handle and raise Python
exceptions.  It is important to understand some of the basics of Python
exception handling.  It works somewhat like the Unix <tt class="xref c c-data docutils literal"><span class="pre">errno</span></tt> variable:
there is a global indicator (per thread) of the last error that occurred.  Most
functions don&#8217;t clear this on success, but will set it to indicate the cause of
the error on failure.  Most functions also return an error indicator, usually
<em>NULL</em> if they are supposed to return a pointer, or <tt class="docutils literal"><span class="pre">-1</span></tt> if they return an
integer (exception: the <tt class="xref c c-func docutils literal"><span class="pre">PyArg_*()</span></tt> functions return <tt class="docutils literal"><span class="pre">1</span></tt> for success and
<tt class="docutils literal"><span class="pre">0</span></tt> for failure).</p>
<p>When a function must fail because some function it called failed, it generally
doesn&#8217;t set the error indicator; the function it called already set it.  It is
responsible for either handling the error and clearing the exception or
returning after cleaning up any resources it holds (such as object references or
memory allocations); it should <em>not</em> continue normally if it is not prepared to
handle the error.  If returning due to an error, it is important to indicate to
the caller that an error has been set.  If the error is not handled or carefully
propagated, additional calls into the Python/C API may not behave as intended
and may fail in mysterious ways.</p>
<p>The error indicator consists of three Python objects corresponding to the result
of <tt class="docutils literal"><span class="pre">sys.exc_info()</span></tt>.  API functions exist to interact with the error indicator
in various ways.  There is a separate error indicator for each thread.</p>
<dl class="function">
<dt id="PyErr_PrintEx">
void <tt class="descname">PyErr_PrintEx</tt><big>(</big>int<em>&nbsp;set_sys_last_vars</em><big>)</big><a class="headerlink" href="#PyErr_PrintEx" title="Permalink to this definition">¶</a></dt>
<dd><p>Print a standard traceback to <tt class="docutils literal"><span class="pre">sys.stderr</span></tt> and clear the error indicator.
Call this function only when the error indicator is set.  (Otherwise it will
cause a fatal error!)</p>
<p>If <em>set_sys_last_vars</em> is nonzero, the variables <a class="reference internal" href="../library/sys.html#sys.last_type" title="sys.last_type"><tt class="xref py py-data docutils literal"><span class="pre">sys.last_type</span></tt></a>,
<a class="reference internal" href="../library/sys.html#sys.last_value" title="sys.last_value"><tt class="xref py py-data docutils literal"><span class="pre">sys.last_value</span></tt></a> and <a class="reference internal" href="../library/sys.html#sys.last_traceback" title="sys.last_traceback"><tt class="xref py py-data docutils literal"><span class="pre">sys.last_traceback</span></tt></a> will be set to the
type, value and traceback of the printed exception, respectively.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_Print">
void <tt class="descname">PyErr_Print</tt><big>(</big><big>)</big><a class="headerlink" href="#PyErr_Print" title="Permalink to this definition">¶</a></dt>
<dd><p>Alias for <tt class="docutils literal"><span class="pre">PyErr_PrintEx(1)</span></tt>.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_Occurred">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyErr_Occurred</tt><big>(</big><big>)</big><a class="headerlink" href="#PyErr_Occurred" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: Borrowed reference.</em><p>Test whether the error indicator is set.  If set, return the exception <em>type</em>
(the first argument to the last call to one of the <tt class="xref c c-func docutils literal"><span class="pre">PyErr_Set*()</span></tt>
functions or to <a class="reference internal" href="#PyErr_Restore" title="PyErr_Restore"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_Restore()</span></tt></a>).  If not set, return <em>NULL</em>.  You do not
own a reference to the return value, so you do not need to <a class="reference internal" href="refcounting.html#Py_DECREF" title="Py_DECREF"><tt class="xref c c-func docutils literal"><span class="pre">Py_DECREF()</span></tt></a>
it.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Do not compare the return value to a specific exception; use
<a class="reference internal" href="#PyErr_ExceptionMatches" title="PyErr_ExceptionMatches"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_ExceptionMatches()</span></tt></a> instead, shown below.  (The comparison could
easily fail since the exception may be an instance instead of a class, in the
case of a class exception, or it may the a subclass of the expected exception.)</p>
</div>
</dd></dl>

<dl class="function">
<dt id="PyErr_ExceptionMatches">
int <tt class="descname">PyErr_ExceptionMatches</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em><big>)</big><a class="headerlink" href="#PyErr_ExceptionMatches" title="Permalink to this definition">¶</a></dt>
<dd><p>Equivalent to <tt class="docutils literal"><span class="pre">PyErr_GivenExceptionMatches(PyErr_Occurred(),</span> <span class="pre">exc)</span></tt>.  This
should only be called when an exception is actually set; a memory access
violation will occur if no exception has been raised.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_GivenExceptionMatches">
int <tt class="descname">PyErr_GivenExceptionMatches</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*given</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em><big>)</big><a class="headerlink" href="#PyErr_GivenExceptionMatches" title="Permalink to this definition">¶</a></dt>
<dd><p>Return true if the <em>given</em> exception matches the exception in <em>exc</em>.  If
<em>exc</em> is a class object, this also returns true when <em>given</em> is an instance
of a subclass.  If <em>exc</em> is a tuple, all exceptions in the tuple (and
recursively in subtuples) are searched for a match.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_NormalizeException">
void <tt class="descname">PyErr_NormalizeException</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>**exc, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>**val, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>**tb<big>)</big><a class="headerlink" href="#PyErr_NormalizeException" title="Permalink to this definition">¶</a></dt>
<dd><p>Under certain circumstances, the values returned by <a class="reference internal" href="#PyErr_Fetch" title="PyErr_Fetch"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_Fetch()</span></tt></a> below
can be &#8220;unnormalized&#8221;, meaning that <tt class="docutils literal"><span class="pre">*exc</span></tt> is a class object but <tt class="docutils literal"><span class="pre">*val</span></tt> is
not an instance of the  same class.  This function can be used to instantiate
the class in that case.  If the values are already normalized, nothing happens.
The delayed normalization is implemented to improve performance.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_Clear">
void <tt class="descname">PyErr_Clear</tt><big>(</big><big>)</big><a class="headerlink" href="#PyErr_Clear" title="Permalink to this definition">¶</a></dt>
<dd><p>Clear the error indicator.  If the error indicator is not set, there is no
effect.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_Fetch">
void <tt class="descname">PyErr_Fetch</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;**ptype</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;**pvalue</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;**ptraceback</em><big>)</big><a class="headerlink" href="#PyErr_Fetch" title="Permalink to this definition">¶</a></dt>
<dd><p>Retrieve the error indicator into three variables whose addresses are passed.
If the error indicator is not set, set all three variables to <em>NULL</em>.  If it is
set, it will be cleared and you own a reference to each object retrieved.  The
value and traceback object may be <em>NULL</em> even when the type object is not.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This function is normally only used by code that needs to handle exceptions or
by code that needs to save and restore the error indicator temporarily.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="PyErr_Restore">
void <tt class="descname">PyErr_Restore</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*type</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*value</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*traceback</em><big>)</big><a class="headerlink" href="#PyErr_Restore" title="Permalink to this definition">¶</a></dt>
<dd><p>Set  the error indicator from the three objects.  If the error indicator is
already set, it is cleared first.  If the objects are <em>NULL</em>, the error
indicator is cleared.  Do not pass a <em>NULL</em> type and non-<em>NULL</em> value or
traceback.  The exception type should be a class.  Do not pass an invalid
exception type or value. (Violating these rules will cause subtle problems
later.)  This call takes away a reference to each object: you must own a
reference to each object before the call and after the call you no longer own
these references.  (If you don&#8217;t understand this, don&#8217;t use this function.  I
warned you.)</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This function is normally only used by code that needs to save and restore the
error indicator temporarily; use <a class="reference internal" href="#PyErr_Fetch" title="PyErr_Fetch"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_Fetch()</span></tt></a> to save the current
exception state.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="PyErr_GetExcInfo">
void <tt class="descname">PyErr_GetExcInfo</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;**ptype</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;**pvalue</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;**ptraceback</em><big>)</big><a class="headerlink" href="#PyErr_GetExcInfo" title="Permalink to this definition">¶</a></dt>
<dd><p>Retrieve the exception info, as known from <tt class="docutils literal"><span class="pre">sys.exc_info()</span></tt>.  This refers
to an exception that was already caught, not to an exception that was
freshly raised.  Returns new references for the three objects, any of which
may be <em>NULL</em>.  Does not modify the exception info state.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This function is not normally used by code that wants to handle exceptions.
Rather, it can be used when code needs to save and restore the exception
state temporarily.  Use <a class="reference internal" href="#PyErr_SetExcInfo" title="PyErr_SetExcInfo"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_SetExcInfo()</span></tt></a> to restore or clear the
exception state.</p>
</div>
<p class="versionadded">
<span class="versionmodified">New in version 3.3.</span> </p>
</dd></dl>

<dl class="function">
<dt id="PyErr_SetExcInfo">
void <tt class="descname">PyErr_SetExcInfo</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*type</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*value</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*traceback</em><big>)</big><a class="headerlink" href="#PyErr_SetExcInfo" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the exception info, as known from <tt class="docutils literal"><span class="pre">sys.exc_info()</span></tt>.  This refers
to an exception that was already caught, not to an exception that was
freshly raised.  This function steals the references of the arguments.
To clear the exception state, pass <em>NULL</em> for all three arguments.
For general rules about the three arguments, see <a class="reference internal" href="#PyErr_Restore" title="PyErr_Restore"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_Restore()</span></tt></a>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This function is not normally used by code that wants to handle exceptions.
Rather, it can be used when code needs to save and restore the exception
state temporarily.  Use <a class="reference internal" href="#PyErr_GetExcInfo" title="PyErr_GetExcInfo"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_GetExcInfo()</span></tt></a> to read the exception
state.</p>
</div>
<p class="versionadded">
<span class="versionmodified">New in version 3.3.</span> </p>
</dd></dl>

<dl class="function">
<dt id="PyErr_SetString">
void <tt class="descname">PyErr_SetString</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*type</em>, const char<em>&nbsp;*message</em><big>)</big><a class="headerlink" href="#PyErr_SetString" title="Permalink to this definition">¶</a></dt>
<dd><p>This is the most common way to set the error indicator.  The first argument
specifies the exception type; it is normally one of the standard exceptions,
e.g. <tt class="xref c c-data docutils literal"><span class="pre">PyExc_RuntimeError</span></tt>.  You need not increment its reference count.
The second argument is an error message; it is decoded from <tt class="docutils literal"><span class="pre">'utf-8</span></tt>&#8216;.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_SetObject">
void <tt class="descname">PyErr_SetObject</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*type</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*value</em><big>)</big><a class="headerlink" href="#PyErr_SetObject" title="Permalink to this definition">¶</a></dt>
<dd><p>This function is similar to <a class="reference internal" href="#PyErr_SetString" title="PyErr_SetString"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_SetString()</span></tt></a> but lets you specify an
arbitrary Python object for the &#8220;value&#8221; of the exception.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_Format">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyErr_Format</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exception</em>, const char<em>&nbsp;*format</em>, ...<big>)</big><a class="headerlink" href="#PyErr_Format" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: Always NULL.</em><p>This function sets the error indicator and returns <em>NULL</em>.  <em>exception</em>
should be a Python exception class.  The <em>format</em> and subsequent
parameters help format the error message; they have the same meaning and
values as in <a class="reference internal" href="unicode.html#PyUnicode_FromFormat" title="PyUnicode_FromFormat"><tt class="xref c c-func docutils literal"><span class="pre">PyUnicode_FromFormat()</span></tt></a>. <em>format</em> is an ASCII-encoded
string.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_SetNone">
void <tt class="descname">PyErr_SetNone</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*type</em><big>)</big><a class="headerlink" href="#PyErr_SetNone" title="Permalink to this definition">¶</a></dt>
<dd><p>This is a shorthand for <tt class="docutils literal"><span class="pre">PyErr_SetObject(type,</span> <span class="pre">Py_None)</span></tt>.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_BadArgument">
int <tt class="descname">PyErr_BadArgument</tt><big>(</big><big>)</big><a class="headerlink" href="#PyErr_BadArgument" title="Permalink to this definition">¶</a></dt>
<dd><p>This is a shorthand for <tt class="docutils literal"><span class="pre">PyErr_SetString(PyExc_TypeError,</span> <span class="pre">message)</span></tt>, where
<em>message</em> indicates that a built-in operation was invoked with an illegal
argument.  It is mostly for internal use.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_NoMemory">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyErr_NoMemory</tt><big>(</big><big>)</big><a class="headerlink" href="#PyErr_NoMemory" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: Always NULL.</em><p>This is a shorthand for <tt class="docutils literal"><span class="pre">PyErr_SetNone(PyExc_MemoryError)</span></tt>; it returns <em>NULL</em>
so an object allocation function can write <tt class="docutils literal"><span class="pre">return</span> <span class="pre">PyErr_NoMemory();</span></tt> when it
runs out of memory.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_SetFromErrno">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyErr_SetFromErrno</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*type</em><big>)</big><a class="headerlink" href="#PyErr_SetFromErrno" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: Always NULL.</em><p id="index-0">This is a convenience function to raise an exception when a C library function
has returned an error and set the C variable <tt class="xref c c-data docutils literal"><span class="pre">errno</span></tt>.  It constructs a
tuple object whose first item is the integer <tt class="xref c c-data docutils literal"><span class="pre">errno</span></tt> value and whose
second item is the corresponding error message (gotten from <tt class="xref c c-func docutils literal"><span class="pre">strerror()</span></tt>),
and then calls <tt class="docutils literal"><span class="pre">PyErr_SetObject(type,</span> <span class="pre">object)</span></tt>.  On Unix, when the
<tt class="xref c c-data docutils literal"><span class="pre">errno</span></tt> value is <tt class="xref py py-const docutils literal"><span class="pre">EINTR</span></tt>, indicating an interrupted system call,
this calls <a class="reference internal" href="#PyErr_CheckSignals" title="PyErr_CheckSignals"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_CheckSignals()</span></tt></a>, and if that set the error indicator,
leaves it set to that.  The function always returns <em>NULL</em>, so a wrapper
function around a system call can write <tt class="docutils literal"><span class="pre">return</span> <span class="pre">PyErr_SetFromErrno(type);</span></tt>
when the system call returns an error.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_SetFromErrnoWithFilename">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyErr_SetFromErrnoWithFilename</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*type</em>, const char<em>&nbsp;*filename</em><big>)</big><a class="headerlink" href="#PyErr_SetFromErrnoWithFilename" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: Always NULL.</em><p>Similar to <a class="reference internal" href="#PyErr_SetFromErrno" title="PyErr_SetFromErrno"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_SetFromErrno()</span></tt></a>, with the additional behavior that if
<em>filename</em> is not <em>NULL</em>, it is passed to the constructor of <em>type</em> as a third
parameter.  In the case of exceptions such as <a class="reference internal" href="../library/exceptions.html#IOError" title="IOError"><tt class="xref py py-exc docutils literal"><span class="pre">IOError</span></tt></a> and <a class="reference internal" href="../library/exceptions.html#OSError" title="OSError"><tt class="xref py py-exc docutils literal"><span class="pre">OSError</span></tt></a>,
this is used to define the <tt class="xref py py-attr docutils literal"><span class="pre">filename</span></tt> attribute of the exception instance.
<em>filename</em> is decoded from the filesystem encoding
(<a class="reference internal" href="../library/sys.html#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><tt class="xref py py-func docutils literal"><span class="pre">sys.getfilesystemencoding()</span></tt></a>).</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_SetFromWindowsErr">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyErr_SetFromWindowsErr</tt><big>(</big>int<em>&nbsp;ierr</em><big>)</big><a class="headerlink" href="#PyErr_SetFromWindowsErr" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: Always NULL.</em><p>This is a convenience function to raise <a class="reference internal" href="../library/exceptions.html#WindowsError" title="WindowsError"><tt class="xref py py-exc docutils literal"><span class="pre">WindowsError</span></tt></a>. If called with
<em>ierr</em> of <tt class="xref c c-data docutils literal"><span class="pre">0</span></tt>, the error code returned by a call to <tt class="xref c c-func docutils literal"><span class="pre">GetLastError()</span></tt>
is used instead.  It calls the Win32 function <tt class="xref c c-func docutils literal"><span class="pre">FormatMessage()</span></tt> to retrieve
the Windows description of error code given by <em>ierr</em> or <tt class="xref c c-func docutils literal"><span class="pre">GetLastError()</span></tt>,
then it constructs a tuple object whose first item is the <em>ierr</em> value and whose
second item is the corresponding error message (gotten from
<tt class="xref c c-func docutils literal"><span class="pre">FormatMessage()</span></tt>), and then calls <tt class="docutils literal"><span class="pre">PyErr_SetObject(PyExc_WindowsError,</span>
<span class="pre">object)</span></tt>. This function always returns <em>NULL</em>. Availability: Windows.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_SetExcFromWindowsErr">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyErr_SetExcFromWindowsErr</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*type</em>, int<em>&nbsp;ierr</em><big>)</big><a class="headerlink" href="#PyErr_SetExcFromWindowsErr" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: Always NULL.</em><p>Similar to <a class="reference internal" href="#PyErr_SetFromWindowsErr" title="PyErr_SetFromWindowsErr"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_SetFromWindowsErr()</span></tt></a>, with an additional parameter
specifying the exception type to be raised. Availability: Windows.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_SetFromWindowsErrWithFilename">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyErr_SetFromWindowsErrWithFilename</tt><big>(</big>int<em>&nbsp;ierr</em>, const char<em>&nbsp;*filename</em><big>)</big><a class="headerlink" href="#PyErr_SetFromWindowsErrWithFilename" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: Always NULL.</em><p>Similar to <a class="reference internal" href="#PyErr_SetFromWindowsErr" title="PyErr_SetFromWindowsErr"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_SetFromWindowsErr()</span></tt></a>, with the additional behavior that
if <em>filename</em> is not <em>NULL</em>, it is passed to the constructor of
<a class="reference internal" href="../library/exceptions.html#WindowsError" title="WindowsError"><tt class="xref py py-exc docutils literal"><span class="pre">WindowsError</span></tt></a> as a third parameter.  <em>filename</em> is decoded from the
filesystem encoding (<a class="reference internal" href="../library/sys.html#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><tt class="xref py py-func docutils literal"><span class="pre">sys.getfilesystemencoding()</span></tt></a>).  Availability:
Windows.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_SetExcFromWindowsErrWithFilename">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyErr_SetExcFromWindowsErrWithFilename</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*type</em>, int<em>&nbsp;ierr</em>, char<em>&nbsp;*filename</em><big>)</big><a class="headerlink" href="#PyErr_SetExcFromWindowsErrWithFilename" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: Always NULL.</em><p>Similar to <a class="reference internal" href="#PyErr_SetFromWindowsErrWithFilename" title="PyErr_SetFromWindowsErrWithFilename"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_SetFromWindowsErrWithFilename()</span></tt></a>, with an additional
parameter specifying the exception type to be raised. Availability: Windows.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_SetImportError">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyErr_SetImportError</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*msg</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*name</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*path</em><big>)</big><a class="headerlink" href="#PyErr_SetImportError" title="Permalink to this definition">¶</a></dt>
<dd><p>This is a convenience function to raise <a class="reference internal" href="../library/exceptions.html#ImportError" title="ImportError"><tt class="xref py py-exc docutils literal"><span class="pre">ImportError</span></tt></a>. <em>msg</em> will be
set as the exception&#8217;s message string. <em>name</em> and <em>path</em>, both of which can
be <tt class="docutils literal"><span class="pre">NULL</span></tt>, will be set as the <a class="reference internal" href="../library/exceptions.html#ImportError" title="ImportError"><tt class="xref py py-exc docutils literal"><span class="pre">ImportError</span></tt></a>&#8216;s respective <tt class="docutils literal"><span class="pre">name</span></tt>
and <tt class="docutils literal"><span class="pre">path</span></tt> attributes.</p>
<p class="versionadded">
<span class="versionmodified">New in version 3.3.</span> </p>
</dd></dl>

<dl class="function">
<dt id="PyErr_SyntaxLocationEx">
void <tt class="descname">PyErr_SyntaxLocationEx</tt><big>(</big>char<em>&nbsp;*filename</em>, int<em>&nbsp;lineno</em>, int<em>&nbsp;col_offset</em><big>)</big><a class="headerlink" href="#PyErr_SyntaxLocationEx" title="Permalink to this definition">¶</a></dt>
<dd><p>Set file, line, and offset information for the current exception.  If the
current exception is not a <a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><tt class="xref py py-exc docutils literal"><span class="pre">SyntaxError</span></tt></a>, then it sets additional
attributes, which make the exception printing subsystem think the exception
is a <a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><tt class="xref py py-exc docutils literal"><span class="pre">SyntaxError</span></tt></a>. <em>filename</em> is decoded from the filesystem encoding
(<a class="reference internal" href="../library/sys.html#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><tt class="xref py py-func docutils literal"><span class="pre">sys.getfilesystemencoding()</span></tt></a>).</p>
<p class="versionadded">
<span class="versionmodified">New in version 3.2.</span> </p>
</dd></dl>

<dl class="function">
<dt id="PyErr_SyntaxLocation">
void <tt class="descname">PyErr_SyntaxLocation</tt><big>(</big>char<em>&nbsp;*filename</em>, int<em>&nbsp;lineno</em><big>)</big><a class="headerlink" href="#PyErr_SyntaxLocation" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <tt class="xref c c-func docutils literal"><span class="pre">PyErr_SyntaxLocationExc()</span></tt>, but the col_offset parameter is
omitted.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_BadInternalCall">
void <tt class="descname">PyErr_BadInternalCall</tt><big>(</big><big>)</big><a class="headerlink" href="#PyErr_BadInternalCall" title="Permalink to this definition">¶</a></dt>
<dd><p>This is a shorthand for <tt class="docutils literal"><span class="pre">PyErr_SetString(PyExc_SystemError,</span> <span class="pre">message)</span></tt>,
where <em>message</em> indicates that an internal operation (e.g. a Python/C API
function) was invoked with an illegal argument.  It is mostly for internal
use.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_WarnEx">
int <tt class="descname">PyErr_WarnEx</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*category</em>, char<em>&nbsp;*message</em>, int<em>&nbsp;stack_level</em><big>)</big><a class="headerlink" href="#PyErr_WarnEx" title="Permalink to this definition">¶</a></dt>
<dd><p>Issue a warning message.  The <em>category</em> argument is a warning category (see
below) or <em>NULL</em>; the <em>message</em> argument is an UTF-8 encoded string.  <em>stack_level</em> is a
positive number giving a number of stack frames; the warning will be issued from
the  currently executing line of code in that stack frame.  A <em>stack_level</em> of 1
is the function calling <a class="reference internal" href="#PyErr_WarnEx" title="PyErr_WarnEx"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_WarnEx()</span></tt></a>, 2 is  the function above that,
and so forth.</p>
<p>This function normally prints a warning message to <em>sys.stderr</em>; however, it is
also possible that the user has specified that warnings are to be turned into
errors, and in that case this will raise an exception.  It is also possible that
the function raises an exception because of a problem with the warning machinery
(the implementation imports the <a class="reference internal" href="../library/warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><tt class="xref py py-mod docutils literal"><span class="pre">warnings</span></tt></a> module to do the heavy lifting).
The return value is <tt class="docutils literal"><span class="pre">0</span></tt> if no exception is raised, or <tt class="docutils literal"><span class="pre">-1</span></tt> if an exception
is raised.  (It is not possible to determine whether a warning message is
actually printed, nor what the reason is for the exception; this is
intentional.)  If an exception is raised, the caller should do its normal
exception handling (for example, <a class="reference internal" href="refcounting.html#Py_DECREF" title="Py_DECREF"><tt class="xref c c-func docutils literal"><span class="pre">Py_DECREF()</span></tt></a> owned references and return
an error value).</p>
<p>Warning categories must be subclasses of <tt class="xref c c-data docutils literal"><span class="pre">Warning</span></tt>; the default warning
category is <tt class="xref c c-data docutils literal"><span class="pre">RuntimeWarning</span></tt>.  The standard Python warning categories are
available as global variables whose names are <tt class="docutils literal"><span class="pre">PyExc_</span></tt> followed by the Python
exception name. These have the type <a class="reference internal" href="structures.html#PyObject" title="PyObject"><tt class="xref c c-type docutils literal"><span class="pre">PyObject*</span></tt></a>; they are all class
objects. Their names are <tt class="xref c c-data docutils literal"><span class="pre">PyExc_Warning</span></tt>, <tt class="xref c c-data docutils literal"><span class="pre">PyExc_UserWarning</span></tt>,
<tt class="xref c c-data docutils literal"><span class="pre">PyExc_UnicodeWarning</span></tt>, <tt class="xref c c-data docutils literal"><span class="pre">PyExc_DeprecationWarning</span></tt>,
<tt class="xref c c-data docutils literal"><span class="pre">PyExc_SyntaxWarning</span></tt>, <tt class="xref c c-data docutils literal"><span class="pre">PyExc_RuntimeWarning</span></tt>, and
<tt class="xref c c-data docutils literal"><span class="pre">PyExc_FutureWarning</span></tt>.  <tt class="xref c c-data docutils literal"><span class="pre">PyExc_Warning</span></tt> is a subclass of
<tt class="xref c c-data docutils literal"><span class="pre">PyExc_Exception</span></tt>; the other warning categories are subclasses of
<tt class="xref c c-data docutils literal"><span class="pre">PyExc_Warning</span></tt>.</p>
<p>For information about warning control, see the documentation for the
<a class="reference internal" href="../library/warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><tt class="xref py py-mod docutils literal"><span class="pre">warnings</span></tt></a> module and the <a class="reference internal" href="../using/cmdline.html#cmdoption-W"><em class="xref std std-option">-W</em></a> option in the command line
documentation.  There is no C API for warning control.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_WarnExplicit">
int <tt class="descname">PyErr_WarnExplicit</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*category</em>, const char<em>&nbsp;*message</em>, const char<em>&nbsp;*filename</em>, int<em>&nbsp;lineno</em>, const char<em>&nbsp;*module</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*registry</em><big>)</big><a class="headerlink" href="#PyErr_WarnExplicit" title="Permalink to this definition">¶</a></dt>
<dd><p>Issue a warning message with explicit control over all warning attributes.  This
is a straightforward wrapper around the Python function
<a class="reference internal" href="../library/warnings.html#warnings.warn_explicit" title="warnings.warn_explicit"><tt class="xref py py-func docutils literal"><span class="pre">warnings.warn_explicit()</span></tt></a>, see there for more information.  The <em>module</em>
and <em>registry</em> arguments may be set to <em>NULL</em> to get the default effect
described there. <em>message</em> and <em>module</em> are UTF-8 encoded strings,
<em>filename</em> is decoded from the filesystem encoding
(<a class="reference internal" href="../library/sys.html#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><tt class="xref py py-func docutils literal"><span class="pre">sys.getfilesystemencoding()</span></tt></a>).</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_WarnFormat">
int <tt class="descname">PyErr_WarnFormat</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*category</em>, Py_ssize_t<em>&nbsp;stack_level</em>, const char<em>&nbsp;*format</em>, ...<big>)</big><a class="headerlink" href="#PyErr_WarnFormat" title="Permalink to this definition">¶</a></dt>
<dd><p>Function similar to <a class="reference internal" href="#PyErr_WarnEx" title="PyErr_WarnEx"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_WarnEx()</span></tt></a>, but use
<a class="reference internal" href="unicode.html#PyUnicode_FromFormat" title="PyUnicode_FromFormat"><tt class="xref c c-func docutils literal"><span class="pre">PyUnicode_FromFormat()</span></tt></a> to format the warning message.  <em>format</em> is
an ASCII-encoded string.</p>
<p class="versionadded">
<span class="versionmodified">New in version 3.2.</span> </p>
</dd></dl>

<dl class="function">
<dt id="PyErr_CheckSignals">
int <tt class="descname">PyErr_CheckSignals</tt><big>(</big><big>)</big><a class="headerlink" href="#PyErr_CheckSignals" title="Permalink to this definition">¶</a></dt>
<dd><p id="index-1">This function interacts with Python&#8217;s signal handling.  It checks whether a
signal has been sent to the processes and if so, invokes the corresponding
signal handler.  If the <a class="reference internal" href="../library/signal.html#module-signal" title="signal: Set handlers for asynchronous events."><tt class="xref py py-mod docutils literal"><span class="pre">signal</span></tt></a> module is supported, this can invoke a
signal handler written in Python.  In all cases, the default effect for
<tt class="xref py py-const docutils literal"><span class="pre">SIGINT</span></tt> is to raise the  <a class="reference internal" href="../library/exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><tt class="xref py py-exc docutils literal"><span class="pre">KeyboardInterrupt</span></tt></a> exception.  If an
exception is raised the error indicator is set and the function returns <tt class="docutils literal"><span class="pre">-1</span></tt>;
otherwise the function returns <tt class="docutils literal"><span class="pre">0</span></tt>.  The error indicator may or may not be
cleared if it was previously set.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_SetInterrupt">
void <tt class="descname">PyErr_SetInterrupt</tt><big>(</big><big>)</big><a class="headerlink" href="#PyErr_SetInterrupt" title="Permalink to this definition">¶</a></dt>
<dd><p id="index-2">This function simulates the effect of a <tt class="xref py py-const docutils literal"><span class="pre">SIGINT</span></tt> signal arriving &#8212; the
next time <a class="reference internal" href="#PyErr_CheckSignals" title="PyErr_CheckSignals"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_CheckSignals()</span></tt></a> is called,  <a class="reference internal" href="../library/exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><tt class="xref py py-exc docutils literal"><span class="pre">KeyboardInterrupt</span></tt></a> will
be raised.  It may be called without holding the interpreter lock.</p>
</dd></dl>

<dl class="function">
<dt id="PySignal_SetWakeupFd">
int <tt class="descname">PySignal_SetWakeupFd</tt><big>(</big>int<em>&nbsp;fd</em><big>)</big><a class="headerlink" href="#PySignal_SetWakeupFd" title="Permalink to this definition">¶</a></dt>
<dd><p>This utility function specifies a file descriptor to which a <tt class="docutils literal"><span class="pre">'\0'</span></tt> byte will
be written whenever a signal is received.  It returns the previous such file
descriptor.  The value <tt class="docutils literal"><span class="pre">-1</span></tt> disables the feature; this is the initial state.
This is equivalent to <a class="reference internal" href="../library/signal.html#signal.set_wakeup_fd" title="signal.set_wakeup_fd"><tt class="xref py py-func docutils literal"><span class="pre">signal.set_wakeup_fd()</span></tt></a> in Python, but without any
error checking.  <em>fd</em> should be a valid file descriptor.  The function should
only be called from the main thread.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_NewException">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyErr_NewException</tt><big>(</big>char<em>&nbsp;*name</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*base</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*dict</em><big>)</big><a class="headerlink" href="#PyErr_NewException" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>This utility function creates and returns a new exception class. The <em>name</em>
argument must be the name of the new exception, a C string of the form
<tt class="docutils literal"><span class="pre">module.classname</span></tt>.  The <em>base</em> and <em>dict</em> arguments are normally <em>NULL</em>.
This creates a class object derived from <a class="reference internal" href="../library/exceptions.html#Exception" title="Exception"><tt class="xref py py-exc docutils literal"><span class="pre">Exception</span></tt></a> (accessible in C as
<tt class="xref c c-data docutils literal"><span class="pre">PyExc_Exception</span></tt>).</p>
<p>The <tt class="xref py py-attr docutils literal"><span class="pre">__module__</span></tt> attribute of the new class is set to the first part (up
to the last dot) of the <em>name</em> argument, and the class name is set to the last
part (after the last dot).  The <em>base</em> argument can be used to specify alternate
base classes; it can either be only one class or a tuple of classes. The <em>dict</em>
argument can be used to specify a dictionary of class variables and methods.</p>
</dd></dl>

<dl class="function">
<dt id="PyErr_NewExceptionWithDoc">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyErr_NewExceptionWithDoc</tt><big>(</big>char<em>&nbsp;*name</em>, char<em>&nbsp;*doc</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*base</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*dict</em><big>)</big><a class="headerlink" href="#PyErr_NewExceptionWithDoc" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Same as <a class="reference internal" href="#PyErr_NewException" title="PyErr_NewException"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_NewException()</span></tt></a>, except that the new exception class can
easily be given a docstring: If <em>doc</em> is non-<em>NULL</em>, it will be used as the
docstring for the exception class.</p>
<p class="versionadded">
<span class="versionmodified">New in version 3.2.</span> </p>
</dd></dl>

<dl class="function">
<dt id="PyErr_WriteUnraisable">
void <tt class="descname">PyErr_WriteUnraisable</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*obj</em><big>)</big><a class="headerlink" href="#PyErr_WriteUnraisable" title="Permalink to this definition">¶</a></dt>
<dd><p>This utility function prints a warning message to <tt class="docutils literal"><span class="pre">sys.stderr</span></tt> when an
exception has been set but it is impossible for the interpreter to actually
raise the exception.  It is used, for example, when an exception occurs in an
<a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><tt class="xref py py-meth docutils literal"><span class="pre">__del__()</span></tt></a> method.</p>
<p>The function is called with a single argument <em>obj</em> that identifies the context
in which the unraisable exception occurred. The repr of <em>obj</em> will be printed in
the warning message.</p>
</dd></dl>

<div class="section" id="exception-objects">
<h2>Exception Objects<a class="headerlink" href="#exception-objects" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="PyException_GetTraceback">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyException_GetTraceback</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*ex</em><big>)</big><a class="headerlink" href="#PyException_GetTraceback" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Return the traceback associated with the exception as a new reference, as
accessible from Python through <tt class="xref py py-attr docutils literal"><span class="pre">__traceback__</span></tt>.  If there is no
traceback associated, this returns <em>NULL</em>.</p>
</dd></dl>

<dl class="function">
<dt id="PyException_SetTraceback">
int <tt class="descname">PyException_SetTraceback</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*ex</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*tb</em><big>)</big><a class="headerlink" href="#PyException_SetTraceback" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the traceback associated with the exception to <em>tb</em>.  Use <tt class="docutils literal"><span class="pre">Py_None</span></tt> to
clear it.</p>
</dd></dl>

<dl class="function">
<dt id="PyException_GetContext">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyException_GetContext</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*ex</em><big>)</big><a class="headerlink" href="#PyException_GetContext" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the context (another exception instance during whose handling <em>ex</em> was
raised) associated with the exception as a new reference, as accessible from
Python through <tt class="xref py py-attr docutils literal"><span class="pre">__context__</span></tt>.  If there is no context associated, this
returns <em>NULL</em>.</p>
</dd></dl>

<dl class="function">
<dt id="PyException_SetContext">
void <tt class="descname">PyException_SetContext</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*ex</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*ctx</em><big>)</big><a class="headerlink" href="#PyException_SetContext" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the context associated with the exception to <em>ctx</em>.  Use <em>NULL</em> to clear
it.  There is no type check to make sure that <em>ctx</em> is an exception instance.
This steals a reference to <em>ctx</em>.</p>
</dd></dl>

<dl class="function">
<dt id="PyException_GetCause">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyException_GetCause</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*ex</em><big>)</big><a class="headerlink" href="#PyException_GetCause" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the cause (either an exception instance, or <a class="reference internal" href="../library/constants.html#None" title="None"><tt class="xref py py-const xref docutils literal"><span class="pre">None</span></tt></a>,
set by <tt class="docutils literal"><span class="pre">raise</span> <span class="pre">...</span> <span class="pre">from</span> <span class="pre">...</span></tt>) associated with the exception as a new
reference, as accessible from Python through <tt class="xref py py-attr docutils literal"><span class="pre">__cause__</span></tt>.</p>
</dd></dl>

<dl class="function">
<dt id="PyException_SetCause">
void <tt class="descname">PyException_SetCause</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*ex</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*ctx</em><big>)</big><a class="headerlink" href="#PyException_SetCause" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the cause associated with the exception to <em>ctx</em>.  Use <em>NULL</em> to clear
it.  There is no type check to make sure that <em>ctx</em> is either an exception
instance or <a class="reference internal" href="../library/constants.html#None" title="None"><tt class="xref py py-const xref docutils literal"><span class="pre">None</span></tt></a>.  This steals a reference to <em>ctx</em>.</p>
<p><tt class="xref py py-attr docutils literal"><span class="pre">__suppress_context__</span></tt> is implicitly set to <tt class="xref docutils literal"><span class="pre">True</span></tt> by this function.</p>
</dd></dl>

</div>
<div class="section" id="unicode-exception-objects">
<span id="unicodeexceptions"></span><h2>Unicode Exception Objects<a class="headerlink" href="#unicode-exception-objects" title="Permalink to this headline">¶</a></h2>
<p>The following functions are used to create and modify Unicode exceptions from C.</p>
<dl class="function">
<dt id="PyUnicodeDecodeError_Create">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyUnicodeDecodeError_Create</tt><big>(</big>const char<em>&nbsp;*encoding</em>, const char<em>&nbsp;*object</em>, Py_ssize_t<em>&nbsp;length</em>, Py_ssize_t<em>&nbsp;start</em>, Py_ssize_t<em>&nbsp;end</em>, const char<em>&nbsp;*reason</em><big>)</big><a class="headerlink" href="#PyUnicodeDecodeError_Create" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a <a class="reference internal" href="../library/exceptions.html#UnicodeDecodeError" title="UnicodeDecodeError"><tt class="xref py py-class docutils literal"><span class="pre">UnicodeDecodeError</span></tt></a> object with the attributes <em>encoding</em>,
<em>object</em>, <em>length</em>, <em>start</em>, <em>end</em> and <em>reason</em>. <em>encoding</em> and <em>reason</em> are
UTF-8 encoded strings.</p>
</dd></dl>

<dl class="function">
<dt id="PyUnicodeEncodeError_Create">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyUnicodeEncodeError_Create</tt><big>(</big>const char<em>&nbsp;*encoding</em>, const <a class="reference internal" href="unicode.html#Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em>&nbsp;*object</em>, Py_ssize_t<em>&nbsp;length</em>, Py_ssize_t<em>&nbsp;start</em>, Py_ssize_t<em>&nbsp;end</em>, const char<em>&nbsp;*reason</em><big>)</big><a class="headerlink" href="#PyUnicodeEncodeError_Create" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a <a class="reference internal" href="../library/exceptions.html#UnicodeEncodeError" title="UnicodeEncodeError"><tt class="xref py py-class docutils literal"><span class="pre">UnicodeEncodeError</span></tt></a> object with the attributes <em>encoding</em>,
<em>object</em>, <em>length</em>, <em>start</em>, <em>end</em> and <em>reason</em>. <em>encoding</em> and <em>reason</em> are
UTF-8 encoded strings.</p>
</dd></dl>

<dl class="function">
<dt id="PyUnicodeTranslateError_Create">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyUnicodeTranslateError_Create</tt><big>(</big>const <a class="reference internal" href="unicode.html#Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em>&nbsp;*object</em>, Py_ssize_t<em>&nbsp;length</em>, Py_ssize_t<em>&nbsp;start</em>, Py_ssize_t<em>&nbsp;end</em>, const char<em>&nbsp;*reason</em><big>)</big><a class="headerlink" href="#PyUnicodeTranslateError_Create" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a <a class="reference internal" href="../library/exceptions.html#UnicodeTranslateError" title="UnicodeTranslateError"><tt class="xref py py-class docutils literal"><span class="pre">UnicodeTranslateError</span></tt></a> object with the attributes <em>object</em>,
<em>length</em>, <em>start</em>, <em>end</em> and <em>reason</em>. <em>reason</em> is an UTF-8 encoded string.</p>
</dd></dl>

<dl class="function">
<dt id="PyUnicodeDecodeError_GetEncoding">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyUnicodeDecodeError_GetEncoding</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em><big>)</big><a class="headerlink" href="#PyUnicodeDecodeError_GetEncoding" title="Permalink to this definition">¶</a></dt>
<dt id="PyUnicodeEncodeError_GetEncoding">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyUnicodeEncodeError_GetEncoding</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em><big>)</big><a class="headerlink" href="#PyUnicodeEncodeError_GetEncoding" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the <em>encoding</em> attribute of the given exception object.</p>
</dd></dl>

<dl class="function">
<dt id="PyUnicodeDecodeError_GetObject">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyUnicodeDecodeError_GetObject</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em><big>)</big><a class="headerlink" href="#PyUnicodeDecodeError_GetObject" title="Permalink to this definition">¶</a></dt>
<dt id="PyUnicodeEncodeError_GetObject">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyUnicodeEncodeError_GetObject</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em><big>)</big><a class="headerlink" href="#PyUnicodeEncodeError_GetObject" title="Permalink to this definition">¶</a></dt>
<dt id="PyUnicodeTranslateError_GetObject">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyUnicodeTranslateError_GetObject</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em><big>)</big><a class="headerlink" href="#PyUnicodeTranslateError_GetObject" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the <em>object</em> attribute of the given exception object.</p>
</dd></dl>

<dl class="function">
<dt id="PyUnicodeDecodeError_GetStart">
int <tt class="descname">PyUnicodeDecodeError_GetStart</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em>, Py_ssize_t<em>&nbsp;*start</em><big>)</big><a class="headerlink" href="#PyUnicodeDecodeError_GetStart" title="Permalink to this definition">¶</a></dt>
<dt id="PyUnicodeEncodeError_GetStart">
int <tt class="descname">PyUnicodeEncodeError_GetStart</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em>, Py_ssize_t<em>&nbsp;*start</em><big>)</big><a class="headerlink" href="#PyUnicodeEncodeError_GetStart" title="Permalink to this definition">¶</a></dt>
<dt id="PyUnicodeTranslateError_GetStart">
int <tt class="descname">PyUnicodeTranslateError_GetStart</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em>, Py_ssize_t<em>&nbsp;*start</em><big>)</big><a class="headerlink" href="#PyUnicodeTranslateError_GetStart" title="Permalink to this definition">¶</a></dt>
<dd><p>Get the <em>start</em> attribute of the given exception object and place it into
<em>*start</em>.  <em>start</em> must not be <em>NULL</em>.  Return <tt class="docutils literal"><span class="pre">0</span></tt> on success, <tt class="docutils literal"><span class="pre">-1</span></tt> on
failure.</p>
</dd></dl>

<dl class="function">
<dt id="PyUnicodeDecodeError_SetStart">
int <tt class="descname">PyUnicodeDecodeError_SetStart</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em>, Py_ssize_t<em>&nbsp;start</em><big>)</big><a class="headerlink" href="#PyUnicodeDecodeError_SetStart" title="Permalink to this definition">¶</a></dt>
<dt id="PyUnicodeEncodeError_SetStart">
int <tt class="descname">PyUnicodeEncodeError_SetStart</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em>, Py_ssize_t<em>&nbsp;start</em><big>)</big><a class="headerlink" href="#PyUnicodeEncodeError_SetStart" title="Permalink to this definition">¶</a></dt>
<dt id="PyUnicodeTranslateError_SetStart">
int <tt class="descname">PyUnicodeTranslateError_SetStart</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em>, Py_ssize_t<em>&nbsp;start</em><big>)</big><a class="headerlink" href="#PyUnicodeTranslateError_SetStart" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the <em>start</em> attribute of the given exception object to <em>start</em>.  Return
<tt class="docutils literal"><span class="pre">0</span></tt> on success, <tt class="docutils literal"><span class="pre">-1</span></tt> on failure.</p>
</dd></dl>

<dl class="function">
<dt id="PyUnicodeDecodeError_GetEnd">
int <tt class="descname">PyUnicodeDecodeError_GetEnd</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em>, Py_ssize_t<em>&nbsp;*end</em><big>)</big><a class="headerlink" href="#PyUnicodeDecodeError_GetEnd" title="Permalink to this definition">¶</a></dt>
<dt id="PyUnicodeEncodeError_GetEnd">
int <tt class="descname">PyUnicodeEncodeError_GetEnd</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em>, Py_ssize_t<em>&nbsp;*end</em><big>)</big><a class="headerlink" href="#PyUnicodeEncodeError_GetEnd" title="Permalink to this definition">¶</a></dt>
<dt id="PyUnicodeTranslateError_GetEnd">
int <tt class="descname">PyUnicodeTranslateError_GetEnd</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em>, Py_ssize_t<em>&nbsp;*end</em><big>)</big><a class="headerlink" href="#PyUnicodeTranslateError_GetEnd" title="Permalink to this definition">¶</a></dt>
<dd><p>Get the <em>end</em> attribute of the given exception object and place it into
<em>*end</em>.  <em>end</em> must not be <em>NULL</em>.  Return <tt class="docutils literal"><span class="pre">0</span></tt> on success, <tt class="docutils literal"><span class="pre">-1</span></tt> on
failure.</p>
</dd></dl>

<dl class="function">
<dt id="PyUnicodeDecodeError_SetEnd">
int <tt class="descname">PyUnicodeDecodeError_SetEnd</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em>, Py_ssize_t<em>&nbsp;end</em><big>)</big><a class="headerlink" href="#PyUnicodeDecodeError_SetEnd" title="Permalink to this definition">¶</a></dt>
<dt id="PyUnicodeEncodeError_SetEnd">
int <tt class="descname">PyUnicodeEncodeError_SetEnd</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em>, Py_ssize_t<em>&nbsp;end</em><big>)</big><a class="headerlink" href="#PyUnicodeEncodeError_SetEnd" title="Permalink to this definition">¶</a></dt>
<dt id="PyUnicodeTranslateError_SetEnd">
int <tt class="descname">PyUnicodeTranslateError_SetEnd</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em>, Py_ssize_t<em>&nbsp;end</em><big>)</big><a class="headerlink" href="#PyUnicodeTranslateError_SetEnd" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the <em>end</em> attribute of the given exception object to <em>end</em>.  Return <tt class="docutils literal"><span class="pre">0</span></tt>
on success, <tt class="docutils literal"><span class="pre">-1</span></tt> on failure.</p>
</dd></dl>

<dl class="function">
<dt id="PyUnicodeDecodeError_GetReason">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyUnicodeDecodeError_GetReason</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em><big>)</big><a class="headerlink" href="#PyUnicodeDecodeError_GetReason" title="Permalink to this definition">¶</a></dt>
<dt id="PyUnicodeEncodeError_GetReason">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyUnicodeEncodeError_GetReason</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em><big>)</big><a class="headerlink" href="#PyUnicodeEncodeError_GetReason" title="Permalink to this definition">¶</a></dt>
<dt id="PyUnicodeTranslateError_GetReason">
<a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyUnicodeTranslateError_GetReason</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em><big>)</big><a class="headerlink" href="#PyUnicodeTranslateError_GetReason" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the <em>reason</em> attribute of the given exception object.</p>
</dd></dl>

<dl class="function">
<dt id="PyUnicodeDecodeError_SetReason">
int <tt class="descname">PyUnicodeDecodeError_SetReason</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em>, const char<em>&nbsp;*reason</em><big>)</big><a class="headerlink" href="#PyUnicodeDecodeError_SetReason" title="Permalink to this definition">¶</a></dt>
<dt id="PyUnicodeEncodeError_SetReason">
int <tt class="descname">PyUnicodeEncodeError_SetReason</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em>, const char<em>&nbsp;*reason</em><big>)</big><a class="headerlink" href="#PyUnicodeEncodeError_SetReason" title="Permalink to this definition">¶</a></dt>
<dt id="PyUnicodeTranslateError_SetReason">
int <tt class="descname">PyUnicodeTranslateError_SetReason</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*exc</em>, const char<em>&nbsp;*reason</em><big>)</big><a class="headerlink" href="#PyUnicodeTranslateError_SetReason" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the <em>reason</em> attribute of the given exception object to <em>reason</em>.  Return
<tt class="docutils literal"><span class="pre">0</span></tt> on success, <tt class="docutils literal"><span class="pre">-1</span></tt> on failure.</p>
</dd></dl>

</div>
<div class="section" id="recursion-control">
<h2>Recursion Control<a class="headerlink" href="#recursion-control" title="Permalink to this headline">¶</a></h2>
<p>These two functions provide a way to perform safe recursive calls at the C
level, both in the core and in extension modules.  They are needed if the
recursive code does not necessarily invoke Python code (which tracks its
recursion depth automatically).</p>
<dl class="function">
<dt id="Py_EnterRecursiveCall">
int <tt class="descname">Py_EnterRecursiveCall</tt><big>(</big>char<em>&nbsp;*where</em><big>)</big><a class="headerlink" href="#Py_EnterRecursiveCall" title="Permalink to this definition">¶</a></dt>
<dd><p>Marks a point where a recursive C-level call is about to be performed.</p>
<p>If <tt class="xref py py-const docutils literal"><span class="pre">USE_STACKCHECK</span></tt> is defined, this function checks if the OS
stack overflowed using <a class="reference internal" href="sys.html#PyOS_CheckStack" title="PyOS_CheckStack"><tt class="xref c c-func docutils literal"><span class="pre">PyOS_CheckStack()</span></tt></a>.  In this is the case, it
sets a <a class="reference internal" href="../library/exceptions.html#MemoryError" title="MemoryError"><tt class="xref py py-exc docutils literal"><span class="pre">MemoryError</span></tt></a> and returns a nonzero value.</p>
<p>The function then checks if the recursion limit is reached.  If this is the
case, a <a class="reference internal" href="../library/exceptions.html#RuntimeError" title="RuntimeError"><tt class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></tt></a> is set and a nonzero value is returned.
Otherwise, zero is returned.</p>
<p><em>where</em> should be a string such as <tt class="docutils literal"><span class="pre">&quot;</span> <span class="pre">in</span> <span class="pre">instance</span> <span class="pre">check&quot;</span></tt> to be
concatenated to the <a class="reference internal" href="../library/exceptions.html#RuntimeError" title="RuntimeError"><tt class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></tt></a> message caused by the recursion depth
limit.</p>
</dd></dl>

<dl class="function">
<dt id="Py_LeaveRecursiveCall">
void <tt class="descname">Py_LeaveRecursiveCall</tt><big>(</big><big>)</big><a class="headerlink" href="#Py_LeaveRecursiveCall" title="Permalink to this definition">¶</a></dt>
<dd><p>Ends a <a class="reference internal" href="#Py_EnterRecursiveCall" title="Py_EnterRecursiveCall"><tt class="xref c c-func docutils literal"><span class="pre">Py_EnterRecursiveCall()</span></tt></a>.  Must be called once for each
<em>successful</em> invocation of <a class="reference internal" href="#Py_EnterRecursiveCall" title="Py_EnterRecursiveCall"><tt class="xref c c-func docutils literal"><span class="pre">Py_EnterRecursiveCall()</span></tt></a>.</p>
</dd></dl>

<p>Properly implementing <tt class="xref py py-attr docutils literal"><span class="pre">tp_repr</span></tt> for container types requires
special recursion handling.  In addition to protecting the stack,
<tt class="xref py py-attr docutils literal"><span class="pre">tp_repr</span></tt> also needs to track objects to prevent cycles.  The
following two functions facilitate this functionality.  Effectively,
these are the C equivalent to <a class="reference internal" href="../library/reprlib.html#reprlib.recursive_repr" title="reprlib.recursive_repr"><tt class="xref py py-func docutils literal"><span class="pre">reprlib.recursive_repr()</span></tt></a>.</p>
<dl class="function">
<dt id="Py_ReprEnter">
int <tt class="descname">Py_ReprEnter</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*object</em><big>)</big><a class="headerlink" href="#Py_ReprEnter" title="Permalink to this definition">¶</a></dt>
<dd><p>Called at the beginning of the <tt class="xref py py-attr docutils literal"><span class="pre">tp_repr</span></tt> implementation to
detect cycles.</p>
<p>If the object has already been processed, the function returns a
positive integer.  In that case the <tt class="xref py py-attr docutils literal"><span class="pre">tp_repr</span></tt> implementation
should return a string object indicating a cycle.  As examples,
<a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt></a> objects return <tt class="docutils literal"><span class="pre">{...}</span></tt> and <a class="reference internal" href="../library/stdtypes.html#list" title="list"><tt class="xref py py-class docutils literal"><span class="pre">list</span></tt></a> objects
return <tt class="docutils literal"><span class="pre">[...]</span></tt>.</p>
<p>The function will return a negative integer if the recursion limit
is reached.  In that case the <tt class="xref py py-attr docutils literal"><span class="pre">tp_repr</span></tt> implementation should
typically return <tt class="docutils literal"><span class="pre">NULL</span></tt>.</p>
<p>Otherwise, the function returns zero and the <tt class="xref py py-attr docutils literal"><span class="pre">tp_repr</span></tt>
implementation can continue normally.</p>
</dd></dl>

<dl class="function">
<dt id="Py_ReprLeave">
void <tt class="descname">Py_ReprLeave</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*object</em><big>)</big><a class="headerlink" href="#Py_ReprLeave" title="Permalink to this definition">¶</a></dt>
<dd><p>Ends a <a class="reference internal" href="#Py_ReprEnter" title="Py_ReprEnter"><tt class="xref c c-func docutils literal"><span class="pre">Py_ReprEnter()</span></tt></a>.  Must be called once for each
invocation of <a class="reference internal" href="#Py_ReprEnter" title="Py_ReprEnter"><tt class="xref c c-func docutils literal"><span class="pre">Py_ReprEnter()</span></tt></a> that returns zero.</p>
</dd></dl>

</div>
<div class="section" id="standard-exceptions">
<span id="standardexceptions"></span><h2>Standard Exceptions<a class="headerlink" href="#standard-exceptions" title="Permalink to this headline">¶</a></h2>
<p>All standard Python exceptions are available as global variables whose names are
<tt class="docutils literal"><span class="pre">PyExc_</span></tt> followed by the Python exception name.  These have the type
<a class="reference internal" href="structures.html#PyObject" title="PyObject"><tt class="xref c c-type docutils literal"><span class="pre">PyObject*</span></tt></a>; they are all class objects.  For completeness, here are all
the variables:</p>
<table border="1" class="docutils">
<colgroup>
<col width="49%" />
<col width="39%" />
<col width="12%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">C Name</th>
<th class="head">Python Name</th>
<th class="head">Notes</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_BaseException</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#BaseException" title="BaseException"><tt class="xref py py-exc docutils literal"><span class="pre">BaseException</span></tt></a></td>
<td>(1)</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_Exception</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#Exception" title="Exception"><tt class="xref py py-exc docutils literal"><span class="pre">Exception</span></tt></a></td>
<td>(1)</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_ArithmeticError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#ArithmeticError" title="ArithmeticError"><tt class="xref py py-exc docutils literal"><span class="pre">ArithmeticError</span></tt></a></td>
<td>(1)</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_LookupError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#LookupError" title="LookupError"><tt class="xref py py-exc docutils literal"><span class="pre">LookupError</span></tt></a></td>
<td>(1)</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_AssertionError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#AssertionError" title="AssertionError"><tt class="xref py py-exc docutils literal"><span class="pre">AssertionError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_AttributeError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#AttributeError" title="AttributeError"><tt class="xref py py-exc docutils literal"><span class="pre">AttributeError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_BlockingIOError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#BlockingIOError" title="BlockingIOError"><tt class="xref py py-exc docutils literal"><span class="pre">BlockingIOError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_BrokenPipeError</span></tt></td>
<td><tt class="xref py py-exc docutils literal"><span class="pre">BrokenPipeError</span></tt></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_ChildProcessError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#ChildProcessError" title="ChildProcessError"><tt class="xref py py-exc docutils literal"><span class="pre">ChildProcessError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_ConnectionError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#ConnectionError" title="ConnectionError"><tt class="xref py py-exc docutils literal"><span class="pre">ConnectionError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_ConnectionAbortedError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#ConnectionAbortedError" title="ConnectionAbortedError"><tt class="xref py py-exc docutils literal"><span class="pre">ConnectionAbortedError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_ConnectionRefusedError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#ConnectionRefusedError" title="ConnectionRefusedError"><tt class="xref py py-exc docutils literal"><span class="pre">ConnectionRefusedError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_ConnectionResetError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#ConnectionResetError" title="ConnectionResetError"><tt class="xref py py-exc docutils literal"><span class="pre">ConnectionResetError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_FileExistsError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#FileExistsError" title="FileExistsError"><tt class="xref py py-exc docutils literal"><span class="pre">FileExistsError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_FileNotFoundError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#FileNotFoundError" title="FileNotFoundError"><tt class="xref py py-exc docutils literal"><span class="pre">FileNotFoundError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_EOFError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#EOFError" title="EOFError"><tt class="xref py py-exc docutils literal"><span class="pre">EOFError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_FloatingPointError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#FloatingPointError" title="FloatingPointError"><tt class="xref py py-exc docutils literal"><span class="pre">FloatingPointError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_ImportError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#ImportError" title="ImportError"><tt class="xref py py-exc docutils literal"><span class="pre">ImportError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_IndexError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#IndexError" title="IndexError"><tt class="xref py py-exc docutils literal"><span class="pre">IndexError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_InterruptedError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#InterruptedError" title="InterruptedError"><tt class="xref py py-exc docutils literal"><span class="pre">InterruptedError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_IsADirectoryError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#IsADirectoryError" title="IsADirectoryError"><tt class="xref py py-exc docutils literal"><span class="pre">IsADirectoryError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_KeyError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#KeyError" title="KeyError"><tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_KeyboardInterrupt</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><tt class="xref py py-exc docutils literal"><span class="pre">KeyboardInterrupt</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_MemoryError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#MemoryError" title="MemoryError"><tt class="xref py py-exc docutils literal"><span class="pre">MemoryError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_NameError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#NameError" title="NameError"><tt class="xref py py-exc docutils literal"><span class="pre">NameError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_NotADirectoryError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#NotADirectoryError" title="NotADirectoryError"><tt class="xref py py-exc docutils literal"><span class="pre">NotADirectoryError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_NotImplementedError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#NotImplementedError" title="NotImplementedError"><tt class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_OSError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#OSError" title="OSError"><tt class="xref py py-exc docutils literal"><span class="pre">OSError</span></tt></a></td>
<td>(1)</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_OverflowError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#OverflowError" title="OverflowError"><tt class="xref py py-exc docutils literal"><span class="pre">OverflowError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_PermissionError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#PermissionError" title="PermissionError"><tt class="xref py py-exc docutils literal"><span class="pre">PermissionError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_ProcessLookupError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#ProcessLookupError" title="ProcessLookupError"><tt class="xref py py-exc docutils literal"><span class="pre">ProcessLookupError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_ReferenceError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#ReferenceError" title="ReferenceError"><tt class="xref py py-exc docutils literal"><span class="pre">ReferenceError</span></tt></a></td>
<td>(2)</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_RuntimeError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#RuntimeError" title="RuntimeError"><tt class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_SyntaxError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><tt class="xref py py-exc docutils literal"><span class="pre">SyntaxError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_SystemError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#SystemError" title="SystemError"><tt class="xref py py-exc docutils literal"><span class="pre">SystemError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_TimeoutError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#TimeoutError" title="TimeoutError"><tt class="xref py py-exc docutils literal"><span class="pre">TimeoutError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_SystemExit</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#SystemExit" title="SystemExit"><tt class="xref py py-exc docutils literal"><span class="pre">SystemExit</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_TypeError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_ValueError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#ValueError" title="ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_ZeroDivisionError</span></tt></td>
<td><a class="reference internal" href="../library/exceptions.html#ZeroDivisionError" title="ZeroDivisionError"><tt class="xref py py-exc docutils literal"><span class="pre">ZeroDivisionError</span></tt></a></td>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
<p class="versionadded">
<span class="versionmodified">New in version 3.3:</span> <tt class="xref c c-data docutils literal"><span class="pre">PyExc_BlockingIOError</span></tt>, <tt class="xref c c-data docutils literal"><span class="pre">PyExc_BrokenPipeError</span></tt>,
<tt class="xref c c-data docutils literal"><span class="pre">PyExc_ChildProcessError</span></tt>, <tt class="xref c c-data docutils literal"><span class="pre">PyExc_ConnectionError</span></tt>,
<tt class="xref c c-data docutils literal"><span class="pre">PyExc_ConnectionAbortedError</span></tt>, <tt class="xref c c-data docutils literal"><span class="pre">PyExc_ConnectionRefusedError</span></tt>,
<tt class="xref c c-data docutils literal"><span class="pre">PyExc_ConnectionResetError</span></tt>, <tt class="xref c c-data docutils literal"><span class="pre">PyExc_FileExistsError</span></tt>,
<tt class="xref c c-data docutils literal"><span class="pre">PyExc_FileNotFoundError</span></tt>, <tt class="xref c c-data docutils literal"><span class="pre">PyExc_InterruptedError</span></tt>,
<tt class="xref c c-data docutils literal"><span class="pre">PyExc_IsADirectoryError</span></tt>, <tt class="xref c c-data docutils literal"><span class="pre">PyExc_NotADirectoryError</span></tt>,
<tt class="xref c c-data docutils literal"><span class="pre">PyExc_PermissionError</span></tt>, <tt class="xref c c-data docutils literal"><span class="pre">PyExc_ProcessLookupError</span></tt>
and <tt class="xref c c-data docutils literal"><span class="pre">PyExc_TimeoutError</span></tt> were introduced following <span class="target" id="index-3"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3151"><strong>PEP 3151</strong></a>.</p>
<p>These are compatibility aliases to <tt class="xref c c-data docutils literal"><span class="pre">PyExc_OSError</span></tt>:</p>
<table border="1" class="docutils">
<colgroup>
<col width="79%" />
<col width="21%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">C Name</th>
<th class="head">Notes</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_EnvironmentError</span></tt></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_IOError</span></tt></td>
<td>&nbsp;</td>
</tr>
<tr><td><tt class="xref c c-data docutils literal"><span class="pre">PyExc_WindowsError</span></tt></td>
<td>(3)</td>
</tr>
</tbody>
</table>
<p class="versionchanged">
<span class="versionmodified">Changed in version 3.3:</span> These aliases used to be separate exception types.</p>
<p id="index-4">Notes:</p>
<ol class="arabic simple">
<li>This is a base class for other standard exceptions.</li>
<li>This is the same as <a class="reference internal" href="../library/weakref.html#weakref.ReferenceError" title="weakref.ReferenceError"><tt class="xref py py-exc docutils literal"><span class="pre">weakref.ReferenceError</span></tt></a>.</li>
<li>Only defined on Windows; protect code that uses this by testing that the
preprocessor macro <tt class="docutils literal"><span class="pre">MS_WINDOWS</span></tt> is defined.</li>
</ol>
</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 internal" href="#">Exception Handling</a><ul>
<li><a class="reference internal" href="#exception-objects">Exception Objects</a></li>
<li><a class="reference internal" href="#unicode-exception-objects">Unicode Exception Objects</a></li>
<li><a class="reference internal" href="#recursion-control">Recursion Control</a></li>
<li><a class="reference internal" href="#standard-exceptions">Standard Exceptions</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="refcounting.html"
                        title="previous chapter">Reference Counting</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="utilities.html"
                        title="next chapter">Utilities</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/c-api/exceptions.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="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="utilities.html" title="Utilities"
             >next</a> |</li>
        <li class="right" >
          <a href="refcounting.html" title="Reference Counting"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li><a href="../index.html">3.3.0 Documentation</a> &raquo;</li>

          <li><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2012, 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 Sep 29, 2012.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>