“Python/docs/3.9/c-api/exceptions”的版本间差异

来自菜鸟教程
Python/docs/3.9/c-api/exceptions
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:异常处理 — Python 文档}}
 
<div id="exception-handling" class="section">
 
<div id="exception-handling" class="section">
  
 
<span id="exceptionhandling"></span>
 
<span id="exceptionhandling"></span>
= Exception Handling =
+
= 异常处理 =
  
The functions described in this chapter will let you handle and raise Python
+
本章中描述的函数将让您处理和引发 Python 异常。 了解 Python 异常处理的一些基础知识很重要。 它的工作方式有点像 POSIX <code>errno</code> 变量:上次发生的错误有一个全局指示器(每个线程)。 大多数 C API 函数在成功时不会清除它,但会设置它以指示失败时错误的原因。 大多数 C API 函数也返回一个错误指示符,通常是 <code>NULL</code> 如果它们应该返回一个指针,或者 <code>-1</code> 如果它们返回一个整数(例外:<span class="xref c c-texpr">PyArg_*</span>函数返回 <code>1</code> 表示成功,返回 <code>0</code> 表示失败)。
exceptions. It is important to understand some of the basics of Python
 
exception handling. It works somewhat like the POSIX <code>errno</code> variable:
 
there is a global indicator (per thread) of the last error that occurred. Most
 
C API functions don't clear this on success, but will set it to indicate the
 
cause of the error on failure. Most C API functions also return an error
 
indicator, usually <code>NULL</code> if they are supposed to return a pointer, or <code>-1</code>
 
if they return an integer (exception: the <span class="xref c c-texpr">PyArg_*</span> functions
 
return <code>1</code> for success and <code>0</code> for failure).
 
  
Concretely, the error indicator consists of three object pointers: the
+
具体来说,错误指示器由三个对象指针组成:异常类型、异常值和回溯对象。 如果未设置,这些指针中的任何一个都可以是 <code>NULL</code>(尽管禁止某些组合,例如,如果异常类型是 [X175X,则不能有非 <code>NULL</code> 回溯) ])。
exception's type, the exception's value, and the traceback object. Any
 
of those pointers can be <code>NULL</code> if non-set (although some combinations are
 
forbidden, for example you can't have a non-<code>NULL</code> traceback if the exception
 
type is <code>NULL</code>).
 
  
When a function must fail because some function it called failed, it generally
+
当一个函数因为它调用的某个函数失败而必须失败时,它通常不设置错误指示器; 它调用的函数已经设置了它。 它负责处理错误和清除异常,或者在清理它持有的任何资源(例如对象引用或内存分配)后返回; 如果它不准备处理错误,它应该 ''not'' 继续正常。 如果由于错误而返回,则向调用者表明已设置错误很重要。 如果错误没有得到处理或小心传播,对 Python/C API 的额外调用可能不会按预期运行,并且可能会以神秘的方式失败。
doesn'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 ''not'' 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.
 
  
 
<div class="admonition note">
 
<div class="admonition note">
  
注解
+
笔记
  
