forked from AFPy/python_doc_fr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextending.po
More file actions
3741 lines (3261 loc) · 151 KB
/
extending.po
File metadata and controls
3741 lines (3261 loc) · 151 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) 1990-2010, Python Software Foundation
# This file is distributed under the same license as the Python package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2012-11-05 09:33\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Translate Toolkit 1.7.0\n"
#: ../src/Doc/extending/building.rst:8
#, fuzzy
msgid "Building C and C++ Extensions with distutils"
msgstr "Construire les extensions C et C + + avec distutils"
#: ../src/Doc/extending/building.rst:13
msgid ""
"Starting in Python 1.4, Python provides, on Unix, a special make file for "
"building make files for building dynamically-linked extensions and custom "
"interpreters. Starting with Python 2.0, this mechanism (known as related to "
"Makefile.pre.in, and Setup files) is no longer supported. Building custom "
"interpreters was rarely used, and extension modules can be built using "
"distutils."
msgstr ""
#: ../src/Doc/extending/building.rst:20
msgid ""
"Building an extension module using distutils requires that distutils is "
"installed on the build machine, which is included in Python 2.x and "
"available separately for Python 1.5. Since distutils also supports creation "
"of binary packages, users don't necessarily need a compiler and distutils to "
"install the extension."
msgstr ""
#: ../src/Doc/extending/building.rst:26
msgid ""
"A distutils package contains a driver script, :file:`setup.py`. This is a "
"plain Python file, which, in the most simple case, could look like this::"
msgstr ""
#: ../src/Doc/extending/building.rst:40
msgid "With this :file:`setup.py`, and a file :file:`demo.c`, running ::"
msgstr ""
#: ../src/Doc/extending/building.rst:44
msgid ""
"will compile :file:`demo.c`, and produce an extension module named ``demo`` "
"in the :file:`build` directory. Depending on the system, the module file "
"will end up in a subdirectory :file:`build/lib.system`, and may have a name "
"like :file:`demo.so` or :file:`demo.pyd`."
msgstr ""
#: ../src/Doc/extending/building.rst:49
msgid ""
"In the :file:`setup.py`, all execution is performed by calling the ``setup`` "
"function. This takes a variable number of keyword arguments, of which the "
"example above uses only a subset. Specifically, the example specifies meta-"
"information to build packages, and it specifies the contents of the "
"package. Normally, a package will contain of addition modules, like Python "
"source modules, documentation, subpackages, etc. Please refer to the "
"distutils documentation in :ref:`distutils-index` to learn more about the "
"features of distutils; this section explains building extension modules only."
msgstr ""
#: ../src/Doc/extending/building.rst:58
msgid ""
"It is common to pre-compute arguments to :func:`setup`, to better structure "
"the driver script. In the example above, the\\ ``ext_modules`` argument to :"
"func:`setup` is a list of extension modules, each of which is an instance of "
"the :class:`Extension`. In the example, the instance defines an extension "
"named ``demo`` which is build by compiling a single source file, :file:`demo."
"c`."
msgstr ""
#: ../src/Doc/extending/building.rst:64
msgid ""
"In many cases, building an extension is more complex, since additional "
"preprocessor defines and libraries may be needed. This is demonstrated in "
"the example below. ::"
msgstr ""
#: ../src/Doc/extending/building.rst:90
msgid ""
"In this example, :func:`setup` is called with additional meta-information, "
"which is recommended when distribution packages have to be built. For the "
"extension itself, it specifies preprocessor defines, include directories, "
"library directories, and libraries. Depending on the compiler, distutils "
"passes this information in different ways to the compiler. For example, on "
"Unix, this may result in the compilation commands ::"
msgstr ""
#: ../src/Doc/extending/building.rst:101
msgid ""
"These lines are for demonstration purposes only; distutils users should "
"trust that distutils gets the invocations right."
msgstr ""
#: ../src/Doc/extending/building.rst:108
msgid "Distributing your extension modules"
msgstr ""
#: ../src/Doc/extending/building.rst:110
msgid ""
"When an extension has been successfully build, there are three ways to use "
"it."
msgstr ""
#: ../src/Doc/extending/building.rst:112
msgid ""
"End-users will typically want to install the module, they do so by running ::"
msgstr ""
#: ../src/Doc/extending/building.rst:116
msgid ""
"Module maintainers should produce source packages; to do so, they run ::"
msgstr ""
#: ../src/Doc/extending/building.rst:120
msgid ""
"In some cases, additional files need to be included in a source "
"distribution; this is done through a :file:`MANIFEST.in` file; see the "
"distutils documentation for details."
msgstr ""
#: ../src/Doc/extending/building.rst:124
msgid ""
"If the source distribution has been build successfully, maintainers can also "
"create binary distributions. Depending on the platform, one of the following "
"commands can be used to do so. ::"
msgstr ""
#: ../src/Doc/extending/embedding.rst:8
msgid "Embedding Python in Another Application"
msgstr ""
#: ../src/Doc/extending/embedding.rst:10
msgid ""
"The previous chapters discussed how to extend Python, that is, how to extend "
"the functionality of Python by attaching a library of C functions to it. It "
"is also possible to do it the other way around: enrich your C/C++ "
"application by embedding Python in it. Embedding provides your application "
"with the ability to implement some of the functionality of your application "
"in Python rather than C or C++. This can be used for many purposes; one "
"example would be to allow users to tailor the application to their needs by "
"writing some scripts in Python. You can also use it yourself if some of the "
"functionality can be written in Python more easily."
msgstr ""
#: ../src/Doc/extending/embedding.rst:20
msgid ""
"Embedding Python is similar to extending it, but not quite. The difference "
"is that when you extend Python, the main program of the application is still "
"the Python interpreter, while if you embed Python, the main program may have "
"nothing to do with Python --- instead, some parts of the application "
"occasionally call the Python interpreter to run some Python code."
msgstr ""
# 3349405bb5884228afac17315062a7b3
#: ../src/Doc/extending/embedding.rst:26
msgid ""
"So if you are embedding Python, you are providing your own main program. "
"One of the things this main program has to do is initialize the Python "
"interpreter. At the very least, you have to call the function :c:func:"
"`Py_Initialize`. There are optional calls to pass command line arguments to "
"Python. Then later you can call the interpreter from any part of the "
"application."
msgstr ""
# 2fee2b21444c48fcbc33bf615faa320a
#: ../src/Doc/extending/embedding.rst:32
msgid ""
"There are several different ways to call the interpreter: you can pass a "
"string containing Python statements to :c:func:`PyRun_SimpleString`, or you "
"can pass a stdio file pointer and a file name (for identification in error "
"messages only) to :c:func:`PyRun_SimpleFile`. You can also call the lower-"
"level operations described in the previous chapters to construct and use "
"Python objects."
msgstr ""
#: ../src/Doc/extending/embedding.rst:38
msgid ""
"A simple demo of embedding Python can be found in the directory :file:`Demo/"
"embed/` of the source distribution."
msgstr ""
#: ../src/Doc/extending/embedding.rst:45
msgid ""
"The details of Python's C interface are given in this manual. A great deal "
"of necessary information can be found here."
msgstr ""
#: ../src/Doc/extending/embedding.rst:52
msgid "Very High Level Embedding"
msgstr ""
#: ../src/Doc/extending/embedding.rst:54
msgid ""
"The simplest form of embedding Python is the use of the very high level "
"interface. This interface is intended to execute a Python script without "
"needing to interact with the application directly. This can for example be "
"used to perform some operation on a file. ::"
msgstr ""
# a8a877d20c464cfd840409fbdeba8d65
#: ../src/Doc/extending/embedding.rst:72
msgid ""
"The :c:func:`Py_SetProgramName` function should be called before :c:func:"
"`Py_Initialize` to inform the interpreter about paths to Python run-time "
"libraries. Next, the Python interpreter is initialized with :c:func:"
"`Py_Initialize`, followed by the execution of a hard-coded Python script "
"that prints the date and time. Afterwards, the :c:func:`Py_Finalize` call "
"shuts the interpreter down, followed by the end of the program. In a real "
"program, you may want to get the Python script from another source, perhaps "
"a text-editor routine, a file, or a database. Getting the Python code from "
"a file can better be done by using the :c:func:`PyRun_SimpleFile` function, "
"which saves you the trouble of allocating memory space and loading the file "
"contents."
msgstr ""
#: ../src/Doc/extending/embedding.rst:87
msgid "Beyond Very High Level Embedding: An overview"
msgstr ""
#: ../src/Doc/extending/embedding.rst:89
msgid ""
"The high level interface gives you the ability to execute arbitrary pieces "
"of Python code from your application, but exchanging data values is quite "
"cumbersome to say the least. If you want that, you should use lower level "
"calls. At the cost of having to write more C code, you can achieve almost "
"anything."
msgstr ""
#: ../src/Doc/extending/embedding.rst:94
msgid ""
"It should be noted that extending Python and embedding Python is quite the "
"same activity, despite the different intent. Most topics discussed in the "
"previous chapters are still valid. To show this, consider what the extension "
"code from Python to C really does:"
msgstr ""
#: ../src/Doc/extending/embedding.rst:99
msgid "Convert data values from Python to C,"
msgstr ""
#: ../src/Doc/extending/embedding.rst:101
msgid "Perform a function call to a C routine using the converted values, and"
msgstr ""
#: ../src/Doc/extending/embedding.rst:103
msgid "Convert the data values from the call from C to Python."
msgstr ""
#: ../src/Doc/extending/embedding.rst:105
msgid "When embedding Python, the interface code does:"
msgstr ""
#: ../src/Doc/extending/embedding.rst:107
msgid "Convert data values from C to Python,"
msgstr ""
#: ../src/Doc/extending/embedding.rst:109
msgid ""
"Perform a function call to a Python interface routine using the converted "
"values, and"
msgstr ""
#: ../src/Doc/extending/embedding.rst:112
msgid "Convert the data values from the call from Python to C."
msgstr ""
#: ../src/Doc/extending/embedding.rst:114
msgid ""
"As you can see, the data conversion steps are simply swapped to accommodate "
"the different direction of the cross-language transfer. The only difference "
"is the routine that you call between both data conversions. When extending, "
"you call a C routine, when embedding, you call a Python routine."
msgstr ""
#: ../src/Doc/extending/embedding.rst:119
msgid ""
"This chapter will not discuss how to convert data from Python to C and vice "
"versa. Also, proper use of references and dealing with errors is assumed to "
"be understood. Since these aspects do not differ from extending the "
"interpreter, you can refer to earlier chapters for the required information."
msgstr ""
#: ../src/Doc/extending/embedding.rst:128
msgid "Pure Embedding"
msgstr ""
#: ../src/Doc/extending/embedding.rst:130
msgid ""
"The first program aims to execute a function in a Python script. Like in the "
"section about the very high level interface, the Python interpreter does not "
"directly interact with the application (but that will change in the next "
"section)."
msgstr ""
#: ../src/Doc/extending/embedding.rst:135
msgid "The code to run a function defined in a Python script is:"
msgstr ""
#: ../src/Doc/extending/embedding.rst:140
msgid ""
"This code loads a Python script using ``argv[1]``, and calls the function "
"named in ``argv[2]``. Its integer arguments are the other values of the "
"``argv`` array. If you compile and link this program (let's call the "
"finished executable :program:`call`), and use it to execute a Python script, "
"such as::"
msgstr ""
#: ../src/Doc/extending/embedding.rst:152
msgid "then the result should be::"
msgstr ""
#: ../src/Doc/extending/embedding.rst:158
msgid ""
"Although the program is quite large for its functionality, most of the code "
"is for data conversion between Python and C, and for error reporting. The "
"interesting part with respect to embedding Python starts with ::"
msgstr ""
# dc2a9d1c15044827aafd426bf84b472a
#: ../src/Doc/extending/embedding.rst:167
msgid ""
"After initializing the interpreter, the script is loaded using :c:func:"
"`PyImport_Import`. This routine needs a Python string as its argument, "
"which is constructed using the :c:func:`PyString_FromString` data conversion "
"routine. ::"
msgstr ""
# 61325f6f158d4f3e93f341879b3d9276
#: ../src/Doc/extending/embedding.rst:180
msgid ""
"Once the script is loaded, the name we're looking for is retrieved using :c:"
"func:`PyObject_GetAttrString`. If the name exists, and the object returned "
"is callable, you can safely assume that it is a function. The program then "
"proceeds by constructing a tuple of arguments as normal. The call to the "
"Python function is then made with::"
msgstr ""
#: ../src/Doc/extending/embedding.rst:188
msgid ""
"Upon return of the function, ``pValue`` is either *NULL* or it contains a "
"reference to the return value of the function. Be sure to release the "
"reference after examining the value."
msgstr ""
#: ../src/Doc/extending/embedding.rst:196
msgid "Extending Embedded Python"
msgstr ""
#: ../src/Doc/extending/embedding.rst:198
msgid ""
"Until now, the embedded Python interpreter had no access to functionality "
"from the application itself. The Python API allows this by extending the "
"embedded interpreter. That is, the embedded interpreter gets extended with "
"routines provided by the application. While it sounds complex, it is not so "
"bad. Simply forget for a while that the application starts the Python "
"interpreter. Instead, consider the application to be a set of subroutines, "
"and write some glue code that gives Python access to those routines, just "
"like you would write a normal Python extension. For example::"
msgstr ""
# e3a28dc7453b48aaa3152dbb130353ec
#: ../src/Doc/extending/embedding.rst:224
msgid ""
"Insert the above code just above the :c:func:`main` function. Also, insert "
"the following two statements directly after :c:func:`Py_Initialize`::"
msgstr ""
#: ../src/Doc/extending/embedding.rst:230
msgid ""
"These two lines initialize the ``numargs`` variable, and make the :func:`emb."
"numargs` function accessible to the embedded Python interpreter. With these "
"extensions, the Python script can do things like ::"
msgstr ""
#: ../src/Doc/extending/embedding.rst:237
msgid ""
"In a real application, the methods will expose an API of the application to "
"Python."
msgstr ""
#: ../src/Doc/extending/embedding.rst:247
msgid "Embedding Python in C++"
msgstr ""
#: ../src/Doc/extending/embedding.rst:249
msgid ""
"It is also possible to embed Python in a C++ program; precisely how this is "
"done will depend on the details of the C++ system used; in general you will "
"need to write the main program in C++, and use the C++ compiler to compile "
"and link your program. There is no need to recompile Python itself using C+"
"+."
msgstr ""
#: ../src/Doc/extending/embedding.rst:258
msgid "Linking Requirements"
msgstr ""
#: ../src/Doc/extending/embedding.rst:260
msgid ""
"While the :program:`configure` script shipped with the Python sources will "
"correctly build Python to export the symbols needed by dynamically linked "
"extensions, this is not automatically inherited by applications which embed "
"the Python library statically, at least on Unix. This is an issue when the "
"application is linked to the static runtime library (:file:`libpython.a`) "
"and needs to load dynamic extensions (implemented as :file:`.so` files)."
msgstr ""
#: ../src/Doc/extending/embedding.rst:267
msgid ""
"The problem is that some entry points are defined by the Python runtime "
"solely for extension modules to use. If the embedding application does not "
"use any of these entry points, some linkers will not include those entries "
"in the symbol table of the finished executable. Some additional options are "
"needed to inform the linker not to remove these symbols."
msgstr ""
#: ../src/Doc/extending/embedding.rst:273
msgid ""
"Determining the right options to use for any given platform can be quite "
"difficult, but fortunately the Python configuration already has those "
"values. To retrieve them from an installed Python interpreter, start an "
"interactive interpreter and have a short session like this::"
msgstr ""
#: ../src/Doc/extending/embedding.rst:284
msgid ""
"The contents of the string presented will be the options that should be "
"used. If the string is empty, there's no need to add any additional "
"options. The :const:`LINKFORSHARED` definition corresponds to the variable "
"of the same name in Python's top-level :file:`Makefile`."
msgstr ""
#: ../src/Doc/extending/extending.rst:8
msgid "Extending Python with C or C++"
msgstr ""
#: ../src/Doc/extending/extending.rst:10
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 ""
#: ../src/Doc/extending/extending.rst:15
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 ""
#: ../src/Doc/extending/extending.rst:20
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 ""
#: ../src/Doc/extending/extending.rst:23
msgid ""
"Do note that if your use case is calling C library functions or system "
"calls, you should consider using the :mod:`ctypes` module rather than "
"writing custom C code. Not only does :mod:`ctypes` let you write Python code "
"to interface with C code, but it is more portable between implementations of "
"Python than writing and compiling an extension module which typically ties "
"you to CPython."
msgstr ""
#: ../src/Doc/extending/extending.rst:34
#, fuzzy
msgid "A Simple Example"
msgstr "Un exemple simple"
# f29189ec471c41cd8fab25f67821795d
#: ../src/Doc/extending/extending.rst:36
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 ""
#: ../src/Doc/extending/extending.rst:45
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 ""
#: ../src/Doc/extending/extending.rst:50
#, fuzzy
msgid "The first line of our file can be::"
msgstr "La première ligne de notre fichier peut être:"
#: ../src/Doc/extending/extending.rst:54
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 ""
#: ../src/Doc/extending/extending.rst:59
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 ""
# fa43971877c844418f03b4566324fed6
#: ../src/Doc/extending/extending.rst:63
msgid ""
"All user-visible symbols defined by :file:`Python.h` have a prefix of ``Py`` "
"or ``PY``, except those defined in standard header files. For convenience, "
"and since they are used extensively by the Python interpreter, ``\"Python.h"
"\"`` includes a few standard header files: ``<stdio.h>``, ``<string.h>``, "
"``<errno.h>``, and ``<stdlib.h>``. If the latter header file does not exist "
"on your system, it declares the functions :c:func:`malloc`, :c:func:`free` "
"and :c:func:`realloc` directly."
msgstr ""
#: ../src/Doc/extending/extending.rst:71
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 ""
#: ../src/Doc/extending/extending.rst:87
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 ""
#: ../src/Doc/extending/extending.rst:92
msgid ""
"The *self* argument points to the module object for module-level functions; "
"for a method it would point to the object instance."
msgstr ""
# e61902774f844d1b8dce17ac441fa96a
#: ../src/Doc/extending/extending.rst:95
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 ""
# d458a69cd398407e82c060b308578a62
#: ../src/Doc/extending/extending.rst:104
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 ""
#: ../src/Doc/extending/extending.rst:114
msgid "Intermezzo: Errors and Exceptions"
msgstr ""
# 0ec9cd212ade4dfc8a29b8a10b609289
#: ../src/Doc/extending/extending.rst:116
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 a *NULL* pointer). Exceptions are stored in a static "
"global variable inside the interpreter; if this variable is *NULL* no "
"exception has occurred. A second global variable stores the \"associated "
"value\" of the exception (the second argument to :keyword:`raise`). A third "
"variable contains the stack traceback in case the error originated in Python "
"code. These three variables are the C equivalents of the Python variables "
"``sys.exc_type``, ``sys.exc_value`` and ``sys.exc_traceback`` (see the "
"section on module :mod:`sys` in the Python Library Reference). It is "
"important to know about them to understand how errors are passed around."
msgstr ""
#: ../src/Doc/extending/extending.rst:128
msgid ""
"The Python API defines a number of functions to set various types of "
"exceptions."
msgstr ""
# 3b52f1f297084e77b5eeaa2944e452e9
#: ../src/Doc/extending/extending.rst:130
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 ""
# 001c9fa90bbe4d1ea1a579fe28bb70c7
#: ../src/Doc/extending/extending.rst:136
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 ""
# 83b4af02ae624c988ab82888b1811c18
#: ../src/Doc/extending/extending.rst:143
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 ""
# 2db65c5053424043b6d8619f45b62c74
#: ../src/Doc/extending/extending.rst:149
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 :c:func:`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 :c:func:`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 ""
# a60d5722171e461d908fd5c12be5c595
#: ../src/Doc/extending/extending.rst:159
msgid ""
"(There are situations where a module can actually give a more detailed error "
"message by calling another :c:func:`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 ""
# bc0ae3fdb33642178f4235364ca70345
#: ../src/Doc/extending/extending.rst:165
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 ""
# a3f1e3ed53ff4e9e84717c861c3050dc
#: ../src/Doc/extending/extending.rst:171
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:`PyInt_FromLong`) already do this, "
"so this note is only relevant to those who call :c:func:`malloc` directly."
msgstr ""
# b1880b1a76504ea495d77744d7309b43
#: ../src/Doc/extending/extending.rst:177
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 ""
# 4033d5b3db594253a10a4a61fdcca46c
#: ../src/Doc/extending/extending.rst:181
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 ""
# a4e6513d01294765a4c83895d1cc41d7
#: ../src/Doc/extending/extending.rst:185
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_IOError`). 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 ""
#: ../src/Doc/extending/extending.rst:195
msgid ""
"You can also define a new exception that is unique to your module. For this, "
"you usually declare a static object variable at the beginning of your file::"
msgstr ""
# 018562d96ab044ee9733e3dc5a66e2d9
#: ../src/Doc/extending/extending.rst:200
msgid ""
"and initialize it in your module's initialization function (:c:func:"
"`initspam`) with an exception object (leaving out the error checking for "
"now)::"
msgstr ""
# a542a8a47a2e4145973b937428c8c4ba
#: ../src/Doc/extending/extending.rst:217
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 ""
# e4ea678a9dac41b0a0d697b67004fd86
#: ../src/Doc/extending/extending.rst:222
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 ""
# 9bb58cdf6e0248b897ca3339d1e71acf
#: ../src/Doc/extending/extending.rst:229
msgid ""
"We discuss the use of ``PyMODINIT_FUNC`` as a function return type later in "
"this sample."
msgstr ""
# f2207311e9cd47a09df71e0ee5b09abb
#: ../src/Doc/extending/extending.rst:232
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 ""
#: ../src/Doc/extending/extending.rst:255
msgid "Back to the Example"
msgstr ""
#: ../src/Doc/extending/extending.rst:257
msgid ""
"Going back to our example function, you should now be able to understand "
"this statement::"
msgstr ""
# b9ce94da94db4682b9df03a7e11a1f90
#: ../src/Doc/extending/extending.rst:263
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 ""
# a9a5fa4f6dc14f6cba0a145b3843e4ae
#: ../src/Doc/extending/extending.rst:271
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 ""
# 34ecacf047f847ad9e0365f4910c5985
#: ../src/Doc/extending/extending.rst:276
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:`Py_BuildValue`, "
"which is something like the inverse of :c:func:`PyArg_ParseTuple`: it takes "
"a format string and an arbitrary number of C values, and returns a new "
"Python object. More info on :c:func:`Py_BuildValue` is given later. ::"
msgstr ""
#: ../src/Doc/extending/extending.rst:284
msgid ""
"In this case, it will return an integer object. (Yes, even integers are "
"objects on the heap in Python!)"
msgstr ""
# 03157000bd2b4c3b982c21957d552b2f
#: ../src/Doc/extending/extending.rst:287
msgid ""
"If you have a C function that returns no useful argument (a function "
"returning :c:type:`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 ""
# f736f602207c474897a72b25505f4f7c
#: ../src/Doc/extending/extending.rst:295
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 ""
#: ../src/Doc/extending/extending.rst:303
msgid "The Module's Method Table and Initialization Function"
msgstr ""
# c8c2986539094d0eb5c0c8b80478e3b5
#: ../src/Doc/extending/extending.rst:305
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 ""
# 38dd15849ef548d39f059171b23ddec6
#: ../src/Doc/extending/extending.rst:316
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 ""
# bb032e727939456ca78a5d4e4fdd6065
#: ../src/Doc/extending/extending.rst:321
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 ""
# 511a00001b2d4e028ce779b15d0e3aa4
#: ../src/Doc/extending/extending.rst:325
msgid ""
"The :const:`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 ""
# cc9de65bdce54c0a991ce0a2103f2f55
#: ../src/Doc/extending/extending.rst:331
msgid ""
"The method table must be passed to the interpreter in the module's "
"initialization function. The initialization function must be named :c:func:"
"`initname`, where *name* is the name of the module, and should be the only "
"non-\\ ``static`` item defined in the module file::"
msgstr ""
# dff31425e0cf4905831c3fadd6f24b87
#: ../src/Doc/extending/extending.rst:342
msgid ""
"Note that PyMODINIT_FUNC declares the function as ``void`` return type, "
"declares any special linkage declarations required by the platform, and for "
"C++ declares the function as ``extern \"C\"``."
msgstr ""
# 725ecbe2a3a641b9bf4b4f047a5bf175
#: ../src/Doc/extending/extending.rst:346
msgid ""
"When the Python program imports module :mod:`spam` for the first time, :c:"
"func:`initspam` is called. (See below for comments about embedding Python.) "
"It calls :c:func:`Py_InitModule`, which creates a \"module object\" (which "
"is inserted in the dictionary ``sys.modules`` under the key ``\"spam\"``), "
"and inserts built-in function objects into the newly created module based "
"upon the table (an array of :c:type:`PyMethodDef` structures) that was "
"passed as its second argument. :c:func:`Py_InitModule` returns a pointer to "
"the module object that it creates (which is unused here). It may abort with "
"a fatal error for certain errors, or return *NULL* if the module could not "
"be initialized satisfactorily."
msgstr ""
# b689098d952a43d5908f62169cf86f2a
#: ../src/Doc/extending/extending.rst:357
msgid ""
"When embedding Python, the :c:func:`initspam` function is not called "
"automatically unless there's an entry in the :c:data:`_PyImport_Inittab` "
"table. The easiest way to handle this is to statically initialize your "
"statically-linked modules by directly calling :c:func:`initspam` after the "
"call to :c:func:`Py_Initialize`::"
msgstr ""
#: ../src/Doc/extending/extending.rst:375
msgid ""
"An example may be found in the file :file:`Demo/embed/demo.c` in the Python "
"source distribution."
msgstr ""
# ada00ca2e2c648baa1c2f4ce1724d95c
#: ../src/Doc/extending/extending.rst:380
msgid ""
"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`) can create problems for some "
"extension modules. Extension module authors should exercise caution when "
"initializing internal data structures. Note also that the :func:`reload` "
"function can be used with extension modules, and will call the module "
"initialization function (:c:func:`initspam` in the example), but will not "
"load the module again if it was loaded from a dynamically loadable object "
"file (:file:`.so` on Unix, :file:`.dll` on Windows)."
msgstr ""
#: ../src/Doc/extending/extending.rst:390
msgid ""
"A more substantial example module is included in the Python source "
"distribution as :file:`Modules/xxmodule.c`. This file may be used as a "
"template or simply read as an example."
msgstr ""
#: ../src/Doc/extending/extending.rst:398
msgid "Compilation and Linkage"
msgstr ""
#: ../src/Doc/extending/extending.rst:400
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 ""
#: ../src/Doc/extending/extending.rst:407
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 ""
#: ../src/Doc/extending/extending.rst:416
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 ""
#: ../src/Doc/extending/extending.rst:422
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 ""
#: ../src/Doc/extending/extending.rst:431
msgid "Calling Python Functions from C"
msgstr ""
#: ../src/Doc/extending/extending.rst:433
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 ""
#: ../src/Doc/extending/extending.rst:441
msgid ""
"Fortunately, the Python interpreter is easily called recursively, and there "
"is a standard interface to call a Python function. (I won't dwell on how to "
"call the Python parser with a particular string as input --- if you're "
"interested, have a look at the implementation of the :option:`-c` command "
"line option in :file:`Modules/main.c` from the Python source code.)"
msgstr ""
# d84f06a0ed614ecb9d7bde191044c82e
#: ../src/Doc/extending/extending.rst:447
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 "