-
-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathintro.po
1451 lines (1310 loc) · 68.2 KB
/
intro.po
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SOME DESCRIPTIVE TITLE.
# Copyright (C) 2001-2022, Python Software Foundation
# This file is distributed under the same license as the Python package.
#
# Translators:
# Matt Wang <[email protected]>, 2023
msgid ""
msgstr ""
"Project-Id-Version: Python 3.13\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-09-23 07:52+0800\n"
"PO-Revision-Date: 2023-04-25 18:01+0800\n"
"Last-Translator: Matt Wang <[email protected]>\n"
"Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-"
"tw)\n"
"Language: zh_TW\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=1; plural=0;\n"
"X-Generator: Poedit 3.2.2\n"
#: ../../c-api/intro.rst:8
msgid "Introduction"
msgstr "簡介"
#: ../../c-api/intro.rst:10
msgid ""
"The Application Programmer's Interface to Python gives C and C++ programmers "
"access to the Python interpreter at a variety of levels. The API is equally "
"usable from C++, but for brevity it is generally referred to as the Python/C "
"API. There are two fundamentally different reasons for using the Python/C "
"API. The first reason is to write *extension modules* for specific purposes; "
"these are C modules that extend the Python interpreter. This is probably "
"the most common use. The second reason is to use Python as a component in a "
"larger application; this technique is generally referred to as :dfn:"
"`embedding` Python in an application."
msgstr ""
"對於 Python 的應用程式開發介面使得 C 和 C++ 開發者能夠在各種層級存取 Python "
"直譯器。該 API 同樣可用於 C++,但為簡潔起見,通常將其稱為 Python/C API。使用 "
"Python/C API 有兩個不同的原因,第一個是為特定目的來編寫\\ *擴充模組*;這些是"
"擴充 Python 直譯器的 C 模組,這可能是最常見的用法。第二個原因是在更大的應用程"
"式中將 Python 作為零件使用;這種技術通常在應用程式中稱為 :dfn:`embedding`\\ "
"(嵌入式)Python。"
#: ../../c-api/intro.rst:20
msgid ""
"Writing an extension module is a relatively well-understood process, where a "
"\"cookbook\" approach works well. There are several tools that automate the "
"process to some extent. While people have embedded Python in other "
"applications since its early existence, the process of embedding Python is "
"less straightforward than writing an extension."
msgstr ""
"編寫擴充模組是一個相對容易理解的過程,其中「食譜 (cookbook)」方法很有效。有幾"
"種工具可以在一定程度上自動化該過程,儘管人們從早期就將 Python 嵌入到其他應用"
"程式中,但嵌入 Python 的過程並不像編寫擴充那樣簡單。"
#: ../../c-api/intro.rst:26
msgid ""
"Many API functions are useful independent of whether you're embedding or "
"extending Python; moreover, most applications that embed Python will need "
"to provide a custom extension as well, so it's probably a good idea to "
"become familiar with writing an extension before attempting to embed Python "
"in a real application."
msgstr ""
"不論你是嵌入還是擴充 Python,許多 API 函式都是很有用的;此外,大多數嵌入 "
"Python 的應用程式也需要提供自定義擴充模組,因此在嘗試將 Python 嵌入實際應用程"
"式之前熟悉編寫擴充可能是個好主意。"
#: ../../c-api/intro.rst:34
msgid "Coding standards"
msgstr "編寫標準"
#: ../../c-api/intro.rst:36
msgid ""
"If you're writing C code for inclusion in CPython, you **must** follow the "
"guidelines and standards defined in :PEP:`7`. These guidelines apply "
"regardless of the version of Python you are contributing to. Following "
"these conventions is not necessary for your own third party extension "
"modules, unless you eventually expect to contribute them to Python."
msgstr ""
"如果你正在編寫要引入於 CPython 中的 C 程式碼,你\\ **必須**\\ 遵循 :PEP:`7` "
"中定義的指南和標準。無論你貢獻的 Python 版本如何,這些指南都適用。對於你自己"
"的第三方擴充模組,則不必遵循這些約定,除非你希望最終將它們貢獻給 Python。"
#: ../../c-api/intro.rst:46
msgid "Include Files"
msgstr "引入檔案 (include files)"
#: ../../c-api/intro.rst:48
msgid ""
"All function, type and macro definitions needed to use the Python/C API are "
"included in your code by the following line::"
msgstr ""
"使用 Python/C API 所需的所有函式、型別和巨集的定義都透過以下這幾行來在你的程"
"式碼中引入:"
#: ../../c-api/intro.rst:51
msgid ""
"#define PY_SSIZE_T_CLEAN\n"
"#include <Python.h>"
msgstr ""
"#define PY_SSIZE_T_CLEAN\n"
"#include <Python.h>"
#: ../../c-api/intro.rst:54
msgid ""
"This implies inclusion of the following standard headers: ``<stdio.h>``, "
"``<string.h>``, ``<errno.h>``, ``<limits.h>``, ``<assert.h>`` and ``<stdlib."
"h>`` (if available)."
msgstr ""
"這意味著會引入以下標準標頭:``<stdio.h>``、``<string.h>``、``<errno.h>``、"
"``<limits.h>``、``<assert.h>`` 和 ``<stdlib.h>``\\ (如果可用)。"
#: ../../c-api/intro.rst:60
msgid ""
"Since Python may define some pre-processor definitions which affect the "
"standard headers on some systems, you *must* include :file:`Python.h` before "
"any standard headers are included."
msgstr ""
"由於 Python 可能會定義一些會影響某些系統上標準標頭檔的預處理器 (pre-"
"processor),因此你\\ *必須*\\ 在引入任何標準標頭檔之前引入 :file:`Python.h`。"
#: ../../c-api/intro.rst:64
msgid ""
"It is recommended to always define ``PY_SSIZE_T_CLEAN`` before including "
"``Python.h``. See :ref:`arg-parsing` for a description of this macro."
msgstr ""
"建議在引入 ``Python.h`` 之前都要定義 ``PY_SSIZE_T_CLEAN``。有關此巨集的說明,"
"請參閱\\ :ref:`arg-parsing`。"
#: ../../c-api/intro.rst:67
msgid ""
"All user visible names defined by Python.h (except those defined by the "
"included standard headers) have one of the prefixes ``Py`` or ``_Py``. "
"Names beginning with ``_Py`` are for internal use by the Python "
"implementation and should not be used by extension writers. Structure member "
"names do not have a reserved prefix."
msgstr ""
"所有定義於 Python.h 中且使用者可見的名稱(另外透過標準標頭檔引入的除外)都具"
"有 ``Py`` 或 ``_Py`` 前綴。以 ``_Py`` 開頭的名稱供 Python 實作內部使用,擴充"
"編寫者不應使用。結構成員名稱沒有保留前綴。"
#: ../../c-api/intro.rst:74
msgid ""
"User code should never define names that begin with ``Py`` or ``_Py``. This "
"confuses the reader, and jeopardizes the portability of the user code to "
"future Python versions, which may define additional names beginning with one "
"of these prefixes."
msgstr ""
"使用者程式碼不應定義任何以 ``Py`` 或 ``_Py`` 開頭的名稱。這會讓讀者感到困惑,"
"並危及使用者程式碼在未來 Python 版本上的可移植性,這些版本可能會定義以這些前"
"綴之一開頭的其他名稱。"
#: ../../c-api/intro.rst:79
msgid ""
"The header files are typically installed with Python. On Unix, these are "
"located in the directories :file:`{prefix}/include/pythonversion/` and :file:"
"`{exec_prefix}/include/pythonversion/`, where :option:`prefix <--prefix>` "
"and :option:`exec_prefix <--exec-prefix>` are defined by the corresponding "
"parameters to Python's :program:`configure` script and *version* is ``'%d."
"%d' % sys.version_info[:2]``. On Windows, the headers are installed in :"
"file:`{prefix}/include`, where ``prefix`` is the installation directory "
"specified to the installer."
msgstr ""
"標頭檔通常隨 Python 一起安裝。在 Unix 上它們位於目錄 :file:`{prefix}/include/"
"pythonversion/` 和 :file:`{exec_prefix}/include/pythonversion/`,其中 :"
"option:`prefix <--prefix>` 和 :option:`exec_prefix <--exec-prefix>` 由 "
"Python 的 :program:`configure` 腳本的相應參數定義,*version* 是 ``'%d.%d' % "
"sys.version_info[:2]``。在 Windows 上,標頭安裝在 :file:`{prefix}/include` "
"中,其中 ``prefix`` 是指定給安裝程式 (installer) 用的安裝目錄。"
#: ../../c-api/intro.rst:88
msgid ""
"To include the headers, place both directories (if different) on your "
"compiler's search path for includes. Do *not* place the parent directories "
"on the search path and then use ``#include <pythonX.Y/Python.h>``; this will "
"break on multi-platform builds since the platform independent headers under :"
"option:`prefix <--prefix>` include the platform specific headers from :"
"option:`exec_prefix <--exec-prefix>`."
msgstr ""
"要引入標頭,請將兩個(如果不同)目錄放在編譯器的引入搜尋路徑 (search path) "
"中。*不要*\\ 將父目錄放在搜尋路徑上,然後使用 ``#include <pythonX.Y/Python."
"h>``;這會在多平台建置上壞掉,因為 :option:`prefix <--prefix>` 下獨立於平台的"
"標頭包括來自 :option:`exec_prefix <--exec-prefix>` 的平台特定標頭。"
#: ../../c-api/intro.rst:95
msgid ""
"C++ users should note that although the API is defined entirely using C, the "
"header files properly declare the entry points to be ``extern \"C\"``. As a "
"result, there is no need to do anything special to use the API from C++."
msgstr ""
"C++ 使用者應注意,儘管 API 完全使用 C 來定義,但標頭檔適當地將入口點聲明為 "
"``extern \"C\"``。因此,無需執行任何特殊操作即可使用 C++ 中的 API。"
#: ../../c-api/intro.rst:101
msgid "Useful macros"
msgstr "有用的巨集"
#: ../../c-api/intro.rst:103
msgid ""
"Several useful macros are defined in the Python header files. Many are "
"defined closer to where they are useful (e.g. :c:macro:`Py_RETURN_NONE`). "
"Others of a more general utility are defined here. This is not necessarily "
"a complete listing."
msgstr ""
"Python 標頭檔中定義了幾個有用的巨集,大多被定義在它們有用的地方附近(例如 :c:"
"macro:`Py_RETURN_NONE`),其他是更通用的工具程式。以下並不一定是完整的列表。"
#: ../../c-api/intro.rst:110
msgid ""
"Declare an extension module ``PyInit`` initialization function. The function "
"return type is :c:expr:`PyObject*`. The macro declares any special linkage "
"declarations required by the platform, and for C++ declares the function as "
"``extern \"C\"``."
msgstr ""
#: ../../c-api/intro.rst:115
msgid ""
"The initialization function must be named :samp:`PyInit_{name}`, where "
"*name* is the name of the module, and should be the only non-\\ ``static`` "
"item defined in the module file. Example::"
msgstr ""
#: ../../c-api/intro.rst:119
msgid ""
"static struct PyModuleDef spam_module = {\n"
" PyModuleDef_HEAD_INIT,\n"
" .m_name = \"spam\",\n"
" ...\n"
"};\n"
"\n"
"PyMODINIT_FUNC\n"
"PyInit_spam(void)\n"
"{\n"
" return PyModule_Create(&spam_module);\n"
"}"
msgstr ""
"static struct PyModuleDef spam_module = {\n"
" PyModuleDef_HEAD_INIT,\n"
" .m_name = \"spam\",\n"
" ...\n"
"};\n"
"\n"
"PyMODINIT_FUNC\n"
"PyInit_spam(void)\n"
"{\n"
" return PyModule_Create(&spam_module);\n"
"}"
#: ../../c-api/intro.rst:134
msgid "Return the absolute value of ``x``."
msgstr "回傳 ``x`` 的絕對值。"
#: ../../c-api/intro.rst:140
msgid ""
"Ask the compiler to always inline a static inline function. The compiler can "
"ignore it and decides to not inline the function."
msgstr ""
"要求編譯器總是嵌入靜態行內函式 (static inline function),編譯器可以忽略它並決"
"定不嵌入該函式。"
#: ../../c-api/intro.rst:143
msgid ""
"It can be used to inline performance critical static inline functions when "
"building Python in debug mode with function inlining disabled. For example, "
"MSC disables function inlining when building in debug mode."
msgstr ""
"在禁用函式嵌入的除錯模式下建置 Python 時,它可用於嵌入有性能要求的靜態行內函"
"式。例如,MSC 在除錯模式下建置時禁用函式嵌入。"
#: ../../c-api/intro.rst:147
msgid ""
"Marking blindly a static inline function with Py_ALWAYS_INLINE can result in "
"worse performances (due to increased code size for example). The compiler is "
"usually smarter than the developer for the cost/benefit analysis."
msgstr ""
"盲目地使用 Py_ALWAYS_INLINE 標記靜態行內函式可能會導致更差的性能(例如程式碼"
"大小增加)。在成本/收益分析方面,編譯器通常比開發人員更聰明。"
#: ../../c-api/intro.rst:151
msgid ""
"If Python is :ref:`built in debug mode <debug-build>` (if the :c:macro:"
"`Py_DEBUG` macro is defined), the :c:macro:`Py_ALWAYS_INLINE` macro does "
"nothing."
msgstr ""
"如果 Python 是\\ :ref:`在除錯模式下建置 <debug-build>`\\ (如果 :c:macro:"
"`Py_DEBUG` 巨集有被定義),:c:macro:`Py_ALWAYS_INLINE` 巨集就什麼都不會做。"
#: ../../c-api/intro.rst:154
msgid "It must be specified before the function return type. Usage::"
msgstr "它必須在函式回傳型別之前被指定。用法: ::"
#: ../../c-api/intro.rst:156
msgid "static inline Py_ALWAYS_INLINE int random(void) { return 4; }"
msgstr "static inline Py_ALWAYS_INLINE int random(void) { return 4; }"
#: ../../c-api/intro.rst:162
msgid ""
"Argument must be a character or an integer in the range [-128, 127] or [0, "
"255]. This macro returns ``c`` cast to an ``unsigned char``."
msgstr ""
"引數必須是 [-128, 127] 或 [0, 255] 範圍內的字元或整數。這個巨集會將 ``c`` 轉"
"換為 ``unsigned char`` 並回傳。"
#: ../../c-api/intro.rst:167
msgid ""
"Use this for deprecated declarations. The macro must be placed before the "
"symbol name."
msgstr "將其用於已棄用的聲明。巨集必須放在符號名稱之前。"
#: ../../c-api/intro.rst:170 ../../c-api/intro.rst:256
#: ../../c-api/intro.rst:274
msgid "Example::"
msgstr "範例: ::"
#: ../../c-api/intro.rst:172
msgid "Py_DEPRECATED(3.8) PyAPI_FUNC(int) Py_OldFunction(void);"
msgstr "Py_DEPRECATED(3.8) PyAPI_FUNC(int) Py_OldFunction(void);"
#: ../../c-api/intro.rst:174
msgid "MSVC support was added."
msgstr "新增了 MSVC 支援。"
#: ../../c-api/intro.rst:179
msgid ""
"Like ``getenv(s)``, but returns ``NULL`` if :option:`-E` was passed on the "
"command line (see :c:member:`PyConfig.use_environment`)."
msgstr ""
"類似於 ``getenv(s)``,但如果在命令列上傳遞了 :option:`-E` 則回傳 ``NULL`` "
"(請見 :c:member:`PyConfig.use_environment`)。"
#: ../../c-api/intro.rst:184
msgid "Return the maximum value between ``x`` and ``y``."
msgstr "回傳 ``x`` 和 ``y`` 之間的最大值。"
#: ../../c-api/intro.rst:190
msgid "Return the size of a structure (``type``) ``member`` in bytes."
msgstr "以位元組為單位回傳結構 (``type``) ``member`` 的大小。"
#: ../../c-api/intro.rst:196
msgid "Return the minimum value between ``x`` and ``y``."
msgstr "回傳 ``x`` 和 ``y`` 之間的最小值。"
#: ../../c-api/intro.rst:202
msgid ""
"Disable inlining on a function. For example, it reduces the C stack "
"consumption: useful on LTO+PGO builds which heavily inline code (see :issue:"
"`33720`)."
msgstr ""
"禁用函式的嵌入。例如,它減少了 C 堆疊的消耗:對大量嵌入程式碼的 LTO+PGO 建置"
"很有用(請參閱 :issue:`33720`)。"
#: ../../c-api/intro.rst:206
msgid "Usage::"
msgstr "用法: ::"
#: ../../c-api/intro.rst:208
msgid "Py_NO_INLINE static int random(void) { return 4; }"
msgstr "Py_NO_INLINE static int random(void) { return 4; }"
#: ../../c-api/intro.rst:214
msgid ""
"Convert ``x`` to a C string. E.g. ``Py_STRINGIFY(123)`` returns ``\"123\"``."
msgstr ""
"將 ``x`` 轉換為 C 字串。例如 ``Py_STRINGIFY(123)`` 會回傳 ``\"123\"``。"
#: ../../c-api/intro.rst:221
msgid ""
"Use this when you have a code path that cannot be reached by design. For "
"example, in the ``default:`` clause in a ``switch`` statement for which all "
"possible values are covered in ``case`` statements. Use this in places "
"where you might be tempted to put an ``assert(0)`` or ``abort()`` call."
msgstr ""
"當你的設計中有無法達到的程式碼路徑時,請使用此選項。例如在 ``case`` 語句已涵"
"蓋了所有可能值的 ``switch`` 陳述式中的 ``default:`` 子句。在你可能想要呼叫 "
"``assert(0)`` 或 ``abort()`` 的地方使用它。"
#: ../../c-api/intro.rst:226
msgid ""
"In release mode, the macro helps the compiler to optimize the code, and "
"avoids a warning about unreachable code. For example, the macro is "
"implemented with ``__builtin_unreachable()`` on GCC in release mode."
msgstr ""
"在發布模式 (release mode) 下,巨集幫助編譯器最佳化程式碼,並避免有關無法存取"
"程式碼的警告。例如該巨集是在發布模式下於 GCC 使用 "
"``__builtin_unreachable()`` 來實作。"
#: ../../c-api/intro.rst:230
msgid ""
"A use for ``Py_UNREACHABLE()`` is following a call a function that never "
"returns but that is not declared :c:macro:`_Py_NO_RETURN`."
msgstr ""
"``Py_UNREACHABLE()`` 的一個用途是,在對一個永不回傳但並未聲明為 :c:macro:"
"`_Py_NO_RETURN` 的函式之呼叫後使用。"
#: ../../c-api/intro.rst:233
msgid ""
"If a code path is very unlikely code but can be reached under exceptional "
"case, this macro must not be used. For example, under low memory condition "
"or if a system call returns a value out of the expected range. In this "
"case, it's better to report the error to the caller. If the error cannot be "
"reported to caller, :c:func:`Py_FatalError` can be used."
msgstr ""
"如果程式碼路徑是極不可能但在特殊情況下可以到達,則不得使用此巨集。例如在低記"
"憶體條件下或系統呼叫回傳了超出預期範圍的值。在這種情況下,最好將錯誤回報給呼"
"叫者。如果無法回報錯誤則可以使用 :c:func:`Py_FatalError`。"
#: ../../c-api/intro.rst:243
msgid ""
"Use this for unused arguments in a function definition to silence compiler "
"warnings. Example: ``int func(int a, int Py_UNUSED(b)) { return a; }``."
msgstr ""
"將此用於函式定義中未使用的參數以消除編譯器警告。例如:``int func(int a, int "
"Py_UNUSED(b)) { return a; }``。"
#: ../../c-api/intro.rst:250
msgid ""
"Creates a variable with name ``name`` that can be used in docstrings. If "
"Python is built without docstrings, the value will be empty."
msgstr ""
"建立一個名為 ``name`` 的變數,可以在文件字串中使用。如果 Python 是在沒有文件"
"字串的情況下建置,則該值將為空。"
#: ../../c-api/intro.rst:253
msgid ""
"Use :c:macro:`PyDoc_STRVAR` for docstrings to support building Python "
"without docstrings, as specified in :pep:`7`."
msgstr ""
"如 :pep:`7` 中所指明,使用 :c:macro:`PyDoc_STRVAR` 作為文件字串可以支援在沒有"
"文件字串的情況下建置 Python。"
#: ../../c-api/intro.rst:258
msgid ""
"PyDoc_STRVAR(pop_doc, \"Remove and return the rightmost element.\");\n"
"\n"
"static PyMethodDef deque_methods[] = {\n"
" // ...\n"
" {\"pop\", (PyCFunction)deque_pop, METH_NOARGS, pop_doc},\n"
" // ...\n"
"}"
msgstr ""
#: ../../c-api/intro.rst:268
msgid ""
"Creates a docstring for the given input string or an empty string if "
"docstrings are disabled."
msgstr "為給定的輸入字串建立一個文件字串,如果文件字串被禁用則建立空字串。"
#: ../../c-api/intro.rst:271
msgid ""
"Use :c:macro:`PyDoc_STR` in specifying docstrings to support building Python "
"without docstrings, as specified in :pep:`7`."
msgstr ""
"如 :pep:`7` 中所指明,使用 :c:macro:`PyDoc_STR` 指定文件字串以支援在沒有文件"
"字串下建置 Python。"
#: ../../c-api/intro.rst:276
msgid ""
"static PyMethodDef pysqlite_row_methods[] = {\n"
" {\"keys\", (PyCFunction)pysqlite_row_keys, METH_NOARGS,\n"
" PyDoc_STR(\"Returns the keys of the row.\")},\n"
" {NULL, NULL}\n"
"};"
msgstr ""
#: ../../c-api/intro.rst:286
msgid "Objects, Types and Reference Counts"
msgstr "物件、型別和參照計數"
#: ../../c-api/intro.rst:290
msgid ""
"Most Python/C API functions have one or more arguments as well as a return "
"value of type :c:expr:`PyObject*`. This type is a pointer to an opaque data "
"type representing an arbitrary Python object. Since all Python object types "
"are treated the same way by the Python language in most situations (e.g., "
"assignments, scope rules, and argument passing), it is only fitting that "
"they should be represented by a single C type. Almost all Python objects "
"live on the heap: you never declare an automatic or static variable of type :"
"c:type:`PyObject`, only pointer variables of type :c:expr:`PyObject*` can "
"be declared. The sole exception are the type objects; since these must "
"never be deallocated, they are typically static :c:type:`PyTypeObject` "
"objects."
msgstr ""
"大多數 Python/C API 函式都有一個或多個引數以及一個型別為 :c:expr:`PyObject*` "
"的回傳值,此型別是一個指標,指向一個表示任意 Python 物件的晦暗 (opaque) 資料"
"型別。由於在大多數情況下,Python 語言以相同的方式處理所有 Python 物件型別(例"
"如賦值、作用域規則和引數傳遞),因此它們應該由單個 C 型別來表示。幾乎所有的 "
"Python 物件都存在於堆積 (heap) 中:你永遠不會聲明 :c:type:`PyObject` 型別的自"
"動變數或靜態變數,只能聲明 :c:expr:`PyObject*` 型別的指標變數。唯一的例外是型"
"別物件;由於它們絕不能被釋放,因此它們通常是靜態 :c:type:`PyTypeObject` 物"
"件。"
#: ../../c-api/intro.rst:301
msgid ""
"All Python objects (even Python integers) have a :dfn:`type` and a :dfn:"
"`reference count`. An object's type determines what kind of object it is (e."
"g., an integer, a list, or a user-defined function; there are many more as "
"explained in :ref:`types`). For each of the well-known types there is a "
"macro to check whether an object is of that type; for instance, "
"``PyList_Check(a)`` is true if (and only if) the object pointed to by *a* is "
"a Python list."
msgstr ""
"所有 Python 物件(甚至是 Python 整數)都有一個型別 (:dfn:`type`) 和一個參照計"
"數 (:dfn:`reference count`)。一個物件的型別決定了它是什麼種類的物件(例如一個"
"整數、一個 list 或一個使用者定義的函式;還有更多型別,請見\\ :ref:"
"`types`\\ )。對於每個眾所周知的型別,都有一個巨集來檢查物件是否屬於該型別;"
"例如,若(且唯若)*a* 指向的物件是 Python list 時,``PyList_Check(a)`` 為真。"
#: ../../c-api/intro.rst:312
msgid "Reference Counts"
msgstr "參照計數"
#: ../../c-api/intro.rst:314
msgid ""
"The reference count is important because today's computers have a finite "
"(and often severely limited) memory size; it counts how many different "
"places there are that have a :term:`strong reference` to an object. Such a "
"place could be another object, or a global (or static) C variable, or a "
"local variable in some C function. When the last :term:`strong reference` to "
"an object is released (i.e. its reference count becomes zero), the object is "
"deallocated. If it contains references to other objects, those references "
"are released. Those other objects may be deallocated in turn, if there are "
"no more references to them, and so on. (There's an obvious problem with "
"objects that reference each other here; for now, the solution is \"don't do "
"that.\")"
msgstr ""
"參照計數很重要,因為現今的電腦記憶體大小是有限的(而且通常是非常有限的);它"
"計算有多少個不同的地方用有了一個物件的\\ :term:`強參照 <strong reference>`。"
"這樣的地方可以是另一個物件,或者全域(或靜態)C 變數,或者某個 C 函式中的本地"
"變數。當一個物件的最後一個\\ :term:`強參照 <strong reference>`\\ 被釋放時(即"
"其的參照計數變為零),該物件將被解除配置 (deallocated)。如果它包含對其他物件"
"的參照,則它們的參照會被釋放。如果這樣的釋放使得再也沒有任何對於它們的參照,"
"則可以依次為那些其他物件解除配置,依此類推。(此處相互參照物件的存在是個明顯"
"的問題;目前,解決方案是「就不要那樣做」。)"
#: ../../c-api/intro.rst:331
msgid ""
"Reference counts are always manipulated explicitly. The normal way is to "
"use the macro :c:func:`Py_INCREF` to take a new reference to an object (i.e. "
"increment its reference count by one), and :c:func:`Py_DECREF` to release "
"that reference (i.e. decrement the reference count by one). The :c:func:"
"`Py_DECREF` macro is considerably more complex than the incref one, since it "
"must check whether the reference count becomes zero and then cause the "
"object's deallocator to be called. The deallocator is a function pointer "
"contained in the object's type structure. The type-specific deallocator "
"takes care of releasing references for other objects contained in the object "
"if this is a compound object type, such as a list, as well as performing any "
"additional finalization that's needed. There's no chance that the reference "
"count can overflow; at least as many bits are used to hold the reference "
"count as there are distinct memory locations in virtual memory (assuming "
"``sizeof(Py_ssize_t) >= sizeof(void*)``). Thus, the reference count "
"increment is a simple operation."
msgstr ""
"參照計數總是被明確地操作。正常的方法是使用巨集 :c:func:`Py_INCREF` 來取得對於"
"物件的參照(即參照計數加一),並使用巨集 :c:func:`Py_DECREF` 來釋放參照(即將"
"參照計數減一)。:c:func:`Py_DECREF` 巨集比 incref 巨集複雜得多,因為它必須檢"
"查參照計數是否變為零,然後呼叫物件的釋放器 (deallocator)。釋放器是包含在物件"
"型別結構中的函式指標。特定型別的釋放器,在如果是一個複合物件型別(例如 list)"
"時負責釋放物件中包含的其他物件的參照,並執行任何需要的額外完結步驟。參照計數"
"不可能溢出;至少與虛擬記憶體中用來保存參照計數的不同記憶體位置數量一樣多的位"
"元會被使用(假設 ``sizeof(Py_ssize_t) >= sizeof(void*)``)。因此參照計數增加"
"是一個簡單的操作。"
#: ../../c-api/intro.rst:347
msgid ""
"It is not necessary to hold a :term:`strong reference` (i.e. increment the "
"reference count) for every local variable that contains a pointer to an "
"object. In theory, the object's reference count goes up by one when the "
"variable is made to point to it and it goes down by one when the variable "
"goes out of scope. However, these two cancel each other out, so at the end "
"the reference count hasn't changed. The only real reason to use the "
"reference count is to prevent the object from being deallocated as long as "
"our variable is pointing to it. If we know that there is at least one "
"other reference to the object that lives at least as long as our variable, "
"there is no need to take a new :term:`strong reference` (i.e. increment the "
"reference count) temporarily. An important situation where this arises is in "
"objects that are passed as arguments to C functions in an extension module "
"that are called from Python; the call mechanism guarantees to hold a "
"reference to every argument for the duration of the call."
msgstr ""
"沒有必要為每個包含物件指標的本地變數物件都持有一個\\ :term:`強參照 <strong "
"reference>`\\ (即增加參照計數)。理論上,當變數指向它時,物件的參照計數會增"
"加 1,而當變數離開作用域時就會減少 1。然而這兩者會相互抵消,所以最後參照計數"
"沒有改變。使用參照計數的唯一真正原因是防止物件還有變數指向它時被解除配置。如"
"果我們知道至少有一個物件的其他參照生存了至少與我們的變數一樣久,就不需要臨時"
"增加建立新的\\ :term:`強參照 <strong reference>`\\ (即增加參照計數)。出現這"
"種情況的一個重要情況是在從 Python 呼叫的擴充模組中作為引數傳遞給 C 函式的物"
"件;呼叫機制保證在呼叫期間保持對每個參數的參照。"
#: ../../c-api/intro.rst:363
msgid ""
"However, a common pitfall is to extract an object from a list and hold on to "
"it for a while without taking a new reference. Some other operation might "
"conceivably remove the object from the list, releasing that reference, and "
"possibly deallocating it. The real danger is that innocent-looking "
"operations may invoke arbitrary Python code which could do this; there is a "
"code path which allows control to flow back to the user from a :c:func:"
"`Py_DECREF`, so almost any operation is potentially dangerous."
msgstr ""
"然而,一個常見的陷阱是從一個 list 中提取一個物件並保留它一段時間而不取得其參"
"照。某些其他操作可能會從列表中刪除該物件,減少其參照計數並可能取消分配它。真"
"正的危險是看似無害的操作可能會叫用可以執行此操作的任意 Python 程式碼;有一個"
"程式碼路徑允許控制權從 :c:func:`Py_DECREF` 回歸使用者,因此幾乎任何操作都有潛"
"在危險。"
#: ../../c-api/intro.rst:371
msgid ""
"A safe approach is to always use the generic operations (functions whose "
"name begins with ``PyObject_``, ``PyNumber_``, ``PySequence_`` or "
"``PyMapping_``). These operations always create a new :term:`strong "
"reference` (i.e. increment the reference count) of the object they return. "
"This leaves the caller with the responsibility to call :c:func:`Py_DECREF` "
"when they are done with the result; this soon becomes second nature."
msgstr ""
"一種安全的方法是都使用通用 (generics) 操作(名稱以 ``PyObject_``、"
"``PyNumber_``、``PySequence_`` 或 ``PyMapping_`` 開頭的函式)。這些操作總是建"
"立新的對於它們回傳物件的\\ :term:`強參照 <strong reference>`\\ (即增加其參照"
"計數)。這讓呼叫者有責任在處理完結果後呼叫 :c:func:`Py_DECREF`;這就成為第二"
"本質。"
#: ../../c-api/intro.rst:382
msgid "Reference Count Details"
msgstr "參照計數詳細資訊"
#: ../../c-api/intro.rst:384
msgid ""
"The reference count behavior of functions in the Python/C API is best "
"explained in terms of *ownership of references*. Ownership pertains to "
"references, never to objects (objects are not owned: they are always "
"shared). \"Owning a reference\" means being responsible for calling "
"Py_DECREF on it when the reference is no longer needed. Ownership can also "
"be transferred, meaning that the code that receives ownership of the "
"reference then becomes responsible for eventually releasing it by calling :c:"
"func:`Py_DECREF` or :c:func:`Py_XDECREF` when it's no longer needed---or "
"passing on this responsibility (usually to its caller). When a function "
"passes ownership of a reference on to its caller, the caller is said to "
"receive a *new* reference. When no ownership is transferred, the caller is "
"said to *borrow* the reference. Nothing needs to be done for a :term:"
"`borrowed reference`."
msgstr ""
"Python/C API 中函式的參照計數行為最好用\\ *參照的所有權*\\ 來解釋。所有權附屬"
"於參照而非物件(物件並非被擁有,它們總是共享的)。「擁有參照」意味著當不再需"
"要該參照時,負責在其上呼叫 Py_DECREF。所有權也可以轉移,這意味著接收參照所有"
"權的程式碼最終會負責在不需要參照時透過呼叫 :c:func:`Py_DECREF` 或 :c:func:"
"`Py_XDECREF` 釋放參照 --- 或者將這個責任再傳遞出去(通常是給它的呼叫者)。當"
"一個函式將參照的所有權傳遞給它的呼叫者時,呼叫者被稱為接收到一個\\ *新*\\ 參"
"照。當沒有所有權轉移時,呼叫者被稱為\\ *借用*\\ 參照。如果是\\ :term:`借用參"
"照 <borrowed reference>`\\ 就不需要做任何事情。"
#: ../../c-api/intro.rst:397
msgid ""
"Conversely, when a calling function passes in a reference to an object, "
"there are two possibilities: the function *steals* a reference to the "
"object, or it does not. *Stealing a reference* means that when you pass a "
"reference to a function, that function assumes that it now owns that "
"reference, and you are not responsible for it any longer."
msgstr ""
"相反地,當呼叫的函式傳入物件的參照時,有兩種可能性:函式有\\ *竊取 (steal)* "
"物件的參照,或者沒有。 *竊取參照*\\ 意味著當你將參照傳遞給函式時,該函式假定"
"它現在擁有該參照,並且你不再對它負責。"
#: ../../c-api/intro.rst:407
msgid ""
"Few functions steal references; the two notable exceptions are :c:func:"
"`PyList_SetItem` and :c:func:`PyTuple_SetItem`, which steal a reference to "
"the item (but not to the tuple or list into which the item is put!). These "
"functions were designed to steal a reference because of a common idiom for "
"populating a tuple or list with newly created objects; for example, the code "
"to create the tuple ``(1, 2, \"three\")`` could look like this (forgetting "
"about error handling for the moment; a better way to code this is shown "
"below)::"
msgstr ""
"很少有函式會竊取參照;兩個值得注意的例外是 :c:func:`PyList_SetItem` 和 :c:"
"func:`PyTuple_SetItem`,它們竊取了對項目的參照(但不是對項目所在的 tuple 或 "
"list 的參照!)。因為有著使用新建立的物件來增加 (populate) tuple 或 list 的習"
"慣,這些函式旨在竊取參照;例如,建立 tuple ``(1, 2, \"three\")`` 的程式碼可以"
"如下所示(先暫時忘記錯誤處理;更好的編寫方式如下所示):"
#: ../../c-api/intro.rst:415
msgid ""
"PyObject *t;\n"
"\n"
"t = PyTuple_New(3);\n"
"PyTuple_SetItem(t, 0, PyLong_FromLong(1L));\n"
"PyTuple_SetItem(t, 1, PyLong_FromLong(2L));\n"
"PyTuple_SetItem(t, 2, PyUnicode_FromString(\"three\"));"
msgstr ""
"PyObject *t;\n"
"\n"
"t = PyTuple_New(3);\n"
"PyTuple_SetItem(t, 0, PyLong_FromLong(1L));\n"
"PyTuple_SetItem(t, 1, PyLong_FromLong(2L));\n"
"PyTuple_SetItem(t, 2, PyUnicode_FromString(\"three\"));"
#: ../../c-api/intro.rst:422
msgid ""
"Here, :c:func:`PyLong_FromLong` returns a new reference which is immediately "
"stolen by :c:func:`PyTuple_SetItem`. When you want to keep using an object "
"although the reference to it will be stolen, use :c:func:`Py_INCREF` to grab "
"another reference before calling the reference-stealing function."
msgstr ""
"這裡 :c:func:`PyLong_FromLong` 會回傳一個新的參照,它立即被 :c:func:"
"`PyTuple_SetItem` 竊取。如果你想繼續使用一個物件,儘管對它的參照將被竊取,請"
"在呼叫參照竊取函式之前使用 :c:func:`Py_INCREF` 來取得另一個參照。"
#: ../../c-api/intro.rst:427
msgid ""
"Incidentally, :c:func:`PyTuple_SetItem` is the *only* way to set tuple "
"items; :c:func:`PySequence_SetItem` and :c:func:`PyObject_SetItem` refuse to "
"do this since tuples are an immutable data type. You should only use :c:"
"func:`PyTuple_SetItem` for tuples that you are creating yourself."
msgstr ""
"附帶地說,:c:func:`PyTuple_SetItem` 是設定 tuple 項目的\\ *唯一*\\ 方法; :c:"
"func:`PySequence_SetItem` 和 :c:func:`PyObject_SetItem` 拒絕這樣做,因為 "
"tuple 是一種不可變 (immutable) 的資料型別。你應該只對你自己建立的 tuple 使"
"用 :c:func:`PyTuple_SetItem`。"
#: ../../c-api/intro.rst:432
msgid ""
"Equivalent code for populating a list can be written using :c:func:"
"`PyList_New` and :c:func:`PyList_SetItem`."
msgstr ""
"可以使用 :c:func:`PyList_New` 和 :c:func:`PyList_SetItem` 編寫用於填充列表的"
"等效程式碼。"
#: ../../c-api/intro.rst:435
msgid ""
"However, in practice, you will rarely use these ways of creating and "
"populating a tuple or list. There's a generic function, :c:func:"
"`Py_BuildValue`, that can create most common objects from C values, directed "
"by a :dfn:`format string`. For example, the above two blocks of code could "
"be replaced by the following (which also takes care of the error checking)::"
msgstr ""
"但是在實際操作中你很少會使用這些方法來建立和增加 tuple 和 list。有一個通用函"
"式 :c:func:`Py_BuildValue` 可以從 C 值建立最常見的物件,由 :dfn:`format "
"string` 引導。例如上面的兩個程式碼可以用以下程式碼替換(它還負責了錯誤檢"
"查): ::"
#: ../../c-api/intro.rst:441
msgid ""
"PyObject *tuple, *list;\n"
"\n"
"tuple = Py_BuildValue(\"(iis)\", 1, 2, \"three\");\n"
"list = Py_BuildValue(\"[iis]\", 1, 2, \"three\");"
msgstr ""
"PyObject *tuple, *list;\n"
"\n"
"tuple = Py_BuildValue(\"(iis)\", 1, 2, \"three\");\n"
"list = Py_BuildValue(\"[iis]\", 1, 2, \"three\");"
#: ../../c-api/intro.rst:446
msgid ""
"It is much more common to use :c:func:`PyObject_SetItem` and friends with "
"items whose references you are only borrowing, like arguments that were "
"passed in to the function you are writing. In that case, their behaviour "
"regarding references is much saner, since you don't have to take a new "
"reference just so you can give that reference away (\"have it be stolen\"). "
"For example, this function sets all items of a list (actually, any mutable "
"sequence) to a given item::"
msgstr ""
"更常見的是以那些借用參照的項目來使用 :c:func:`PyObject_SetItem` 及其系列函"
"式,比如傳遞給你正在編寫的函式的引數。在那種情況下,他們關於參照的行為會比較"
"穩健,因為你不取得新的一個參照就可以放棄參照(「讓它被竊取」)。例如,此函式"
"將 list(實際上是任何可變序列)的所有項目設定於給定項目:"
#: ../../c-api/intro.rst:453
msgid ""
"int\n"
"set_all(PyObject *target, PyObject *item)\n"
"{\n"
" Py_ssize_t i, n;\n"
"\n"
" n = PyObject_Length(target);\n"
" if (n < 0)\n"
" return -1;\n"
" for (i = 0; i < n; i++) {\n"
" PyObject *index = PyLong_FromSsize_t(i);\n"
" if (!index)\n"
" return -1;\n"
" if (PyObject_SetItem(target, index, item) < 0) {\n"
" Py_DECREF(index);\n"
" return -1;\n"
" }\n"
" Py_DECREF(index);\n"
" }\n"
" return 0;\n"
"}"
msgstr ""
"int\n"
"set_all(PyObject *target, PyObject *item)\n"
"{\n"
" Py_ssize_t i, n;\n"
"\n"
" n = PyObject_Length(target);\n"
" if (n < 0)\n"
" return -1;\n"
" for (i = 0; i < n; i++) {\n"
" PyObject *index = PyLong_FromSsize_t(i);\n"
" if (!index)\n"
" return -1;\n"
" if (PyObject_SetItem(target, index, item) < 0) {\n"
" Py_DECREF(index);\n"
" return -1;\n"
" }\n"
" Py_DECREF(index);\n"
" }\n"
" return 0;\n"
"}"
#: ../../c-api/intro.rst:476
msgid ""
"The situation is slightly different for function return values. While "
"passing a reference to most functions does not change your ownership "
"responsibilities for that reference, many functions that return a reference "
"to an object give you ownership of the reference. The reason is simple: in "
"many cases, the returned object is created on the fly, and the reference "
"you get is the only reference to the object. Therefore, the generic "
"functions that return object references, like :c:func:`PyObject_GetItem` "
"and :c:func:`PySequence_GetItem`, always return a new reference (the caller "
"becomes the owner of the reference)."
msgstr ""
"函式回傳值的情況略有不同。雖然傳遞對大多數函式的參照不會改變你對該參照的所有"
"權責任,但許多回傳物件參照的函式會給你該參照的所有權。原因很簡單:在很多情況"
"下,回傳的物件是即時建立的,你獲得的參照是對該物件的唯一參照。因此回傳物件參"
"照的通用函式,如 :c:func:`PyObject_GetItem` 和 :c:func:`PySequence_GetItem`,"
"總是回傳一個新的參照(呼叫者成為參照的所有者)。"
#: ../../c-api/intro.rst:485
msgid ""
"It is important to realize that whether you own a reference returned by a "
"function depends on which function you call only --- *the plumage* (the type "
"of the object passed as an argument to the function) *doesn't enter into it!"
"* Thus, if you extract an item from a list using :c:func:`PyList_GetItem`, "
"you don't own the reference --- but if you obtain the same item from the "
"same list using :c:func:`PySequence_GetItem` (which happens to take exactly "
"the same arguments), you do own a reference to the returned object."
msgstr ""
"重要的是要意識到你是否擁有一個函式回傳的參照只取決於你呼叫哪個函式 --- *羽毛 "
"(plumage)*(作為引數傳遞給函式的物件之型別)\\ *不會進入它!*\\ 因此,如果你"
"使用 :c:func:`PyList_GetItem` 從 list 中提取一個項目,你不會擁有其參照 --- 但"
"如果你使用 :c:func:`PySequence_GetItem` 從同一 list 中取得相同的項目(且恰好"
"使用完全相同的引數),你確實會擁有對回傳物件的參照。"
#: ../../c-api/intro.rst:497
msgid ""
"Here is an example of how you could write a function that computes the sum "
"of the items in a list of integers; once using :c:func:`PyList_GetItem`, "
"and once using :c:func:`PySequence_GetItem`. ::"
msgstr ""
"以下是一個範例,說明如何編寫函式來計算一個整數 list 中項目的總和;一次使用 :"
"c:func:`PyList_GetItem`,一次使用 :c:func:`PySequence_GetItem`: ::"
#: ../../c-api/intro.rst:501
msgid ""
"long\n"
"sum_list(PyObject *list)\n"
"{\n"
" Py_ssize_t i, n;\n"
" long total = 0, value;\n"
" PyObject *item;\n"
"\n"
" n = PyList_Size(list);\n"
" if (n < 0)\n"
" return -1; /* Not a list */\n"
" for (i = 0; i < n; i++) {\n"
" item = PyList_GetItem(list, i); /* Can't fail */\n"
" if (!PyLong_Check(item)) continue; /* Skip non-integers */\n"
" value = PyLong_AsLong(item);\n"
" if (value == -1 && PyErr_Occurred())\n"
" /* Integer too big to fit in a C long, bail out */\n"
" return -1;\n"
" total += value;\n"
" }\n"
" return total;\n"
"}"
msgstr ""
#: ../../c-api/intro.rst:527
msgid ""
"long\n"
"sum_sequence(PyObject *sequence)\n"
"{\n"
" Py_ssize_t i, n;\n"
" long total = 0, value;\n"
" PyObject *item;\n"
" n = PySequence_Length(sequence);\n"
" if (n < 0)\n"
" return -1; /* Has no length */\n"
" for (i = 0; i < n; i++) {\n"
" item = PySequence_GetItem(sequence, i);\n"
" if (item == NULL)\n"
" return -1; /* Not a sequence, or other failure */\n"
" if (PyLong_Check(item)) {\n"
" value = PyLong_AsLong(item);\n"
" Py_DECREF(item);\n"
" if (value == -1 && PyErr_Occurred())\n"
" /* Integer too big to fit in a C long, bail out */\n"
" return -1;\n"
" total += value;\n"
" }\n"
" else {\n"
" Py_DECREF(item); /* Discard reference ownership */\n"
" }\n"
" }\n"
" return total;\n"
"}"
msgstr ""
#: ../../c-api/intro.rst:561
msgid "Types"
msgstr "型別"
#: ../../c-api/intro.rst:563
msgid ""
"There are few other data types that play a significant role in the Python/C "
"API; most are simple C types such as :c:expr:`int`, :c:expr:`long`, :c:expr:"
"`double` and :c:expr:`char*`. A few structure types are used to describe "
"static tables used to list the functions exported by a module or the data "
"attributes of a new object type, and another is used to describe the value "
"of a complex number. These will be discussed together with the functions "
"that use them."
msgstr ""
"有少數幾個其他的資料型別在 Python/C API 中發揮重要作用;大多數是簡單的 C 型"
"別,例如 :c:expr:`int`、:c:expr:`long`、:c:expr:`double` 和 :c:expr:`char*`。"
"一些結構型別被用於描述用於列出模組所匯出的函式或新物件型別的資料屬性的靜態"
"表,其他則用於描述複數的值。這些將與使用它們的函式一起討論。"
#: ../../c-api/intro.rst:573
msgid ""
"A signed integral type such that ``sizeof(Py_ssize_t) == sizeof(size_t)``. "
"C99 doesn't define such a thing directly (size_t is an unsigned integral "
"type). See :pep:`353` for details. ``PY_SSIZE_T_MAX`` is the largest "
"positive value of type :c:type:`Py_ssize_t`."
msgstr ""
"一個帶符號的整數型別,使得 ``sizeof(Py_ssize_t) == sizeof(size_t)``。 C99 沒"
"有直接定義這樣的東西(size_t 是無符號整數型別)。有關詳細資訊,請參閱 :pep:"
"`353`。 ``PY_SSIZE_T_MAX`` 是 :c:type:`Py_ssize_t` 型別的最大正值。"
#: ../../c-api/intro.rst:582
msgid "Exceptions"
msgstr "例外"
#: ../../c-api/intro.rst:584
msgid ""
"The Python programmer only needs to deal with exceptions if specific error "
"handling is required; unhandled exceptions are automatically propagated to "
"the caller, then to the caller's caller, and so on, until they reach the top-"
"level interpreter, where they are reported to the user accompanied by a "
"stack traceback."
msgstr ""
"如果需要特定的錯誤處理,Python 開發者就只需要處理例外;未處理的例外會自動傳遞"
"給呼叫者,然後傳遞給呼叫者的呼叫者,依此類推,直到它們到達頂層直譯器,在那裡"
"它們透過堆疊回溯 (stack trace) 回報給使用者。"
#: ../../c-api/intro.rst:592
msgid ""
"For C programmers, however, error checking always has to be explicit. All "
"functions in the Python/C API can raise exceptions, unless an explicit claim "
"is made otherwise in a function's documentation. In general, when a "
"function encounters an error, it sets an exception, discards any object "
"references that it owns, and returns an error indicator. If not documented "
"otherwise, this indicator is either ``NULL`` or ``-1``, depending on the "
"function's return type. A few functions return a Boolean true/false result, "
"with false indicating an error. Very few functions return no explicit error "
"indicator or have an ambiguous return value, and require explicit testing "
"for errors with :c:func:`PyErr_Occurred`. These exceptions are always "
"explicitly documented."
msgstr ""
"然而,對於 C 開發者來說,錯誤檢查總是必須是顯式的。除非在函式的文件中另有明確"
"聲明,否則 Python/C API 中的所有函式都可以引發例外。通常當一個函式遇到錯誤"
"時,它會設定一個例外,丟棄它擁有的任何物件參照,並回傳一個錯誤指示器。如果沒"
"有另外文件記錄,這個指示器要麼是 ``NULL`` 不然就是 ``-1``,取決於函式的回傳型"
"別。有些函式會回傳布林值 true/false 結果,false 表示錯誤。很少有函式不回傳明"
"確的錯誤指示器或者有不明確的回傳值,而需要使用 :c:func:`PyErr_Occurred` 明確"
"測試錯誤。這些例外都會被明確地記錄於文件。"
#: ../../c-api/intro.rst:607
msgid ""
"Exception state is maintained in per-thread storage (this is equivalent to "
"using global storage in an unthreaded application). A thread can be in one "
"of two states: an exception has occurred, or not. The function :c:func:"
"`PyErr_Occurred` can be used to check for this: it returns a borrowed "
"reference to the exception type object when an exception has occurred, and "
"``NULL`` otherwise. There are a number of functions to set the exception "
"state: :c:func:`PyErr_SetString` is the most common (though not the most "
"general) function to set the exception state, and :c:func:`PyErr_Clear` "
"clears the exception state."
msgstr ""
"例外的狀態會在個別執行緒的存儲空間 (per-thread storage) 中維護(這相當於在非"
"執行緒應用程式中使用全域存儲空間)。執行緒可以處於兩種狀態之一:發生例外或未"
"發生例外。函式 :c:func:`PyErr_Occurred` 可用於檢查這一點:當例外發生時,它回"
"傳對例外型別物件的借用參照,否則回傳 ``NULL``。設定例外狀態的函式有很多::c:"
"func:`PyErr_SetString` 是最常見的(儘管不是最通用的)設定例外狀態的函式,而 :"
"c:func:`PyErr_Clear` 是用來清除例外狀態。"
#: ../../c-api/intro.rst:617
msgid ""
"The full exception state consists of three objects (all of which can be "
"``NULL``): the exception type, the corresponding exception value, and the "