@@ -323,6 +323,8 @@ msgid ""
323323"make the type usable as a base class. We'll create a new module, "
324324":mod:`!custom2` that adds these capabilities:"
325325msgstr ""
326+ "让我们通过添加一些数据和方法来扩展这个基本示例。 让我们再使该类型可以作为基类使用。 我们将创建一个新模块 :mod:`!custom2` "
327+ "来添加这些功能:"
326328
327329#: ../../extending/newtypes_tutorial.rst:236
328330msgid "This version of the module has a number of changes."
@@ -335,6 +337,8 @@ msgid ""
335337"strings containing first and last names. The *number* attribute is a C "
336338"integer."
337339msgstr ""
340+ "现在 :class:`!Custom` 类型的 C 结构体中有三个数据属性,*first*、*last* 和 *number*。 其中 *first* "
341+ "和 *last* 变量是包含名字和姓氏的 Python 字符串。 *number* 属性是一个 C 整数。"
338342
339343#: ../../extending/newtypes_tutorial.rst:242
340344msgid "The object structure is updated accordingly::"
@@ -360,6 +364,10 @@ msgid ""
360364"object's type might not be :class:`!CustomType`, because the object may be "
361365"an instance of a subclass."
362366msgstr ""
367+ "此方法会先清空两个 Python 属性的引用计数。 :c:func:`Py_XDECREF` 可以正确处理参数为 ``NULL`` 的情况(这可能在 "
368+ "``tp_new`` 中途失败时发生)。 随后它将调用对象类型的 :c:member:`~PyTypeObject.tp_free` 成员(通过 "
369+ "``Py_TYPE(self)`` 计算得到)来释放对象的内存。 请注意对象类型可以不是 "
370+ ":class:`!CustomType`,因为对象可能是一个子类的实例。"
363371
364372#: ../../extending/newtypes_tutorial.rst:275
365373msgid ""
@@ -394,6 +402,10 @@ msgid ""
394402" initialize the ``first`` and ``last`` attributes to non-``NULL`` default "
395403"values."
396404msgstr ""
405+ "``tp_new`` 处理句柄负责创建(而不是初始化)该类型的对象。 它在 Python 中被暴露为 :meth:`~object.__new__` "
406+ "方法。 它不需要定义 ``tp_new`` 成员,实际上许多扩展类型会简单地重用 :c:func:`PyType_GenericNew`,就像上面 "
407+ ":class:`!Custom` 类型的第一个版本所做的那样。 在此情况下,我们使用 ``tp_new`` 处理句柄来将 ``first`` 和 "
408+ "``last`` 属性初始化为非 ``NULL`` 的默认值。"
397409
398410#: ../../extending/newtypes_tutorial.rst:317
399411msgid ""
@@ -453,6 +465,12 @@ msgid ""
453465" may not be able to create instances of such subclasses without getting a "
454466":exc:`TypeError`.)"
455467msgstr ""
468+ "如果您要创建一个协作式 :c:member:`~PyTypeObject.tp_new` (它会调用基类型的 "
469+ ":c:member:`~PyTypeObject.tp_new` 或 :meth:`~object.__new__`),那么你 *不能* "
470+ "在运行时尝试使用方法解析顺序来确定要调用的方法。 必须总是静态地确定你要调用的类型,并直接调用它的 "
471+ ":c:member:`~PyTypeObject.tp_new`,或是通过 ``type->tp_base->tp_new``。 "
472+ "如果你不这样做,你的类型的同样继承自其它由 Python 定义的类的 Python 子类可能无法正常工作。 "
473+ "(具体地说,你可能无法创建这样的子类的实例而是会引发 :exc:`TypeError`。)"
456474
457475#: ../../extending/newtypes_tutorial.rst:353
458476msgid ""
0 commit comments