-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathextending.po
More file actions
1523 lines (1297 loc) · 149 KB
/
Copy pathextending.po
File metadata and controls
1523 lines (1297 loc) · 149 KB
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 Python Software Foundation
# This file is distributed under the same license as the Python package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
# Translators:
# Alireza Shabani (Revisto) <theRevisto@gmail.com>, 2025
# Rafael Fontenelle <rffontenelle@gmail.com>, 2025
# Sepehr Rasouli <sepehrrasouli06@gmail.com>, 2026
#
msgid ""
msgstr ""
"Project-Id-Version: Python 3.14\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2026-09-21 11:59+0000\n"
"PO-Revision-Date: 2021-06-28 00:51+0000\n"
"Last-Translator: Sepehr Rasouli <sepehrrasouli06@gmail.com>, 2026\n"
"Language-Team: Persian (https://github.com/python/python-docs-fa/fa/)\n"
"Language: fa\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
msgid "Extending Python with C or C++"
msgstr "توسعه پایتون با C یا C++"
msgid "It is quite easy to add new built-in modules to Python, if you know how to program in C. Such :dfn:`extension modules` can do two things that can't be done directly in Python: they can implement new built-in object types, and they can call C library functions and system calls."
msgstr "اگر بدانید چگونه به زبان C برنامهنویسی کنید، افزودن ماژولهای توکار جدید به پایتون کاملاً آسان است. چنین :dfn:`ماژولهای توسعهای (extension modules)` میتوانند دو کاری انجام دهند که مستقیماً در پایتون امکانپذیر نیست: میتوانند نوعهای شیء توکار جدید را پیادهسازی کنند و میتوانند توابع کتابخانه C و فراخوانهای سیستمی را فراخوانی کنند."
msgid "To support extensions, the Python API (Application Programmers Interface) defines a set of functions, macros and variables that provide access to most aspects of the Python run-time system. The Python API is incorporated in a C source file by including the header ``\"Python.h\"``."
msgstr "برای پشتیبانی از توسعهها، API پایتون (رابط برنامهنویسی کاربردی) مجموعهای از توابع، ماکروها و متغیرها را تعریف میکند که دسترسی به بیشتر جنبههای سیستم زمان اجرای پایتون را فراهم میکنند. API پایتون با گنجاندن سرآیند ``\"Python.h\"`` در یک پرونده منبع C ادغام میشود."
msgid "The compilation of an extension module depends on its intended use as well as on your system setup; details are given in later chapters."
msgstr "کامپایل یک ماژول توسعهای به کاربرد موردنظر آن و همچنین به پیکربندی سیستم شما بستگی دارد؛ جزئیات در فصلهای بعدی ارائه شده است."
msgid "The C extension interface is specific to CPython, and extension modules do not work on other Python implementations. In many cases, it is possible to avoid writing C extensions and preserve portability to other implementations. For example, if your use case is calling C library functions or system calls, you should consider using the :mod:`ctypes` module or the `cffi <https://cffi.readthedocs.io/>`_ library rather than writing custom C code. These modules let you write Python code to interface with C code and are more portable between implementations of Python than writing and compiling a C extension module."
msgstr "رابط توسعه C مختص سیپایتون است و ماژولهای توسعهای روی پیادهسازیهای دیگر پایتون کار نمیکنند. در بسیاری از موارد میتوان از نوشتن توسعههای C پرهیز کرد و قابلیت حمل به پیادهسازیهای دیگر را حفظ نمود. برای مثال، اگر مورد استفادهی شما فراخوانی توابع کتابخانه C یا فراخوانیهای سیستمی است، باید به جای نوشتن کد C سفارشی، استفاده از ماژول :mod:`ctypes` یا کتابخانهی `cffi <https://cffi.readthedocs.io/>`_ را در نظر بگیرید. این ماژولها به شما اجازه میدهند برای برقراری رابط با کد C، کد پایتون بنویسید و نسبت به نوشتن و کامپایل کردن یک ماژول توسعهای C، بین پیادهسازیهای پایتون قابلیت حمل بیشتری دارند."
msgid "A Simple Example"
msgstr "یک مثال ساده"
msgid "Let's create an extension module called ``spam`` (the favorite food of Monty Python fans...) and let's say we want to create a Python interface to the C library function :c:func:`system` [#]_. This function takes a null-terminated character string as argument and returns an integer. We want this function to be callable from Python as follows:"
msgstr "بیایید یک ماژول توسعهای به نام ``spam`` بسازیم (غذای مورد علاقهی طرفداران مونتی پایتون...) و فرض کنیم میخواهیم یک رابط پایتونی برای تابع کتابخانهی C یعنی :c:func:`system` [#]_ بسازیم. این تابع یک رشتهی نویسهای پایانیافته با نویسهی تهی را بهعنوان آرگومان میگیرد و یک عدد صحیح برمیگرداند. میخواهیم این تابع از پایتون به صورت زیر فراخوانیپذیر باشد:"
msgid ""
">>> import spam\n"
">>> status = spam.system(\"ls -l\")"
msgstr ""
">>> import spam\n"
">>> status = spam.system(\"ls -l\")"
msgid "Begin by creating a file :file:`spammodule.c`. (Historically, if a module is called ``spam``, the C file containing its implementation is called :file:`spammodule.c`; if the module name is very long, like ``spammify``, the module name can be just :file:`spammify.c`.)"
msgstr "کار را با ایجاد یک پروندهی :file:`spammodule.c` آغاز کنید. (از نظر تاریخی، اگر ماژولی ``spam`` نام داشته باشد، پروندهی C حاوی پیادهسازی آن :file:`spammodule.c` نامیده میشود؛ اگر نام ماژول بسیار طولانی باشد، مانند ``spammify``، نام ماژول میتواند تنها :file:`spammify.c` باشد.)"
msgid "The first two lines of our file can be::"
msgstr "دو سطر نخست پروندهی ما میتواند باشد::"
msgid ""
"#define PY_SSIZE_T_CLEAN\n"
"#include <Python.h>"
msgstr ""
"#define PY_SSIZE_T_CLEAN\n"
"#include <Python.h>"
msgid "which pulls in the Python API (you can add a comment describing the purpose of the module and a copyright notice if you like)."
msgstr "که API پایتون را وارد میکند (در صورت تمایل میتوانید کامنتی که هدف ماژول را شرح میدهد و یک اعلان حق نشر اضافه کنید)."
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 "از آنجا که پایتون ممکن است برخی تعریفهای پیشپردازنده را تعریف کند که در برخی سیستمها بر سرآیندهای استاندارد تأثیر میگذارند، شما *باید* :file:`Python.h` را پیش از گنجاندن هر سرآیند استانداردی بگنجانید."
msgid "``#define PY_SSIZE_T_CLEAN`` was used to indicate that ``Py_ssize_t`` should be used in some APIs instead of ``int``. It is not necessary since Python 3.13, but we keep it here for backward compatibility. See :ref:`arg-parsing-string-and-buffers` for a description of this macro."
msgstr "``#define PY_SSIZE_T_CLEAN`` برای نشان دادن اینکه ``Py_ssize_t`` باید در برخی APIها بهجای ``int`` استفاده شود، به کار میرفت. از پایتون 3.13 به بعد دیگر ضروری نیست، اما ما آن را برای سازگاری با گذشته در اینجا نگه میداریم. برای توضیح این ماکرو به :ref:`arg-parsing-string-and-buffers` مراجعه کنید."
msgid "All user-visible symbols defined by :file:`Python.h` have a prefix of ``Py`` or ``PY``, except those defined in standard header files."
msgstr "تمام نمادهای قابل مشاهده توسط کاربر که توسط :file:`Python.h` تعریف شدهاند، پیشوند ``Py`` یا ``PY`` دارند؛ بهجز نمادهایی که در پروندههای سرآیند استاندارد تعریف شدهاند."
msgid "For backward compatibility, :file:`Python.h` includes several standard header files. C extensions should include the standard headers that they use, and should not rely on these implicit includes. If using the limited C API version 3.13 or newer, the implicit includes are:"
msgstr "برای سازگاری با نسخههای پیشین، :file:`Python.h` چندین پروندهی سرآیند استاندارد را include میکند. افزونههای C باید سرآیندهای استاندارد مورد استفادهی خود را include کنند و نباید به این includeهای ضمنی تکیه کنند. اگر از C API محدود نسخهی 3.13 یا جدیدتر استفاده میکنید، includeهای ضمنی عبارتاند از:"
msgid "``<assert.h>``"
msgstr "``<assert.h>``"
msgid "``<intrin.h>`` (on Windows)"
msgstr "``<intrin.h>`` (در ویندوز)"
msgid "``<inttypes.h>``"
msgstr "``<inttypes.h>``"
msgid "``<limits.h>``"
msgstr "``<limits.h>``"
msgid "``<math.h>``"
msgstr "``<math.h>``"
msgid "``<stdarg.h>``"
msgstr "``<stdarg.h>``"
msgid "``<wchar.h>``"
msgstr "``<wchar.h>``"
msgid "``<sys/types.h>`` (if present)"
msgstr "``<sys/types.h>`` (در صورت وجود)"
msgid "If :c:macro:`Py_LIMITED_API` is not defined, or is set to version 3.12 or older, the headers below are also included:"
msgstr "اگر :c:macro:`Py_LIMITED_API` تعریف نشده باشد، یا روی نسخه 3.12 یا قدیمیتر تنظیم شده باشد، سرآیندهای زیر نیز گنجانده میشوند:"
msgid "``<ctype.h>``"
msgstr "``<ctype.h>``"
msgid "``<unistd.h>`` (on POSIX)"
msgstr "``<unistd.h>`` (در POSIX)"
msgid "If :c:macro:`Py_LIMITED_API` is not defined, or is set to version 3.10 or older, the headers below are also included:"
msgstr "اگر :c:macro:`Py_LIMITED_API` تعریف نشده باشد، یا روی نسخه 3.10 یا قدیمیتر تنظیم شده باشد، سرآیندهای زیر نیز گنجانده میشوند:"
msgid "``<errno.h>``"
msgstr "``<errno.h>``"
msgid "``<stdio.h>``"
msgstr "``<stdio.h>``"
msgid "``<stdlib.h>``"
msgstr "``<stdlib.h>``"
msgid "``<string.h>``"
msgstr "``<string.h>``"
msgid "The next thing we add to our module file is the C function that will be called when the Python expression ``spam.system(string)`` is evaluated (we'll see shortly how it ends up being called)::"
msgstr "مورد بعدی که به پرونده ماژول خود اضافه میکنیم، تابع C است که هنگام ارزیابی عبارت پایتون ``spam.system(string)`` فراخوانی خواهد شد (بهزودی خواهیم دید که چگونه در نهایت فراخوانی میشود)::"
msgid ""
"static PyObject *\n"
"spam_system(PyObject *self, PyObject *args)\n"
"{\n"
" const char *command;\n"
" int sts;\n"
"\n"
" if (!PyArg_ParseTuple(args, \"s\", &command))\n"
" return NULL;\n"
" sts = system(command);\n"
" return PyLong_FromLong(sts);\n"
"}"
msgstr ""
"static PyObject *\n"
"spam_system(PyObject *self, PyObject *args)\n"
"{\n"
" const char *command;\n"
" int sts;\n"
"\n"
" if (!PyArg_ParseTuple(args, \"s\", &command))\n"
" return NULL;\n"
" sts = system(command);\n"
" return PyLong_FromLong(sts);\n"
"}"
msgid "There is a straightforward translation from the argument list in Python (for example, the single expression ``\"ls -l\"``) to the arguments passed to the C function. The C function always has two arguments, conventionally named *self* and *args*."
msgstr "ترجمهای سرراست از فهرست آرگومانها در پایتون (برای مثال، عبارت واحد ``\"ls -l\"``) به آرگومانهای پاسدادهشده به تابع C وجود دارد. تابع C همیشه دو آرگومان دارد که بهطور قراردادی *self* و *args* نامیده میشوند."
msgid "The *self* argument points to the module object for module-level functions; for a method it would point to the object instance."
msgstr "آرگومان *self* برای توابع در سطح ماژول به شیء ماژول اشاره میکند؛ برای یک متد، به نمونهی شیء اشاره خواهد کرد."
msgid "The *args* argument will be a pointer to a Python tuple object containing the arguments. Each item of the tuple corresponds to an argument in the call's argument list. The arguments are Python objects --- in order to do anything with them in our C function we have to convert them to C values. The function :c:func:`PyArg_ParseTuple` in the Python API checks the argument types and converts them to C values. It uses a template string to determine the required types of the arguments as well as the types of the C variables into which to store the converted values. More about this later."
msgstr "آرگومان *args* اشارهگری به یک شیء تاپل پایتون حاوی آرگومانها خواهد بود. هر آیتم از تاپل با یک آرگومان در فهرست آرگومانهای فراخوانی مطابقت دارد. آرگومانها اشیاء پایتون هستند --- برای اینکه بتوانیم در تابع C خود کاری با آنها انجام دهیم، باید آنها را به مقادیر C تبدیل کنیم. تابع :c:func:`PyArg_ParseTuple` در API پایتون انواع آرگومانها را بررسی میکند و آنها را به مقادیر C تبدیل میکند. این تابع از یک رشتهی قالب برای تعیین انواع مورد نیاز آرگومانها و همچنین انواع متغیرهای C که مقادیر تبدیلشده در آنها ذخیره میشوند، استفاده میکند. در ادامه بیشتر دربارهی این موضوع توضیح داده خواهد شد."
msgid ":c:func:`PyArg_ParseTuple` returns true (nonzero) if all arguments have the right type and its components have been stored in the variables whose addresses are passed. It returns false (zero) if an invalid argument list was passed. In the latter case it also raises an appropriate exception so the calling function can return ``NULL`` immediately (as we saw in the example)."
msgstr "تابع :c:func:`PyArg_ParseTuple` در صورتی مقدار درست (غیرصفر) را برمیگرداند که همهی آرگومانها نوع درست را داشته باشند و اجزای آن در متغیرهایی که آدرسهایشان پاس داده شده، ذخیره شده باشند. اگر فهرست آرگومان نامعتبری پاس داده شده باشد، مقدار نادرست (صفر) را برمیگرداند. در حالت دوم، همچنین یک استثنای مناسب ایجاد میکند تا تابع فراخواننده بتواند بلافاصله ``NULL`` را برگرداند (همانطور که در مثال دیدیم)."
msgid "Intermezzo: Errors and Exceptions"
msgstr "میانپرده: خطاها و استثناها"
msgid "An important convention throughout the Python interpreter is the following: when a function fails, it should set an exception condition and return an error value (usually ``-1`` or a ``NULL`` pointer). Exception information is stored in three members of the interpreter's thread state. These are ``NULL`` if there is no exception. Otherwise they are the C equivalents of the members of the Python tuple returned by :meth:`sys.exc_info`. These are the exception type, exception instance, and a traceback object. It is important to know about them to understand how errors are passed around."
msgstr "یک قرارداد مهم در سراسر مفسر پایتون به شرح زیر است: هنگامی که تابعی شکست میخورد، باید وضعیت استثنا را تنظیم کند و یک مقدار خطا (معمولاً ``-1`` یا یک اشارهگر ``NULL``) برگرداند. اطلاعات استثنا در سه عضو از وضعیت نخ مفسر ذخیره میشود. در صورت نبود استثنا، اینها ``NULL`` هستند. در غیر این صورت، آنها معادلهای C اعضای تاپل پایتونیای هستند که توسط :meth:`sys.exc_info` برگردانده میشود. اینها عبارتاند از نوع استثنا، نمونه استثنا، و یک شیء ردگیری. دانستن این موارد برای درک چگونگی انتقال خطاها اهمیت دارد."
msgid "The Python API defines a number of functions to set various types of exceptions."
msgstr "API پایتون تعدادی تابع برای تنظیم انواع مختلف استثناها تعریف میکند."
msgid "The most common one is :c:func:`PyErr_SetString`. Its arguments are an exception object and a C string. The exception object is usually a predefined object like :c:data:`PyExc_ZeroDivisionError`. The C string indicates the cause of the error and is converted to a Python string object and stored as the \"associated value\" of the exception."
msgstr "رایجترین آنها :c:func:`PyErr_SetString` است. آرگومانهای آن یک شیء استثنا و یک رشته C هستند. شیء استثنا معمولاً شیء از پیش تعریفشدهای مانند :c:data:`PyExc_ZeroDivisionError` است. رشته C علت خطا را نشان میدهد، به یک شیء رشته پایتون تبدیل میشود و به عنوان «مقدار مرتبط» استثنا ذخیره میشود."
msgid "Another useful function is :c:func:`PyErr_SetFromErrno`, which only takes an exception argument and constructs the associated value by inspection of the global variable :c:data:`errno`. The most general function is :c:func:`PyErr_SetObject`, which takes two object arguments, the exception and its associated value. You don't need to :c:func:`Py_INCREF` the objects passed to any of these functions."
msgstr "تابع مفید دیگر :c:func:`PyErr_SetFromErrno` است که تنها یک آرگومان استثنا میگیرد و مقدار مرتبط را با بررسی متغیر سراسری :c:data:`errno` میسازد. عمومیترین تابع :c:func:`PyErr_SetObject` است که دو آرگومان شیء میگیرد: استثنا و مقدار مرتبط با آن. نیازی نیست اشیاء ارسالشده به هیچیک از این توابع را :c:func:`Py_INCREF` کنید."
msgid "You can test non-destructively whether an exception has been set with :c:func:`PyErr_Occurred`. This returns the current exception object, or ``NULL`` if no exception has occurred. You normally don't need to call :c:func:`PyErr_Occurred` to see whether an error occurred in a function call, since you should be able to tell from the return value."
msgstr "میتوانید بهصورت غیرمخرب با :c:func:`PyErr_Occurred` بررسی کنید که آیا استثنایی تنظیم شده است یا خیر. این تابع شیء استثنای فعلی را برمیگرداند، یا اگر استثنایی رخ نداده باشد، ``NULL``. شما معمولاً برای فهمیدن اینکه آیا خطایی در فراخوانی یک تابع رخ داده است، نیازی به فراخوانی :c:func:`PyErr_Occurred` ندارید، زیرا باید بتوانید از روی مقدار بازگشتی متوجه آن شوید."
msgid "When a function *f* that calls another function *g* detects that the latter fails, *f* should itself return an error value (usually ``NULL`` or ``-1``). It should *not* call one of the ``PyErr_*`` functions --- one has already been called by *g*. *f*'s caller is then supposed to also return an error indication to *its* caller, again *without* calling ``PyErr_*``, and so on --- the most detailed cause of the error was already reported by the function that first detected it. Once the error reaches the Python interpreter's main loop, this aborts the currently executing Python code and tries to find an exception handler specified by the Python programmer."
msgstr "وقتی تابعی *f* که تابع دیگری به نام *g* را فراخوانی میکند تشخیص میدهد که آن تابع شکست خورده است، *f* باید خودش یک مقدار خطا برگرداند (معمولاً ``NULL`` یا ``-1``). این تابع *نباید* یکی از توابع ``PyErr_*`` را فراخوانی کند --- یکی از آنها قبلاً توسط *g* فراخوانی شده است. سپس فراخوانکننده *f* نیز باید نشانهای از خطا را به فراخوانکننده *خودش* برگرداند، باز هم *بدون* فراخوانی ``PyErr_*``، و به همین ترتیب --- دقیقترین علت خطا قبلاً توسط تابعی که نخست آن را تشخیص داده است گزارش شده است. وقتی خطا به حلقه اصلی مفسر پایتون میرسد، این امر کد پایتون در حال اجرا را متوقف میکند و تلاش میکند هندلر استثنایی را که برنامهنویس پایتون تعیین کرده است بیابد."
msgid "(There are situations where a module can actually give a more detailed error message by calling another ``PyErr_*`` function, and in such cases it is fine to do so. As a general rule, however, this is not necessary, and can cause information about the cause of the error to be lost: most operations can fail for a variety of reasons.)"
msgstr "(گاهی موقعیتهایی پیش میآید که یک ماژول میتواند در واقع با فراخوانی تابع دیگری از نوع ``PyErr_*`` پیام خطای دقیقتری ارائه دهد، و در چنین مواردی انجام این کار اشکالی ندارد. با این حال، بهعنوان یک قاعده کلی، این کار ضروری نیست و میتواند باعث از دست رفتن اطلاعاتی درباره علت خطا شود: بیشتر عملیات میتوانند به دلایل مختلفی شکست بخورند.)"
msgid "To ignore an exception set by a function call that failed, the exception condition must be cleared explicitly by calling :c:func:`PyErr_Clear`. The only time C code should call :c:func:`PyErr_Clear` is if it doesn't want to pass the error on to the interpreter but wants to handle it completely by itself (possibly by trying something else, or pretending nothing went wrong)."
msgstr "برای نادیدهگرفتن استثنایی که توسط یک فراخوانی تابع ناموفق تنظیم شده است، باید وضعیت استثنا بهطور صریح با فراخوانی :c:func:`PyErr_Clear` پاک شود. کد C تنها باید زمانی :c:func:`PyErr_Clear` را فراخوانی کند که نخواهد خطا را به مفسر منتقل کند، بلکه بخواهد خودش بهطور کامل آن را مدیریت کند (احتمالاً با امتحان کردن چیز دیگری، یا وانمود کردن به اینکه هیچ مشکلی پیش نیامده است)."
msgid "Every failing :c:func:`malloc` call must be turned into an exception --- the direct caller of :c:func:`malloc` (or :c:func:`realloc`) must call :c:func:`PyErr_NoMemory` and return a failure indicator itself. All the object-creating functions (for example, :c:func:`PyLong_FromLong`) already do this, so this note is only relevant to those who call :c:func:`malloc` directly."
msgstr "هر فراخوانی ناموفق :c:func:`malloc` باید به یک استثنا تبدیل شود --- فراخوانیکنندهی مستقیم :c:func:`malloc` (یا :c:func:`realloc`) باید :c:func:`PyErr_NoMemory` را فراخوانی کند و خودش نشانگر شکست را برگرداند. همهی توابع ایجادکنندهی شیء (برای مثال، :c:func:`PyLong_FromLong`) از قبل این کار را انجام میدهند، بنابراین این نکته فقط به کسانی مربوط است که :c:func:`malloc` را مستقیماً فراخوانی میکنند."
msgid "Also note that, with the important exception of :c:func:`PyArg_ParseTuple` and friends, functions that return an integer status usually return a positive value or zero for success and ``-1`` for failure, like Unix system calls."
msgstr "همچنین توجه داشته باشید که، با استثنای مهم :c:func:`PyArg_ParseTuple` و امثال آن، توابعی که یک وضعیت عدد صحیح برمیگردانند معمولاً مانند فراخوانیهای سیستمی یونیکس، برای موفقیت مقداری مثبت یا صفر و برای شکست ``-1`` برمیگردانند."
msgid "Finally, be careful to clean up garbage (by making :c:func:`Py_XDECREF` or :c:func:`Py_DECREF` calls for objects you have already created) when you return an error indicator!"
msgstr "در نهایت، هنگامی که نشانگر خطا را برمیگردانید، مراقب باشید که زبالهها را پاکسازی کنید (با انجام فراخوانیهای :c:func:`Py_XDECREF` یا :c:func:`Py_DECREF` برای اشیایی که از قبل ایجاد کردهاید)!"
msgid "The choice of which exception to raise is entirely yours. There are predeclared C objects corresponding to all built-in Python exceptions, such as :c:data:`PyExc_ZeroDivisionError`, which you can use directly. Of course, you should choose exceptions wisely --- don't use :c:data:`PyExc_TypeError` to mean that a file couldn't be opened (that should probably be :c:data:`PyExc_OSError`). If something's wrong with the argument list, the :c:func:`PyArg_ParseTuple` function usually raises :c:data:`PyExc_TypeError`. If you have an argument whose value must be in a particular range or must satisfy other conditions, :c:data:`PyExc_ValueError` is appropriate."
msgstr "انتخاب اینکه کدام استثنا را برافرازید، کاملاً با شماست. اشیاء C از پیش اعلانشدهای متناظر با تمام استثناهای توکار پایتون وجود دارند، مانند :c:data:`PyExc_ZeroDivisionError`، که میتوانید مستقیماً از آنها استفاده کنید. البته، باید استثناها را هوشمندانه انتخاب کنید --- از :c:data:`PyExc_TypeError` برای بیان اینکه پروندهای نتوانست باز شود استفاده نکنید (آن مورد احتمالاً باید :c:data:`PyExc_OSError` باشد). اگر مشکلی در فهرست آرگومانها وجود داشته باشد، تابع :c:func:`PyArg_ParseTuple` معمولاً استثنای :c:data:`PyExc_TypeError` را برمیافرازد. اگر آرگومانی داشته باشید که مقدار آن باید در محدودهی خاصی باشد یا باید شرایط دیگری را برآورده کند، :c:data:`PyExc_ValueError` مناسب است."
msgid "You can also define a new exception that is unique to your module. The simplest way to do this is to declare a static global object variable at the beginning of the file::"
msgstr "شما همچنین میتوانید یک استثنای جدید تعریف کنید که مختص ماژول شما باشد. سادهترین راه برای انجام این کار، اعلان یک متغیر شیء سراسری ایستا در ابتدای پرونده است::"
msgid "static PyObject *SpamError = NULL;"
msgstr "static PyObject *SpamError = NULL;"
msgid "and initialize it by calling :c:func:`PyErr_NewException` in the module's :c:data:`Py_mod_exec` function (:c:func:`!spam_module_exec`)::"
msgstr "و آن را با فراخوانی :c:func:`PyErr_NewException` در تابع :c:data:`Py_mod_exec` ماژول (:c:func:`!spam_module_exec`) مقداردهی اولیه کنید::"
msgid "SpamError = PyErr_NewException(\"spam.error\", NULL, NULL);"
msgstr "SpamError = PyErr_NewException(\"spam.error\", NULL, NULL);"
msgid "Since :c:data:`!SpamError` is a global variable, it will be overwritten every time the module is reinitialized, when the :c:data:`Py_mod_exec` function is called."
msgstr "از آنجا که :c:data:`!SpamError` یک متغیر سراسری است، هر بار که ماژول مجدداً مقداردهی اولیه میشود — یعنی هنگامی که تابع :c:data:`Py_mod_exec` فراخوانی میشود — بازنویسی خواهد شد."
msgid "For now, let's avoid the issue: we will block repeated initialization by raising an :py:exc:`ImportError`::"
msgstr "فعلاً، اجازه دهید از این مسئله اجتناب کنیم: راهاندازی مکرر را با پرتاب :py:exc:`ImportError` مسدود خواهیم کرد::"
msgid ""
"static PyObject *SpamError = NULL;\n"
"\n"
"static int\n"
"spam_module_exec(PyObject *m)\n"
"{\n"
" if (SpamError != NULL) {\n"
" PyErr_SetString(PyExc_ImportError,\n"
" \"cannot initialize spam module more than once\");\n"
" return -1;\n"
" }\n"
" SpamError = PyErr_NewException(\"spam.error\", NULL, NULL);\n"
" if (PyModule_AddObjectRef(m, \"SpamError\", SpamError) < 0) {\n"
" return -1;\n"
" }\n"
"\n"
" return 0;\n"
"}\n"
"\n"
"static PyModuleDef_Slot spam_module_slots[] = {\n"
" {Py_mod_exec, spam_module_exec},\n"
" {0, NULL}\n"
"};\n"
"\n"
"static struct PyModuleDef spam_module = {\n"
" .m_base = PyModuleDef_HEAD_INIT,\n"
" .m_name = \"spam\",\n"
" .m_size = 0, // non-negative\n"
" .m_slots = spam_module_slots,\n"
"};\n"
"\n"
"PyMODINIT_FUNC\n"
"PyInit_spam(void)\n"
"{\n"
" return PyModuleDef_Init(&spam_module);\n"
"}"
msgstr ""
"static PyObject *SpamError = NULL;\n"
"\n"
"static int\n"
"spam_module_exec(PyObject *m)\n"
"{\n"
" if (SpamError != NULL) {\n"
" PyErr_SetString(PyExc_ImportError,\n"
" \"cannot initialize spam module more than once\");\n"
" return -1;\n"
" }\n"
" SpamError = PyErr_NewException(\"spam.error\", NULL, NULL);\n"
" if (PyModule_AddObjectRef(m, \"SpamError\", SpamError) < 0) {\n"
" return -1;\n"
" }\n"
"\n"
" return 0;\n"
"}\n"
"\n"
"static PyModuleDef_Slot spam_module_slots[] = {\n"
" {Py_mod_exec, spam_module_exec},\n"
" {0, NULL}\n"
"};\n"
"\n"
"static struct PyModuleDef spam_module = {\n"
" .m_base = PyModuleDef_HEAD_INIT,\n"
" .m_name = \"spam\",\n"
" .m_size = 0, // non-negative\n"
" .m_slots = spam_module_slots,\n"
"};\n"
"\n"
"PyMODINIT_FUNC\n"
"PyInit_spam(void)\n"
"{\n"
" return PyModuleDef_Init(&spam_module);\n"
"}"
msgid "Note that the Python name for the exception object is :exc:`!spam.error`. The :c:func:`PyErr_NewException` function may create a class with the base class being :exc:`Exception` (unless another class is passed in instead of ``NULL``), described in :ref:`bltin-exceptions`."
msgstr "توجه داشته باشید که نام پایتونی شیء استثنا :exc:`!spam.error` است. تابع :c:func:`PyErr_NewException` میتواند کلاسی ایجاد کند که کلاس پایهی آن :exc:`Exception` باشد (مگر اینکه کلاس دیگری به جای ``NULL`` ارسال شود)؛ این کلاس پایه در :ref:`bltin-exceptions` توصیف شده است."
msgid "Note also that the :c:data:`!SpamError` variable retains a reference to the newly created exception class; this is intentional! Since the exception could be removed from the module by external code, an owned reference to the class is needed to ensure that it will not be discarded, causing :c:data:`!SpamError` to become a dangling pointer. Should it become a dangling pointer, C code which raises the exception could cause a core dump or other unintended side effects."
msgstr "همچنین توجه داشته باشید که متغیر :c:data:`!SpamError` ارجاعی به کلاس استثنای بهتازگی ایجادشده را نگه میدارد؛ این کار عمدی است! از آنجا که ممکن است کد خارجی استثنا را از ماژول حذف کند، به یک ارجاع مالکانه (owned reference) به کلاس نیاز است تا اطمینان حاصل شود که کلاس دور انداخته نمیشود و در نتیجه :c:data:`!SpamError` به اشارهگر سرگردان (dangling pointer) تبدیل نمیشود. اگر این اشارهگر سرگردان شود، کد C که استثنا را برمیانگیزد ممکن است باعث برونریزی هسته یا سایر اثرات جانبی ناخواسته شود."
msgid "For now, the :c:func:`Py_DECREF` call to remove this reference is missing. Even when the Python interpreter shuts down, the global :c:data:`!SpamError` variable will not be garbage-collected. It will \"leak\". We did, however, ensure that this will happen at most once per process."
msgstr "در حال حاضر، فراخوانی :c:func:`Py_DECREF` برای حذف این ارجاع وجود ندارد. حتی هنگام خاموش شدن مفسر پایتون، متغیر سراسری :c:data:`!SpamError` زبالهروبی نخواهد شد. این متغیر «نشت» خواهد کرد. با این حال، اطمینان حاصل کردیم که این اتفاق حداکثر یک بار به ازای هر فرایند رخ خواهد داد."
msgid "We discuss the use of :c:macro:`PyMODINIT_FUNC` as a function return type later in this sample."
msgstr "استفاده از :c:macro:`PyMODINIT_FUNC` بهعنوان نوع بازگشتی تابع را در ادامهی این نمونه بحث میکنیم."
msgid "The :exc:`!spam.error` exception can be raised in your extension module using a call to :c:func:`PyErr_SetString` as shown below::"
msgstr "میتوانید استثنای :exc:`!spam.error` را در ماژول توسعهای خود با فراخوانی :c:func:`PyErr_SetString` ایجاد کنید، همانطور که در ادامه نشان داده شده است::"
msgid ""
"static PyObject *\n"
"spam_system(PyObject *self, PyObject *args)\n"
"{\n"
" const char *command;\n"
" int sts;\n"
"\n"
" if (!PyArg_ParseTuple(args, \"s\", &command))\n"
" return NULL;\n"
" sts = system(command);\n"
" if (sts < 0) {\n"
" PyErr_SetString(SpamError, \"System command failed\");\n"
" return NULL;\n"
" }\n"
" return PyLong_FromLong(sts);\n"
"}"
msgstr ""
"static PyObject *\n"
"spam_system(PyObject *self, PyObject *args)\n"
"{\n"
" const char *command;\n"
" int sts;\n"
"\n"
" if (!PyArg_ParseTuple(args, \"s\", &command))\n"
" return NULL;\n"
" sts = system(command);\n"
" if (sts < 0) {\n"
" PyErr_SetString(SpamError, \"System command failed\");\n"
" return NULL;\n"
" }\n"
" return PyLong_FromLong(sts);\n"
"}"
msgid "Back to the Example"
msgstr "بازگشت به مثال"
msgid "Going back to our example function, you should now be able to understand this statement::"
msgstr "با بازگشت به تابع مثال خودمان، اکنون باید بتوانید این دستور را درک کنید::"
msgid ""
"if (!PyArg_ParseTuple(args, \"s\", &command))\n"
" return NULL;"
msgstr ""
"if (!PyArg_ParseTuple(args, \"s\", &command))\n"
" return NULL;"
msgid "It returns ``NULL`` (the error indicator for functions returning object pointers) if an error is detected in the argument list, relying on the exception set by :c:func:`PyArg_ParseTuple`. Otherwise the string value of the argument has been copied to the local variable :c:data:`!command`. This is a pointer assignment and you are not supposed to modify the string to which it points (so in Standard C, the variable :c:data:`!command` should properly be declared as ``const char *command``)."
msgstr "در صورت تشخیص خطا در فهرست آرگومانها، این تابع ``NULL`` (نشانگر خطا برای توابعی که اشارهگر شیء برمیگردانند) را برمیگرداند و به استثنای تنظیمشده توسط :c:func:`PyArg_ParseTuple` تکیه میکند. در غیر این صورت، مقدار رشتهی آرگومان به متغیر محلی :c:data:`!command` کپی شده است. این یک انتساب اشارهگر است و شما نباید رشتهای را که به آن اشاره میکند تغییر دهید (بنابراین در C استاندارد، متغیر :c:data:`!command` باید بهدرستی بهصورت ``const char *command`` اعلان شود)."
msgid "The next statement is a call to the Unix function :c:func:`system`, passing it the string we just got from :c:func:`PyArg_ParseTuple`::"
msgstr "دستور بعدی، تابع یونیکسی :c:func:`system` را فراخوانی میکند و رشتهای را که بهتازگی از :c:func:`PyArg_ParseTuple` دریافت کردهایم به آن پاس میدهد::"
msgid "sts = system(command);"
msgstr "sts = system(command);"
msgid "Our :func:`!spam.system` function must return the value of :c:data:`!sts` as a Python object. This is done using the function :c:func:`PyLong_FromLong`. ::"
msgstr "تابع :func:`!spam.system` ما باید مقدار :c:data:`!sts` را بهعنوان یک شیء پایتون بازگرداند. این کار با استفاده از تابع :c:func:`PyLong_FromLong` انجام میشود. ::"
msgid "return PyLong_FromLong(sts);"
msgstr "return PyLong_FromLong(sts);"
msgid "In this case, it will return an integer object. (Yes, even integers are objects on the heap in Python!)"
msgstr "در این حالت، یک شیء عدد صحیح را برمیگرداند. (بله، در پایتون حتی اعداد صحیح هم اشیایی روی هیپ هستند!)"
msgid "If you have a C function that returns no useful argument (a function returning :c:expr:`void`), the corresponding Python function must return ``None``. You need this idiom to do so (which is implemented by the :c:macro:`Py_RETURN_NONE` macro)::"
msgstr "اگر تابعی در C داشته باشید که هیچ آرگومان مفیدی برنمیگرداند (تابعی که :c:expr:`void` برمیگرداند)، تابع پایتونِ متناظر باید ``None`` را برگرداند. برای انجام این کار به این اصطلاح نیاز دارید (که توسط ماکروی :c:macro:`Py_RETURN_NONE` پیادهسازی شده است)::"
msgid ""
"Py_INCREF(Py_None);\n"
"return Py_None;"
msgstr ""
"Py_INCREF(Py_None);\n"
"return Py_None;"
msgid ":c:data:`Py_None` is the C name for the special Python object ``None``. It is a genuine Python object rather than a ``NULL`` pointer, which means \"error\" in most contexts, as we have seen."
msgstr ":c:data:`Py_None` نام C برای شیء ویژهی پایتون ``None`` است. این یک شیء واقعی پایتون است، نه یک اشارهگر ``NULL`` که همانطور که دیدیم، در بیشتر زمینهها به معنای «خطا» است."
msgid "The Module's Method Table and Initialization Function"
msgstr "جدول متدهای ماژول و تابع مقداردهی اولیه"
msgid "I promised to show how :c:func:`!spam_system` is called from Python programs. First, we need to list its name and address in a \"method table\"::"
msgstr "قول داده بودم که نشان دهم :c:func:`!spam_system` چگونه از برنامههای پایتون فراخوانی میشود. ابتدا، باید نام و نشانی آن را در یک «جدول متد» فهرست کنیم::"
msgid ""
"static PyMethodDef spam_methods[] = {\n"
" ...\n"
" {\"system\", spam_system, METH_VARARGS,\n"
" \"Execute a shell command.\"},\n"
" ...\n"
" {NULL, NULL, 0, NULL} /* Sentinel */\n"
"};"
msgstr ""
"static PyMethodDef spam_methods[] = {\n"
" ...\n"
" {\"system\", spam_system, METH_VARARGS,\n"
" \"Execute a shell command.\"},\n"
" ...\n"
" {NULL, NULL, 0, NULL} /* Sentinel */\n"
"};"
msgid "Note the third entry (``METH_VARARGS``). This is a flag telling the interpreter the calling convention to be used for the C function. It should normally always be ``METH_VARARGS`` or ``METH_VARARGS | METH_KEYWORDS``; a value of ``0`` means that an obsolete variant of :c:func:`PyArg_ParseTuple` is used."
msgstr "به ورودی سوم (``METH_VARARGS``) توجه کنید. این یک پرچم است که به مفسر اعلام میکند از کدام قرارداد فراخوانی برای تابع C استفاده شود. این پرچم معمولاً همیشه باید ``METH_VARARGS`` یا ``METH_VARARGS | METH_KEYWORDS`` باشد؛ مقدار ``0`` به این معنی است که گونهی منسوخی از :c:func:`PyArg_ParseTuple` به کار میرود."
msgid "When using only ``METH_VARARGS``, the function should expect the Python-level parameters to be passed in as a tuple acceptable for parsing via :c:func:`PyArg_ParseTuple`; more information on this function is provided below."
msgstr "هنگام استفادهی تنها از ``METH_VARARGS``، تابع باید انتظار داشته باشد که پارامترهای سطح پایتون بهصورت تاپلی مناسب برای پارس کردن توسط :c:func:`PyArg_ParseTuple` ارسال شوند؛ اطلاعات بیشتر دربارهی این تابع در ادامه ارائه شده است."
msgid "The :c:macro:`METH_KEYWORDS` bit may be set in the third field if keyword arguments should be passed to the function. In this case, the C function should accept a third ``PyObject *`` parameter which will be a dictionary of keywords. Use :c:func:`PyArg_ParseTupleAndKeywords` to parse the arguments to such a function."
msgstr "اگر قرار است آرگومانهای کلیدواژهای به تابع ارسال شوند، میتوان بیت :c:macro:`METH_KEYWORDS` را در فیلد سوم تنظیم کرد. در این حالت، تابع C باید پارامتر سومی از نوع ``PyObject *`` را بپذیرد که دیکشنریای از کلیدواژهها خواهد بود. برای پارس کردن آرگومانهای چنین تابعی، از :c:func:`PyArg_ParseTupleAndKeywords` استفاده کنید."
msgid "The method table must be referenced in the module definition structure::"
msgstr "جدول متدها باید در ساختار تعریف ماژول ارجاع داده شود::"
msgid ""
"static struct PyModuleDef spam_module = {\n"
" ...\n"
" .m_methods = spam_methods,\n"
" ...\n"
"};"
msgstr ""
"static struct PyModuleDef spam_module = {\n"
" ...\n"
" .m_methods = spam_methods,\n"
" ...\n"
"};"
msgid "This structure, in turn, must be passed to the interpreter in the module's initialization function. The initialization function must be named :c:func:`!PyInit_name`, where *name* is the name of the module, and should be the only non-\\ ``static`` item defined in the module file::"
msgstr "این ساختار، به نوبه خود، باید در تابع مقداردهی اولیهی ماژول، به مفسر پاس داده شود. تابع مقداردهی اولیه باید نام :c:func:`!PyInit_name` داشته باشد، که در آن *name* نام ماژول است، و باید تنها آیتم غیر ``static`` تعریفشده در پروندهی ماژول باشد::"
msgid ""
"PyMODINIT_FUNC\n"
"PyInit_spam(void)\n"
"{\n"
" return PyModuleDef_Init(&spam_module);\n"
"}"
msgstr ""
"PyMODINIT_FUNC\n"
"PyInit_spam(void)\n"
"{\n"
" return PyModuleDef_Init(&spam_module);\n"
"}"
msgid "Note that :c:macro:`PyMODINIT_FUNC` declares the function as ``PyObject *`` return type, declares any special linkage declarations required by the platform, and for C++ declares the function as ``extern \"C\"``."
msgstr "توجه داشته باشید که :c:macro:`PyMODINIT_FUNC` تابع را با نوع بازگشتی ``PyObject *`` اعلام میکند، هرگونه اعلان پیوند (linkage) خاص مورد نیاز پلتفرم را اعلام میکند و برای C++ تابع را بهصورت ``extern \"C\"`` اعلام میکند."
msgid ":c:func:`!PyInit_spam` is called when each interpreter imports its module :mod:`!spam` for the first time. (See below for comments about embedding Python.) A pointer to the module definition must be returned via :c:func:`PyModuleDef_Init`, so that the import machinery can create the module and store it in ``sys.modules``."
msgstr ":c:func:`!PyInit_spam` هنگامی فراخوانی میشود که هر مفسر، ماژول :mod:`!spam` خود را برای نخستین بار ایمپورت کند. (برای نکات مربوط به تعبیه پایتون، به ادامه مراجعه کنید.) اشارهگری به تعریف ماژول باید از طریق :c:func:`PyModuleDef_Init` بازگردانده شود تا سازوکار ایمپورت بتواند ماژول را ایجاد کرده و آن را در ``sys.modules`` ذخیره کند."
msgid "When embedding Python, the :c:func:`!PyInit_spam` function is not called automatically unless there's an entry in the :c:data:`PyImport_Inittab` table. To add the module to the initialization table, use :c:func:`PyImport_AppendInittab`, optionally followed by an import of the module::"
msgstr "هنگام تعبیه پایتون، تابع :c:func:`!PyInit_spam` بهطور خودکار فراخوانی نمیشود، مگر آنکه ورودیای در جدول :c:data:`PyImport_Inittab` وجود داشته باشد. برای افزودن ماژول به جدول مقداردهی اولیه، از :c:func:`PyImport_AppendInittab` استفاده کنید و در صورت تمایل، ماژول را ایمپورت کنید::"
msgid ""
"#define PY_SSIZE_T_CLEAN\n"
"#include <Python.h>\n"
"\n"
"int\n"
"main(int argc, char *argv[])\n"
"{\n"
" PyStatus status;\n"
" PyConfig config;\n"
" PyConfig_InitPythonConfig(&config);\n"
"\n"
" /* Add a built-in module, before Py_Initialize */\n"
" if (PyImport_AppendInittab(\"spam\", PyInit_spam) == -1) {\n"
" fprintf(stderr, \"Error: could not extend in-built modules table\\n\");\n"
" exit(1);\n"
" }\n"
"\n"
" /* Pass argv[0] to the Python interpreter */\n"
" status = PyConfig_SetBytesString(&config, &config.program_name, argv[0]);\n"
" if (PyStatus_Exception(status)) {\n"
" goto exception;\n"
" }\n"
"\n"
" /* Initialize the Python interpreter. Required.\n"
" If this step fails, it will be a fatal error. */\n"
" status = Py_InitializeFromConfig(&config);\n"
" if (PyStatus_Exception(status)) {\n"
" goto exception;\n"
" }\n"
" PyConfig_Clear(&config);\n"
"\n"
" /* Optionally import the module; alternatively,\n"
" import can be deferred until the embedded script\n"
" imports it. */\n"
" PyObject *pmodule = PyImport_ImportModule(\"spam\");\n"
" if (!pmodule) {\n"
" PyErr_Print();\n"
" fprintf(stderr, \"Error: could not import module 'spam'\\n\");\n"
" }\n"
"\n"
" // ... use Python C API here ...\n"
"\n"
" return 0;\n"
"\n"
" exception:\n"
" PyConfig_Clear(&config);\n"
" Py_ExitStatusException(status);\n"
"}"
msgstr ""
"#define PY_SSIZE_T_CLEAN\n"
"#include <Python.h>\n"
"\n"
"int\n"
"main(int argc, char *argv[])\n"
"{\n"
" PyStatus status;\n"
" PyConfig config;\n"
" PyConfig_InitPythonConfig(&config);\n"
"\n"
" /* Add a built-in module, before Py_Initialize */\n"
" if (PyImport_AppendInittab(\"spam\", PyInit_spam) == -1) {\n"
" fprintf(stderr, \"Error: could not extend in-built modules table\\n\");\n"
" exit(1);\n"
" }\n"
"\n"
" /* Pass argv[0] to the Python interpreter */\n"
" status = PyConfig_SetBytesString(&config, &config.program_name, argv[0]);\n"
" if (PyStatus_Exception(status)) {\n"
" goto exception;\n"
" }\n"
"\n"
" /* Initialize the Python interpreter. Required.\n"
" If this step fails, it will be a fatal error. */\n"
" status = Py_InitializeFromConfig(&config);\n"
" if (PyStatus_Exception(status)) {\n"
" goto exception;\n"
" }\n"
" PyConfig_Clear(&config);\n"
"\n"
" /* Optionally import the module; alternatively,\n"
" import can be deferred until the embedded script\n"
" imports it. */\n"
" PyObject *pmodule = PyImport_ImportModule(\"spam\");\n"
" if (!pmodule) {\n"
" PyErr_Print();\n"
" fprintf(stderr, \"Error: could not import module 'spam'\\n\");\n"
" }\n"
"\n"
" // ... use Python C API here ...\n"
"\n"
" return 0;\n"
"\n"
" exception:\n"
" PyConfig_Clear(&config);\n"
" Py_ExitStatusException(status);\n"
"}"
msgid "If you declare a global variable or a local static one, the module may experience unintended side-effects on re-initialisation, for example when removing entries from ``sys.modules`` or importing compiled modules into multiple interpreters within a process (or following a :c:func:`fork` without an intervening :c:func:`exec`). If module state is not yet fully :ref:`isolated <isolating-extensions-howto>`, authors should consider marking the module as having no support for subinterpreters (via :c:macro:`Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED`)."
msgstr "اگر یک متغیر سراسری یا یک متغیر ایستای محلی تعریف کنید، ماژول ممکن است هنگام مقداردهی مجدد دچار عوارض جانبی ناخواسته شود، برای مثال هنگام حذف ورودیها از ``sys.modules`` یا ایمپورت کردن ماژولهای کامپایلشده در چندین مفسر درون یک فرایند (یا پس از یک :c:func:`fork` بدون :c:func:`exec` در میان). اگر وضعیت ماژول هنوز بهطور کامل :ref:`جداسازیشده <isolating-extensions-howto>` نباشد، نویسندگان باید در نظر بگیرند که ماژول را بهعنوان فاقد پشتیبانی از زیرمفسرها علامتگذاری کنند (از طریق :c:macro:`Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED`)."
msgid "A more substantial example module is included in the Python source distribution as :file:`Modules/xxlimited.c`. This file may be used as a template or simply read as an example."
msgstr "یک ماژول نمونهی جامعتر در توزیع کد منبع پایتون بهصورت :file:`Modules/xxlimited.c` گنجانده شده است. میتوان از این پرونده بهعنوان یک قالب استفاده کرد یا صرفاً آن را بهعنوان مثال خواند."
msgid "Compilation and Linkage"
msgstr "کامپایل و پیونددهی (linkage)"
msgid "There are two more things to do before you can use your new extension: compiling and linking it with the Python system. If you use dynamic loading, the details may depend on the style of dynamic loading your system uses; see the chapters about building extension modules (chapter :ref:`building`) and additional information that pertains only to building on Windows (chapter :ref:`building-on-windows`) for more information about this."
msgstr "پیش از آنکه بتوانید از ماژول توسعهای جدید خود استفاده کنید، باید دو کار دیگر انجام دهید: کامپایل کردن و پیوند دادن آن با سیستم پایتون. اگر از بارگذاری پویا استفاده کنید، جزئیات ممکن است به سبک بارگذاری پویایی که سیستم شما از آن استفاده میکند بستگی داشته باشد؛ برای اطلاعات بیشتر دربارهی این موضوع، به فصلهای مربوط به ساخت ماژولهای توسعهای (فصل :ref:`building`) و اطلاعات تکمیلیای که تنها به ساخت روی ویندوز مربوط میشود (فصل :ref:`building-on-windows`) مراجعه کنید."
msgid "If you can't use dynamic loading, or if you want to make your module a permanent part of the Python interpreter, you will have to change the configuration setup and rebuild the interpreter. Luckily, this is very simple on Unix: just place your file (:file:`spammodule.c` for example) in the :file:`Modules/` directory of an unpacked source distribution, add a line to the file :file:`Modules/Setup.local` describing your file:"
msgstr "اگر نتوانید از بارگذاری پویا استفاده کنید، یا اگر میخواهید ماژول خود را به بخشی دائمی از مفسر پایتون تبدیل کنید، باید تنظیمات پیکربندی را تغییر دهید و مفسر را بازسازی کنید. خوشبختانه، این کار در یونیکس بسیار ساده است: کافی است پروندهی خود (برای مثال :file:`spammodule.c`) را در پوشهی :file:`Modules/` یک توزیع کد منبعِ استخراجشده قرار دهید، و سطری به پروندهی :file:`Modules/Setup.local` اضافه کنید که پروندهی شما را توصیف کند:"
msgid "spam spammodule.o"
msgstr "spam spammodule.o"
msgid "and rebuild the interpreter by running :program:`make` in the toplevel directory. You can also run :program:`make` in the :file:`Modules/` subdirectory, but then you must first rebuild :file:`Makefile` there by running ':program:`make` Makefile'. (This is necessary each time you change the :file:`Setup` file.)"
msgstr "و مفسر را با اجرای :program:`make` در پوشهی سطح بالا بازسازی کنید. همچنین میتوانید :program:`make` را در زیرپوشهی :file:`Modules/` اجرا کنید، اما در این صورت باید ابتدا :file:`Makefile` را در آنجا با اجرای ':program:`make` Makefile' بازسازی کنید. (این کار هر بار که پروندهی :file:`Setup` را تغییر میدهید، ضروری است.)"
msgid "If your module requires additional libraries to link with, these can be listed on the line in the configuration file as well, for instance:"
msgstr "اگر ماژول شما به کتابخانههای اضافی برای پیوند دادن نیاز دارد، میتوانید آنها را نیز در همان سطر در پرونده پیکربندی فهرست کنید، برای مثال:"
msgid "spam spammodule.o -lX11"
msgstr "spam spammodule.o -lX11"
msgid "Calling Python Functions from C"
msgstr "فراخوانی توابع پایتون از زبان C"
msgid "So far we have concentrated on making C functions callable from Python. The reverse is also useful: calling Python functions from C. This is especially the case for libraries that support so-called \"callback\" functions. If a C interface makes use of callbacks, the equivalent Python often needs to provide a callback mechanism to the Python programmer; the implementation will require calling the Python callback functions from a C callback. Other uses are also imaginable."
msgstr "تا اینجا بر فراخوانیپذیر کردن توابع C از پایتون تمرکز کردهایم. حالت معکوس نیز کاربردی است: فراخوانی توابع پایتون از C. این موضوع بهویژه در مورد کتابخانههایی که از توابعی به اصطلاح «کالبک» پشتیبانی میکنند صادق است. اگر یک رابط C از کالبکها استفاده کند، معادل پایتونی آن اغلب نیاز دارد که سازوکار کالبکی را در اختیار برنامهنویس پایتون قرار دهد؛ پیادهسازی این کار مستلزم فراخوانی توابع کالبک پایتون از یک کالبک C خواهد بود. کاربردهای دیگری نیز قابل تصور است."
msgid "Fortunately, the Python interpreter is easily called recursively, and there is a standard interface to call a Python function. (If you're interested in how to call the Python parser with a particular string as input, see :ref:`veryhigh`.)"
msgstr "خوشبختانه، مفسر پایتون بهراحتی بهصورت بازگشتی فراخوانی میشود و یک رابط استاندارد برای فراخوانی یک تابع پایتون وجود دارد. (اگر میخواهید بدانید چگونه میتوان پارسر پایتون را با یک رشته خاص بهعنوان ورودی فراخوانی کرد، به :ref:`veryhigh` مراجعه کنید.)"
msgid "Calling a Python function is easy. First, the Python program must somehow pass you the Python function object. You should provide a function (or some other interface) to do this. When this function is called, save a pointer to the Python function object (be careful to :c:func:`Py_INCREF` it!) in a global variable --- or wherever you see fit. For example, the following function might be part of a module definition::"
msgstr "فراخوانی یک تابع پایتون آسان است. نخست، برنامه پایتون باید به نحوی شیء تابع پایتون را به شما پاس دهد. شما باید تابعی (یا رابط دیگری) برای انجام این کار فراهم کنید. وقتی این تابع فراخوانی شد، اشارهگری به شیء تابع پایتون را (مراقب باشید که آن را :c:func:`Py_INCREF` کنید!) در یک متغیر سراسری --- یا هر جای دیگری که صلاح میدانید --- ذخیره کنید. برای مثال، تابع زیر ممکن است بخشی از تعریف یک ماژول باشد::"
msgid ""
"static PyObject *my_callback = NULL;\n"
"\n"
"static PyObject *\n"
"my_set_callback(PyObject *dummy, PyObject *args)\n"
"{\n"
" PyObject *result = NULL;\n"
" PyObject *temp;\n"
"\n"
" if (PyArg_ParseTuple(args, \"O:set_callback\", &temp)) {\n"
" if (!PyCallable_Check(temp)) {\n"
" PyErr_SetString(PyExc_TypeError, \"parameter must be callable\");\n"
" return NULL;\n"
" }\n"
" Py_XINCREF(temp); /* Add a reference to new callback */\n"
" Py_XDECREF(my_callback); /* Dispose of previous callback */\n"
" my_callback = temp; /* Remember new callback */\n"
" /* Boilerplate to return \"None\" */\n"
" Py_INCREF(Py_None);\n"
" result = Py_None;\n"
" }\n"
" return result;\n"
"}"
msgstr ""
"static PyObject *my_callback = NULL;\n"
"\n"
"static PyObject *\n"
"my_set_callback(PyObject *dummy, PyObject *args)\n"
"{\n"
" PyObject *result = NULL;\n"
" PyObject *temp;\n"
"\n"
" if (PyArg_ParseTuple(args, \"O:set_callback\", &temp)) {\n"
" if (!PyCallable_Check(temp)) {\n"
" PyErr_SetString(PyExc_TypeError, \"parameter must be callable\");\n"
" return NULL;\n"
" }\n"
" Py_XINCREF(temp); /* Add a reference to new callback */\n"
" Py_XDECREF(my_callback); /* Dispose of previous callback */\n"
" my_callback = temp; /* Remember new callback */\n"
" /* Boilerplate to return \"None\" */\n"
" Py_INCREF(Py_None);\n"
" result = Py_None;\n"
" }\n"
" return result;\n"
"}"
msgid "This function must be registered with the interpreter using the :c:macro:`METH_VARARGS` flag; this is described in section :ref:`methodtable`. The :c:func:`PyArg_ParseTuple` function and its arguments are documented in section :ref:`parsetuple`."
msgstr "این تابع باید با استفاده از پرچم :c:macro:`METH_VARARGS` در مفسر ثبت شود؛ این موضوع در بخش :ref:`methodtable` توضیح داده شده است. تابع :c:func:`PyArg_ParseTuple` و آرگومانهای آن در بخش :ref:`parsetuple` مستندسازی شدهاند."
msgid "The macros :c:func:`Py_XINCREF` and :c:func:`Py_XDECREF` increment/decrement the reference count of an object and are safe in the presence of ``NULL`` pointers (but note that *temp* will not be ``NULL`` in this context). More info on them in section :ref:`refcounts`."
msgstr "ماکروهای :c:func:`Py_XINCREF` و :c:func:`Py_XDECREF` شمارش ارجاع یک شیء را افزایش/کاهش میدهند و در صورت وجود اشارهگرهای ``NULL`` ایمن هستند (اما توجه داشته باشید که *temp* در این زمینه ``NULL`` نخواهد بود). اطلاعات بیشتر دربارهی آنها در بخش :ref:`refcounts` آمده است."
msgid "Later, when it is time to call the function, you call the C function :c:func:`PyObject_CallObject`. This function has two arguments, both pointers to arbitrary Python objects: the Python function, and the argument list. The argument list must always be a tuple object, whose length is the number of arguments. To call the Python function with no arguments, pass in ``NULL``, or an empty tuple; to call it with one argument, pass a singleton tuple. :c:func:`Py_BuildValue` returns a tuple when its format string consists of zero or more format codes between parentheses. For example::"
msgstr "بعداً، وقتی زمان فراخوانی تابع فرا میرسد، شما تابع Cِ :c:func:`PyObject_CallObject` را فراخوانی میکنید. این تابع دو آرگومان دارد که هر دو اشارهگر به اشیاء پایتونی دلخواه هستند: تابع پایتونی و فهرست آرگومانها. فهرست آرگومانها باید همیشه یک شیء تاپل باشد که طول آن برابر با تعداد آرگومانهاست. برای فراخوانی تابع پایتونی بدون هیچ آرگومانی، ``NULL`` یا یک تاپل خالی را ارسال کنید؛ برای فراخوانی آن با یک آرگومان، یک تاپل تکنمونه ارسال کنید. :c:func:`Py_BuildValue` زمانی که رشته قالب آن از صفر یا چند کد قالب درون پرانتز تشکیل شده باشد، یک تاپل برمیگرداند. برای مثال::"
msgid ""
"int arg;\n"
"PyObject *arglist;\n"
"PyObject *result;\n"
"...\n"
"arg = 123;\n"
"...\n"
"/* Time to call the callback */\n"
"arglist = Py_BuildValue(\"(i)\", arg);\n"
"result = PyObject_CallObject(my_callback, arglist);\n"
"Py_DECREF(arglist);"
msgstr ""
"int arg;\n"
"PyObject *arglist;\n"
"PyObject *result;\n"
"...\n"
"arg = 123;\n"
"...\n"
"/* Time to call the callback */\n"
"arglist = Py_BuildValue(\"(i)\", arg);\n"
"result = PyObject_CallObject(my_callback, arglist);\n"
"Py_DECREF(arglist);"
msgid ":c:func:`PyObject_CallObject` returns a Python object pointer: this is the return value of the Python function. :c:func:`PyObject_CallObject` is \"reference-count-neutral\" with respect to its arguments. In the example a new tuple was created to serve as the argument list, which is :c:func:`Py_DECREF`\\ -ed immediately after the :c:func:`PyObject_CallObject` call."
msgstr ":c:func:`PyObject_CallObject` یک اشارهگر به شیء پایتون برمیگرداند: این همان مقدار بازگشتی تابع پایتون است. :c:func:`PyObject_CallObject` نسبت به آرگومانهای خود، از نظر شمارش ارجاع خنثی (reference-count-neutral) است. در این مثال، یک تاپل جدید برای استفاده بهعنوان فهرست آرگومانها ساخته میشود که بلافاصله پس از فراخوانی :c:func:`PyObject_CallObject`، :c:func:`Py_DECREF` روی آن اعمال میشود."
msgid "The return value of :c:func:`PyObject_CallObject` is \"new\": either it is a brand new object, or it is an existing object whose reference count has been incremented. So, unless you want to save it in a global variable, you should somehow :c:func:`Py_DECREF` the result, even (especially!) if you are not interested in its value."
msgstr "مقدار بازگشتی :c:func:`PyObject_CallObject` «جدید» است: یا شیئی کاملاً جدید است، یا شیئی موجود است که شمارش ارجاع آن افزایش یافته است. بنابراین، مگر آنکه بخواهید آن را در یک متغیر سراسری ذخیره کنید، باید به نحوی نتیجه را :c:func:`Py_DECREF` کنید، حتی (بهویژه!) اگر علاقهای به مقدار آن نداشته باشید."
msgid "Before you do this, however, it is important to check that the return value isn't ``NULL``. If it is, the Python function terminated by raising an exception. If the C code that called :c:func:`PyObject_CallObject` is called from Python, it should now return an error indication to its Python caller, so the interpreter can print a stack trace, or the calling Python code can handle the exception. If this is not possible or desirable, the exception should be cleared by calling :c:func:`PyErr_Clear`. For example::"
msgstr "با این حال، پیش از انجام این کار، مهم است که بررسی کنید مقدار بازگشتی ``NULL`` نیست. اگر چنین باشد، تابع پایتونی با ایجاد یک استثنا خاتمه یافته است. اگر کد C که :c:func:`PyObject_CallObject` را فراخوانی کرده است از پایتون فراخوانی شده باشد، باید اکنون نشان خطایی را به فراخواننده پایتونی خود بازگرداند تا مفسر بتواند ردگیری پشته را چاپ کند، یا کد پایتونی فراخواننده بتواند استثنا را مدیریت کند. اگر این کار ممکن یا مطلوب نباشد، استثنا باید با فراخوانی :c:func:`PyErr_Clear` پاک شود. برای مثال::"
msgid ""
"if (result == NULL)\n"
" return NULL; /* Pass error back */\n"
"...use result...\n"
"Py_DECREF(result);"
msgstr ""
"if (result == NULL)\n"
" return NULL; /* Pass error back */\n"
"...use result...\n"
"Py_DECREF(result);"
msgid "Depending on the desired interface to the Python callback function, you may also have to provide an argument list to :c:func:`PyObject_CallObject`. In some cases the argument list is also provided by the Python program, through the same interface that specified the callback function. It can then be saved and used in the same manner as the function object. In other cases, you may have to construct a new tuple to pass as the argument list. The simplest way to do this is to call :c:func:`Py_BuildValue`. For example, if you want to pass an integral event code, you might use the following code::"
msgstr "بسته به رابط مورد نظر برای تابع کالبک پایتون، ممکن است لازم باشد فهرست آرگومانها را نیز به :c:func:`PyObject_CallObject` ارائه کنید. در برخی موارد، فهرست آرگومانها نیز توسط برنامه پایتون و از طریق همان رابطی که تابع کالبک را مشخص کرده بود، فراهم میشود. در این صورت میتوان آن را ذخیره کرد و به همان شیوهی شیء تابع از آن استفاده کرد. در موارد دیگر، ممکن است لازم باشد یک تاپل جدید ایجاد کنید تا آن را بهعنوان فهرست آرگومانها ارسال کنید. سادهترین راه انجام این کار، فراخوانی :c:func:`Py_BuildValue` است. برای مثال، اگر بخواهید کد رویدادی از نوع عدد صحیح ارسال کنید، ممکن است از کد زیر استفاده کنید::"
msgid ""
"PyObject *arglist;\n"
"...\n"
"arglist = Py_BuildValue(\"(l)\", eventcode);\n"
"result = PyObject_CallObject(my_callback, arglist);\n"
"Py_DECREF(arglist);\n"
"if (result == NULL)\n"
" return NULL; /* Pass error back */\n"
"/* Here maybe use the result */\n"
"Py_DECREF(result);"
msgstr ""
"PyObject *arglist;\n"
"...\n"
"arglist = Py_BuildValue(\"(l)\", eventcode);\n"
"result = PyObject_CallObject(my_callback, arglist);\n"
"Py_DECREF(arglist);\n"
"if (result == NULL)\n"
" return NULL; /* بازگرداندن خطا */\n"
"/* اینجا شاید از نتیجه استفاده شود */\n"
"Py_DECREF(result);"
msgid "Note the placement of ``Py_DECREF(arglist)`` immediately after the call, before the error check! Also note that strictly speaking this code is not complete: :c:func:`Py_BuildValue` may run out of memory, and this should be checked."
msgstr "به قرارگیری ``Py_DECREF(arglist)`` بلافاصله پس از فراخوانی و پیش از بررسی خطا توجه کنید! همچنین توجه داشته باشید که بهطور دقیق این کد کامل نیست: ممکن است :c:func:`Py_BuildValue` با کمبود حافظه مواجه شود و این موضوع باید بررسی شود."
msgid "You may also call a function with keyword arguments by using :c:func:`PyObject_Call`, which supports arguments and keyword arguments. As in the above example, we use :c:func:`Py_BuildValue` to construct the dictionary. ::"
msgstr "شما همچنین میتوانید با استفاده از :c:func:`PyObject_Call` که از آرگومانها و آرگومانهای کلیدواژهای پشتیبانی میکند، تابعی را با آرگومانهای کلیدواژهای فراخوانی کنید. مانند مثال بالا، از :c:func:`Py_BuildValue` برای ساخت دیکشنری استفاده میکنیم. ::"
msgid ""
"PyObject *dict;\n"
"...\n"
"dict = Py_BuildValue(\"{s:i}\", \"name\", val);\n"
"result = PyObject_Call(my_callback, NULL, dict);\n"
"Py_DECREF(dict);\n"
"if (result == NULL)\n"
" return NULL; /* Pass error back */\n"
"/* Here maybe use the result */\n"
"Py_DECREF(result);"
msgstr ""
"PyObject *dict;\n"
"...\n"
"dict = Py_BuildValue(\"{s:i}\", \"name\", val);\n"
"result = PyObject_Call(my_callback, NULL, dict);\n"
"Py_DECREF(dict);\n"
"if (result == NULL)\n"
" return NULL; /* بازگرداندن خطا */\n"
"/* اینجا شاید از نتیجه استفاده کنید */\n"
"Py_DECREF(result);"
msgid "Extracting Parameters in Extension Functions"
msgstr "استخراج پارامترها در توابع توسعهای"
msgid "The :c:func:`PyArg_ParseTuple` function is declared as follows::"
msgstr "تابع :c:func:`PyArg_ParseTuple` به شرح زیر تعریف شده است::"
msgid "int PyArg_ParseTuple(PyObject *arg, const char *format, ...);"
msgstr "int PyArg_ParseTuple(PyObject *arg, const char *format, ...);"
msgid "The *arg* argument must be a tuple object containing an argument list passed from Python to a C function. The *format* argument must be a format string, whose syntax is explained in :ref:`arg-parsing` in the Python/C API Reference Manual. The remaining arguments must be addresses of variables whose type is determined by the format string."
msgstr "آرگومان *arg* باید یک شیء تاپل حاوی فهرست آرگومانی باشد که از پایتون به یک تابع C پاس داده میشود. آرگومان *format* باید یک رشته قالب باشد که سینتکس آن در :ref:`arg-parsing` در راهنمای مرجع Python/C API توضیح داده شده است. آرگومانهای باقیمانده باید آدرسهای متغیرهایی باشند که نوع آنها توسط رشته قالب تعیین میشود."
msgid "Note that while :c:func:`PyArg_ParseTuple` checks that the Python arguments have the required types, it cannot check the validity of the addresses of C variables passed to the call: if you make mistakes there, your code will probably crash or at least overwrite random bits in memory. So be careful!"
msgstr "توجه داشته باشید که هرچند :c:func:`PyArg_ParseTuple` بررسی میکند که آرگومانهای پایتون نوعهای مورد نیاز را داشته باشند، نمیتواند اعتبار نشانیهای متغیرهای C که به فراخوانی ارسال شدهاند را بررسی کند: اگر در آنجا اشتباهی کنید، کد شما احتمالاً فروپاشی میکند یا دستکم بیتهای تصادفی در حافظه را بازنویسی میکند. پس مراقب باشید!"
msgid "Note that any Python object references which are provided to the caller are *borrowed* references; do not decrement their reference count!"
msgstr "توجه داشته باشید که هر ارجاع به شیء پایتون که در اختیار فراخواننده قرار میگیرد، یک ارجاع *امانتی* است؛ شمارش ارجاع آنها را کاهش ندهید!"
msgid "Some example calls::"
msgstr "چند نمونه فراخوانی::"
msgid ""
"int ok;\n"
"int i, j;\n"
"long k, l;\n"
"const char *s;\n"
"Py_ssize_t size;\n"
"\n"
"ok = PyArg_ParseTuple(args, \"\"); /* No arguments */\n"
" /* Python call: f() */"
msgstr ""
"int ok;\n"
"int i, j;\n"
"long k, l;\n"
"const char *s;\n"
"Py_ssize_t size;\n"
"\n"
"ok = PyArg_ParseTuple(args, \"\"); /* No arguments */\n"
" /* Python call: f() */"
msgid ""
"ok = PyArg_ParseTuple(args, \"s\", &s); /* A string */\n"
" /* Possible Python call: f('whoops!') */"
msgstr ""
"ok = PyArg_ParseTuple(args, \"s\", &s); /* A string */\n"
" /* Possible Python call: f('whoops!') */"
msgid ""
"ok = PyArg_ParseTuple(args, \"lls\", &k, &l, &s); /* Two longs and a string */\n"
" /* Possible Python call: f(1, 2, 'three') */"
msgstr ""
"ok = PyArg_ParseTuple(args, \"lls\", &k, &l, &s); /* دو long و یک رشته */\n"
" /* فراخوانی ممکن پایتون: f(1, 2, 'three') */"
msgid ""
"ok = PyArg_ParseTuple(args, \"(ii)s#\", &i, &j, &s, &size);\n"
" /* A pair of ints and a string, whose size is also returned */\n"
" /* Possible Python call: f((1, 2), 'three') */"
msgstr ""
"ok = PyArg_ParseTuple(args, \"(ii)s#\", &i, &j, &s, &size);\n"
" /* یک جفت عدد صحیح و یک رشته، که اندازهی آن نیز برگردانده میشود */\n"
" /* فراخوانی ممکن پایتون: f((1, 2), 'three') */"
msgid ""
"{\n"
" const char *file;\n"
" const char *mode = \"r\";\n"
" int bufsize = 0;\n"
" ok = PyArg_ParseTuple(args, \"s|si\", &file, &mode, &bufsize);\n"
" /* A string, and optionally another string and an integer */\n"
" /* Possible Python calls:\n"
" f('spam')\n"
" f('spam', 'w')\n"
" f('spam', 'wb', 100000) */\n"
"}"
msgstr ""
"{\n"
" const char *file;\n"
" const char *mode = \"r\";\n"
" int bufsize = 0;\n"
" ok = PyArg_ParseTuple(args, \"s|si\", &file, &mode, &bufsize);\n"
" /* A string, and optionally another string and an integer */\n"
" /* Possible Python calls:\n"
" f('spam')\n"
" f('spam', 'w')\n"
" f('spam', 'wb', 100000) */\n"
"}"
msgid ""
"{\n"
" int left, top, right, bottom, h, v;\n"
" ok = PyArg_ParseTuple(args, \"((ii)(ii))(ii)\",\n"
" &left, &top, &right, &bottom, &h, &v);\n"
" /* A rectangle and a point */\n"
" /* Possible Python call:\n"
" f(((0, 0), (400, 300)), (10, 10)) */\n"
"}"
msgstr ""
"{\n"
" int left, top, right, bottom, h, v;\n"
" ok = PyArg_ParseTuple(args, \"((ii)(ii))(ii)\",\n"
" &left, &top, &right, &bottom, &h, &v);\n"
" /* A rectangle and a point */\n"
" /* Possible Python call:\n"
" f(((0, 0), (400, 300)), (10, 10)) */\n"
"}"
msgid ""
"{\n"
" Py_complex c;\n"
" ok = PyArg_ParseTuple(args, \"D:myfunction\", &c);\n"
" /* a complex, also providing a function name for errors */\n"
" /* Possible Python call: myfunction(1+2j) */\n"
"}"
msgstr ""
"{\n"
" Py_complex c;\n"
" ok = PyArg_ParseTuple(args, \"D:myfunction\", &c);\n"
" /* a complex, also providing a function name for errors */\n"
" /* Possible Python call: myfunction(1+2j) */\n"
"}"
msgid "Keyword Parameters for Extension Functions"
msgstr "پارامترهای کلیدواژهای برای توابع توسعهای"
msgid "The :c:func:`PyArg_ParseTupleAndKeywords` function is declared as follows::"
msgstr "تابع :c:func:`PyArg_ParseTupleAndKeywords` به صورت زیر اعلان میشود::"
msgid ""
"int PyArg_ParseTupleAndKeywords(PyObject *arg, PyObject *kwdict,\n"
" const char *format, char * const *kwlist, ...);"
msgstr ""
"int PyArg_ParseTupleAndKeywords(PyObject *arg, PyObject *kwdict,\n"
" const char *format, char * const *kwlist, ...);"
msgid "The *arg* and *format* parameters are identical to those of the :c:func:`PyArg_ParseTuple` function. The *kwdict* parameter is the dictionary of keywords received as the third parameter from the Python runtime. The *kwlist* parameter is a ``NULL``-terminated list of strings which identify the parameters; the names are matched with the type information from *format* from left to right. On success, :c:func:`PyArg_ParseTupleAndKeywords` returns true, otherwise it returns false and raises an appropriate exception."
msgstr "پارامترهای *arg* و *format* با پارامترهای تابع :c:func:`PyArg_ParseTuple` یکسان هستند. پارامتر *kwdict* دیکشنری کلیدواژههایی است که بهعنوان پارامتر سوم از رانتایم پایتون دریافت میشود. پارامتر *kwlist* فهرستی از رشتههاست که با ``NULL`` پایان مییابد و پارامترها را شناسایی میکند؛ نامها از چپ به راست با اطلاعات نوع گرفتهشده از *format* تطبیق داده میشوند. در صورت موفقیت، :c:func:`PyArg_ParseTupleAndKeywords` مقدار true را برمیگرداند؛ در غیر این صورت مقدار false را برمیگرداند و استثنای مناسبی ایجاد میکند."
msgid "Nested tuples cannot be parsed when using keyword arguments! Keyword parameters passed in which are not present in the *kwlist* will cause :exc:`TypeError` to be raised."
msgstr "تاپلهای تودرتو هنگام استفاده از آرگومانهای کلیدواژهای قابل تجزیه نیستند! پارامترهای کلیدواژهای که پاس داده میشوند و در *kwlist* وجود ندارند، باعث بهوجود آمدن :exc:`TypeError` میشوند."
msgid "Here is an example module which uses keywords, based on an example by Geoff Philbrick (philbrick@hks.com)::"
msgstr "در اینجا یک ماژول نمونه که از کلیدواژهها استفاده میکند، بر اساس نمونهای از Geoff Philbrick (philbrick@hks.com) آمده است::"
msgid ""
"#define PY_SSIZE_T_CLEAN\n"
"#include <Python.h>\n"
"\n"
"static PyObject *\n"
"keywdarg_parrot(PyObject *self, PyObject *args, PyObject *keywds)\n"
"{\n"
" int voltage;\n"
" const char *state = \"a stiff\";\n"
" const char *action = \"voom\";\n"
" const char *type = \"Norwegian Blue\";\n"
"\n"
" static char *kwlist[] = {\"voltage\", \"state\", \"action\", \"type\", NULL};\n"
"\n"
" if (!PyArg_ParseTupleAndKeywords(args, keywds, \"i|sss\", kwlist,\n"
" &voltage, &state, &action, &type))\n"
" return NULL;\n"
"\n"
" printf(\"-- This parrot wouldn't %s if you put %i Volts through it.\\n\",\n"
" action, voltage);\n"
" printf(\"-- Lovely plumage, the %s -- It's %s!\\n\", type, state);\n"
"\n"
" Py_RETURN_NONE;\n"
"}\n"
"\n"
"static PyMethodDef keywdarg_methods[] = {\n"
" /* The cast of the function is necessary since PyCFunction values\n"
" * only take two PyObject* parameters, and keywdarg_parrot() takes\n"
" * three.\n"
" */\n"
" {\"parrot\", (PyCFunction)(void(*)(void))keywdarg_parrot, METH_VARARGS | METH_KEYWORDS,\n"
" \"Print a lovely skit to standard output.\"},\n"
" {NULL, NULL, 0, NULL} /* sentinel */\n"
"};\n"
"\n"
"static struct PyModuleDef keywdarg_module = {\n"
" .m_base = PyModuleDef_HEAD_INIT,\n"
" .m_name = \"keywdarg\",\n"
" .m_size = 0,\n"
" .m_methods = keywdarg_methods,\n"
"};\n"
"\n"
"PyMODINIT_FUNC\n"
"PyInit_keywdarg(void)\n"
"{\n"
" return PyModuleDef_Init(&keywdarg_module);\n"
"}"
msgstr ""
"#define PY_SSIZE_T_CLEAN\n"
"#include <Python.h>\n"
"\n"
"static PyObject *\n"
"keywdarg_parrot(PyObject *self, PyObject *args, PyObject *keywds)\n"
"{\n"
" int voltage;\n"
" const char *state = \"a stiff\";\n"
" const char *action = \"voom\";\n"
" const char *type = \"Norwegian Blue\";\n"
"\n"
" static char *kwlist[] = {\"voltage\", \"state\", \"action\", \"type\", NULL};\n"
"\n"
" if (!PyArg_ParseTupleAndKeywords(args, keywds, \"i|sss\", kwlist,\n"
" &voltage, &state, &action, &type))\n"
" return NULL;\n"
"\n"
" printf(\"-- This parrot wouldn't %s if you put %i Volts through it.\\n\",\n"
" action, voltage);\n"
" printf(\"-- Lovely plumage, the %s -- It's %s!\\n\", type, state);\n"
"\n"
" Py_RETURN_NONE;\n"
"}\n"
"\n"
"static PyMethodDef keywdarg_methods[] = {\n"
" /* The cast of the function is necessary since PyCFunction values\n"
" * only take two PyObject* parameters, and keywdarg_parrot() takes\n"
" * three.\n"
" */\n"
" {\"parrot\", (PyCFunction)(void(*)(void))keywdarg_parrot, METH_VARARGS | METH_KEYWORDS,\n"
" \"Print a lovely skit to standard output.\"},\n"
" {NULL, NULL, 0, NULL} /* sentinel */\n"
"};\n"
"\n"
"static struct PyModuleDef keywdarg_module = {\n"
" .m_base = PyModuleDef_HEAD_INIT,\n"
" .m_name = \"keywdarg\",\n"
" .m_size = 0,\n"
" .m_methods = keywdarg_methods,\n"
"};\n"
"\n"
"PyMODINIT_FUNC\n"
"PyInit_keywdarg(void)\n"
"{\n"
" return PyModuleDef_Init(&keywdarg_module);\n"
"}"
msgid "Building Arbitrary Values"
msgstr "ساخت مقادیر دلخواه"
msgid "This function is the counterpart to :c:func:`PyArg_ParseTuple`. It is declared as follows::"
msgstr "این تابع همتای :c:func:`PyArg_ParseTuple` است. این تابع به صورت زیر اعلان میشود::"
msgid "PyObject *Py_BuildValue(const char *format, ...);"
msgstr "PyObject *Py_BuildValue(const char *format, ...);"
msgid "It recognizes a set of format units similar to the ones recognized by :c:func:`PyArg_ParseTuple`, but the arguments (which are input to the function, not output) must not be pointers, just values. It returns a new Python object, suitable for returning from a C function called from Python."
msgstr "این تابع مجموعهای از واحدهای قالب (format units) را تشخیص میدهد که مشابه واحدهای شناساییشده توسط :c:func:`PyArg_ParseTuple` هستند، اما آرگومانها (که ورودی تابع هستند، نه خروجی آن) نباید اشارهگر باشند، بلکه باید فقط مقدار باشند. این تابع یک شیء جدید پایتون برمیگرداند که برای بازگرداندن از یک تابع C فراخوانیشده از پایتون مناسب است."