2020\begin {abstract }
2121
2222\noindent
23- This manual documents the API used by C (or C++ ) programmers who want
23+ This manual documents the API used by \C {} (or \Cpp {} ) programmers who want
2424to write extension modules or embed Python. It is a companion to
2525`` Extending and Embedding the Python Interpreter'' , which describes
2626the general principles of extension writing but does not document the
4242
4343\chapter {Introduction }
4444
45- The Application Programmer's Interface to Python gives C and C++
45+ The Application Programmer's Interface to Python gives \C {} and \Cpp {}
4646programmers access to the Python interpreter at a variety of levels.
47- The API is equally usable from C++ , but for brevity it is generally
48- referred to as the Python/C API. There are two fundamentally
49- different reasons for using the Python/C API. The first reason is to
50- write `` extension modules'' for specific purposes; these are C modules
47+ The API is equally usable from \Cpp {} , but for brevity it is generally
48+ referred to as the Python/\C {} API. There are two fundamentally
49+ different reasons for using the Python/\C {} API. The first reason is to
50+ write `` extension modules'' for specific purposes; these are \C {} modules
5151that extend the Python interpreter. This is probably the most common
5252use. The second reason is to use Python as a component in a larger
5353application; this technique is generally referred to as `` embedding''
@@ -97,7 +97,7 @@ \section{Objects, Types and Reference Counts}
9797object. Since all Python object types are treated the same way by the
9898Python language in most situations (e.g., assignments, scope rules,
9999and argument passing), it is only fitting that they should be
100- represented by a single C type. All Python objects live on the heap:
100+ represented by a single \C {} type. All Python objects live on the heap:
101101you never declare an automatic or static variable of type
102102\code {PyObject}, only pointer variables of type \code {PyObject *} can
103103be declared.
@@ -115,8 +115,8 @@ \subsection{Reference Counts}
115115The reference count is important because today's computers have a
116116finite (and often severly limited) memory size; it counts how many
117117different places there are that have a reference to an object. Such a
118- place could be another object, or a global (or static) C variable, or
119- a local variable in some C function. When an object's reference count
118+ place could be another object, or a global (or static) \C {} variable, or
119+ a local variable in some \C {} function. When an object's reference count
120120becomes zero, the object is deallocated. If it contains references to
121121other objects, their reference count is decremented. Those other
122122objects may be deallocated in turn, if this decrement makes their
@@ -150,7 +150,7 @@ \subsection{Reference Counts}
150150least one other reference to the object that lives at least as long as
151151our variable, there is no need to increment the reference count
152152temporarily. An important situation where this arises is in objects
153- that are passed as arguments to C functions in an extension module
153+ that are passed as arguments to \C {} functions in an extension module
154154that are called from Python; the call mechanism guarantees to hold a
155155reference to every argument for the duration of the call.
156156
@@ -228,7 +228,7 @@ \subsubsection{Reference Count Details}
228228You might find it strange that the `` recommended'' approach takes more
229229code. However, in practice, you will rarely use these ways of
230230creating and populating a tuple or list. There's a generic function,
231- \code {Py_BuildValue()}, that can create most common objects from C
231+ \code {Py_BuildValue()}, that can create most common objects from \C {}
232232values, directed by a `` format string'' . For example, the above two
233233blocks of code could be replaced by the following (which also takes
234234care of the error checking!):
@@ -328,7 +328,7 @@ \subsubsection{Reference Count Details}
328328\subsection {Types }
329329
330330There are few other data types that play a significant role in
331- the Python/C API; most are simple C types such as \code {int},
331+ the Python/C API; most are simple \C {} types such as \code {int},
332332\code {long}, \code {double} and \code {char *}. A few structure types
333333are used to describe static tables used to list the functions exported
334334by a module or the data attributes of a new object type. These will
@@ -342,7 +342,7 @@ \section{Exceptions}
342342they reach the top-level interpreter, where they are reported to the
343343user accompanied by a stack traceback.
344344
345- For C programmers, however, error checking always has to be explicit.
345+ For \C {} programmers, however, error checking always has to be explicit.
346346All functions in the Python/C API can raise exceptions, unless an
347347explicit claim is made otherwise in a function's documentation. In
348348general, when a function encounters an error, it sets an exception,
@@ -369,8 +369,8 @@ \section{Exceptions}
369369object \code {sys.exc_type}, \code {sys.exc_value},
370370\code {sys.exc_traceback}; however, they are not the same: the Python
371371objects represent the last exception being handled by a Python
372- \code {try...except} statement, while the C level exception state only
373- exists while an exception is being passed on between C functions until
372+ \code {try...except} statement, while the \C {} level exception state only
373+ exists while an exception is being passed on between \C {} functions until
374374it reaches the Python interpreter, which takes care of transferring it
375375to \code {sys.exc_type} and friends.
376376
@@ -410,7 +410,7 @@ \section{Exceptions}
410410 return item + 1
411411\end {verbatim }
412412
413- Here is the corresponding C code, in all its glory:
413+ Here is the corresponding \C {} code, in all its glory:
414414
415415\begin {verbatim }
416416int incr_item(PyObject *dict, PyObject *key)
@@ -453,7 +453,7 @@ \section{Exceptions}
453453\end {verbatim }
454454
455455This example represents an endorsed use of the \code {goto} statement
456- in C ! It illustrates the use of \code {PyErr_ExceptionMatches()} and
456+ in \C {} ! It illustrates the use of \code {PyErr_ExceptionMatches()} and
457457\code {PyErr_Clear()} to handle specific exceptions, and the use of
458458\code {Py_XDECREF()} to dispose of owned references that may be
459459\NULL {} (note the `X' in the name; \code {Py_DECREF()} would crash
@@ -483,7 +483,7 @@ \section{Embedding Python}
483483\code {PySys_SetArgv(\var {argc}, \var {argv})} subsequent to the call
484484to \code {Py_Initialize()}.
485485
486- On most systems (in particular, on Unix and Windows, although the
486+ On most systems (in particular, on \UNIX {} and Windows, although the
487487details are slightly different), \code {Py_Initialize()} calculates the
488488module search path based upon its best guess for the location of the
489489standard Python interpreter executable, assuming that the Python
@@ -532,13 +532,13 @@ \chapter{Basic Utilities}
532532performed. This function should only be invoked when a condition is
533533detected that would make it dangerous to continue using the Python
534534interpreter; e.g., when the object administration appears to be
535- corrupted. On Unix , the standard C library function \code {abort()} is
535+ corrupted. On \UNIX {} , the standard \C {} library function \code {abort()} is
536536called which will attempt to produce a \file {core} file.
537537\end {cfuncdesc }
538538
539539\begin {cfuncdesc }{void}{Py_Exit}{int status}
540540Exit the current process. This calls \code {Py_Finalize()} and then
541- calls the standard C library function \code {exit(0)}.
541+ calls the standard \C {} library function \code {exit(0)}.
542542\end {cfuncdesc }
543543
544544\begin {cfuncdesc }{int}{Py_AtExit}{void (*func) ()}
@@ -605,7 +605,7 @@ \chapter{Exception Handling}
605605
606606The functions in this chapter will let you handle and raise Python
607607exceptions. It is important to understand some of the basics of
608- Python exception handling. It works somewhat like the Unix
608+ Python exception handling. It works somewhat like the \UNIX {}
609609\code {errno} variable: there is a global indicator (per thread) of the
610610last error that occurred. Most functions don't clear this on success,
611611but will set it to indicate the cause of the error on failure. Most
@@ -729,8 +729,8 @@ \chapter{Exception Handling}
729729\end {cfuncdesc }
730730
731731\begin {cfuncdesc }{PyObject *}{PyErr_SetFromErrno}{PyObject *type}
732- This is a convenience function to raise an exception when a C library
733- function has returned an error and set the C variable \code {errno}.
732+ This is a convenience function to raise an exception when a \C {} library
733+ function has returned an error and set the \C {} variable \code {errno}.
734734It constructs a tuple object whose first item is the integer
735735\code {errno} value and whose second item is the corresponding error
736736message (gotten from \code {strerror()}), and then calls
@@ -772,11 +772,11 @@ \chapter{Exception Handling}
772772PyObject *base, PyObject *dict}
773773\strong {(NEW in 1.5a4!)}
774774This utility function creates and returns a new exception object. The
775- \var {name} argument must be the name of the new exception, a C string
775+ \var {name} argument must be the name of the new exception, a \C {} string
776776of the form \code {module.class}. The \var {base} and \var {dict}
777777arguments are normally \NULL {}. Normally, this creates a class
778778object derived from the root for all exceptions, the built-in name
779- \code {Exception} (accessible in C as \code {PyExc_Exception}). In this
779+ \code {Exception} (accessible in \C {} as \code {PyExc_Exception}). In this
780780case the \code {__module__} attribute of the new class is set to the
781781first part (up to the last dot) of the \var {name} argument, and the
782782class name is set to the last part (after the last dot). When the
@@ -825,7 +825,7 @@ \section{Standard Exceptions}
825825\chapter {Utilities }
826826
827827The functions in this chapter perform various utility tasks, such as
828- parsing function arguments and constructing Python values from C
828+ parsing function arguments and constructing Python values from \C {}
829829values.
830830
831831\section {OS Utilities }
@@ -842,7 +842,7 @@ \section{OS Utilities}
842842\begin {cfuncdesc }{long}{PyOS_GetLastModificationTime}{char *filename}
843843Return the time of last modification of the file \code {filename}.
844844The result is encoded in the same way as the timestamp returned by
845- the standard C library function \code {time()}.
845+ the standard \C {} library function \code {time()}.
846846\end {cfuncdesc }
847847
848848
@@ -1142,7 +1142,7 @@ \section{Object Protocol}
11421142
11431143\begin {cfuncdesc }{PyObject*}{PyObject_CallFunction}{PyObject *callable_object, char *format, ...}
11441144Call a callable Python object \code {callable_object}, with a
1145- variable number of C arguments. The C arguments are described
1145+ variable number of \C {} arguments. The \C {} arguments are described
11461146using a mkvalue-style format string. The format may be \NULL {},
11471147indicating that no arguments are provided. Returns the
11481148result of the call on success, or \NULL {} on failure. This is
@@ -1152,7 +1152,7 @@ \section{Object Protocol}
11521152
11531153\begin {cfuncdesc }{PyObject*}{PyObject_CallMethod}{PyObject *o, char *m, char *format, ...}
11541154Call the method named \code {m} of object \code {o} with a variable number of
1155- C arguments. The C arguments are described by a mkvalue
1155+ C arguments. The \C {} arguments are described by a mkvalue
11561156format string. The format may be \NULL {}, indicating that no
11571157arguments are provided. Returns the result of the call on
11581158success, or \NULL {} on failure. This is the equivalent of the
@@ -1541,12 +1541,12 @@ \section{Constructors}
15411541\begin {cfuncdesc }{PyObject*}{PyFile_FromString}{char *file_name, char *mode}
15421542On success, returns a new file object that is opened on the
15431543file given by \code {file_name}, with a file mode given by \code {mode},
1544- where \code {mode} has the same semantics as the standard C routine,
1544+ where \code {mode} has the same semantics as the standard \C {} routine,
15451545fopen. On failure, return -1.
15461546\end {cfuncdesc }
15471547
15481548\begin {cfuncdesc }{PyObject*}{PyFile_FromFile}{FILE *fp, char *file_name, char *mode, int close_on_del}
1549- Return a new file object for an already opened standard C
1549+ Return a new file object for an already opened standard \C {}
15501550file pointer, \code {fp}. A file name, \code {file_name}, and open mode,
15511551\code {mode}, must be provided as well as a flag, \code {close_on_del}, that
15521552indicates whether the file is to be closed when the file
@@ -1691,7 +1691,7 @@ \chapter{Initialization, Finalization, and Threads}
16911691also separate. The new environment has no \code {sys.argv} variable.
16921692It has new standard I/O stream file objects \code {sys.stdin},
16931693\code {sys.stdout} and \code {sys.stderr} (however these refer to the
1694- same underlying \code {FILE} structures in the C library).
1694+ same underlying \code {FILE} structures in the \C {} library).
16951695
16961696The return value points to the first thread state created in the new
16971697sub-interpreter. This thread state is made the current thread state.
@@ -1775,7 +1775,7 @@ \chapter{Initialization, Finalization, and Threads}
17751775corresponds to the \code {prefix} variable in the top-level
17761776\code {Makefile} and the \code {--prefix} argument to the
17771777\code {configure} script at build time. The value is available to
1778- Python code as \code {sys.prefix}. It is only useful on Unix . See
1778+ Python code as \code {sys.prefix}. It is only useful on \UNIX {} . See
17791779also the next function.
17801780\end {cfuncdesc }
17811781
@@ -1790,7 +1790,7 @@ \chapter{Initialization, Finalization, and Threads}
17901790\code {exec_prefix} variable in the top-level \code {Makefile} and the
17911791\code {--exec_prefix} argument to the \code {configure} script at build
17921792time. The value is available to Python code as
1793- \code {sys.exec_prefix}. It is only useful on Unix .
1793+ \code {sys.exec_prefix}. It is only useful on \UNIX {} .
17941794
17951795Background: The exec-prefix differs from the prefix when platform
17961796dependent files (such as executables and shared libraries) are
@@ -1804,7 +1804,7 @@ \chapter{Initialization, Finalization, and Threads}
18041804operating system are considered the same platform, but Intel machines
18051805running Solaris 2.x are another platform, and Intel machines running
18061806Linux are yet another platform. Different major revisions of the same
1807- operating system generally also form different platforms. Non-Unix
1807+ operating system generally also form different platforms. Non-\UNIX {}
18081808operating systems are a different story; the installation strategies
18091809on those systems are so different that the prefix and exec-prefix are
18101810meaningless, and set to the empty string. Note that compiled Python
@@ -1832,7 +1832,7 @@ \chapter{Initialization, Finalization, and Threads}
18321832program name (set by \code {Py_SetProgramName()} above) and some
18331833environment variables. The returned string consists of a series of
18341834directory names separated by a platform dependent delimiter character.
1835- The delimiter character is \code {':'} on Unix , \code {';'} on
1835+ The delimiter character is \code {':'} on \UNIX {} , \code {';'} on
18361836DOS/Windows, and \code {'\\ n'} (the ASCII newline character) on
18371837Macintosh. The returned string points into static storage; the caller
18381838should not modify its value. The value is available to Python code
@@ -1858,7 +1858,7 @@ \chapter{Initialization, Finalization, and Threads}
18581858\end {cfuncdesc }
18591859
18601860\begin {cfuncdesc }{const char *}{Py_GetPlatform}{}
1861- Return the platform identifier for the current platform. On Unix ,
1861+ Return the platform identifier for the current platform. On \UNIX {} ,
18621862this is formed from the `` official'' name of the operating system,
18631863converted to lower case, followed by the major revision number; e.g.,
18641864for Solaris 2.x, which is also known as SunOS 5.x, the value is
@@ -2014,7 +2014,7 @@ \section{Thread State and the Global Interpreter Lock}
20142014lock must be acquired before storing the thread state pointer.
20152015
20162016Why am I going on with so much detail about this? Because when
2017- threads are created from C , they don't have the global interpreter
2017+ threads are created from \C {} , they don't have the global interpreter
20182018lock, nor is there a thread state data structure for them. Such
20192019threads must bootstrap themselves into existence, by first creating a
20202020thread state data structure, then acquiring the lock, and finally
0 commit comments