The error indicator is '''not''' the result of [[../../library/sys#sys|<code>sys.exc_info()</code>]].
+
错误指示符是 '''不是''' [[../../library/sys#sys|sys.exc_info()]] 的结果。 前者对应于尚未捕获的异常(因此仍在传播),而后者在捕获后返回异常(因此已停止传播)。
The former corresponds to an exception that is not yet caught (and is
 
therefore still propagating), while the latter returns an exception after
 
it is caught (and has therefore stopped propagating).
 
  
  
第43行: 第21行:
 
<div id="printing-and-clearing" class="section">
 
<div id="printing-and-clearing" class="section">
  
== Printing and clearing ==
+
== 打印和清算 ==
  
; void <code>PyErr_Clear</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_Clear</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><br />
: Clear the error indicator. If the error indicator is not set, there is no effect.
+
 
 +
: 清除错误指示器。 如果未设置错误指示器,则没有效果。
  
 
<dl>
 
<dl>
<dt>void <code>PyErr_PrintEx</code><span class="sig-paren">(</span>int ''set_sys_last_vars''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_PrintEx</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">set_sys_last_vars</span></span><span class="sig-paren">)</span><br />
<dd><p>Print a standard traceback to <code>sys.stderr</code> and clear the error indicator.
+
</dt>
'''Unless''' the error is a <code>SystemExit</code>, in that case no traceback is
+
<dd><p>将标准回溯打印到 <code>sys.stderr</code> 并清除错误指示器。 '''除非''' 错误是 <code>SystemExit</code>,在这种情况下,不会打印回溯,Python 进程将以 <code>SystemExit</code> 实例指定的错误代码退出。</p>
printed and the Python process will exit with the error code specified by
+
<p>设置错误指示器时,仅调用此函数 '''''' 。 否则会导致致命错误!</p>
the <code>SystemExit</code> instance.</p>
+
<p>如果 ''set_sys_last_vars'' 不为零,则变量 [[../../library/sys#sys|sys.last_type]][[../../library/sys#sys|sys.last_value]] [[../../library/sys#sys|sys.last_traceback]] 将被设置为类型、值和打印异常的回溯,分别。</p></dd></dl>
<p>Call this function '''only''' when the error indicator is set. Otherwise it
+
 
will cause a fatal error!</p>
+
; <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_Print</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><br />
<p>If ''set_sys_last_vars'' is nonzero, the variables [[../../library/sys#sys|<code>sys.last_type</code>]],
 
[[../../library/sys#sys|<code>sys.last_value</code>]] and [[../../library/sys#sys|<code>sys.last_traceback</code>]] will be set to the
 
type, value and traceback of the printed exception, respectively.</p></dd></dl>
 
  
; void <code>PyErr_Print</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
: <code>PyErr_PrintEx(1)</code> 的别名。
: Alias for <code>PyErr_PrintEx(1)</code>.
 
  
 
<dl>
 
<dl>
<dt>void <code>PyErr_WriteUnraisable</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''obj''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_WriteUnraisable</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">obj</span></span><span class="sig-paren">)</span><br />
<dd><p>Call [[../../library/sys#sys|<code>sys.unraisablehook()</code>]] using the current exception and ''obj''
+
</dt>
argument.</p>
+
<dd><p>使用当前异常和 ''obj'' 参数调用 [[../../library/sys#sys|sys.unraisablehook()]]</p>
<p>This utility function prints a warning message to <code>sys.stderr</code> when an
+
<p>当设置了异常但解释器不可能实际引发异常时,此实用程序函数会向 <code>sys.stderr</code> 打印一条警告消息。 例如,在 <code>__del__()</code> 方法中发生异常时使用。</p>
exception has been set but it is impossible for the interpreter to actually
+
<p>使用单个参数 ''obj'' 调用该函数,该参数标识发生不可引发异常的上下文。 如果可能,''obj'' 的 repr 将打印在警告消息中。</p>
raise the exception. It is used, for example, when an exception occurs in an
+
<p>调用此函数时必须设置异常。</p></dd></dl>
<code>__del__()</code> method.</p>
 
<p>The function is called with a single argument ''obj'' that identifies the context
 
in which the unraisable exception occurred. If possible,
 
the repr of ''obj'' will be printed in the warning message.</p>
 
<p>An exception must be set when calling this function.</p></dd></dl>
 
  
  
第80行: 第50行:
 
<div id="raising-exceptions" class="section">
 
<div id="raising-exceptions" class="section">
  
== Raising exceptions ==
+
== 引发异常 ==
 +
 
 +
这些函数帮助您设置当前线程的错误指示器。 为方便起见,其中一些函数将始终返回 <code>NULL</code> 指针以用于 <code>return</code> 语句。
 +
 
 +
; <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SetString</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">message</span></span><span class="sig-paren">)</span><br />
 +
 
 +
: 这是设置错误指示器的最常用方法。 第一个参数指定异常类型; 它通常是标准例外之一,例如 <code>PyExc_RuntimeError</code>。 你不需要增加它的引用计数。 第二个参数是错误信息; 它是从 <code>'utf-8'</code> 解码的。
  
These functions help you set the current thread's error indicator.
+
; <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SetObject</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">value</span></span><span class="sig-paren">)</span><br />
For convenience, some of these functions will always return a
 
<code>NULL</code> pointer for use in a <code>return</code> statement.
 
  
; void <code>PyErr_SetString</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''type'', ''const'' char *''message''<span class="sig-paren">)</span>
+
: 此函数类似于 [[#c.PyErr_SetString|PyErr_SetString()]] 但允许您为异常的“值”指定任意 Python 对象。
: 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. <code>PyExc_RuntimeError</code>. You need not increment its reference count. The second argument is an error message; it is decoded from <code>'utf-8</code>'.
 
  
; void <code>PyErr_SetObject</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''type'', [[../structures#c|PyObject]] *''value''<span class="sig-paren">)</span>
+
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_Format</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exception</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">format</span></span>, <span class="p"><span class="pre">...</span></span><span class="sig-paren">)</span><br />
: This function is similar to [[#c.PyErr_SetString|<code>PyErr_SetString()</code>]] but lets you specify an arbitrary Python object for the &quot;value&quot; of the exception.
 
  
<dl>
+
: 该函数设置错误指示器并返回<code>NULL</code>''exception'' 应该是一个 Python 异常类。 ''format'' 和后续参数帮助格式化错误信息; 它们与 [[../unicode#c|PyUnicode_FromFormat()]] 具有相同的含义和值。 ''format'' 是一个 ASCII 编码的字符串。
<dt>[[../structures#c|PyObject]] *<code>PyErr_Format</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exception'', ''const'' char *''format'', ...<span class="sig-paren">)</span></dt>
 
<dd><p>''Return value: Always NULL.''</p>
 
<p>This function sets the error indicator and returns <code>NULL</code>. ''exception''
 
should be a Python exception class. The ''format'' and subsequent
 
parameters help format the error message; they have the same meaning and
 
values as in [[../unicode#c|<code>PyUnicode_FromFormat()</code>]]. ''format'' is an ASCII-encoded
 
string.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyErr_FormatV</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exception'', ''const'' char *''format'', va_list ''vargs''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_FormatV</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exception</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">format</span></span>, <span class="n"><span class="pre">va_list</span></span><span class="w"> </span><span class="n"><span class="pre">vargs</span></span><span class="sig-paren">)</span><br />
<dd><p>''Return value: Always NULL.''</p>
+
</dt>
<p>Same as [[#c.PyErr_Format|<code>PyErr_Format()</code>]], but taking a <code>va_list</code> argument rather
+
<dd><p>[[#c.PyErr_Format|PyErr_Format()]] 相同,但采用 <code>va_list</code> 参数而不是可变数量的参数。</p>
than a variable number of arguments.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.5 新版功能.</span></p>
+
<p><span class="versionmodified added">3.5 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
; void <code>PyErr_SetNone</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''type''<span class="sig-paren">)</span>
+
; <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SetNone</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span><span class="sig-paren">)</span><br />
: This is a shorthand for <code>PyErr_SetObject(type, Py_None)</code>.
+
 
 +
: 这是 <code>PyErr_SetObject(type, Py_None)</code> 的简写。
  
; int <code>PyErr_BadArgument</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_BadArgument</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><br />
: This is a shorthand for <code>PyErr_SetString(PyExc_TypeError, message)</code>, where ''message'' indicates that a built-in operation was invoked with an illegal argument. It is mostly for internal use.
 
  
<dl>
+
: 这是 <code>PyErr_SetString(PyExc_TypeError, message)</code> 的简写,其中 ''message'' 表示使用非法参数调用了内置操作。 它主要供内部使用。
<dt>[[../structures#c|PyObject]] *<code>PyErr_NoMemory</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
 
<dd><p>''Return value: Always NULL.''</p>
+
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_NoMemory</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><br />
<p>This is a shorthand for <code>PyErr_SetNone(PyExc_MemoryError)</code>; it returns <code>NULL</code>
+
 
so an object allocation function can write <code>return PyErr_NoMemory();</code> when it
+
: 这是 <code>PyErr_SetNone(PyExc_MemoryError)</code> 的简写; 它返回 <code>NULL</code>,因此对象分配函数可以在内存不足时写入 <code>return PyErr_NoMemory();</code>。
runs out of memory.</p></dd></dl>
+
 
 +
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SetFromErrno</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span><span class="sig-paren">)</span><br />
 +
 
 +
: 这是一个方便的函数,可以在 C 库函数返回错误并设置 C 变量 <code>errno</code> 时引发异常。 它构造一个元组对象,其第一项是整数 <code>errno</code> 值,第二项是相应的错误消息(从 <code>strerror()</code> 中获取),然后调用 <code>PyErr_SetObject(type, object)</code>。 在 Unix 上,当 <code>errno</code> 值为 <code>EINTR</code> 时,表示系统调用中断,这将调用 [[#c.PyErr_CheckSignals|PyErr_CheckSignals()]],如果设置了错误指示符,则将其设置为那。 该函数始终返回 <code>NULL</code>,因此系统调用周围的包装函数可以在系统调用返回错误时写入 <code>return PyErr_SetFromErrno(type);</code>
  
<dl>
+
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SetFromErrnoWithFilenameObject</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">filenameObject</span></span><span class="sig-paren">)</span><br />
<dt>[[../structures#c|PyObject]] *<code>PyErr_SetFromErrno</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''type''<span class="sig-paren">)</span></dt>
 
<dd><p>''Return value: Always NULL.''</p>
 
<p>This is a convenience function to raise an exception when a C library function
 
has returned an error and set the C variable <code>errno</code>. It constructs a
 
tuple object whose first item is the integer <code>errno</code> value and whose
 
second item is the corresponding error message (gotten from <code>strerror()</code>),
 
and then calls <code>PyErr_SetObject(type, object)</code>. On Unix, when the
 
<code>errno</code> value is <code>EINTR</code>, indicating an interrupted system call,
 
this calls [[#c.PyErr_CheckSignals|<code>PyErr_CheckSignals()</code>]], and if that set the error indicator,
 
leaves it set to that. The function always returns <code>NULL</code>, so a wrapper
 
function around a system call can write <code>return PyErr_SetFromErrno(type);</code>
 
when the system call returns an error.</p></dd></dl>
 
  
<dl>
+
: [[#c.PyErr_SetFromErrno|PyErr_SetFromErrno()]] 类似,附加的行为是如果 ''filenameObject'' 不是 <code>NULL</code>,它会作为一个传递给 ''type'' 的构造函数第三个参数。 在 [[../../library/exceptions#OSError|OSError]] 异常的情况下,这用于定义异常实例的 <code>filename</code> 属性。
<dt>[[../structures#c|PyObject]] *<code>PyErr_SetFromErrnoWithFilenameObject</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''type'', [[../structures#c|PyObject]] *''filenameObject''<span class="sig-paren">)</span></dt>
 
<dd><p>''Return value: Always NULL.''</p>
 
<p>Similar to [[#c.PyErr_SetFromErrno|<code>PyErr_SetFromErrno()</code>]], with the additional behavior that if
 
''filenameObject'' is not <code>NULL</code>, it is passed to the constructor of ''type'' as
 
a third parameter. In the case of [[../../library/exceptions#OSError|<code>OSError</code>]] exception,
 
this is used to define the <code>filename</code> attribute of the
 
exception instance.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyErr_SetFromErrnoWithFilenameObjects</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''type'', [[../structures#c|PyObject]] *''filenameObject'', [[../structures#c|PyObject]] *''filenameObject2''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SetFromErrnoWithFilenameObjects</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">filenameObject</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">filenameObject2</span></span><span class="sig-paren">)</span><br />
<dd><p>''Return value: Always NULL.''</p>
+
</dt>
<p>Similar to [[#c.PyErr_SetFromErrnoWithFilenameObject|<code>PyErr_SetFromErrnoWithFilenameObject()</code>]], but takes a second
+
<dd><p>类似于 [[#c.PyErr_SetFromErrnoWithFilenameObject|PyErr_SetFromErrnoWithFilenameObject()]],但需要第二个文件名对象,用于在需要两个文件名的函数失败时引发错误。</p>
filename object, for raising errors when a function that takes two filenames
 
fails.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.4 新版功能.</span></p>
+
<p><span class="versionmodified added">3.4 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
<dl>
+
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SetFromErrnoWithFilename</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">filename</span></span><span class="sig-paren">)</span><br />
<dt>[[../structures#c|PyObject]] *<code>PyErr_SetFromErrnoWithFilename</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''type'', ''const'' char *''filename''<span class="sig-paren">)</span></dt>
+
 
<dd><p>''Return value: Always NULL.''</p>
+
: 类似于 [[#c.PyErr_SetFromErrnoWithFilenameObject|PyErr_SetFromErrnoWithFilenameObject()]],但文件名以 C 字符串形式给出。 ''filename'' 从文件系统编码 ([[../../library/os#os|os.fsdecode()]]) 解码。
<p>Similar to [[#c.PyErr_SetFromErrnoWithFilenameObject|<code>PyErr_SetFromErrnoWithFilenameObject()</code>]], but the filename
+
 
is given as a C string. ''filename'' is decoded from the filesystem encoding
+
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SetFromWindowsErr</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">ierr</span></span><span class="sig-paren">)</span><br />
([[../../library/os#os|<code>os.fsdecode()</code>]]).</p></dd></dl>
+
 
 +
: 这是一个方便的函数来引发 [[../../library/exceptions#WindowsError|WindowsError]]。 如果使用 <span class="xref c c-texpr">0</span> ''ierr'' 调用,则使用调用 <code>GetLastError()</code> 返回的错误代码。 它调用 Win32 函数 <code>FormatMessage()</code> 来检索由 ''ierr'' 或 <code>GetLastError()</code> 给出的错误代码的 Windows 描述,然后构造一个元组对象,其第一项是 ''ierr'' 值,其第二项是对应的错误信息(取自 <code>FormatMessage()</code>),然后调用 <code>PyErr_SetObject(PyExc_WindowsError, object)</code>。 此函数始终返回 <code>NULL</code>。
 +
 
 +
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SetExcFromWindowsErr</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">ierr</span></span><span class="sig-paren">)</span><br />
 +
 
 +
: 类似于 [[#c.PyErr_SetFromWindowsErr|PyErr_SetFromWindowsErr()]],附加参数指定要引发的异常类型。
  
<dl>
+
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SetFromWindowsErrWithFilename</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">ierr</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">filename</span></span><span class="sig-paren">)</span><br />
<dt>[[../structures#c|PyObject]] *<code>PyErr_SetFromWindowsErr</code><span class="sig-paren">(</span>int ''ierr''<span class="sig-paren">)</span></dt>
 
<dd><p>''Return value: Always NULL.''</p>
 
<p>This is a convenience function to raise [[../../library/exceptions#WindowsError|<code>WindowsError</code>]]. If called with
 
''ierr'' of <span class="xref c c-texpr">0</span>, the error code returned by a call to <code>GetLastError()</code>
 
is used instead. It calls the Win32 function <code>FormatMessage()</code> to retrieve
 
the Windows description of error code given by ''ierr'' or <code>GetLastError()</code>,
 
then it constructs a tuple object whose first item is the ''ierr'' value and whose
 
second item is the corresponding error message (gotten from
 
<code>FormatMessage()</code>), and then calls <code>PyErr_SetObject(PyExc_WindowsError, object)</code>. This function always returns <code>NULL</code>.</p>
 
<p>[[../../library/intro#availability|<span class="std std-ref">Availability</span>]]: Windows.</p></dd></dl>
 
  
<dl>
+
: 类似于 <code>PyErr_SetFromWindowsErrWithFilenameObject()</code>,但文件名以 C 字符串形式给出。 ''filename'' 从文件系统编码 ([[../../library/os#os|os.fsdecode()]]) 解码。
<dt>[[../structures#c|PyObject]] *<code>PyErr_SetExcFromWindowsErr</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''type'', int ''ierr''<span class="sig-paren">)</span></dt>
 
<dd><p>''Return value: Always NULL.''</p>
 
<p>Similar to [[#c.PyErr_SetFromWindowsErr|<code>PyErr_SetFromWindowsErr()</code>]], with an additional parameter
 
specifying the exception type to be raised.</p>
 
<p>[[../../library/intro#availability|<span class="std std-ref">Availability</span>]]: Windows.</p></dd></dl>
 
  
<dl>
+
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SetExcFromWindowsErrWithFilenameObject</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">ierr</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">filename</span></span><span class="sig-paren">)</span><br />
<dt>[[../structures#c|PyObject]] *<code>PyErr_SetFromWindowsErrWithFilename</code><span class="sig-paren">(</span>int ''ierr'', ''const'' char *''filename''<span class="sig-paren">)</span></dt>
 
<dd><p>''Return value: Always NULL.''</p>
 
<p>Similar to <code>PyErr_SetFromWindowsErrWithFilenameObject()</code>, but the
 
filename is given as a C string. ''filename'' is decoded from the filesystem
 
encoding ([[../../library/os#os|<code>os.fsdecode()</code>]]).</p>
 
<p>[[../../library/intro#availability|<span class="std std-ref">Availability</span>]]: Windows.</p></dd></dl>
 
  
<dl>
+
: <code>PyErr_SetFromWindowsErrWithFilenameObject()</code> 类似,附加参数指定要引发的异常类型。
<dt>[[../structures#c|PyObject]] *<code>PyErr_SetExcFromWindowsErrWithFilenameObject</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''type'', int ''ierr'', [[../structures#c|PyObject]] *''filename''<span class="sig-paren">)</span></dt>
 
<dd><p>''Return value: Always NULL.''</p>
 
<p>Similar to <code>PyErr_SetFromWindowsErrWithFilenameObject()</code>, with an
 
additional parameter specifying the exception type to be raised.</p>
 
<p>[[../../library/intro#availability|<span class="std std-ref">Availability</span>]]: Windows.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyErr_SetExcFromWindowsErrWithFilenameObjects</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''type'', int ''ierr'', [[../structures#c|PyObject]] *''filename'', [[../structures#c|PyObject]] *''filename2''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SetExcFromWindowsErrWithFilenameObjects</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">ierr</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">filename</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">filename2</span></span><span class="sig-paren">)</span><br />
<dd><p>''Return value: Always NULL.''</p>
+
</dt>
<p>Similar to [[#c.PyErr_SetExcFromWindowsErrWithFilenameObject|<code>PyErr_SetExcFromWindowsErrWithFilenameObject()</code>]],
+
<dd><p>类似于 [[#c.PyErr_SetExcFromWindowsErrWithFilenameObject|PyErr_SetExcFromWindowsErrWithFilenameObject()]],但接受第二个文件名对象。</p>
but accepts a second filename object.</p>
 
<p>[[../../library/intro#availability|<span class="std std-ref">Availability</span>]]: Windows.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.4 新版功能.</span></p>
+
<p><span class="versionmodified added">3.4 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
<dl>
+
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SetExcFromWindowsErrWithFilename</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">ierr</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">filename</span></span><span class="sig-paren">)</span><br />
<dt>[[../structures#c|PyObject]] *<code>PyErr_SetExcFromWindowsErrWithFilename</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''type'', int ''ierr'', ''const'' char *''filename''<span class="sig-paren">)</span></dt>
+
 
<dd><p>''Return value: Always NULL.''</p>
+
: 类似于 [[#c.PyErr_SetFromWindowsErrWithFilename|PyErr_SetFromWindowsErrWithFilename()]],附加参数指定要引发的异常类型。
<p>Similar to [[#c.PyErr_SetFromWindowsErrWithFilename|<code>PyErr_SetFromWindowsErrWithFilename()</code>]], with an additional
 
parameter specifying the exception type to be raised.</p>
 
<p>[[../../library/intro#availability|<span class="std std-ref">Availability</span>]]: Windows.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyErr_SetImportError</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''msg'', [[../structures#c|PyObject]] *''name'', [[../structures#c|PyObject]] *''path''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SetImportError</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">msg</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">path</span></span><span class="sig-paren">)</span><br />
<dd><p>''Return value: Always NULL.''</p>
+
</dt>
<p>This is a convenience function to raise [[../../library/exceptions#ImportError|<code>ImportError</code>]]. ''msg'' will be
+
<dd><p>这是一个提高 [[../../library/exceptions#ImportError|ImportError]] 的便利函数。 ''msg'' 将被设置为异常的消息字符串。 ''name''''path'',两者都可以是<code>NULL</code>,将被设置为[[../../library/exceptions#ImportError|ImportError]]各自的<code>name</code><code>path</code> 属性。</p>
set as the exception's message string. ''name'' and ''path'', both of which can
 
be <code>NULL</code>, will be set as the [[../../library/exceptions#ImportError|<code>ImportError</code>]]'s respective <code>name</code>
 
and <code>path</code> attributes.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.3 新版功能.</span></p>
+
<p><span class="versionmodified added">3.3 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>void <code>PyErr_SyntaxLocationObject</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''filename'', int ''lineno'', int ''col_offset''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SyntaxLocationObject</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">filename</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">lineno</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">col_offset</span></span><span class="sig-paren">)</span><br />
<dd><p>Set file, line, and offset information for the current exception. If the
+
</dt>
current exception is not a [[../../library/exceptions#SyntaxError|<code>SyntaxError</code>]], then it sets additional
+
<dd><p>为当前异常设置文件、行和偏移量信息。 如果当前异常不是 [[../../library/exceptions#SyntaxError|SyntaxError]],则设置附加属性,使异常打印子系统认为异常是 [[../../library/exceptions#SyntaxError|SyntaxError]]</p>
attributes, which make the exception printing subsystem think the exception
 
is a [[../../library/exceptions#SyntaxError|<code>SyntaxError</code>]].</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.4 新版功能.</span></p>
+
<p><span class="versionmodified added">3.4 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>void <code>PyErr_SyntaxLocationEx</code><span class="sig-paren">(</span>''const'' char *''filename'', int ''lineno'', int ''col_offset''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SyntaxLocationEx</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">filename</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">lineno</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">col_offset</span></span><span class="sig-paren">)</span><br />
<dd><p>Like [[#c.PyErr_SyntaxLocationObject|<code>PyErr_SyntaxLocationObject()</code>]], but ''filename'' is a byte string
+
</dt>
decoded from the filesystem encoding ([[../../library/os#os|<code>os.fsdecode()</code>]]).</p>
+
<dd><p>类似于 [[#c.PyErr_SyntaxLocationObject|PyErr_SyntaxLocationObject()]],但 ''filename'' 是从文件系统编码 ([[../../library/os#os|os.fsdecode()]]) 解码的字节串。</p>
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.2 新版功能.</span></p>
+
<p><span class="versionmodified added">3.2 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
; void <code>PyErr_SyntaxLocation</code><span class="sig-paren">(</span>''const'' char *''filename'', int ''lineno''<span class="sig-paren">)</span>
+
; <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SyntaxLocation</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">filename</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">lineno</span></span><span class="sig-paren">)</span><br />
: Like [[#c.PyErr_SyntaxLocationEx|<code>PyErr_SyntaxLocationEx()</code>]], but the col_offset parameter is omitted.
 
  
; void <code>PyErr_BadInternalCall</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
: 类似于 [[#c.PyErr_SyntaxLocationEx|PyErr_SyntaxLocationEx()]],但省略了 col_offset 参数。
: This is a shorthand for <code>PyErr_SetString(PyExc_SystemError, message)</code>, where ''message'' indicates that an internal operation (e.g. a Python/C API function) was invoked with an illegal argument. It is mostly for internal use.
+
 
 +
; <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_BadInternalCall</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><br />
 +
 
 +
: 这是 <code>PyErr_SetString(PyExc_SystemError, message)</code> 的简写,其中 ''message'' 表示内部操作(例如 一个 Python/C API 函数)被非法参数调用。 它主要供内部使用。
  
  
第265行: 第182行:
 
<div id="issuing-warnings" class="section">
 
<div id="issuing-warnings" class="section">
  
== Issuing warnings ==
+
== 发出警告 ==
  
Use these functions to issue warnings from C code. They mirror similar
+
使用这些函数从 C 代码发出警告。 它们反映了 Python [[../../library/warnings#module-warnings|warnings]] 模块导出的类似函数。 他们通常会向 ''sys.stderr'' 打印警告消息; 然而,也有可能用户已指定将警告转换为错误,在这种情况下,它们将引发异常。 由于警告机制的问题,这些函数也可能引发异常。 如果未引发异常,则返回值为 <code>0</code>,如果引发异常,则返回值为 <code>-1</code>。 (无法确定是否实际打印了警告消息,也无法确定异常的原因是什么;这是故意的。)如果引发异常,调用者应该进行正常的异常处理(例如, [[../refcounting#c|Py_DECREF()]] 拥有引用并返回错误值)。
functions exported by the Python [[../../library/warnings#module-warnings|<code>warnings</code>]] module. They normally
 
print a warning message to ''sys.stderr''; however, it is
 
also possible that the user has specified that warnings are to be turned into
 
errors, and in that case they will raise an exception. It is also possible that
 
the functions raise an exception because of a problem with the warning machinery.
 
The return value is <code>0</code> if no exception is raised, or <code>-1</code> 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, [[../refcounting#c|<code>Py_DECREF()</code>]] owned references and return
 
an error value).
 
  
 
<dl>
 
<dl>
<dt>int <code>PyErr_WarnEx</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''category'', ''const'' char *''message'', Py_ssize_t ''stack_level''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_WarnEx</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">category</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">message</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">stack_level</span></span><span class="sig-paren">)</span><br />
<dd><p>Issue a warning message. The ''category'' argument is a warning category (see
+
</dt>
below) or <code>NULL</code>; the ''message'' argument is a UTF-8 encoded string. ''stack_level'' is a
+
<dd><p>发出警告消息。 ''category'' 参数是一个警告类别(见下文)或 <code>NULL</code>''message'' 参数是一个 UTF-8 编码的字符串。 ''stack_level'' 是一个正数,给出了堆栈帧的数量; 警告将从该堆栈帧中当前正在执行的代码行发出。 ''stack_level'' 1 是调用 [[#c.PyErr_WarnEx|PyErr_WarnEx()]] 的函数,2 是上面的函数,依此类推。</p>
positive number giving a number of stack frames; the warning will be issued from
+
<p>警告类别必须是 <code>PyExc_Warning</code> 的子类; <code>PyExc_Warning</code><code>PyExc_Exception</code>的子类; 默认警告类别为 <code>PyExc_RuntimeWarning</code>。 标准 Python 警告类别可用作全局变量,其名称在 [[#standardwarningcategories|标准警告类别]] 中枚举。</p>
the currently executing line of code in that stack frame. A ''stack_level'' of 1
+
<p>有关警告控制的信息,请参阅命令行文档中 [[../../library/warnings#module-warnings|warnings]] 模块和 [[../../using/cmdline#cmdoption-W|-W]] 选项的文档。 没有用于警告控制的 C API。</p></dd></dl>
is the function calling [[#c.PyErr_WarnEx|<code>PyErr_WarnEx()</code>]], 2 is the function above that,
 
and so forth.</p>
 
<p>Warning categories must be subclasses of <code>PyExc_Warning</code>;
 
<code>PyExc_Warning</code> is a subclass of <code>PyExc_Exception</code>;
 
the default warning category is <code>PyExc_RuntimeWarning</code>. The standard
 
Python warning categories are available as global variables whose names are
 
enumerated at [[#standardwarningcategories|<span class="std std-ref">Standard Warning Categories</span>]].</p>
 
<p>For information about warning control, see the documentation for the
 
[[../../library/warnings#module-warnings|<code>warnings</code>]] module and the [[../../using/cmdline#cmdoption-W|<code>-W</code>]] option in the command line
 
documentation. There is no C API for warning control.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyErr_SetImportErrorSubclass</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exception'', [[../structures#c|PyObject]] *''msg'', [[../structures#c|PyObject]] *''name'', [[../structures#c|PyObject]] *''path''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SetImportErrorSubclass</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exception</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">msg</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">path</span></span><span class="sig-paren">)</span><br />
<dd><p>''Return value: Always NULL.''</p>
+
</dt>
<p>Much like [[#c.PyErr_SetImportError|<code>PyErr_SetImportError()</code>]] but this function allows for
+
<dd><p>很像 [[#c.PyErr_SetImportError|PyErr_SetImportError()]] 但这个函数允许指定 [[../../library/exceptions#ImportError|ImportError]] 的子类来引发。</p>
specifying a subclass of [[../../library/exceptions#ImportError|<code>ImportError</code>]] to raise.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.6 新版功能.</span></p>
+
<p><span class="versionmodified added">3.6 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>int <code>PyErr_WarnExplicitObject</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''category'', [[../structures#c|PyObject]] *''message'', [[../structures#c|PyObject]] *''filename'', int ''lineno'', [[../structures#c|PyObject]] *''module'', [[../structures#c|PyObject]] *''registry''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_WarnExplicitObject</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">category</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">message</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">filename</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">lineno</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">module</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">registry</span></span><span class="sig-paren">)</span><br />
<dd><p>Issue a warning message with explicit control over all warning attributes. This
+
</dt>
is a straightforward wrapper around the Python function
+
<dd><p>发出警告消息,明确控制所有警告属性。 这是 Python 函数 [[../../library/warnings#warnings|warnings.warn_explicit()]] 的简单包装,请参阅此处了解更多信息。 ''module'' ''registry'' 参数可以设置为 <code>NULL</code> 以获得那里描述的默认效果。</p>
[[../../library/warnings#warnings|<code>warnings.warn_explicit()</code>]], see there for more information. The ''module''
 
and ''registry'' arguments may be set to <code>NULL</code> to get the default effect
 
described there.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.4 新版功能.</span></p>
+
<p><span class="versionmodified added">3.4 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
; int <code>PyErr_WarnExplicit</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''category'', ''const'' char *''message'', ''const'' char *''filename'', int ''lineno'', ''const'' char *''module'', [[../structures#c|PyObject]] *''registry''<span class="sig-paren">)</span>
+
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_WarnExplicit</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">category</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">message</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">filename</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">lineno</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">module</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">registry</span></span><span class="sig-paren">)</span><br />
: Similar to [[#c.PyErr_WarnExplicitObject|<code>PyErr_WarnExplicitObject()</code>]] except that ''message'' and ''module'' are UTF-8 encoded strings, and ''filename'' is decoded from the filesystem encoding ([[../../library/os#os|<code>os.fsdecode()</code>]]).
+
 
 +
: [[#c.PyErr_WarnExplicitObject|PyErr_WarnExplicitObject()]] 类似,除了 ''message'' ''module'' UTF-8 编码的字符串,而 ''filename'' 是从文件系统编码中解码出来的( [[../../library/os#os|os.fsdecode()]])
  
 
<dl>
 
<dl>
<dt>int <code>PyErr_WarnFormat</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''category'', Py_ssize_t ''stack_level'', ''const'' char *''format'', ...<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_WarnFormat</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">category</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">stack_level</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">format</span></span>, <span class="p"><span class="pre">...</span></span><span class="sig-paren">)</span><br />
<dd><p>Function similar to [[#c.PyErr_WarnEx|<code>PyErr_WarnEx()</code>]], but use
+
</dt>
[[../unicode#c|<code>PyUnicode_FromFormat()</code>]] to format the warning message. ''format'' is
+
<dd><p>函数类似于 [[#c.PyErr_WarnEx|PyErr_WarnEx()]],但使用 [[../unicode#c|PyUnicode_FromFormat()]] 来格式化警告消息。 ''format'' 是一个 ASCII 编码的字符串。</p>
an ASCII-encoded string.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.2 新版功能.</span></p>
+
<p><span class="versionmodified added">3.2 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>int <code>PyErr_ResourceWarning</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''source'', Py_ssize_t ''stack_level'', ''const'' char *''format'', ...<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_ResourceWarning</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">source</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">stack_level</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">format</span></span>, <span class="p"><span class="pre">...</span></span><span class="sig-paren">)</span><br />
<dd><p>Function similar to [[#c.PyErr_WarnFormat|<code>PyErr_WarnFormat()</code>]], but ''category'' is
+
</dt>
[[../../library/exceptions#ResourceWarning|<code>ResourceWarning</code>]] and it passes ''source'' to <code>warnings.WarningMessage()</code>.</p>
+
<dd><p>函数类似于 [[#c.PyErr_WarnFormat|PyErr_WarnFormat()]],但 ''category'' [[../../library/exceptions#ResourceWarning|ResourceWarning]] 并将 ''source'' 传递给 <code>warnings.WarningMessage()</code></p>
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.6 新版功能.</span></p>
+
<p><span class="versionmodified added">3.6 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
第349行: 第241行:
 
<div id="querying-the-error-indicator" class="section">
 
<div id="querying-the-error-indicator" class="section">
  
== Querying the error indicator ==
+
== 查询错误指示符 ==
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyErr_Occurred</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_Occurred</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><br />
<dd><p>''Return value: Borrowed reference.''</p>
+
</dt>
<p>Test whether the error indicator is set. If set, return the exception ''type''
+
<dd><p>测试是否设置了错误指示灯。 如果设置,则返回异常 ''type''(最后一次调用 <span class="xref c c-texpr">PyErr_Set*</span> 函数或 [[#c.PyErr_Restore|PyErr_Restore()]] 的第一个参数)。 如果未设置,则返回 <code>NULL</code>。 您不拥有对返回值的引用,因此您不需要 [[../refcounting#c|Py_DECREF()]] 它。</p>
(the first argument to the last call to one of the <span class="xref c c-texpr">PyErr_Set*</span>
+
<p>调用者必须持有 GIL。</p>
functions or to [[#c.PyErr_Restore|<code>PyErr_Restore()</code>]]). If not set, return <code>NULL</code>. You do not
 
own a reference to the return value, so you do not need to [[../refcounting#c|<code>Py_DECREF()</code>]]
 
it.</p>
 
<p>The caller must hold the GIL.</p>
 
 
<div class="admonition note">
 
<div class="admonition note">
  
<p>注解</p>
+
<p>笔记</p>
<p>Do not compare the return value to a specific exception; use
+
<p>不要将返回值与特定异常进行比较; 使用 [[#c.PyErr_ExceptionMatches|PyErr_ExceptionMatches()]] 代替,如下所示。 (比较可能很容易失败,因为异常可能是一个实例而不是一个类,在类异常的情况下,或者它可能是预期异常的子类。)</p>
[[#c.PyErr_ExceptionMatches|<code>PyErr_ExceptionMatches()</code>]] 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 be a subclass of the expected exception.)</p>
 
  
 
</div></dd></dl>
 
</div></dd></dl>
  
; int <code>PyErr_ExceptionMatches</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc''<span class="sig-paren">)</span>
+
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_ExceptionMatches</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span><span class="sig-paren">)</span><br />
: Equivalent to <code>PyErr_GivenExceptionMatches(PyErr_Occurred(), exc)</code>. This should only be called when an exception is actually set; a memory access violation will occur if no exception has been raised.
+
 
 +
: 相当于 <code>PyErr_GivenExceptionMatches(PyErr_Occurred(), exc)</code>。 这应该只在实际设置异常时调用; 如果没有引发异常,则会发生内存访问冲突。
 +
 
 +
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_GivenExceptionMatches</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">given</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span><span class="sig-paren">)</span><br />
  
; int <code>PyErr_GivenExceptionMatches</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''given'', [[../structures#c|PyObject]] *''exc''<span class="sig-paren">)</span>
+
: 如果 ''given'' 异常与 ''exc'' 中的异常类型匹配,则返回 true。 如果 ''exc'' 是一个类对象,当 ''given'' 是一个子类的实例时,这也返回真。 如果 ''exc'' 是元组,则元组中的所有异常类型(以及子元组中的递归)都将搜索匹配项。
: Return true if the ''given'' exception matches the exception type in ''exc''. If ''exc'' is a class object, this also returns true when ''given'' is an instance of a subclass. If ''exc'' is a tuple, all exception types in the tuple (and recursively in subtuples) are searched for a match.
 
  
 
<dl>
 
<dl>
<dt>void <code>PyErr_Fetch</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] **''ptype'', [[../structures#c|PyObject]] **''pvalue'', [[../structures#c|PyObject]] **''ptraceback''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_Fetch</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">ptype</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">pvalue</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">ptraceback</span></span><span class="sig-paren">)</span><br />
<dd><p>Retrieve the error indicator into three variables whose addresses are passed.
+
</dt>
If the error indicator is not set, set all three variables to <code>NULL</code>. If it is
+
<dd><p>将错误指示符检索到三个传递地址的变量中。 如果未设置错误指示器,请将所有三个变量设置为 <code>NULL</code>。 如果设置了它,它将被清除,并且您拥有对检索到的每个对象的引用。 即使类型对象不是,值和回溯对象也可能是 <code>NULL</code></p>
set, it will be cleared and you own a reference to each object retrieved. The
 
value and traceback object may be <code>NULL</code> even when the type object is not.</p>
 
 
<div class="admonition note">
 
<div class="admonition note">
  
<p>注解</p>
+
<p>笔记</p>
<p>This function is normally only used by code that needs to catch exceptions or
+
<p>该函数通常只被需要捕获异常的代码或需要临时保存和恢复错误指示器的代码使用,例如:</p>
by code that needs to save and restore the error indicator temporarily, e.g.:</p>
 
 
<div class="highlight-c notranslate">
 
<div class="highlight-c notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>{
+
<syntaxhighlight lang="c">{
 
   PyObject *type, *value, *traceback;
 
   PyObject *type, *value, *traceback;
   PyErr_Fetch(&amp;type, &amp;value, &amp;traceback);
+
   PyErr_Fetch(&type, &value, &traceback);
  
 
   /* ... code that might produce other errors ... */
 
   /* ... code that might produce other errors ... */
  
 
   PyErr_Restore(type, value, traceback);
 
   PyErr_Restore(type, value, traceback);
}</pre>
+
}</syntaxhighlight>
  
 
</div>
 
</div>
第407行: 第291行:
  
 
<dl>
 
<dl>
<dt>void <code>PyErr_Restore</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''type'', [[../structures#c|PyObject]] *''value'', [[../structures#c|PyObject]] *''traceback''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_Restore</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">value</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">traceback</span></span><span class="sig-paren">)</span><br />
<dd><p>Set the error indicator from the three objects. If the error indicator is
+
</dt>
already set, it is cleared first. If the objects are <code>NULL</code>, the error
+
<dd><p>从三个对象设置错误指示器。 如果错误指示器已设置,则首先将其清除。 如果对象为<code>NULL</code>,则清除错误指示符。 不要传递 <code>NULL</code> 类型和非 <code>NULL</code> 值或回溯。 异常类型应该是一个类。 不要传递无效的异常类型或值。 (违反这些规则会在以后导致一些微妙的问题。)这个调用会带走对每个对象的引用:在调用之前你必须拥有对每个对象的引用,在调用之后你不再拥有这些引用。 (如果您不明白这一点,请不要使用此功能。 我警告过你。)</p>
indicator is cleared. Do not pass a <code>NULL</code> type and non-<code>NULL</code> 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't understand this, don't use this function. I
 
warned you.)</p>
 
 
<div class="admonition note">
 
<div class="admonition note">
  
<p>注解</p>
+
<p>笔记</p>
<p>This function is normally only used by code that needs to save and restore the
+
<p>该函数通常只用于需要临时保存和恢复错误指示器的代码。 使用 [[#c.PyErr_Fetch|PyErr_Fetch()]] 保存当前错误指标。</p>
error indicator temporarily. Use [[#c.PyErr_Fetch|<code>PyErr_Fetch()</code>]] to save the current
 
error indicator.</p>
 
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>void <code>PyErr_NormalizeException</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] **''exc'', [[../structures#c|PyObject]] **''val'', [[../structures#c|PyObject]] **''tb''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_NormalizeException</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">val</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">tb</span></span><span class="sig-paren">)</span><br />
<dd><p>Under certain circumstances, the values returned by [[#c.PyErr_Fetch|<code>PyErr_Fetch()</code>]] below
+
</dt>
can be &quot;unnormalized&quot;, meaning that <code>*exc</code> is a class object but <code>*val</code> is
+
<dd><p>在某些情况下,下面的 [[#c.PyErr_Fetch|PyErr_Fetch()]] 返回的值可以是“非规范化的”,这意味着 <code>*exc</code> 是一个类对象但 <code>*val</code> 不是相同的实例班级。 在这种情况下,此函数可用于实例化类。 如果这些值已经标准化,则什么也不会发生。 实施延迟归一化以提高性能。</p>
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>
 
 
<div class="admonition note">
 
<div class="admonition note">
  
<p>注解</p>
+
<p>笔记</p>
<p>This function ''does not'' implicitly set the <code>__traceback__</code>
+
<p>此函数 ''不会'' 隐式设置异常值的 <code>__traceback__</code> 属性。 如果需要适当地设置回溯,则需要以下附加代码段:</p>
attribute on the exception value. If setting the traceback
 
appropriately is desired, the following additional snippet is needed:</p>
 
 
<div class="highlight-c notranslate">
 
<div class="highlight-c notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>if (tb != NULL) {
+
<syntaxhighlight lang="c">if (tb != NULL) {
 
   PyException_SetTraceback(val, tb);
 
   PyException_SetTraceback(val, tb);
}</pre>
+
}</syntaxhighlight>
  
 
</div>
 
</div>
第454行: 第324行:
  
 
<dl>
 
<dl>
<dt>void <code>PyErr_GetExcInfo</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] **''ptype'', [[../structures#c|PyObject]] **''pvalue'', [[../structures#c|PyObject]] **''ptraceback''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_GetExcInfo</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">ptype</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">pvalue</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">ptraceback</span></span><span class="sig-paren">)</span><br />
<dd><p>Retrieve the exception info, as known from <code>sys.exc_info()</code>. This refers
+
</dt>
to an exception that was ''already caught'', not to an exception that was
+
<dd><p><code>sys.exc_info()</code> 中检索异常信息。 这是指 ''已捕获'' 的异常,而不是新引发的异常。 返回三个对象的新引用,其中任何一个都可以是 <code>NULL</code>。 不修改异常信息状态。</p>
freshly raised. Returns new references for the three objects, any of which
 
may be <code>NULL</code>. Does not modify the exception info state.</p>
 
 
<div class="admonition note">
 
<div class="admonition note">
  
<p>注解</p>
+
<p>笔记</p>
<p>This function is not normally used by code that wants to handle exceptions.
+
<p>想要处理异常的代码通常不使用此函数。 相反,它可以在代码需要临时保存和恢复异常状态时使用。 使用 [[#c.PyErr_SetExcInfo|PyErr_SetExcInfo()]] 恢复或清除异常状态。</p>
Rather, it can be used when code needs to save and restore the exception
 
state temporarily. Use [[#c.PyErr_SetExcInfo|<code>PyErr_SetExcInfo()</code>]] to restore or clear the
 
exception state.</p>
 
  
 
</div>
 
</div>
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.3 新版功能.</span></p>
+
<p><span class="versionmodified added">3.3 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>void <code>PyErr_SetExcInfo</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''type'', [[../structures#c|PyObject]] *''value'', [[../structures#c|PyObject]] *''traceback''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SetExcInfo</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">value</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">traceback</span></span><span class="sig-paren">)</span><br />
<dd><p>Set the exception info, as known from <code>sys.exc_info()</code>. This refers
+
</dt>
to an exception that was ''already caught'', not to an exception that was
+
<dd><p>设置异常信息,如从 <code>sys.exc_info()</code> 所知。 这是指 ''已捕获'' 的异常,而不是新引发的异常。 此函数窃取参数的引用。 要清除异常状态,请为所有三个参数传递 <code>NULL</code>。 有关这三个参数的一般规则,请参阅 [[#c.PyErr_Restore|PyErr_Restore()]]</p>
freshly raised. This function steals the references of the arguments.
 
To clear the exception state, pass <code>NULL</code> for all three arguments.
 
For general rules about the three arguments, see [[#c.PyErr_Restore|<code>PyErr_Restore()</code>]].</p>
 
 
<div class="admonition note">
 
<div class="admonition note">
  
<p>注解</p>
+
<p>笔记</p>
<p>This function is not normally used by code that wants to handle exceptions.
+
<p>想要处理异常的代码通常不使用此函数。 相反,它可以在代码需要临时保存和恢复异常状态时使用。 使用 [[#c.PyErr_GetExcInfo|PyErr_GetExcInfo()]] 读取异常状态。</p>
Rather, it can be used when code needs to save and restore the exception
 
state temporarily. Use [[#c.PyErr_GetExcInfo|<code>PyErr_GetExcInfo()</code>]] to read the exception
 
state.</p>
 
  
 
</div>
 
</div>
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.3 新版功能.</span></p>
+
<p><span class="versionmodified added">3.3 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
第500行: 第359行:
 
<div id="signal-handling" class="section">
 
<div id="signal-handling" class="section">
  
== Signal Handling ==
+
== 信号处理 ==
 +
 
 +
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_CheckSignals</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><br />
  
; int <code>PyErr_CheckSignals</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
: 此函数与 Python 的信号处理交互。 它检查信号是否已发送到进程,如果是,则调用相应的信号处理程序。 如果支持 [[../../library/signal#module-signal|signal]] 模块,则可以调用用 Python 编写的信号处理程序。 在所有情况下,<code>SIGINT</code> 的默认效果是引发 [[../../library/exceptions#KeyboardInterrupt|KeyboardInterrupt]] 异常。 如果引发异常,则设置错误指示器并且函数返回 <code>-1</code>; 否则函数返回 <code>0</code>。 如果先前已设置,错误指示器可能会或可能不会被清除。
: This function interacts with Python's signal handling. It checks whether a signal has been sent to the processes and if so, invokes the corresponding signal handler. If the [[../../library/signal#module-signal|<code>signal</code>]] module is supported, this can invoke a signal handler written in Python. In all cases, the default effect for <code>SIGINT</code> is to raise the [[../../library/exceptions#KeyboardInterrupt|<code>KeyboardInterrupt</code>]] exception. If an exception is raised the error indicator is set and the function returns <code>-1</code>; otherwise the function returns <code>0</code>. The error indicator may or may not be cleared if it was previously set.
 
  
 
<dl>
 
<dl>
<dt>void <code>PyErr_SetInterrupt</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_SetInterrupt</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><br />
<dd><p>Simulate the effect of a <code>SIGINT</code> signal arriving. The next time
+
</dt>
[[#c.PyErr_CheckSignals|<code>PyErr_CheckSignals()</code>]] is called, the Python signal handler for
+
<dd><p>模拟 <code>SIGINT</code> 信号到达的效果。 下次调用 [[#c.PyErr_CheckSignals|PyErr_CheckSignals()]] 时,将调用 <code>SIGINT</code> 的 Python 信号处理程序。</p>
<code>SIGINT</code> will be called.</p>
+
<p>如果 <code>SIGINT</code> 没有被 Python 处理(它被设置为 [[../../library/signal#signal|signal.SIG_DFL]] [[../../library/signal#signal|signal.SIG_IGN]]),这个函数什么都不做。</p></dd></dl>
<p>If <code>SIGINT</code> isn't handled by Python (it was set to
 
[[../../library/signal#signal|<code>signal.SIG_DFL</code>]] or [[../../library/signal#signal|<code>signal.SIG_IGN</code>]]), this function does
 
nothing.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>int <code>PySignal_SetWakeupFd</code><span class="sig-paren">(</span>int ''fd''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PySignal_SetWakeupFd</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">fd</span></span><span class="sig-paren">)</span><br />
<dd><p>This utility function specifies a file descriptor to which the signal number
+
</dt>
is written as a single byte whenever a signal is received. ''fd'' must be
+
<dd><p>此实用程序函数指定一个文件描述符,每当接收到信号时,信号编号将作为单个字节写入该文件描述符。 ''fd'' 必须是非阻塞的。 它返回以前的此类文件描述符。</p>
non-blocking. It returns the previous such file descriptor.</p>
+
<p><code>-1</code> 禁用该功能; 这是初始状态。 这相当于 Python 中的 [[../../library/signal#signal|signal.set_wakeup_fd()]],但没有任何错误检查。 ''fd'' 应该是一个有效的文件描述符。 该函数只能从主线程调用。</p>
<p>The value <code>-1</code> disables the feature; this is the initial state.
 
This is equivalent to [[../../library/signal#signal|<code>signal.set_wakeup_fd()</code>]] in Python, but without any
 
error checking. ''fd'' should be a valid file descriptor. The function should
 
only be called from the main thread.</p>
 
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.5 版更改: </span>On Windows, the function now also supports socket handles.</p>
+
<p><span class="versionmodified changed"> 3.5 版更改: </span> Windows 上,该函数现在也支持套接字句柄。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
第533行: 第386行:
 
<div id="exception-classes" class="section">
 
<div id="exception-classes" class="section">
  
== Exception Classes ==
+
== 异常类 ==
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyErr_NewException</code><span class="sig-paren">(</span>''const'' char *''name'', [[../structures#c|PyObject]] *''base'', [[../structures#c|PyObject]] *''dict''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_NewException</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">base</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">dict</span></span><span class="sig-paren">)</span><br />
<dd><p>''Return value: New reference.''</p>
+
</dt>
<p>This utility function creates and returns a new exception class. The ''name''
+
<dd><p>此实用程序函数创建并返回一个新的异常类。 ''name'' 参数必须是新异常的名称,即 <code>module.classname</code> 形式的 C 字符串。 ''base'' ''dict'' 参数通常是 <code>NULL</code>。 这将创建一个派生自 [[../../library/exceptions#Exception|Exception]] 的类对象(在 C 中可作为 <code>PyExc_Exception</code> 访问)。</p>
argument must be the name of the new exception, a C string of the form
+
<p>新类的 <code>__module__</code> 属性设置为 ''name'' 参数的第一部分(直到最后一个点),类名设置为最后一部分(最后一个点之后)点)。 ''base'' 参数可用于指定替代基类; 它可以只是一个类,也可以是一组类。 ''dict'' 参数可用于指定类变量和方法的字典。</p></dd></dl>
<code>module.classname</code>. The ''base'' and ''dict'' arguments are normally <code>NULL</code>.
 
This creates a class object derived from [[../../library/exceptions#Exception|<code>Exception</code>]] (accessible in C as
 
<code>PyExc_Exception</code>).</p>
 
<p>The <code>__module__</code> attribute of the new class is set to the first part (up
 
to the last dot) of the ''name'' argument, and the class name is set to the last
 
part (after the last dot). The ''base'' argument can be used to specify alternate
 
base classes; it can either be only one class or a tuple of classes. The ''dict''
 
argument can be used to specify a dictionary of class variables and methods.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyErr_NewExceptionWithDoc</code><span class="sig-paren">(</span>''const'' char *''name'', ''const'' char *''doc'', [[../structures#c|PyObject]] *''base'', [[../structures#c|PyObject]] *''dict''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyErr_NewExceptionWithDoc</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">doc</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">base</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">dict</span></span><span class="sig-paren">)</span><br />
<dd><p>''Return value: New reference.''</p>
+
</dt>
<p>Same as [[#c.PyErr_NewException|<code>PyErr_NewException()</code>]], except that the new exception class can
+
<dd><p>[[#c.PyErr_NewException|PyErr_NewException()]] 相同,除了新的异常类可以很容易地被赋予一个文档字符串:如果 ''doc'' 是非 <code>NULL</code>,它将被用作文档字符串对于异常类。</p>
easily be given a docstring: If ''doc'' is non-<code>NULL</code>, it will be used as the
 
docstring for the exception class.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.2 新版功能.</span></p>
+
<p><span class="versionmodified added">3.2 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
第565行: 第408行:
 
<div id="exception-objects" class="section">
 
<div id="exception-objects" class="section">
  
== Exception Objects ==
+
== 异常对象 ==
 +
 
 +
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyException_GetTraceback</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">ex</span></span><span class="sig-paren">)</span><br />
 +
 
 +
: 返回与异常关联的回溯作为新引用,可通过 <code>__traceback__</code> 从 Python 访问。 如果没有关联回溯,则返回 <code>NULL</code>。
 +
 
 +
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyException_SetTraceback</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">ex</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">tb</span></span><span class="sig-paren">)</span><br />
 +
 
 +
: 将与异常关联的回溯设置为 ''tb''。 使用 <code>Py_None</code> 清除它。
  
<dl>
+
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyException_GetContext</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">ex</span></span><span class="sig-paren">)</span><br />
<dt>[[../structures#c|PyObject]] *<code>PyException_GetTraceback</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''ex''<span class="sig-paren">)</span></dt>
+
 
<dd><p>''Return value: New reference.''</p>
+
: 返回与异常关联的上下文(在其处理 ''ex'' 期间引发的另一个异常实例)作为新引用,可通过 <code>__context__</code> 从 Python 访问。 如果没有关联的上下文,则返回 <code>NULL</code>
<p>Return the traceback associated with the exception as a new reference, as
 
accessible from Python through <code>__traceback__</code>. If there is no
 
traceback associated, this returns <code>NULL</code>.</p></dd></dl>
 
  
; int <code>PyException_SetTraceback</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''ex'', [[../structures#c|PyObject]] *''tb''<span class="sig-paren">)</span>
+
; <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyException_SetContext</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">ex</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">ctx</span></span><span class="sig-paren">)</span><br />
: Set the traceback associated with the exception to ''tb''. Use <code>Py_None</code> to clear it.
 
  
<dl>
+
: 将与异常关联的上下文设置为 ''ctx''。 使用 <code>NULL</code> 清除它。 没有类型检查来确保 ''ctx'' 是一个异常实例。 这窃取了对 ''ctx'' 的引用。
<dt>[[../structures#c|PyObject]] *<code>PyException_GetContext</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''ex''<span class="sig-paren">)</span></dt>
 
<dd><p>''Return value: New reference.''</p>
 
<p>Return the context (another exception instance during whose handling ''ex'' was
 
raised) associated with the exception as a new reference, as accessible from
 
Python through <code>__context__</code>. If there is no context associated, this
 
returns <code>NULL</code>.</p></dd></dl>
 
  
; void <code>PyException_SetContext</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''ex'', [[../structures#c|PyObject]] *''ctx''<span class="sig-paren">)</span>
+
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyException_GetCause</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">ex</span></span><span class="sig-paren">)</span><br />
: Set the context associated with the exception to ''ctx''. Use <code>NULL</code> to clear it. There is no type check to make sure that ''ctx'' is an exception instance. This steals a reference to ''ctx''.
 
  
<dl>
+
: 返回与异常关联的原因(异常实例或 [[../../library/constants#None|None]],由 <code>raise ... from ...</code> 设置)作为新引用,可通过 <code>__cause__</code> 从 Python 访问。
<dt>[[../structures#c|PyObject]] *<code>PyException_GetCause</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''ex''<span class="sig-paren">)</span></dt>
 
<dd><p>''Return value: New reference.''</p>
 
<p>Return the cause (either an exception instance, or [[../../library/constants#None|<code>None</code>]],
 
set by <code>raise ... from ...</code>) associated with the exception as a new
 
reference, as accessible from Python through <code>__cause__</code>.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>void <code>PyException_SetCause</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''ex'', [[../structures#c|PyObject]] *''cause''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyException_SetCause</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">ex</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">cause</span></span><span class="sig-paren">)</span><br />
<dd><p>Set the cause associated with the exception to ''cause''. Use <code>NULL</code> to clear
+
</dt>
it. There is no type check to make sure that ''cause'' is either an exception
+
<dd><p>将与异常相关的原因设置为 ''cause''。 使用 <code>NULL</code> 清除它。 没有类型检查来确保 ''cause'' 是异常实例或 [[../../library/constants#None|None]]。 这窃取了对 ''cause'' 的引用。</p>
instance or [[../../library/constants#None|<code>None</code>]]. This steals a reference to ''cause''.</p>
+
<p><code>__suppress_context__</code> 被此功能隐式设置为 <code>True</code></p></dd></dl>
<p><code>__suppress_context__</code> is implicitly set to <code>True</code> by this function.</p></dd></dl>
 
  
  
第607行: 第441行:
  
 
<span id="unicodeexceptions"></span>
 
<span id="unicodeexceptions"></span>
== Unicode Exception Objects ==
+
== Unicode 异常对象 ==
  
The following functions are used to create and modify Unicode exceptions from C.
+
以下函数用于从 C 创建和修改 Unicode 异常。
  
<dl>
+
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeDecodeError_Create</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">encoding</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">object</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">length</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">start</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">end</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">reason</span></span><span class="sig-paren">)</span><br />
<dt>[[../structures#c|PyObject]] *<code>PyUnicodeDecodeError_Create</code><span class="sig-paren">(</span>''const'' char *''encoding'', ''const'' char *''object'', Py_ssize_t ''length'', Py_ssize_t ''start'', Py_ssize_t ''end'', ''const'' char *''reason''<span class="sig-paren">)</span></dt>
+
 
<dd><p>''Return value: New reference.''</p>
+
: 创建一个 [[../../library/exceptions#UnicodeDecodeError|UnicodeDecodeError]] 对象,其属性为 ''encoding''''object''''length''''start''、end[ X140X] 和 ''原因'' ''encoding'' ''reason'' UTF-8 编码的字符串。
<p>Create a [[../../library/exceptions#UnicodeDecodeError|<code>UnicodeDecodeError</code>]] object with the attributes ''encoding'',
 
''object'', ''length'', ''start'', ''end'' and ''reason''. ''encoding'' and ''reason'' are
 
UTF-8 encoded strings.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyUnicodeEncodeError_Create</code><span class="sig-paren">(</span>''const'' char *''encoding'', ''const'' [[../unicode#c|Py_UNICODE]] *''object'', Py_ssize_t ''length'', Py_ssize_t ''start'', Py_ssize_t ''end'', ''const'' char *''reason''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeEncodeError_Create</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">encoding</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span>[[../unicode#c|<span class="n"><span class="pre">Py_UNICODE</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">object</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">length</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">start</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">end</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">reason</span></span><span class="sig-paren">)</span><br />
<dd><p>''Return value: New reference.''</p>
+
</dt>
<p>Create a [[../../library/exceptions#UnicodeEncodeError|<code>UnicodeEncodeError</code>]] object with the attributes ''encoding'',
+
<dd><p>创建一个 [[../../library/exceptions#UnicodeEncodeError|UnicodeEncodeError]] 对象,其属性为 ''encoding''''object''''length''''start''、end[ X140X] 和 ''原因'' ''encoding'' ''reason'' UTF-8 编码的字符串。</p>
''object'', ''length'', ''start'', ''end'' and ''reason''. ''encoding'' and ''reason'' are
 
UTF-8 encoded strings.</p>
 
 
<div class="deprecated">
 
<div class="deprecated">
  
<p><span class="versionmodified deprecated">3.3 版后已移除: </span>3.11</p>
+
<p><span class="versionmodified deprecated"> 3.3 版起已弃用:</span>3.11</p>
<p><code>Py_UNICODE</code> is deprecated since Python 3.3. Please migrate to
+
<p><code>Py_UNICODE</code> Python 3.3 起已弃用。 请迁移到<code>PyObject_CallFunction(PyExc_UnicodeEncodeError, &quot;sOnns&quot;, ...)</code></p>
<code>PyObject_CallFunction(PyExc_UnicodeEncodeError, &quot;sOnns&quot;, ...)</code>.</p>
 
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyUnicodeTranslateError_Create</code><span class="sig-paren">(</span>''const'' [[../unicode#c|Py_UNICODE]] *''object'', Py_ssize_t ''length'', Py_ssize_t ''start'', Py_ssize_t ''end'', ''const'' char *''reason''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeTranslateError_Create</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span>[[../unicode#c|<span class="n"><span class="pre">Py_UNICODE</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">object</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">length</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">start</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">end</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">reason</span></span><span class="sig-paren">)</span><br />
<dd><p>''Return value: New reference.''</p>
+
</dt>
<p>Create a [[../../library/exceptions#UnicodeTranslateError|<code>UnicodeTranslateError</code>]] object with the attributes ''object'',
+
<dd><p>创建一个 [[../../library/exceptions#UnicodeTranslateError|UnicodeTranslateError]] 对象,其属性为 ''object''''length''''start''''end'' reason[ X144X]。 ''reason'' 是一个 UTF-8 编码的字符串。</p>
''length'', ''start'', ''end'' and ''reason''. ''reason'' is a UTF-8 encoded string.</p>
 
 
<div class="deprecated">
 
<div class="deprecated">
  
<p><span class="versionmodified deprecated">3.3 版后已移除: </span>3.11</p>
+
<p><span class="versionmodified deprecated"> 3.3 版起已弃用:</span>3.11</p>
<p><code>Py_UNICODE</code> is deprecated since Python 3.3. Please migrate to
+
<p><code>Py_UNICODE</code> Python 3.3 起已弃用。 请迁移到<code>PyObject_CallFunction(PyExc_UnicodeTranslateError, &quot;Onns&quot;, ...)</code></p>
<code>PyObject_CallFunction(PyExc_UnicodeTranslateError, &quot;Onns&quot;, ...)</code>.</p>
 
  
 
</div></dd></dl>
 
</div></dd></dl>
  
<dl>
+
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeDecodeError_GetEncoding</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span><span class="sig-paren">)</span><br />
<dt>[[../structures#c|PyObject]] *<code>PyUnicodeDecodeError_GetEncoding</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc''<span class="sig-paren">)</span><br />
+
<br />
[[../structures#c|PyObject]] *<code>PyUnicodeEncodeError_GetEncoding</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc''<span class="sig-paren">)</span></dt>
+
[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeEncodeError_GetEncoding</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span><span class="sig-paren">)</span><br />
<dd><p>''Return value: New reference.''</p>
+
 
<p>Return the ''encoding'' attribute of the given exception object.</p></dd></dl>
+
: 返回给定异常对象的 ''encoding'' 属性。
 +
 
 +
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeDecodeError_GetObject</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span><span class="sig-paren">)</span><br />
 +
<br />
 +
[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeEncodeError_GetObject</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span><span class="sig-paren">)</span><br />
 +
<br />
 +
[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeTranslateError_GetObject</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span><span class="sig-paren">)</span><br />
 +
 
 +
: 返回给定异常对象的 ''object'' 属性。
 +
 
 +
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeDecodeError_GetStart</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">start</span></span><span class="sig-paren">)</span><br />
 +
<br />
 +
<span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeEncodeError_GetStart</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">start</span></span><span class="sig-paren">)</span><br />
 +
<br />
 +
<span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeTranslateError_GetStart</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">start</span></span><span class="sig-paren">)</span><br />
 +
 
 +
: 获取给定异常对象的 ''start'' 属性并将其放入 ''*start''。 ''start'' 不能是 <code>NULL</code>。 成功返回 <code>0</code>,失败返回 <code>-1</code>。
 +
 
 +
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeDecodeError_SetStart</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">start</span></span><span class="sig-paren">)</span><br />
 +
<br />
 +
<span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeEncodeError_SetStart</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">start</span></span><span class="sig-paren">)</span><br />
 +
<br />
 +
<span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeTranslateError_SetStart</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">start</span></span><span class="sig-paren">)</span><br />
 +
 
 +
: 将给定异常对象的 ''start'' 属性设置为 ''start''。 成功返回 <code>0</code>,失败返回 <code>-1</code>。
 +
 
 +
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeDecodeError_GetEnd</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">end</span></span><span class="sig-paren">)</span><br />
 +
<br />
 +
<span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeEncodeError_GetEnd</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">end</span></span><span class="sig-paren">)</span><br />
 +
<br />
 +
<span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeTranslateError_GetEnd</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">end</span></span><span class="sig-paren">)</span><br />
  
<dl>
+
: 获取给定异常对象的 ''end'' 属性并将其放入 ''*end''。 ''end'' 不能是 <code>NULL</code>。 成功返回 <code>0</code>,失败返回 <code>-1</code>
<dt>[[../structures#c|PyObject]] *<code>PyUnicodeDecodeError_GetObject</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc''<span class="sig-paren">)</span><br />
 
[[../structures#c|PyObject]] *<code>PyUnicodeEncodeError_GetObject</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc''<span class="sig-paren">)</span><br />
 
[[../structures#c|PyObject]] *<code>PyUnicodeTranslateError_GetObject</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc''<span class="sig-paren">)</span></dt>
 
<dd><p>''Return value: New reference.''</p>
 
<p>Return the ''object'' attribute of the given exception object.</p></dd></dl>
 
  
; int <code>PyUnicodeDecodeError_GetStart</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc'', Py_ssize_t *''start''<span class="sig-paren">)</span><br />
+
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeDecodeError_SetEnd</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">end</span></span><span class="sig-paren">)</span><br />
int <code>PyUnicodeEncodeError_GetStart</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc'', Py_ssize_t *''start''<span class="sig-paren">)</span><br />
+
<br />
int <code>PyUnicodeTranslateError_GetStart</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc'', Py_ssize_t *''start''<span class="sig-paren">)</span>
+
<span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeEncodeError_SetEnd</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">end</span></span><span class="sig-paren">)</span><br />
: Get the ''start'' attribute of the given exception object and place it into ''*start''. ''start'' must not be <code>NULL</code>. Return <code>0</code> on success, <code>-1</code> on failure.
+
<br />
 +
<span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeTranslateError_SetEnd</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">end</span></span><span class="sig-paren">)</span><br />
  
; int <code>PyUnicodeDecodeError_SetStart</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc'', Py_ssize_t ''start''<span class="sig-paren">)</span><br />
+
: 将给定异常对象的 ''end'' 属性设置为 ''end''。 成功返回 <code>0</code>,失败返回 <code>-1</code>
int <code>PyUnicodeEncodeError_SetStart</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc'', Py_ssize_t ''start''<span class="sig-paren">)</span><br />
 
int <code>PyUnicodeTranslateError_SetStart</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc'', Py_ssize_t ''start''<span class="sig-paren">)</span>
 
: Set the ''start'' attribute of the given exception object to ''start''. Return <code>0</code> on success, <code>-1</code> on failure.
 
  
; int <code>PyUnicodeDecodeError_GetEnd</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc'', Py_ssize_t *''end''<span class="sig-paren">)</span><br />
+
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeDecodeError_GetReason</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span><span class="sig-paren">)</span><br />
int <code>PyUnicodeEncodeError_GetEnd</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc'', Py_ssize_t *''end''<span class="sig-paren">)</span><br />
+
<br />
int <code>PyUnicodeTranslateError_GetEnd</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc'', Py_ssize_t *''end''<span class="sig-paren">)</span>
+
[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeEncodeError_GetReason</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span><span class="sig-paren">)</span><br />
: Get the ''end'' attribute of the given exception object and place it into ''*end''. ''end'' must not be <code>NULL</code>. Return <code>0</code> on success, <code>-1</code> on failure.
+
<br />
 +
[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeTranslateError_GetReason</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span><span class="sig-paren">)</span><br />
  
; int <code>PyUnicodeDecodeError_SetEnd</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc'', Py_ssize_t ''end''<span class="sig-paren">)</span><br />
+
: 返回给定异常对象的 ''reason'' 属性。
int <code>PyUnicodeEncodeError_SetEnd</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc'', Py_ssize_t ''end''<span class="sig-paren">)</span><br />
 
int <code>PyUnicodeTranslateError_SetEnd</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc'', Py_ssize_t ''end''<span class="sig-paren">)</span>
 
: Set the ''end'' attribute of the given exception object to ''end''. Return <code>0</code> on success, <code>-1</code> on failure.
 
  
<dl>
+
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeDecodeError_SetReason</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">reason</span></span><span class="sig-paren">)</span><br />
<dt>[[../structures#c|PyObject]] *<code>PyUnicodeDecodeError_GetReason</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc''<span class="sig-paren">)</span><br />
+
<br />
[[../structures#c|PyObject]] *<code>PyUnicodeEncodeError_GetReason</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc''<span class="sig-paren">)</span><br />
+
<span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeEncodeError_SetReason</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">reason</span></span><span class="sig-paren">)</span><br />
[[../structures#c|PyObject]] *<code>PyUnicodeTranslateError_GetReason</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc''<span class="sig-paren">)</span></dt>
+
<br />
<dd><p>''Return value: New reference.''</p>
+
<span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyUnicodeTranslateError_SetReason</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">reason</span></span><span class="sig-paren">)</span><br />
<p>Return the ''reason'' attribute of the given exception object.</p></dd></dl>
 
  
; int <code>PyUnicodeDecodeError_SetReason</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc'', ''const'' char *''reason''<span class="sig-paren">)</span><br />
+
: 将给定异常对象的 ''reason'' 属性设置为 ''reason''。 成功返回 <code>0</code>,失败返回 <code>-1</code>
int <code>PyUnicodeEncodeError_SetReason</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc'', ''const'' char *''reason''<span class="sig-paren">)</span><br />
 
int <code>PyUnicodeTranslateError_SetReason</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''exc'', ''const'' char *''reason''<span class="sig-paren">)</span>
 
: Set the ''reason'' attribute of the given exception object to ''reason''. Return <code>0</code> on success, <code>-1</code> on failure.
 
  
  
第695行: 第538行:
  
 
<span id="recursion"></span>
 
<span id="recursion"></span>
== Recursion Control ==
+
== 递归控制 ==
  
These two functions provide a way to perform safe recursive calls at the C
+
这两个函数提供了一种在核心和扩展模块中在 C 级别执行安全递归调用的方法。 如果递归代码不一定调用 Python 代码(自动跟踪其递归深度),则需要它们。 ''tp_call'' 实现也不需要它们,因为 [[../call#call|调用协议]] 负责递归处理。
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).
 
They are also not needed for ''tp_call'' implementations
 
because the [[../call#call|<span class="std std-ref">call protocol</span>]] takes care of recursion handling.
 
  
 
<dl>
 
<dl>
<dt>int <code>Py_EnterRecursiveCall</code><span class="sig-paren">(</span>''const'' char *''where''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_EnterRecursiveCall</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">where</span></span><span class="sig-paren">)</span><br />
<dd><p>Marks a point where a recursive C-level call is about to be performed.</p>
+
</dt>
<p>If <code>USE_STACKCHECK</code> is defined, this function checks if the OS
+
<dd><p>标记将要执行递归 C 级调用的点。</p>
stack overflowed using [[../sys#c|<code>PyOS_CheckStack()</code>]]. In this is the case, it
+
<p>如果定义了 <code>USE_STACKCHECK</code>,则此函数使用 [[../sys#c|PyOS_CheckStack()]] 检查操作系统堆栈是否溢出。 在这种情况下,它设置 [[../../library/exceptions#MemoryError|MemoryError]] 并返回一个非零值。</p>
sets a [[../../library/exceptions#MemoryError|<code>MemoryError</code>]] and returns a nonzero value.</p>
+
<p>然后该函数检查是否达到递归限制。 如果是这种情况,则设置 [[../../library/exceptions#RecursionError|RecursionError]] 并返回非零值。 否则,返回零。</p>
<p>The function then checks if the recursion limit is reached. If this is the
+
<p>''where'' 应该是 UTF-8 编码的字符串,例如 <code>&quot; in instance check&quot;</code> 连接到由递归深度限制引起的 [[../../library/exceptions#RecursionError|RecursionError]] 消息。</p>
case, a [[../../library/exceptions#RecursionError|<code>RecursionError</code>]] is set and a nonzero value is returned.
 
Otherwise, zero is returned.</p>
 
<p>''where'' should be a UTF-8 encoded string such as <code>&quot; in instance check&quot;</code> to
 
be concatenated to the [[../../library/exceptions#RecursionError|<code>RecursionError</code>]] message caused by the recursion
 
depth limit.</p>
 
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.9 版更改: </span>This function is now also available in the limited API.</p>
+
<p><span class="versionmodified changed"> 3.9 版本变更: </span> 此功能现在也可以在有限的 API 中使用。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>void <code>Py_LeaveRecursiveCall</code><span class="sig-paren">(</span>void<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_LeaveRecursiveCall</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="sig-paren">)</span><br />
<dd><p>Ends a [[#c.Py_EnterRecursiveCall|<code>Py_EnterRecursiveCall()</code>]]. Must be called once for each
+
</dt>
''successful'' invocation of [[#c.Py_EnterRecursiveCall|<code>Py_EnterRecursiveCall()</code>]].</p>
+
<dd><p>结束 [[#c.Py_EnterRecursiveCall|Py_EnterRecursiveCall()]]。 对于 [[#c.Py_EnterRecursiveCall|Py_EnterRecursiveCall()]] 的每次 ''successful'' 调用,必须调用一次。</p>
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.9 版更改: </span>This function is now also available in the limited API.</p>
+
<p><span class="versionmodified changed"> 3.9 版本变更: </span> 此功能现在也可以在有限的 API 中使用。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
Properly implementing [[../typeobj#c.PyTypeObject|<code>tp_repr</code>]] for container types requires
+
为容器类型正确实现 [[../typeobj#c.PyTypeObject|tp_repr]] 需要特殊的递归处理。 [[../typeobj#c.PyTypeObject|tp_repr]]除了保护栈,还需要跟踪对象,防止循环。 以下两个函数有助于实现此功能。 实际上,这些是等效于 [[../../library/reprlib#reprlib|reprlib.recursive_repr()]] 的 C。
special recursion handling. In addition to protecting the stack,
 
[[../typeobj#c.PyTypeObject|<code>tp_repr</code>]] also needs to track objects to prevent cycles. The
 
following two functions facilitate this functionality. Effectively,
 
these are the C equivalent to [[../../library/reprlib#reprlib|<code>reprlib.recursive_repr()</code>]].
 
  
 
<dl>
 
<dl>
<dt>int <code>Py_ReprEnter</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''object''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_ReprEnter</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">object</span></span><span class="sig-paren">)</span><br />
<dd><p>Called at the beginning of the [[../typeobj#c.PyTypeObject|<code>tp_repr</code>]] implementation to
+
</dt>
detect cycles.</p>
+
<dd><p>[[../typeobj#c.PyTypeObject|tp_repr]] 实现的开头调用以检测循环。</p>
<p>If the object has already been processed, the function returns a
+
<p>如果对象已经被处理,函数返回一个正整数。 在这种情况下,[[../typeobj#c.PyTypeObject|tp_repr]] 实现应该返回一个字符串对象,指示一个循环。 例如,[[../../library/stdtypes#dict|dict]] 对象返回 <code>{...}</code> [[../../library/stdtypes#list|list]] 对象返回 <code>[...]</code></p>
positive integer. In that case the [[../typeobj#c.PyTypeObject|<code>tp_repr</code>]] implementation
+
<p>如果达到递归限制,该函数将返回一个负整数。 在这种情况下,[[../typeobj#c.PyTypeObject|tp_repr]] 实现通常应该返回 <code>NULL</code></p>
should return a string object indicating a cycle. As examples,
+
<p>否则,函数返回零,[[../typeobj#c.PyTypeObject|tp_repr]] 实现可以正常继续。</p></dd></dl>
[[../../library/stdtypes#dict|<code>dict</code>]] objects return <code>{...}</code> and [[../../library/stdtypes#list|<code>list</code>]] objects
+
 
return <code>[...]</code>.</p>
+
; <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_ReprLeave</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">object</span></span><span class="sig-paren">)</span><br />
<p>The function will return a negative integer if the recursion limit
 
is reached. In that case the [[../typeobj#c.PyTypeObject|<code>tp_repr</code>]] implementation should
 
typically return <code>NULL</code>.</p>
 
<p>Otherwise, the function returns zero and the [[../typeobj#c.PyTypeObject|<code>tp_repr</code>]]
 
implementation can continue normally.</p></dd></dl>
 
  
; void <code>Py_ReprLeave</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''object''<span class="sig-paren">)</span>
+
: 结束 [[#c.Py_ReprEnter|Py_ReprEnter()]]。 每次调用返回零的 [[#c.Py_ReprEnter|Py_ReprEnter()]] 时必须调用一次。
: Ends a [[#c.Py_ReprEnter|<code>Py_ReprEnter()</code>]]. Must be called once for each invocation of [[#c.Py_ReprEnter|<code>Py_ReprEnter()</code>]] that returns zero.
 
  
  
第761行: 第584行:
  
 
<span id="standardexceptions"></span>
 
<span id="standardexceptions"></span>
== Standard Exceptions ==
+
== 标准例外 ==
  
All standard Python exceptions are available as global variables whose names are
+
所有标准 Python 异常都可用作全局变量,其名称为 <code>PyExc_</code> 后跟 Python 异常名称。 它们的类型为 <span class="xref c c-texpr">PyObject*</span>; 它们都是类对象。 为了完整起见,以下是所有变量:
<code>PyExc_</code> followed by the Python exception name. These have the type
 
<span class="xref c c-texpr">[[../structures#c|PyObject]]*</span>; they are all class objects. For completeness, here are all
 
the variables:
 
  
 
{|
 
{|
!width="49%"| C Name
+
!width="49%"| 姓名
!width="39%"| Python Name
+
!width="39%"| 蟒蛇名称
!width="12%"| Notes
+
!width="12%"| 笔记
 
|-
 
|-
 
| <code>PyExc_BaseException</code>
 
| <code>PyExc_BaseException</code>
第988行: 第808行:
 
<div class="versionadded">
 
<div class="versionadded">
  
<span class="versionmodified added">3.3 新版功能: </span><code>PyExc_BlockingIOError</code>, <code>PyExc_BrokenPipeError</code>,
+
<span class="versionmodified added">3.3 新功能:</span><code>PyExc_BlockingIOError</code><code>PyExc_BrokenPipeError</code><code>PyExc_ChildProcessError</code><code>PyExc_ConnectionError</code><code>PyExc_ConnectionAbortedError</code>、[ X88X]、<code>PyExc_ConnectionResetError</code><code>PyExc_FileExistsError</code><code>PyExc_FileNotFoundError</code><code>PyExc_InterruptedError</code><code>PyExc_IsADirectoryError</code><code>PyExc_NotADirectoryError</code>、[X17X] ]、<code>PyExc_ProcessLookupError</code> <code>PyExc_TimeoutError</code> 是在 <span id="index-4" class="target"></span>[https://www.python.org/dev/peps/pep-3151 PEP 3151] 之后引入的。
<code>PyExc_ChildProcessError</code>, <code>PyExc_ConnectionError</code>,
 
<code>PyExc_ConnectionAbortedError</code>, <code>PyExc_ConnectionRefusedError</code>,
 
<code>PyExc_ConnectionResetError</code>, <code>PyExc_FileExistsError</code>,
 
<code>PyExc_FileNotFoundError</code>, <code>PyExc_InterruptedError</code>,
 
<code>PyExc_IsADirectoryError</code>, <code>PyExc_NotADirectoryError</code>,
 
<code>PyExc_PermissionError</code>, <code>PyExc_ProcessLookupError</code>
 
and <code>PyExc_TimeoutError</code> were introduced following <span id="index-4" class="target"></span>[https://www.python.org/dev/peps/pep-3151 '''PEP 3151'''].
 
  
  
第1,001行: 第814行:
 
<div class="versionadded">
 
<div class="versionadded">
  
<span class="versionmodified added">3.5 新版功能: </span><code>PyExc_StopAsyncIteration</code> and <code>PyExc_RecursionError</code>.
+
<span class="versionmodified added">3.5 版新增:</span><code>PyExc_StopAsyncIteration</code> <code>PyExc_RecursionError</code>
  
  
第1,007行: 第820行:
 
<div class="versionadded">
 
<div class="versionadded">
  
<span class="versionmodified added">3.6 新版功能: </span><code>PyExc_ModuleNotFoundError</code>.
+
<span class="versionmodified added">3.6 版新增:</span><code>PyExc_ModuleNotFoundError</code>
  
  
 
</div>
 
</div>
These are compatibility aliases to <code>PyExc_OSError</code>:
+
这些是 <code>PyExc_OSError</code> 的兼容性别名:
  
 
{|
 
{|
!width="79%"| C Name
+
!width="79%"| 姓名
!width="21%"| Notes
+
!width="21%"| 笔记
 
|-
 
|-
 
| <code>PyExc_EnvironmentError</code>
 
| <code>PyExc_EnvironmentError</code>
第1,029行: 第842行:
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<span class="versionmodified changed">3.3 版更改: </span>These aliases used to be separate exception types.
+
<span class="versionmodified changed"> 3.3 版更改: </span> 这些别名曾经是单独的异常类型。
  
  
 
</div>
 
</div>
Notes:
+
笔记:
  
# This is a base class for other standard exceptions.
+
# 这是其他标准异常的基类。
# Only defined on Windows; protect code that uses this by testing that the preprocessor macro <code>MS_WINDOWS</code> is defined.
+
# 仅在 Windows 上定义; 通过测试预处理器宏 <code>MS_WINDOWS</code> 的定义来保护使用它的代码。
  
  
第1,043行: 第856行:
  
 
<span id="standardwarningcategories"></span>
 
<span id="standardwarningcategories"></span>
== Standard Warning Categories ==
+
== 标准警告类别 ==
  
All standard Python warning categories are available as global variables whose
+
所有标准 Python 警告类别都可用作全局变量,其名称为 <code>PyExc_</code> 后跟 Python 异常名称。 它们的类型为 <span class="xref c c-texpr">PyObject*</span>; 它们都是类对象。 为了完整起见,以下是所有变量:
names are <code>PyExc_</code> followed by the Python exception name. These have the type
 
<span class="xref c c-texpr">[[../structures#c|PyObject]]*</span>; they are all class objects. For completeness, here are all
 
the variables:
 
  
 
{|
 
{|
!width="49%"| C Name
+
!width="49%"| 姓名
!width="39%"| Python Name
+
!width="39%"| 蟒蛇名称
!width="12%"| Notes
+
!width="12%"| 笔记
 
|-
 
|-
 
| <code>PyExc_Warning</code>
 
| <code>PyExc_Warning</code>
第1,102行: 第912行:
 
<div class="versionadded">
 
<div class="versionadded">
  
<span class="versionmodified added">3.2 新版功能: </span><code>PyExc_ResourceWarning</code>.
+
<span class="versionmodified added">3.2 版新增:</span><code>PyExc_ResourceWarning</code>
  
  
 
</div>
 
</div>
Notes:
+
笔记:
  
# This is a base class for other standard warning categories.
+
# 这是其他标准警告类别的基类。
  
  
 
</div>
 
</div>
 +
 +
</div>
 +
<div class="clearer">
 +
 +
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:50的最新版本

异常处理

本章中描述的函数将让您处理和引发 Python 异常。 了解 Python 异常处理的一些基础知识很重要。 它的工作方式有点像 POSIX errno 变量:上次发生的错误有一个全局指示器(每个线程)。 大多数 C API 函数在成功时不会清除它,但会设置它以指示失败时错误的原因。 大多数 C API 函数也返回一个错误指示符,通常是 NULL 如果它们应该返回一个指针,或者 -1 如果它们返回一个整数(例外:PyArg_*函数返回 1 表示成功,返回 0 表示失败)。

具体来说,错误指示器由三个对象指针组成:异常类型、异常值和回溯对象。 如果未设置,这些指针中的任何一个都可以是 NULL(尽管禁止某些组合,例如,如果异常类型是 [X175X,则不能有非 NULL 回溯) ])。

当一个函数因为它调用的某个函数失败而必须失败时,它通常不设置错误指示器; 它调用的函数已经设置了它。 它负责处理错误和清除异常,或者在清理它持有的任何资源(例如对象引用或内存分配)后返回; 如果它不准备处理错误,它应该 not 继续正常。 如果由于错误而返回,则向调用者表明已设置错误很重要。 如果错误没有得到处理或小心传播,对 Python/C API 的额外调用可能不会按预期运行,并且可能会以神秘的方式失败。

笔记

错误指示符是 不是 sys.exc_info() 的结果。 前者对应于尚未捕获的异常(因此仍在传播),而后者在捕获后返回异常(因此已停止传播)。


打印和清算

void PyErr_Clear()
清除错误指示器。 如果未设置错误指示器,则没有效果。
void PyErr_PrintEx(int set_sys_last_vars)

将标准回溯打印到 sys.stderr 并清除错误指示器。 除非 错误是 SystemExit,在这种情况下,不会打印回溯,Python 进程将以 SystemExit 实例指定的错误代码退出。

设置错误指示器时,仅调用此函数 ' 。 否则会导致致命错误!

如果 set_sys_last_vars 不为零,则变量 sys.last_typesys.last_valuesys.last_traceback 将被设置为类型、值和打印异常的回溯,分别。

void PyErr_Print()
PyErr_PrintEx(1) 的别名。
void PyErr_WriteUnraisable(PyObject *obj)

使用当前异常和 obj 参数调用 sys.unraisablehook()

当设置了异常但解释器不可能实际引发异常时,此实用程序函数会向 sys.stderr 打印一条警告消息。 例如,在 __del__() 方法中发生异常时使用。

使用单个参数 obj 调用该函数,该参数标识发生不可引发异常的上下文。 如果可能,obj 的 repr 将打印在警告消息中。

调用此函数时必须设置异常。


引发异常

这些函数帮助您设置当前线程的错误指示器。 为方便起见,其中一些函数将始终返回 NULL 指针以用于 return 语句。

void PyErr_SetString(PyObject *type, const char *message)
这是设置错误指示器的最常用方法。 第一个参数指定异常类型; 它通常是标准例外之一,例如 PyExc_RuntimeError。 你不需要增加它的引用计数。 第二个参数是错误信息; 它是从 'utf-8' 解码的。
void PyErr_SetObject(PyObject *type, PyObject *value)
此函数类似于 PyErr_SetString() 但允许您为异常的“值”指定任意 Python 对象。
PyObject *PyErr_Format(PyObject *exception, const char *format, ...)
该函数设置错误指示器并返回NULLexception 应该是一个 Python 异常类。 format 和后续参数帮助格式化错误信息; 它们与 PyUnicode_FromFormat() 具有相同的含义和值。 format 是一个 ASCII 编码的字符串。
PyObject *PyErr_FormatV(PyObject *exception, const char *format, va_list vargs)

PyErr_Format() 相同,但采用 va_list 参数而不是可变数量的参数。

3.5 版中的新功能。

void PyErr_SetNone(PyObject *type)
这是 PyErr_SetObject(type, Py_None) 的简写。
int PyErr_BadArgument()
这是 PyErr_SetString(PyExc_TypeError, message) 的简写,其中 message 表示使用非法参数调用了内置操作。 它主要供内部使用。
PyObject *PyErr_NoMemory()
这是 PyErr_SetNone(PyExc_MemoryError) 的简写; 它返回 NULL,因此对象分配函数可以在内存不足时写入 return PyErr_NoMemory();
PyObject *PyErr_SetFromErrno(PyObject *type)
这是一个方便的函数,可以在 C 库函数返回错误并设置 C 变量 errno 时引发异常。 它构造一个元组对象,其第一项是整数 errno 值,第二项是相应的错误消息(从 strerror() 中获取),然后调用 PyErr_SetObject(type, object)。 在 Unix 上,当 errno 值为 EINTR 时,表示系统调用中断,这将调用 PyErr_CheckSignals(),如果设置了错误指示符,则将其设置为那。 该函数始终返回 NULL,因此系统调用周围的包装函数可以在系统调用返回错误时写入 return PyErr_SetFromErrno(type);
PyObject *PyErr_SetFromErrnoWithFilenameObject(PyObject *type, PyObject *filenameObject)
PyErr_SetFromErrno() 类似,附加的行为是如果 filenameObject 不是 NULL,它会作为一个传递给 type 的构造函数第三个参数。 在 OSError 异常的情况下,这用于定义异常实例的 filename 属性。
PyObject *PyErr_SetFromErrnoWithFilenameObjects(PyObject *type, PyObject *filenameObject, PyObject *filenameObject2)

类似于 PyErr_SetFromErrnoWithFilenameObject(),但需要第二个文件名对象,用于在需要两个文件名的函数失败时引发错误。

3.4 版中的新功能。

PyObject *PyErr_SetFromErrnoWithFilename(PyObject *type, const char *filename)
类似于 PyErr_SetFromErrnoWithFilenameObject(),但文件名以 C 字符串形式给出。 filename 从文件系统编码 (os.fsdecode()) 解码。
PyObject *PyErr_SetFromWindowsErr(int ierr)
这是一个方便的函数来引发 WindowsError。 如果使用 0ierr 调用,则使用调用 GetLastError() 返回的错误代码。 它调用 Win32 函数 FormatMessage() 来检索由 ierrGetLastError() 给出的错误代码的 Windows 描述,然后构造一个元组对象,其第一项是 ierr 值,其第二项是对应的错误信息(取自 FormatMessage()),然后调用 PyErr_SetObject(PyExc_WindowsError, object)。 此函数始终返回 NULL
PyObject *PyErr_SetExcFromWindowsErr(PyObject *type, int ierr)
类似于 PyErr_SetFromWindowsErr(),附加参数指定要引发的异常类型。
PyObject *PyErr_SetFromWindowsErrWithFilename(int ierr, const char *filename)
类似于 PyErr_SetFromWindowsErrWithFilenameObject(),但文件名以 C 字符串形式给出。 filename 从文件系统编码 (os.fsdecode()) 解码。
PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(PyObject *type, int ierr, PyObject *filename)
PyErr_SetFromWindowsErrWithFilenameObject() 类似,附加参数指定要引发的异常类型。
PyObject *PyErr_SetExcFromWindowsErrWithFilenameObjects(PyObject *type, int ierr, PyObject *filename, PyObject *filename2)

类似于 PyErr_SetExcFromWindowsErrWithFilenameObject(),但接受第二个文件名对象。

3.4 版中的新功能。

PyObject *PyErr_SetExcFromWindowsErrWithFilename(PyObject *type, int ierr, const char *filename)
类似于 PyErr_SetFromWindowsErrWithFilename(),附加参数指定要引发的异常类型。
PyObject *PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)

这是一个提高 ImportError 的便利函数。 msg 将被设置为异常的消息字符串。 namepath,两者都可以是NULL,将被设置为ImportError各自的namepath 属性。

3.3 版中的新功能。

void PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)

为当前异常设置文件、行和偏移量信息。 如果当前异常不是 SyntaxError,则设置附加属性,使异常打印子系统认为异常是 SyntaxError

3.4 版中的新功能。

void PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)

类似于 PyErr_SyntaxLocationObject(),但 filename 是从文件系统编码 (os.fsdecode()) 解码的字节串。

3.2 版中的新功能。

void PyErr_SyntaxLocation(const char *filename, int lineno)
类似于 PyErr_SyntaxLocationEx(),但省略了 col_offset 参数。
void PyErr_BadInternalCall()
这是 PyErr_SetString(PyExc_SystemError, message) 的简写,其中 message 表示内部操作(例如 一个 Python/C API 函数)被非法参数调用。 它主要供内部使用。


发出警告

使用这些函数从 C 代码发出警告。 它们反映了 Python warnings 模块导出的类似函数。 他们通常会向 sys.stderr 打印警告消息; 然而,也有可能用户已指定将警告转换为错误,在这种情况下,它们将引发异常。 由于警告机制的问题,这些函数也可能引发异常。 如果未引发异常,则返回值为 0,如果引发异常,则返回值为 -1。 (无法确定是否实际打印了警告消息,也无法确定异常的原因是什么;这是故意的。)如果引发异常,调用者应该进行正常的异常处理(例如, Py_DECREF() 拥有引用并返回错误值)。

int PyErr_WarnEx(PyObject *category, const char *message, Py_ssize_t stack_level)

发出警告消息。 category 参数是一个警告类别(见下文)或 NULLmessage 参数是一个 UTF-8 编码的字符串。 stack_level 是一个正数,给出了堆栈帧的数量; 警告将从该堆栈帧中当前正在执行的代码行发出。 stack_level 为 1 是调用 PyErr_WarnEx() 的函数,2 是上面的函数,依此类推。

警告类别必须是 PyExc_Warning 的子类; PyExc_WarningPyExc_Exception的子类; 默认警告类别为 PyExc_RuntimeWarning。 标准 Python 警告类别可用作全局变量,其名称在 标准警告类别 中枚举。

有关警告控制的信息,请参阅命令行文档中 warnings 模块和 -W 选项的文档。 没有用于警告控制的 C API。

PyObject *PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg, PyObject *name, PyObject *path)

很像 PyErr_SetImportError() 但这个函数允许指定 ImportError 的子类来引发。

3.6 版中的新功能。

int PyErr_WarnExplicitObject(PyObject *category, PyObject *message, PyObject *filename, int lineno, PyObject *module, PyObject *registry)

发出警告消息,明确控制所有警告属性。 这是 Python 函数 warnings.warn_explicit() 的简单包装,请参阅此处了解更多信息。 moduleregistry 参数可以设置为 NULL 以获得那里描述的默认效果。

3.4 版中的新功能。

int PyErr_WarnExplicit(PyObject *category, const char *message, const char *filename, int lineno, const char *module, PyObject *registry)
PyErr_WarnExplicitObject() 类似,除了 messagemodule 是 UTF-8 编码的字符串,而 filename 是从文件系统编码中解码出来的( os.fsdecode())。
int PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level, const char *format, ...)

函数类似于 PyErr_WarnEx(),但使用 PyUnicode_FromFormat() 来格式化警告消息。 format 是一个 ASCII 编码的字符串。

3.2 版中的新功能。

int PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level, const char *format, ...)

函数类似于 PyErr_WarnFormat(),但 categoryResourceWarning 并将 source 传递给 warnings.WarningMessage()

3.6 版中的新功能。


查询错误指示符

PyObject *PyErr_Occurred()

测试是否设置了错误指示灯。 如果设置,则返回异常 type(最后一次调用 PyErr_Set* 函数或 PyErr_Restore() 的第一个参数)。 如果未设置,则返回 NULL。 您不拥有对返回值的引用,因此您不需要 Py_DECREF() 它。

调用者必须持有 GIL。

笔记

不要将返回值与特定异常进行比较; 使用 PyErr_ExceptionMatches() 代替,如下所示。 (比较可能很容易失败,因为异常可能是一个实例而不是一个类,在类异常的情况下,或者它可能是预期异常的子类。)

int PyErr_ExceptionMatches(PyObject *exc)
相当于 PyErr_GivenExceptionMatches(PyErr_Occurred(), exc)。 这应该只在实际设置异常时调用; 如果没有引发异常,则会发生内存访问冲突。
int PyErr_GivenExceptionMatches(PyObject *given, PyObject *exc)
如果 given 异常与 exc 中的异常类型匹配,则返回 true。 如果 exc 是一个类对象,当 given 是一个子类的实例时,这也返回真。 如果 exc 是元组,则元组中的所有异常类型(以及子元组中的递归)都将搜索匹配项。
void PyErr_Fetch(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback)

将错误指示符检索到三个传递地址的变量中。 如果未设置错误指示器,请将所有三个变量设置为 NULL。 如果设置了它,它将被清除,并且您拥有对检索到的每个对象的引用。 即使类型对象不是,值和回溯对象也可能是 NULL

笔记

该函数通常只被需要捕获异常的代码或需要临时保存和恢复错误指示器的代码使用,例如:

{
   PyObject *type, *value, *traceback;
   PyErr_Fetch(&type, &value, &traceback);

   /* ... code that might produce other errors ... */

   PyErr_Restore(type, value, traceback);
}
void PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)

从三个对象设置错误指示器。 如果错误指示器已设置,则首先将其清除。 如果对象为NULL,则清除错误指示符。 不要传递 NULL 类型和非 NULL 值或回溯。 异常类型应该是一个类。 不要传递无效的异常类型或值。 (违反这些规则会在以后导致一些微妙的问题。)这个调用会带走对每个对象的引用:在调用之前你必须拥有对每个对象的引用,在调用之后你不再拥有这些引用。 (如果您不明白这一点,请不要使用此功能。 我警告过你。)

笔记

该函数通常只用于需要临时保存和恢复错误指示器的代码。 使用 PyErr_Fetch() 保存当前错误指标。

void PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)

在某些情况下,下面的 PyErr_Fetch() 返回的值可以是“非规范化的”,这意味着 *exc 是一个类对象但 *val 不是相同的实例班级。 在这种情况下,此函数可用于实例化类。 如果这些值已经标准化,则什么也不会发生。 实施延迟归一化以提高性能。

笔记

此函数 不会 隐式设置异常值的 __traceback__ 属性。 如果需要适当地设置回溯,则需要以下附加代码段:

if (tb != NULL) {
  PyException_SetTraceback(val, tb);
}
void PyErr_GetExcInfo(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback)

sys.exc_info() 中检索异常信息。 这是指 已捕获 的异常,而不是新引发的异常。 返回三个对象的新引用,其中任何一个都可以是 NULL。 不修改异常信息状态。

笔记

想要处理异常的代码通常不使用此函数。 相反,它可以在代码需要临时保存和恢复异常状态时使用。 使用 PyErr_SetExcInfo() 恢复或清除异常状态。

3.3 版中的新功能。

void PyErr_SetExcInfo(PyObject *type, PyObject *value, PyObject *traceback)

设置异常信息,如从 sys.exc_info() 所知。 这是指 已捕获 的异常,而不是新引发的异常。 此函数窃取参数的引用。 要清除异常状态,请为所有三个参数传递 NULL。 有关这三个参数的一般规则,请参阅 PyErr_Restore()

笔记

想要处理异常的代码通常不使用此函数。 相反,它可以在代码需要临时保存和恢复异常状态时使用。 使用 PyErr_GetExcInfo() 读取异常状态。

3.3 版中的新功能。


信号处理

int PyErr_CheckSignals()
此函数与 Python 的信号处理交互。 它检查信号是否已发送到进程,如果是,则调用相应的信号处理程序。 如果支持 signal 模块,则可以调用用 Python 编写的信号处理程序。 在所有情况下,SIGINT 的默认效果是引发 KeyboardInterrupt 异常。 如果引发异常,则设置错误指示器并且函数返回 -1; 否则函数返回 0。 如果先前已设置,错误指示器可能会或可能不会被清除。
void PyErr_SetInterrupt()

模拟 SIGINT 信号到达的效果。 下次调用 PyErr_CheckSignals() 时,将调用 SIGINT 的 Python 信号处理程序。

如果 SIGINT 没有被 Python 处理(它被设置为 signal.SIG_DFLsignal.SIG_IGN),这个函数什么都不做。

int PySignal_SetWakeupFd(int fd)

此实用程序函数指定一个文件描述符,每当接收到信号时,信号编号将作为单个字节写入该文件描述符。 fd 必须是非阻塞的。 它返回以前的此类文件描述符。

-1 禁用该功能; 这是初始状态。 这相当于 Python 中的 signal.set_wakeup_fd(),但没有任何错误检查。 fd 应该是一个有效的文件描述符。 该函数只能从主线程调用。

3.5 版更改: 在 Windows 上,该函数现在也支持套接字句柄。


异常类

PyObject *PyErr_NewException(const char *name, PyObject *base, PyObject *dict)

此实用程序函数创建并返回一个新的异常类。 name 参数必须是新异常的名称,即 module.classname 形式的 C 字符串。 basedict 参数通常是 NULL。 这将创建一个派生自 Exception 的类对象(在 C 中可作为 PyExc_Exception 访问)。

新类的 __module__ 属性设置为 name 参数的第一部分(直到最后一个点),类名设置为最后一部分(最后一个点之后)点)。 base 参数可用于指定替代基类; 它可以只是一个类,也可以是一组类。 dict 参数可用于指定类变量和方法的字典。

PyObject *PyErr_NewExceptionWithDoc(const char *name, const char *doc, PyObject *base, PyObject *dict)

PyErr_NewException() 相同,除了新的异常类可以很容易地被赋予一个文档字符串:如果 doc 是非 NULL,它将被用作文档字符串对于异常类。

3.2 版中的新功能。


异常对象

PyObject *PyException_GetTraceback(PyObject *ex)
返回与异常关联的回溯作为新引用,可通过 __traceback__ 从 Python 访问。 如果没有关联回溯,则返回 NULL
int PyException_SetTraceback(PyObject *ex, PyObject *tb)
将与异常关联的回溯设置为 tb。 使用 Py_None 清除它。
PyObject *PyException_GetContext(PyObject *ex)
返回与异常关联的上下文(在其处理 ex 期间引发的另一个异常实例)作为新引用,可通过 __context__ 从 Python 访问。 如果没有关联的上下文,则返回 NULL
void PyException_SetContext(PyObject *ex, PyObject *ctx)
将与异常关联的上下文设置为 ctx。 使用 NULL 清除它。 没有类型检查来确保 ctx 是一个异常实例。 这窃取了对 ctx 的引用。
PyObject *PyException_GetCause(PyObject *ex)
返回与异常关联的原因(异常实例或 None,由 raise ... from ... 设置)作为新引用,可通过 __cause__ 从 Python 访问。
void PyException_SetCause(PyObject *ex, PyObject *cause)

将与异常相关的原因设置为 cause。 使用 NULL 清除它。 没有类型检查来确保 cause 是异常实例或 None。 这窃取了对 cause 的引用。

__suppress_context__ 被此功能隐式设置为 True


Unicode 异常对象

以下函数用于从 C 创建和修改 Unicode 异常。

PyObject *PyUnicodeDecodeError_Create(const char *encoding, const char *object, Py_ssize_t length, Py_ssize_t start, Py_ssize_t end, const char *reason)
创建一个 UnicodeDecodeError 对象,其属性为 encodingobjectlengthstart、end[ X140X] 和 原因encodingreason 是 UTF-8 编码的字符串。
PyObject *PyUnicodeEncodeError_Create(const char *encoding, const Py_UNICODE *object, Py_ssize_t length, Py_ssize_t start, Py_ssize_t end, const char *reason)

创建一个 UnicodeEncodeError 对象,其属性为 encodingobjectlengthstart、end[ X140X] 和 原因encodingreason 是 UTF-8 编码的字符串。

自 3.3 版起已弃用:3.11

Py_UNICODE 自 Python 3.3 起已弃用。 请迁移到PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnns", ...)

PyObject *PyUnicodeTranslateError_Create(const Py_UNICODE *object, Py_ssize_t length, Py_ssize_t start, Py_ssize_t end, const char *reason)

创建一个 UnicodeTranslateError 对象,其属性为 objectlengthstartend 和 reason[ X144X]。 reason 是一个 UTF-8 编码的字符串。

自 3.3 版起已弃用:3.11

Py_UNICODE 自 Python 3.3 起已弃用。 请迁移到PyObject_CallFunction(PyExc_UnicodeTranslateError, "Onns", ...)

PyObject *PyUnicodeDecodeError_GetEncoding(PyObject *exc)


PyObject *PyUnicodeEncodeError_GetEncoding(PyObject *exc)

返回给定异常对象的 encoding 属性。
PyObject *PyUnicodeDecodeError_GetObject(PyObject *exc)


PyObject *PyUnicodeEncodeError_GetObject(PyObject *exc)

PyObject *PyUnicodeTranslateError_GetObject(PyObject *exc)

返回给定异常对象的 object 属性。
int PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)


int PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)

int PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)

获取给定异常对象的 start 属性并将其放入 *startstart 不能是 NULL。 成功返回 0,失败返回 -1
int PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)


int PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)

int PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)

将给定异常对象的 start 属性设置为 start。 成功返回 0,失败返回 -1
int PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)


int PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)

int PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *end)

获取给定异常对象的 end 属性并将其放入 *endend 不能是 NULL。 成功返回 0,失败返回 -1
int PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)


int PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)

int PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)

将给定异常对象的 end 属性设置为 end。 成功返回 0,失败返回 -1
PyObject *PyUnicodeDecodeError_GetReason(PyObject *exc)


PyObject *PyUnicodeEncodeError_GetReason(PyObject *exc)

PyObject *PyUnicodeTranslateError_GetReason(PyObject *exc)

返回给定异常对象的 reason 属性。
int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)


int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)

int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)

将给定异常对象的 reason 属性设置为 reason。 成功返回 0,失败返回 -1


递归控制

这两个函数提供了一种在核心和扩展模块中在 C 级别执行安全递归调用的方法。 如果递归代码不一定调用 Python 代码(自动跟踪其递归深度),则需要它们。 tp_call 实现也不需要它们,因为 调用协议 负责递归处理。

int Py_EnterRecursiveCall(const char *where)

标记将要执行递归 C 级调用的点。

如果定义了 USE_STACKCHECK,则此函数使用 PyOS_CheckStack() 检查操作系统堆栈是否溢出。 在这种情况下,它设置 MemoryError 并返回一个非零值。

然后该函数检查是否达到递归限制。 如果是这种情况,则设置 RecursionError 并返回非零值。 否则,返回零。

where 应该是 UTF-8 编码的字符串,例如 " in instance check" 连接到由递归深度限制引起的 RecursionError 消息。

3.9 版本变更: 此功能现在也可以在有限的 API 中使用。

void Py_LeaveRecursiveCall(void)

结束 Py_EnterRecursiveCall()。 对于 Py_EnterRecursiveCall() 的每次 successful 调用,必须调用一次。

3.9 版本变更: 此功能现在也可以在有限的 API 中使用。

为容器类型正确实现 tp_repr 需要特殊的递归处理。 tp_repr除了保护栈,还需要跟踪对象,防止循环。 以下两个函数有助于实现此功能。 实际上,这些是等效于 reprlib.recursive_repr() 的 C。

int Py_ReprEnter(PyObject *object)

tp_repr 实现的开头调用以检测循环。

如果对象已经被处理,函数返回一个正整数。 在这种情况下,tp_repr 实现应该返回一个字符串对象,指示一个循环。 例如,dict 对象返回 {...}list 对象返回 [...]

如果达到递归限制,该函数将返回一个负整数。 在这种情况下,tp_repr 实现通常应该返回 NULL

否则,函数返回零,tp_repr 实现可以正常继续。

void Py_ReprLeave(PyObject *object)
结束 Py_ReprEnter()。 每次调用返回零的 Py_ReprEnter() 时必须调用一次。


标准例外

所有标准 Python 异常都可用作全局变量,其名称为 PyExc_ 后跟 Python 异常名称。 它们的类型为 PyObject*; 它们都是类对象。 为了完整起见,以下是所有变量:

姓名 蟒蛇名称 笔记
PyExc_BaseException BaseException (1)
PyExc_Exception Exception (1)
PyExc_ArithmeticError ArithmeticError (1)
PyExc_AssertionError AssertionError
PyExc_AttributeError AttributeError
PyExc_BlockingIOError BlockingIOError
PyExc_BrokenPipeError BrokenPipeError
PyExc_BufferError BufferError
PyExc_ChildProcessError ChildProcessError
PyExc_ConnectionAbortedError ConnectionAbortedError
PyExc_ConnectionError ConnectionError
PyExc_ConnectionRefusedError ConnectionRefusedError
PyExc_ConnectionResetError ConnectionResetError
PyExc_EOFError EOFError
PyExc_FileExistsError FileExistsError
PyExc_FileNotFoundError FileNotFoundError
PyExc_FloatingPointError FloatingPointError
PyExc_GeneratorExit GeneratorExit
PyExc_ImportError ImportError
PyExc_IndentationError IndentationError
PyExc_IndexError IndexError
PyExc_InterruptedError InterruptedError
PyExc_IsADirectoryError IsADirectoryError
PyExc_KeyError KeyError
PyExc_KeyboardInterrupt KeyboardInterrupt
PyExc_LookupError LookupError (1)
PyExc_MemoryError MemoryError
PyExc_ModuleNotFoundError ModuleNotFoundError
PyExc_NameError NameError
PyExc_NotADirectoryError NotADirectoryError
PyExc_NotImplementedError NotImplementedError
PyExc_OSError OSError (1)
PyExc_OverflowError OverflowError
PyExc_PermissionError PermissionError
PyExc_ProcessLookupError ProcessLookupError
PyExc_RecursionError RecursionError
PyExc_ReferenceError ReferenceError (2)
PyExc_RuntimeError RuntimeError
PyExc_StopAsyncIteration StopAsyncIteration
PyExc_StopIteration StopIteration
PyExc_SyntaxError SyntaxError
PyExc_SystemError SystemError
PyExc_SystemExit SystemExit
PyExc_TabError TabError
PyExc_TimeoutError TimeoutError
PyExc_TypeError TypeError
PyExc_UnboundLocalError UnboundLocalError
PyExc_UnicodeDecodeError UnicodeDecodeError
PyExc_UnicodeEncodeError UnicodeEncodeError
PyExc_UnicodeError UnicodeError
PyExc_UnicodeTranslateError UnicodeTranslateError
PyExc_ValueError ValueError
PyExc_ZeroDivisionError ZeroDivisionError

3.3 新功能:PyExc_BlockingIOErrorPyExc_BrokenPipeErrorPyExc_ChildProcessErrorPyExc_ConnectionErrorPyExc_ConnectionAbortedError、[ X88X]、PyExc_ConnectionResetErrorPyExc_FileExistsErrorPyExc_FileNotFoundErrorPyExc_InterruptedErrorPyExc_IsADirectoryErrorPyExc_NotADirectoryError、[X17X] ]、PyExc_ProcessLookupErrorPyExc_TimeoutError 是在 PEP 3151 之后引入的。


3.5 版新增:PyExc_StopAsyncIterationPyExc_RecursionError


3.6 版新增:PyExc_ModuleNotFoundError


这些是 PyExc_OSError 的兼容性别名:

姓名 笔记
PyExc_EnvironmentError
PyExc_IOError
PyExc_WindowsError (3)

3.3 版更改: 这些别名曾经是单独的异常类型。


笔记:

  1. 这是其他标准异常的基类。
  2. 仅在 Windows 上定义; 通过测试预处理器宏 MS_WINDOWS 的定义来保护使用它的代码。


标准警告类别

所有标准 Python 警告类别都可用作全局变量,其名称为 PyExc_ 后跟 Python 异常名称。 它们的类型为 PyObject*; 它们都是类对象。 为了完整起见,以下是所有变量:

姓名 蟒蛇名称 笔记
PyExc_Warning Warning (1)
PyExc_BytesWarning BytesWarning
PyExc_DeprecationWarning DeprecationWarning
PyExc_FutureWarning FutureWarning
PyExc_ImportWarning ImportWarning
PyExc_PendingDeprecationWarning PendingDeprecationWarning
PyExc_ResourceWarning ResourceWarning
PyExc_RuntimeWarning RuntimeWarning
PyExc_SyntaxWarning SyntaxWarning
PyExc_UnicodeWarning UnicodeWarning
PyExc_UserWarning UserWarning

3.2 版新增:PyExc_ResourceWarning


笔记:

  1. 这是其他标准警告类别的基类。