@@ -11,7 +11,7 @@ msgid ""
1111msgstr ""
1212"Project-Id-Version : Python 3.11\n "
1313"Report-Msgid-Bugs-To : \n "
14- "POT-Creation-Date : 2023-09-01 14:43 +0000\n "
14+ "POT-Creation-Date : 2023-09-22 14:44 +0000\n "
1515"PO-Revision-Date : 2023-05-24 02:10+0000\n "
1616"
Last-Translator :
Rafael Fontenelle <[email protected] >, 2023\n "
1717"Language-Team : Chinese (China) (https://app.transifex.com/python-doc/teams/5390/zh_CN/)\n "
@@ -265,12 +265,14 @@ msgid ""
265265"to the appropriate default values, including :c:member:`~PyObject.ob_type` "
266266"that we initially set to ``NULL``. ::"
267267msgstr ""
268+ "这将初始化 :class:`!Custom` 类型,为一些成员填充适当的默认值,包括我们在初始时设为 ``NULL`` 的 "
269+ ":c:member:`~PyObject.ob_type`。 ::"
268270
269271#: ../../extending/newtypes_tutorial.rst:190
270272msgid ""
271273"This adds the type to the module dictionary. This allows us to create "
272274":class:`!Custom` instances by calling the :class:`!Custom` class:"
273- msgstr ""
275+ msgstr "这将把类型添加到模块字典中。 这样我们就能通过调用 :class:`!Custom` 类来创建 :class:`!Custom` 实例: "
274276
275277#: ../../extending/newtypes_tutorial.rst:198
276278msgid ""
@@ -321,6 +323,8 @@ msgid ""
321323"make the type usable as a base class. We'll create a new module, "
322324":mod:`!custom2` that adds these capabilities:"
323325msgstr ""
326+ "让我们通过添加一些数据和方法来扩展这个基本示例。 让我们再使该类型可以作为基类使用。 我们将创建一个新模块 :mod:`!custom2` "
327+ "来添加这些功能:"
324328
325329#: ../../extending/newtypes_tutorial.rst:240
326330msgid "This version of the module has a number of changes."
@@ -343,6 +347,8 @@ msgid ""
343347"strings containing first and last names. The *number* attribute is a C "
344348"integer."
345349msgstr ""
350+ "现在 :class:`!Custom` 类型的 C 结构体中有三个数据属性,*first*、*last* 和 *number*。 其中 *first* "
351+ "和 *last* 变量是包含名字和姓氏的 Python 字符串。 *number* 属性是一个 C 整数。"
346352
347353#: ../../extending/newtypes_tutorial.rst:253
348354msgid "The object structure is updated accordingly::"
@@ -368,6 +374,10 @@ msgid ""
368374"object's type might not be :class:`!CustomType`, because the object may be "
369375"an instance of a subclass."
370376msgstr ""
377+ "此方法会先清空两个 Python 属性的引用计数。 :c:func:`Py_XDECREF` 可以正确处理参数为 ``NULL`` 的情况(这可能在 "
378+ "``tp_new`` 中途失败时发生)。 随后它将调用对象类型的 :c:member:`~PyTypeObject.tp_free` 成员(通过 "
379+ "``Py_TYPE(self)`` 计算得到)来释放对象的内存。 请注意对象类型可以不是 "
380+ ":class:`!CustomType`,因为对象可能是一个子类的实例。"
371381
372382#: ../../extending/newtypes_tutorial.rst:286
373383msgid ""
@@ -402,6 +412,10 @@ msgid ""
402412" initialize the ``first`` and ``last`` attributes to non-``NULL`` default "
403413"values."
404414msgstr ""
415+ "``tp_new`` 处理句柄负责创建(而不是初始化)该类型的对象。 它在 Python 中被暴露为 :meth:`~object.__new__` "
416+ "方法。 它不需要定义 ``tp_new`` 成员,实际上许多扩展类型会简单地重用 :c:func:`PyType_GenericNew`,就像上面 "
417+ ":class:`!Custom` 类型的第一个版本所做的那样。 在此情况下,我们使用 ``tp_new`` 处理句柄来将 ``first`` 和 "
418+ "``last`` 属性初始化为非 ``NULL`` 的默认值。"
405419
406420#: ../../extending/newtypes_tutorial.rst:328
407421msgid ""
@@ -461,6 +475,12 @@ msgid ""
461475" may not be able to create instances of such subclasses without getting a "
462476":exc:`TypeError`.)"
463477msgstr ""
478+ "如果您要创建一个协作式 :c:member:`~PyTypeObject.tp_new` (它会调用基类型的 "
479+ ":c:member:`~PyTypeObject.tp_new` 或 :meth:`~object.__new__`),那么你 *不能* "
480+ "在运行时尝试使用方法解析顺序来确定要调用的方法。 必须总是静态地确定你要调用的类型,并直接调用它的 "
481+ ":c:member:`~PyTypeObject.tp_new`,或是通过 ``type->tp_base->tp_new``。 "
482+ "如果你不这样做,你的类型的同样继承自其它由 Python 定义的类的 Python 子类可能无法正常工作。 "
483+ "(具体地说,你可能无法创建这样的子类的实例而是会引发 :exc:`TypeError`。)"
464484
465485#: ../../extending/newtypes_tutorial.rst:364
466486msgid ""
@@ -479,6 +499,8 @@ msgid ""
479499"it's created. Initializers always accept positional and keyword arguments, "
480500"and they should return either ``0`` on success or ``-1`` on error."
481501msgstr ""
502+ ":c:member:`~PyTypeObject.tp_init` 槽位在 Python 中暴露为 :meth:`~object.__init__` "
503+ "方法。 它被用来在创建对象后对其进行初始化。 初始化器总是接受位置和关键字参数,它们应当在成功时返回 ``0`` 而在出错时返回 ``-1``。"
482504
483505#: ../../extending/newtypes_tutorial.rst:402
484506msgid ""
@@ -490,6 +512,9 @@ msgid ""
490512"new attribute values. We might be tempted, for example to assign the "
491513"``first`` member like this::"
492514msgstr ""
515+ "不同于 ``tp_new`` 处理句柄,``tp_init`` 不保证一定会被调用 (例如,在默认情况下 :mod:`pickle` "
516+ "模块不会在未解封的实例上调用 :meth:`~object.__init__`)。 它还可能被多次调用。 任何人都可以在我们的对象上调用 "
517+ ":meth:`!__init__` 方法。 因此,我们在为属性赋新值时必须格外小心。 例如像这样给 ``first`` 成员赋值::"
493518
494519#: ../../extending/newtypes_tutorial.rst:416
495520msgid ""
@@ -566,7 +591,7 @@ msgstr ""
566591msgid ""
567592"We define a single method, :meth:`!Custom.name()`, that outputs the objects "
568593"name as the concatenation of the first and last names. ::"
569- msgstr ""
594+ msgstr "我们定义了一个单独的方法,:meth:`!Custom.name()`,它将对象名称输出为 first 和 last 的拼接。 :: "
570595
571596#: ../../extending/newtypes_tutorial.rst:480
572597msgid ""
@@ -577,6 +602,9 @@ msgid ""
577602" to accept a positional argument tuple or keyword argument dictionary. This "
578603"method is equivalent to the Python method:"
579604msgstr ""
605+ "该方法以的实现形式是一个接受 :class:`!Custom` (或:class:`!Custom` 的子类) 实例作为第一个参数的 C 函数。 "
606+ "方法总是接受一个实例作为第一个参数。 方法往往也接受位置和关键字参数,但在本例中我们未接受任何参数也不需要接受位置参数元组或关键字参数字典。 "
607+ "该方法等价于以下 Python 方法:"
580608
581609#: ../../extending/newtypes_tutorial.rst:492
582610msgid ""
@@ -586,6 +614,9 @@ msgid ""
586614"deletion of these attributes and to restrict the attribute values to be "
587615"strings. We'll see how to do that in the next section."
588616msgstr ""
617+ "请注意我们必须检查 :attr:`!first` 和 :attr:`!last` 成员是否可能为 ``NULL``。 "
618+ "这是因为它们可以被删除,在此情况下它们会被设为 ``NULL``。 更好的做法是防止删除这些属性并将属性的值限制为字符串。 "
619+ "我们将在下一节了解如何做到这一点。"
589620
590621#: ../../extending/newtypes_tutorial.rst:498
591622msgid ""
@@ -619,14 +650,16 @@ msgid ""
619650"module name in the :c:type:`PyModuleDef` struct, and update the full class "
620651"name in the :c:type:`PyTypeObject` struct."
621652msgstr ""
653+ "我们将 :c:func:`!PyInit_custom` 重命名为 :c:func:`!PyInit_custom2`,更新 "
654+ ":c:type:`PyModuleDef` 结构体中的模块名称,并更新 :c:type:`PyTypeObject` 结构体中的完整类名。"
622655
623656#: ../../extending/newtypes_tutorial.rst:526
624657msgid "Finally, we update our :file:`setup.py` file to build the new module:"
625658msgstr "最后,我们更新我们的 :file:`setup.py` 文件来生成新的模块。"
626659
627660#: ../../extending/newtypes_tutorial.rst:539
628661msgid "Providing finer control over data attributes"
629- msgstr "提供对于数据属性的更精细控制Providing finer control over data attributes "
662+ msgstr "提供对于数据属性的更精细控制 "
630663
631664#: ../../extending/newtypes_tutorial.rst:541
632665msgid ""
@@ -636,13 +669,18 @@ msgid ""
636669":attr:`!last` could be set to non-string values or even deleted. We want to "
637670"make sure that these attributes always contain strings."
638671msgstr ""
672+ "在本节中,我们将对 :class:`!Custom` 示例中 :attr:`!first` 和 :attr:`!last` 属性的设置进行更精细的控制。"
673+ " 在我们上一版本的模块中,实例变量 :attr:`!first` 和 :attr:`!last` 可以被设为非字符串值甚至被删除。 "
674+ "我们希望确保这些属性始终包含字符串。"
639675
640676#: ../../extending/newtypes_tutorial.rst:550
641677msgid ""
642678"To provide greater control, over the :attr:`!first` and :attr:`!last` "
643679"attributes, we'll use custom getter and setter functions. Here are the "
644680"functions for getting and setting the :attr:`!first` attribute::"
645681msgstr ""
682+ "为了更好地控制 :attr:`!first` 和 :attr:`!last` 属性,我们将使用自定义的读取器和设置器函数。 以下就是用于读取和设置 "
683+ ":attr:`!first` 属性的函数::"
646684
647685#: ../../extending/newtypes_tutorial.rst:581
648686msgid ""
@@ -653,6 +691,8 @@ msgid ""
653691"of getter and setter functions that decide the attribute to get or set based"
654692" on data in the closure.)"
655693msgstr ""
694+ "读取器函数接受一个 :class:`!Custom` 对象和一个“闭包”,后者是一个空指针。 在本例中,该闭包将被忽略。 (闭包支持将定义数据传递给 "
695+ "读取器和设置器的高级用法。 例如,这可以被用来允许一组获取器和设置器函数根据闭包中的数据来决定要读取或设置的属性)。"
656696
657697#: ../../extending/newtypes_tutorial.rst:587
658698msgid ""
@@ -661,6 +701,8 @@ msgid ""
661701" is being deleted. In our setter, we raise an error if the attribute is "
662702"deleted or if its new value is not a string."
663703msgstr ""
704+ "设置器函数接受传入 :class:`!Custom` 对象、新值和闭包。 新值可能为 ``NULL``,在这种情况下属性将被删除。 "
705+ "在我们的设置器中,如果属性被删除或者如果其新值不是字符串则会引发一个错误。"
664706
665707#: ../../extending/newtypes_tutorial.rst:592
666708msgid "We create an array of :c:type:`PyGetSetDef` structures::"
@@ -741,6 +783,9 @@ msgid ""
741783":class:`!Custom`, and subclasses may add arbitrary attributes. For any of "
742784"those two reasons, :class:`!Custom` objects can participate in cycles:"
743785msgstr ""
786+ "在 :class:`!Custom` 示例的第二个版本中,我们允许任意类型的对象存储到 :attr:`!first` 或 :attr:`!last` "
787+ "属性中 [#]_。 此外,在第二和第三个版本中,我们还允许子类化 :class:`!Custom`,并且子类可以添加任意属性。 "
788+ "出于这两个原因中的任何一个,:class:`!Custom` 对象都可以加入循环:"
744789
745790#: ../../extending/newtypes_tutorial.rst:690
746791msgid ""
@@ -749,6 +794,8 @@ msgid ""
749794"type needs to fill two additional slots and to enable a flag that enables "
750795"these slots:"
751796msgstr ""
797+ "要允许一个加入引用循环的 :class:`!Custom` 实例能被循环 GC 正确检测和收集,我们的 :class:`!Custom` "
798+ "类型需要填充两个额外的槽位并增加一个旗标来启用这些槽位:"
752799
753800#: ../../extending/newtypes_tutorial.rst:697
754801msgid ""
@@ -764,6 +811,8 @@ msgid ""
764811"argument *arg* passed to the traversal method. It returns an integer value "
765812"that must be returned if it is non-zero."
766813msgstr ""
814+ "对于每个可以加入循环的子对象,我们都需要调用 :c:func:`!visit` 函数,它会被传递给遍历方法。 :c:func:`!visit` "
815+ "函数接受该子对象和传递给遍历方法的额外参数 *arg* 作为其参数。 它返回一个在其为非零值时必须被返回的整数值。"
767816
768817#: ../../extending/newtypes_tutorial.rst:723
769818msgid ""
@@ -863,13 +912,15 @@ msgid ""
863912"with regular lists, but will have an additional :meth:`!increment` method "
864913"that increases an internal counter:"
865914msgstr ""
915+ "在本例中我们将创建一个继承自内置 :class:`list` 类型的 :class:`!SubList` 类型。 "
916+ "这个新类型将完全兼容常规列表,但将拥有一个额外的 :meth:`!increment` 方法用于递增内部计数器的值:"
866917
867918#: ../../extending/newtypes_tutorial.rst:821
868919msgid ""
869920"As you can see, the source code closely resembles the :class:`!Custom` "
870921"examples in previous sections. We will break down the main differences "
871922"between them. ::"
872- msgstr ""
923+ msgstr "如你所见,此源代码与之前几节中的 :class:`!Custom` 示例非常相似。 我们将逐一分析它们之间的主要区别。 :: "
873924
874925#: ../../extending/newtypes_tutorial.rst:829
875926msgid ""
@@ -885,12 +936,14 @@ msgid ""
885936"pointer can be safely cast to both ``PyListObject *`` and ``SubListObject "
886937"*``::"
887938msgstr ""
939+ "当一个 Python 对象是 :class:`!SubList` 的实例时,它的 ``PyObject *`` 指针可以被安全地强制转换为 "
940+ "``PyListObject *`` 和 ``SubListObject *``::"
888941
889942#: ../../extending/newtypes_tutorial.rst:845
890943msgid ""
891944"We see above how to call through to the :meth:`~object.__init__` method of "
892945"the base type."
893- msgstr ""
946+ msgstr "我们可以在上面看到如何将调用传递到基类型的 :meth:`~object.__init__` 方法。 "
894947
895948#: ../../extending/newtypes_tutorial.rst:848
896949msgid ""
@@ -935,6 +988,7 @@ msgid ""
935988"After that, calling :c:func:`PyType_Ready` and adding the type object to the"
936989" module is the same as with the basic :class:`!Custom` examples."
937990msgstr ""
991+ "在那之后,调用 :c:func:`PyType_Ready` 并将类型对象添加到模块中的过程与基本的 :class:`!Custom` 示例是一样的。"
938992
939993#: ../../extending/newtypes_tutorial.rst:893
940994msgid "Footnotes"
0 commit comments