ã¯ããã«Â¶
Python ã®ã¢ããªã±ãŒã·ã§ã³ããã°ã©ãçšã€ã³ã¿ãã§ãŒã¹ (Application Programmer's Interface, API) ã¯ã Python ã€ã³ã¿ããªã¿ã«å¯Ÿããæ§ã ãªã¬ãã«ã§ã®ã¢ã¯ã»ã¹ææ®µã C ã C++ ã®ããã°ã©ãã«æäŸããŠããŸãããã® API ã¯éåžž C++ ãããå šãåãããã«å©çšã§ããã®ã§ãããç°¡æœãªåŒã³åã«ããããã« Python/C API ãšåã¥ããããŠããŸããæ ¹æ¬çã«ç°ãªãäºã€ã®ç®çããã Python/C API ãçšããããŸãã第äžã¯ãç¹å®çšéã® æ¡åŒµã¢ãžã¥ãŒã« (extension module) ãããªãã¡ Python ã€ã³ã¿ããªã¿ãæ¡åŒµãã C ã§æžãããã¢ãžã¥ãŒã«ãèšè¿°ããããšããç®çã§ãã第äºã¯ãããå€§èŠæš¡ãªã¢ããªã±ãŒã·ã§ã³å ã§ Python ãæ§æèŠçŽ (component) ãšããŠå©çšãããšããç®çã§ã; ãã®ãã¯ããã¯ã¯ãäžè¬çã«ã¯ã¢ããªã±ãŒã·ã§ã³ãžã® Python ã®åã蟌㿠(embedding) ãšåŒã³ãŸãã
æ¡åŒµã¢ãžã¥ãŒã«ã®äœæã¯æ¯èŒçããããããããã»ã¹ã§ã "æåŒæž (cookbook)" çãªã¢ãããŒãã§ããŸãå®çŸã§ããŸããäœæ¥ãããçšåºŠãŸã§èªååããŠãããããŒã«ãããã€ããããŸããäžæ¹ãä»ã®ã¢ããªã±ãŒã·ã§ã³ãžã® Python ã®åã蟌ã¿ã¯ãPython ãã§ããŠããæ©ãææããè¡ãããŠããŸããããæ¡åŒµã¢ãžã¥ãŒã«ã®äœæã«æ¯ã¹ããšããé£è§£ã§ãã
å€ãã® API 颿°ã¯ãPython ã®åã蟌ã¿ã§ãããæ¡åŒµã§ãããã«é¢ããã圹ç«ã¡ãŸã; ãšã¯ãããPython ãåã蟌ãã§ããã»ãšãã©ã®ã¢ããªã±ãŒã·ã§ã³ã¯ãåæã«èªäœã®æ¡åŒµã¢ãžã¥ãŒã«ãæäŸããå¿ èŠãçããããšã«ãªãã§ããããããPython ãå®éã«ã¢ããªã±ãŒã·ã§ã³ã«åã蟌ãã§ã¿ãåã«æ¡åŒµã¢ãžã¥ãŒã«ã®æžãæ¹ã«è©³ãããªã£ãŠããã®ã¯ããèãã ãšæããŸãã
èšèªããŒãžã§ã³äºææ§Â¶
Pythonã® C API 㯠C11 ã C++11 ããŒãžã§ã³ã® C ãš C++ ã«äºææ§ããããŸãã
This is a lower limit: the C API does not require features from later C/C++ versions. You do not need to enable your compiler's "c11 mode".
ã³ãŒãã£ã³ã°åºæºÂ¶
CPython ã«å«ãã C ã³ãŒããæžããŠããå Žåã¯ã PEP 7 ã®ã¬ã€ãã©ã€ã³ãšåºæºã«åŸããªããã° ãªããŸãã ã ãã®ã¬ã€ãã©ã€ã³ã¯ãã³ã³ããªãã¥ãŒã察象㮠Python ã®ããŒãžã§ã³ã«é¢ä¿ç¡ãé©çšãããŸãã èªèº«ã®ãµãŒãããŒãã£ãŒã®ã¢ãžã¥ãŒã«ã§ã¯ãããããã€ã Python ã«ã³ã³ããªãã¥ãŒãããã€ããã§ãªããã°ããã®æ £ç¿ã«åŸãå¿ èŠã¯ãããŸããã
ã€ã³ã¯ã«ãŒããã¡ã€ã«Â¶
Python/C API ã䜿ãããã«å¿ èŠãªã颿°ãåããã³ãã¯ãã®å šãŠã®å®çŸ©ãã€ã³ã¯ã«ãŒãããã«ã¯ã以äžã®è¡:
#define PY_SSIZE_T_CLEAN
#include <Python.h>
ããœãŒã¹ã³ãŒãã«èšè¿°ããŸãããã®è¡ãèšè¿°ãããšãæšæºããã: <stdio.h>, <string.h>, <errno.h>, <limits.h>, <assert.h>, <stdlib.h> ã (å©çšã§ããã°) ã€ã³ã¯ã«ãŒãããŸãã
泚é
Python ã¯ãã·ã¹ãã ã«ãã£ãŠã¯æšæºãããã®å®çŸ©ã«åœ±é¿ãããããªããªããã»ããµå®çŸ©ãè¡ã£ãŠããã®ã§ã Python.h ããããã®æšæºããããããåã«ã€ã³ã¯ã«ãŒã ããã°ãªããŸãã ã
Python.h ãã€ã³ã¯ã«ãŒãããåã«ãåžžã« PY_SSIZE_T_CLEAN ãå®çŸ©ããããšãæšå¥šãããŸãã
ãã®ãã¯ãã®è§£èª¬ã«ã€ããŠã¯ åŒæ°ã®è§£éãšå€ã®æ§ç¯ ãåç
§ããŠãã ããã
Python.h ã§å®çŸ©ãããŠããããŠãŒã¶ããèŠããååå
šãŠ (Python.h ãã€ã³ã¯ã«ãŒãããŠããæšæºãããã®ååã¯é€ããŸã) ã«ã¯ãæ¥é æåå Py ãŸã㯠_Py ãä»ããŸãã_Py ã§å§ãŸãåå㯠Python å®è£
ã§å
éšäœ¿çšããããã®ååã§ãæ¡åŒµã¢ãžã¥ãŒã«ã®äœè
ã¯äœ¿ã£ãŠã¯ãªããŸãããæ§é äœã®ã¡ã³ãã«ã¯äºçŽæžã¿ã®æ¥é æååã¯ãããŸããã
泚é
API ã®ãŠãŒã¶ã¯ãPy ã _Py ã§å§ãŸãååãå®çŸ©ããã³ãŒããçµ¶å¯Ÿã«æžããŠã¯ãªããŸããã
åŸããã³ãŒããèªãäººãæ··ä¹±ãããããå°æ¥ã® Python ã®ããŒãžã§ã³ã§åãååãå®çŸ©ãããŠããŠãŒã¶ã®æžããã³ãŒãã®å¯æ¬æ§ãå±ããããå¯èœæ§ããããŸãã
ããããã¡ã€ã«çŸ€ã¯éåžž Python ãšå
±ã«ã€ã³ã¹ããŒã«ãããŸãã Unixã§ã¯ prefix/include/pythonversion/ ããã³ exec_prefix/include/pythonversion/ ã«çœ®ãããŸãã prefix ãš exec_prefix 㯠Python ããã«ãããéã® configure ã¹ã¯ãªããã«äžãããã©ã¡ã¿ã«å¯Ÿå¿ãã version 㯠'%d.%d' % sys.version_info[:2] ã«å¯Ÿå¿ããŸãã Windows ã§ã¯ãããã㯠prefix/include ã«çœ®ãããŸãã prefix ã¯ã€ã³ã¹ããŒã©ã«æå®ããã€ã³ã¹ããŒã«ãã£ã¬ã¯ããªã§ãã
ããããã€ã³ã¯ã«ãŒãããã«ã¯ãåãããã®å
¥ã£ããã£ã¬ã¯ã㪠(å¥ã
ã®ãã£ã¬ã¯ããªã®å Žåã¯äž¡æ¹) ããã³ã³ãã€ã©ãã€ã³ã¯ã«ãŒããã¡ã€ã«ãæ€çŽ¢ããããã®ãã¹ã«å
¥ããŸãã芪ãã£ã¬ã¯ããªããµãŒããã¹ã«å
¥ããŠã #include <pythonX.Y/Python.h> ã®ããã«ããŠã¯ ãªããŸãã ; prefix å
ã®ãã©ãããã©ãŒã ã«äŸåããªããããã¯ã exec_prefix ãããã©ãããã©ãŒã äŸåã®ããããã€ã³ã¯ã«ãŒãããŠããã®ã§ããã®ãããªæäœãè¡ããšè€æ°ã®ãã©ãããã©ãŒã ã§ã®ãã«ããã§ããªããªããŸãã
C++ users should note that although the API is defined entirely using C, the
header files properly declare the entry points to be extern "C". As a result,
there is no need to do anything special to use the API from C++.
䟿å©ãªãã¯ã¶
Several useful macros are defined in the Python header files. Many are
defined closer to where they are useful (for example, Py_RETURN_NONE,
PyMODINIT_FUNC).
Others of a more general utility are defined here. This is not necessarily a
complete listing.
-
Py_CAN_START_THREADS¶
If this macro is defined, then the current system is able to start threads.
Currently, all systems supported by CPython (per PEP 11), with the exception of some WebAssembly platforms, support starting threads.
Added in version 3.13.
-
Py_GETENV(s)¶
Like
getenv(s), but returnsNULLif-Ewas passed on the command line (seePyConfig.use_environment).
Docstring macros¶
-
PyDoc_STRVAR(name, str)¶
Creates a variable with name name that can be used in docstrings. If Python is built without docstrings (
--without-doc-strings), the value will be an empty string.以äžã¯ããã°ã©ã äŸã§ã:
PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element."); static PyMethodDef deque_methods[] = { // ... {"pop", (PyCFunction)deque_pop, METH_NOARGS, pop_doc}, // ... }
Expands to
PyDoc_VAR(name) = PyDoc_STR(str).
-
PyDoc_STR(str)¶
Expands to the given input string, or an empty string if docstrings are disabled (
--without-doc-strings).以äžã¯ããã°ã©ã äŸã§ã:
static PyMethodDef pysqlite_row_methods[] = { {"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS, PyDoc_STR("Returns the keys of the row.")}, {NULL, NULL} };
-
PyDoc_VAR(name)¶
Declares a static character array variable with the given name. Expands to
static const char name[]äŸãã°:
PyDoc_VAR(python_doc) = PyDoc_STR( "A genus of constricting snakes in the Pythonidae family native " "to the tropics and subtropics of the Eastern Hemisphere.");
General utility macros¶
The following macros are for common tasks not specific to Python.
-
Py_UNUSED(arg)¶
ã³ã³ãã€ã©èŠåãæããããã«é¢æ°å®çŸ©ã®äœ¿çšãããªãåŒæ°ã«äœ¿çšããŠãã ãããäŸãã°:
int func(int a, int Py_UNUSED(b)) { return a; }ãAdded in version 3.4.
-
Py_GCC_ATTRIBUTE(name)¶
Use a GCC attribute name, hiding it from compilers that don't support GCC attributes (such as MSVC).
This expands to
__attribute__((name))on a GCC compiler, and expands to nothing on compilers that don't support GCC attributes.
Numeric utilities¶
-
Py_ABS(x)¶
xã®çµ¶å¯Ÿå€ãè¿ããŸããThe argument may be evaluated more than once. Consequently, do not pass an expression with side-effects directly to this macro.
If the result cannot be represented (for example, if
xhasINT_MINvalue for int type), the behavior is undefined.Corresponds roughly to
((x) < 0 ? -(x) : (x))Added in version 3.3.
-
Py_MAX(x, y)¶
-
Py_MIN(x, y)¶
Return the larger or smaller of the arguments, respectively.
Any arguments may be evaluated more than once. Consequently, do not pass an expression with side-effects directly to this macro.
Py_MAXcorresponds roughly to(((x) > (y)) ? (x) : (y)).Added in version 3.3.
-
Py_ARITHMETIC_RIGHT_SHIFT(type, integer, positions)¶
Similar to
integer >> positions, but forces sign extension, as the C standard does not define whether a right-shift of a signed integer will perform sign extension or a zero-fill.integer should be any signed integer type. positions is the number of positions to shift to the right.
Both integer and positions can be evaluated more than once; consequently, avoid directly passing a function call or some other operation with side-effects to this macro. Instead, store the result as a variable and then pass it.
type is unused and only kept for backwards compatibility. Historically, type was used to cast integer.
ããŒãžã§ã³ 3.1 ã§å€æŽ: This macro is now valid for all signed integer types, not just those for which
unsigned typeis legal. As a result, type is no longer used.
-
Py_CHARMASK(c)¶
åŒæ°ã¯æåãã[-128, 127] ããã㯠[0, 255] ã®ç¯å²ã®æŽæ°ã§ãªããã°ãªããŸããã ãã®ãã¯ãã¯
笊å·ãªãæåã«ãã£ã¹ãããcãè¿ããŸãã
Assertion utilities¶
-
Py_UNREACHABLE()¶
Use this when you have a code path that cannot be reached by design. For example, in the
default:clause in aswitchstatement for which all possible values are covered incasestatements. Use this in places where you might be tempted to put anassert(0)orabort()call.In release mode, the macro helps the compiler to optimize the code, and avoids a warning about unreachable code. For example, the macro is implemented with
__builtin_unreachable()on GCC in release mode.In debug mode, and on unsupported compilers, the macro expands to a call to
Py_FatalError().A use for
Py_UNREACHABLE()is following a call to a function that never returns but that is not declared_Noreturn.If a code path is very unlikely code but can be reached under exceptional case, this macro must not be used. For example, under low memory condition or if a system call returns a value out of the expected range. In this case, it's better to report the error to the caller. If the error cannot be reported to caller,
Py_FatalError()can be used.Added in version 3.7.
-
Py_SAFE_DOWNCAST(value, larger, smaller)¶
Cast value to type smaller from type larger, validating that no information was lost.
On release builds of Python, this is roughly equivalent to
((smaller) value)(in C++,static_cast<smaller>(value)will be used instead).On debug builds (implying that
Py_DEBUGis defined), this asserts that no information was lost with the cast from larger to smaller.value, larger, and smaller may all be evaluated more than once in the expression; consequently, do not pass an expression with side-effects directly to this macro.
-
Py_BUILD_ASSERT(cond)¶
Asserts a compile-time condition cond, as a statement. The build will fail if the condition is false or cannot be evaluated at compile time.
Corresponds roughly to
static_assert(cond)on C23 and above.äŸãã°:
Py_BUILD_ASSERT(sizeof(PyTime_t) == sizeof(int64_t));
Added in version 3.3.
-
Py_BUILD_ASSERT_EXPR(cond)¶
Asserts a compile-time condition cond, as an expression that evaluates to
0. The build will fail if the condition is false or cannot be evaluated at compile time.äŸãã°:
#define foo_to_char(foo) \ ((char *)(foo) + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0))
Added in version 3.3.
Type size utilities¶
-
Py_ARRAY_LENGTH(array)¶
Compute the length of a statically allocated C array at compile time.
The array argument must be a C array with a size known at compile time. Passing an array with an unknown size, such as a heap-allocated array, will result in a compilation error on some compilers, or otherwise produce incorrect results.
This is roughly equivalent to:
sizeof(array) / sizeof((array)[0])
-
Py_MEMBER_SIZE(type, member)¶
Return the size of a structure (type) member in bytes.
Corresponds roughly to
sizeof(((type *)NULL)->member).Added in version 3.6.
Macro definition utilities¶
-
Py_FORCE_EXPANSION(X)¶
This is equivalent to
X, which is useful for token-pasting in macros, as macro expansions in X are forcefully evaluated by the preprocessor.
-
Py_STRINGIFY(x)¶
Convert
xto a C string. For example,Py_STRINGIFY(123)returns"123".Added in version 3.4.
Declaration utilities¶
The following macros can be used in declarations. They are most useful for defining the C API itself, and have limited use for extension authors. Most of them expand to compiler-specific spellings of common extensions to the C language.
-
Py_ALWAYS_INLINE¶
Ask the compiler to always inline a static inline function. The compiler can ignore it and decide to not inline the function.
Corresponds to
always_inlineattribute in GCC and__forceinlinein MSVC.It can be used to inline performance critical static inline functions when building Python in debug mode with function inlining disabled. For example, MSC disables function inlining when building in debug mode.
Marking blindly a static inline function with Py_ALWAYS_INLINE can result in worse performances (due to increased code size for example). The compiler is usually smarter than the developer for the cost/benefit analysis.
If Python is built in debug mode (if the
Py_DEBUGmacro is defined), thePy_ALWAYS_INLINEmacro does nothing.It must be specified before the function return type. Usage:
static inline Py_ALWAYS_INLINE int random(void) { return 4; }
Added in version 3.11.
-
Py_NO_INLINE¶
Disable inlining on a function. For example, it reduces the C stack consumption: useful on LTO+PGO builds which heavily inline code (see bpo-33720).
Corresponds to the
noinlineattribute/specification on GCC and MSVC.äœ¿ãæ¹:
Py_NO_INLINE static int random(void) { return 4; }
Added in version 3.11.
-
Py_DEPRECATED(version)¶
Use this to declare APIs that were deprecated in a specific CPython version. The macro must be placed before the symbol name.
以äžã¯ããã°ã©ã äŸã§ã:
Py_DEPRECATED(3.8) PyAPI_FUNC(int) Py_OldFunction(void);
ããŒãžã§ã³ 3.8 ã§å€æŽ: MSVC ãµããŒãã远å ãããŸããã
-
Py_LOCAL(type)¶
Declare a function returning the specified type using a fast-calling qualifier for functions that are local to the current file. Semantically, this is equivalent to
static type.
-
Py_LOCAL_SYMBOL¶
Macro used to declare a symbol as local to the shared library (hidden). On supported platforms, it ensures the symbol is not exported.
On compatible versions of GCC/Clang, it expands to
__attribute__((visibility("hidden"))).
-
Py_EXPORTED_SYMBOL¶
Macro used to declare a symbol (function or data) as exported. On Windows, this expands to
__declspec(dllexport). On compatible versions of GCC/Clang, it expands to__attribute__((visibility("default"))). This macro is for defining the C API itself; extension modules should not use it.
-
Py_IMPORTED_SYMBOL¶
Macro used to declare a symbol as imported. On Windows, this expands to
__declspec(dllimport). This macro is for defining the C API itself; extension modules should not use it.
-
PyAPI_FUNC(type)¶
Macro used by CPython to declare a function as part of the C API. Its expansion depends on the platform and build configuration. This macro is intended for defining CPython's C API itself; extension modules should not use it for their own symbols.
-
PyAPI_DATA(type)¶
Macro used by CPython to declare a public global variable as part of the C API. Its expansion depends on the platform and build configuration. This macro is intended for defining CPython's C API itself; extension modules should not use it for their own symbols.
Outdated macros¶
The following macros have been used to features that have been standardized in C11.
-
Py_ALIGNED(num)¶
Specify alignment to num bytes on compilers that support it.
Consider using the C11 standard
_Alignasspecifier over this macro.
-
Py_LL(number)¶
-
Py_ULL(number)¶
Use number as a
long longorunsigned long longinteger literal, respectively.Expands to number followed by
LLorLLU, respectively, but will expand to some compiler-specific suffixes on some older compilers.Consider using the C99 standard suffixes
LLandLLUdirectly.
-
Py_MEMCPY(dest, src, n)¶
This is an alias to
memcpy().Soft deprecated since version 3.14: Use
memcpy()directly instead.
-
Py_VA_COPY¶
This is an alias to the C99-standard
va_copyfunction.Historically, this would use a compiler-specific method to copy a
va_list.ããŒãžã§ã³ 3.6 ã§å€æŽ: This is now an alias to
va_copy.Soft deprecated since version 3.14.
ãªããžã§ã¯ããåããã³åç §ã«ãŠã³ã¶
Python/C API 颿°ã¯ã PyObject* åã®äžã€ä»¥äžã®åŒæ°ãšæ»ãå€ãæã¡ãŸãããã®åã¯ãä»»æã® Python ãªããžã§ã¯ãã衚çŸããäžéæ (opaque) ãªããŒã¿åãžã®ãã€ã³ã¿ã§ãã Python èšèªã¯ãå
šãŠã® Python ãªããžã§ã¯ãåãã»ãšãã©ã®ç¶æ³ (äŸãã°ä»£å
¥ãã¹ã³ãŒãèŠå (scope rule)ãåŒæ°æž¡ã) ã§åæ§ã«æ±ããŸããã»ãšãã©å
šãŠã® Python ãªããžã§ã¯ãã¯ããŒã (heap) äžã«çœ®ãããŸã: ãã®ããã PyObject åã®ãªããžã§ã¯ãã¯ãèªåèšæ¶ (automatic) ãšããŠãéçèšæ¶ (static) ãšããŠã宣èšã§ããŸããã PyObject* åã®ãã€ã³ã¿å€æ°ã®ã¿å®£èšã§ããŸããå¯äžã®äŸå€ã¯ãåãªããžã§ã¯ãã§ã; åãªããžã§ã¯ãã¯ã¡ã¢ãªè§£æŸ (deallocate) ããŠã¯ãªããªãã®ã§ãéåžžã¯éçèšæ¶ã® PyTypeObject ãªããžã§ã¯ãã«ããŸãã
å
šãŠã® Python ãªããžã§ã¯ãã«ã¯ (Python æŽæ°åã§ãã) å (type) ãšåç
§ã«ãŠã³ã (reference count) ããããŸãããããªããžã§ã¯ãã®åã¯ããã®ãªããžã§ã¯ããã©ã®çš®é¡ã®ãªããžã§ã¯ãã (äŸãã°æŽæ°ããªã¹ãããŠãŒã¶å®çŸ©é¢æ°ããªã©; ãã®ä»å€æ°ã«ã€ããŠã¯ æšæºåã®éå±€ ã§èª¬æããŠããŸã) ãæ±ºå®ããŸããããç¥ãããŠããåã«ã€ããŠã¯ãåã
ãã¯ããååšããŠããããªããžã§ã¯ãããã®åãã©ãã調ã¹ãããŸã; äŸãã°ã PyList_Check(a) ã¯ã a ã§ç€ºããããªããžã§ã¯ãã Python ãªã¹ãåã®ãšã (ãã€ãã®ãšãã«éã) çå€ãè¿ããŸãã
åç §ã«ãŠã³ãæ³Â¶
The reference count is important because today's computers have a finite (and often severely limited) memory size; it counts how many different places there are that have a strong reference to an object. Such a place could be another object, or a global (or static) C variable, or a local variable in some C function. When the last strong reference to an object is released (i.e. its reference count becomes zero), the object is deallocated. If it contains references to other objects, those references are released. Those other objects may be deallocated in turn, if there are no more references to them, and so on. (There's an obvious problem with objects that reference each other here; for now, the solution is "don't do that.")
Reference counts are always manipulated explicitly. The normal way is
to use the macro Py_INCREF() to take a new reference to an
object (i.e. increment its reference count by one),
and Py_DECREF() to release that reference (i.e. decrement the
reference count by one). The Py_DECREF() macro
is considerably more complex than the incref one, since it must check whether
the reference count becomes zero and then cause the object's deallocator to be
called. The deallocator is a function pointer contained in the object's type
structure. The type-specific deallocator takes care of releasing references
for other objects contained in the object if this is a compound
object type, such as a list, as well as performing any additional finalization
that's needed. There's no chance that the reference count can overflow; at
least as many bits are used to hold the reference count as there are distinct
memory locations in virtual memory (assuming sizeof(Py_ssize_t) >= sizeof(void*)).
Thus, the reference count increment is a simple operation.
It is not necessary to hold a strong reference (i.e. increment the reference count) for every local variable that contains a pointer to an object. In theory, the object's reference count goes up by one when the variable is made to point to it and it goes down by one when the variable goes out of scope. However, these two cancel each other out, so at the end the reference count hasn't changed. The only real reason to use the reference count is to prevent the object from being deallocated as long as our variable is pointing to it. If we know that there is at least one other reference to the object that lives at least as long as our variable, there is no need to take a new strong reference (i.e. increment the reference count) temporarily. An important situation where this arises is in objects that are passed as arguments to C functions in an extension module that are called from Python; the call mechanism guarantees to hold a reference to every argument for the duration of the call.
However, a common pitfall is to extract an object from a list and hold on to it
for a while without taking a new reference. Some other operation might
conceivably remove the object from the list, releasing that reference,
and possibly deallocating it. The real danger is that innocent-looking
operations may invoke arbitrary Python code which could do this; there is a code
path which allows control to flow back to the user from a Py_DECREF(), so
almost any operation is potentially dangerous.
A safe approach is to always use the generic operations (functions whose name
begins with PyObject_, PyNumber_, PySequence_ or PyMapping_).
These operations always create a new strong reference
(i.e. increment the reference count) of the object they return.
This leaves the caller with the responsibility to call Py_DECREF() when
they are done with the result; this soon becomes second nature.
åç §ã«ãŠã³ãã®è©³çŽ°Â¶
The reference count behavior of functions in the Python/C API is best explained
in terms of ownership of references. Ownership pertains to references, never
to objects (objects are not owned: they are always shared). "Owning a
reference" means being responsible for calling Py_DECREF on it when the
reference is no longer needed. Ownership can also be transferred, meaning that
the code that receives ownership of the reference then becomes responsible for
eventually releasing it by calling Py_DECREF() or Py_XDECREF()
when it's no longer needed---or passing on this responsibility (usually to its
caller). When a function passes ownership of a reference on to its caller, the
caller is said to receive a new reference. When no ownership is transferred,
the caller is said to borrow the reference. Nothing needs to be done for a
borrowed reference.
Conversely, when a calling function passes in a reference to an object, there are two possibilities: the function steals a reference to the object, or it does not.
Stealing a reference means that when you pass a reference to a
function, that function assumes that it now owns that reference.
Since the new owner can use Py_DECREF() at its discretion,
you (the caller) must not use that reference after the call.
åç
§ãçã¿åã颿°ã¯ã»ãšãã©ãããŸãã; äŸå€ãšããŠããç¥ãããŠããã®ã¯ã PyList_SetItem() ãš PyTuple_SetItem() ã§ããããã¯ã·ãŒã±ã³ã¹ã«å
¥ããèŠçŽ ã«å¯Ÿããåç
§ãçã¿åããŸã (ããããèŠçŽ ã®å
¥ãå
ã®ã¿ãã«ããªã¹ãã®åç
§ã¯çã¿åããŸãã!)ããããã®é¢æ°ã¯ããªã¹ããã¿ãã«ã®äžã«æ°ãã«äœæããããªããžã§ã¯ããå
¥ããŠããéã®åžžå¥çãªæžãæ¹ãããããããããã«ãåç
§ãçã¿åãããã«èšèšãããŠããŸã; äŸãã°ã (1, 2, "three") ãšããã¿ãã«ãçæããã³ãŒãã¯ä»¥äžã®ããã«ãªããŸã (ãšããããäŸå€åŠçã®ããšã¯å¿ããŠãããŸã; ãã£ãšããæžãæ¹ãåŸã§ç€ºããŸã):
PyObject *t;
t = PyTuple_New(3);
PyTuple_SetItem(t, 0, PyLong_FromLong(1L));
PyTuple_SetItem(t, 1, PyLong_FromLong(2L));
PyTuple_SetItem(t, 2, PyUnicode_FromString("three"));
ããã§ã PyLong_FromLong() ã¯æ°ããåç
§ãè¿ããããã« PyTuple_SetItem() ã«çãŸããŸããåç
§ãçãŸããåŸããã®ãªããžã§ã¯ããå©çšãããå Žåã¯ãåç
§çã颿°ãåŒã³åºãåã«ã Py_INCREF() ãå©çšããŠããäžã€ã®åç
§ãååŸããŠãã ããã
ã¡ãªã¿ã«ã PyTuple_SetItem() ã¯ã¿ãã«ã«å€ãã»ããããããã® å¯äžã® æ¹æ³ã§ã; ã¿ãã«ã¯å€æŽäžèœãªããŒã¿åãªã®ã§ã PySequence_SetItem() ã PyObject_SetItem() ã䜿ããšäžã®æäœã¯æåŠãããŠããŸããŸããèªåã§ã¿ãã«ã®å€ãå
¥ããŠããã€ãããªãã PyTuple_SetItem() ã ããã䜿ããŸããã
åããããªã¹ãã«å€ãå
¥ããŠããã³ãŒã㯠PyList_New() ãš PyList_SetItem() ã§æžããŸãã
ãããå®éã«ã¯ãã¿ãã«ããªã¹ããçæããŠå€ãå
¥ããéã«ã¯ãäžèšã®ãããªæ¹æ³ã¯ã»ãšãã©äœ¿ããŸãããããæ±çšæ§ã®ãã颿°ã Py_BuildValue() ããããã»ãšãã©ã®äž»èŠãªãªããžã§ã¯ãããã©ãŒãããæåå format string ã®æå®ã«åºã¥ã㊠C ã®å€ããçæã§ããŸããäŸãã°ãäžã®äºçš®é¡ã®ã³ãŒããããã¯ã¯ã以äžã®ããã«çœ®ãæããããŸã (ãšã©ãŒãã§ãã¯ã«ãé
æ
®ããŠããŸã):
PyObject *tuple, *list;
tuple = Py_BuildValue("(iis)", 1, 2, "three");
list = Py_BuildValue("[iis]", 1, 2, "three");
It is much more common to use PyObject_SetItem() and friends with items
whose references you are only borrowing, like arguments that were passed in to
the function you are writing. In that case, their behaviour regarding references
is much saner, since you don't have to take a new reference just so you
can give that reference away ("have it be stolen"). For example, this function
sets all items of a list (actually, any mutable sequence) to a given item:
int
set_all(PyObject *target, PyObject *item)
{
Py_ssize_t i, n;
n = PyObject_Length(target);
if (n < 0)
return -1;
for (i = 0; i < n; i++) {
PyObject *index = PyLong_FromSsize_t(i);
if (!index)
return -1;
if (PyObject_SetItem(target, index, item) < 0) {
Py_DECREF(index);
return -1;
}
Py_DECREF(index);
}
return 0;
}
颿°ã®æ»ãå€ã®å Žåã«ã¯ãç¶æ³ã¯å°ãç°ãªããŸããã»ãšãã©ã®é¢æ°ã«ã€ããŠã¯ãåç
§ãæž¡ããŠããã®åç
§ã«å¯Ÿããæææš©ãå€ããããšããªãäžæ¹ã§ããããªããžã§ã¯ãã«å¯Ÿããåç
§ãè¿ããããªå€ãã®é¢æ°ã¯ãåç
§ã«å¯Ÿããæææš©ãåŒã³åºãåŽã«äžããŸããçç±ã¯ç°¡åã§ã: å€ãã®å Žåã颿°ãè¿ããªããžã§ã¯ãã¯ãã®å Žã§ (on the fly) çæããããããåŒã³åºãåŽãåŸãåç
§ã¯çæããããªããžã§ã¯ãã«å¯Ÿããå¯äžã®åç
§ã«ãªãããã§ããåŸã£ãŠã PyObject_GetItem() ã PySequence_GetItem() ã®ããã«ããªããžã§ã¯ãã«å¯Ÿããåç
§ãè¿ãæ±çšã®é¢æ°ã¯ãåžžã«æ°ããªåç
§ãè¿ããŸã (åŒã³åºãåŽãåç
§ã®ææè
ã«ãªããŸã)ã
éèŠãªã®ã¯ã颿°ãè¿ãåç
§ã®æææš©ãæãŠããã©ããã¯ãã©ã®é¢æ°ãåŒã³åºããã ãã«ããããšçè§£ããããšã§ã --- 颿°åŒã³åºãæã® ã食ã (颿°ã«åŒæ°ãšããŠæž¡ãããªããžã§ã¯ãã®å) 㯠ãã®åé¡ã«ã¯é¢ä¿ãããŸãã! åŸã£ãŠã PyList_GetItem() ã䜿ã£ãŠãªã¹ãå
ã®èŠçŽ ãåŸãå Žåã«ã¯ãåç
§ã®ææè
ã«ã¯ãªããŸãã --- ããåãèŠçŽ ãåããªã¹ããã PySequence_GetItem() (å³ããããã®é¢æ°ã¯å
šãåãåŒæ°ããšããŸã) ã䜿ã£ãŠåãåºããšãè¿ããããªããžã§ã¯ãã«å¯Ÿããåç
§ãåŸãŸãã
以äžã¯ãæŽæ°ãããªããªã¹ãã«å¯ŸããŠåèŠçŽ ã®åèšãèšç®ãã颿°ãã©ã®ããã«ããŠæžãããã瀺ããäŸã§ã; äžã€ã¯ PyList_GetItem() ã䜿ã£ãŠããŠãããäžã€ã¯ PySequence_GetItem() ã䜿ã£ãŠããŸãã
long
sum_list(PyObject *list)
{
Py_ssize_t i, n;
long total = 0, value;
PyObject *item;
n = PyList_Size(list);
if (n < 0)
return -1; /* Not a list */
for (i = 0; i < n; i++) {
item = PyList_GetItem(list, i); /* Can't fail */
if (!PyLong_Check(item)) continue; /* Skip non-integers */
value = PyLong_AsLong(item);
if (value == -1 && PyErr_Occurred())
/* Integer too big to fit in a C long, bail out */
return -1;
total += value;
}
return total;
}
long
sum_sequence(PyObject *sequence)
{
Py_ssize_t i, n;
long total = 0, value;
PyObject *item;
n = PySequence_Length(sequence);
if (n < 0)
return -1; /* Has no length */
for (i = 0; i < n; i++) {
item = PySequence_GetItem(sequence, i);
if (item == NULL)
return -1; /* Not a sequence, or other failure */
if (PyLong_Check(item)) {
value = PyLong_AsLong(item);
Py_DECREF(item);
if (value == -1 && PyErr_Occurred())
/* Integer too big to fit in a C long, bail out */
return -1;
total += value;
}
else {
Py_DECREF(item); /* Discard reference ownership */
}
}
return total;
}
å¶
ä»ã«ã Python/C API ã«ãããŠéèŠãªåœ¹å²ãæã€ããŒã¿åãããã€ããããŸã; ã»ãšãã©ã¯ int, long, double, ããã³ char* ãšãã£ããåãªã C ã®ããŒã¿åã§ãããŸããã¢ãžã¥ãŒã«ã§å ¬éããŠãã颿°ãåæããéã«çšããããéçãªããŒãã«ããæ°ãããªããžã§ã¯ãåã«ãããããŒã¿å±æ§ãèšè¿°ããããè€çŽ æ°ã®å€ãèšè¿°ãããããããã«æ§é äœãããã€ã䜿ã£ãŠããŸãããããã®åã«ã€ããŠã¯ããã®åã䜿ã颿°ãšãšãã«èª¬æããŠãããŸãã
-
type Py_ssize_t¶
- 次ã«å±ããŸã: Stable ABI.
A signed integral type such that
sizeof(Py_ssize_t) == sizeof(size_t). C99 doesn't define such a thing directly (size_t is an unsigned integral type). See PEP 353 for details.PY_SSIZE_T_MAXis the largest positive value of typePy_ssize_t.
äŸå€Â¶
Python ããã°ã©ãã¯ãç¹å®ã®ãšã©ãŒåŠçãå¿ èŠãªãšãã ãããäŸå€ãæ±ãå¿ èŠã¯ãããŸãã; åŠçããªãã£ãäŸå€ã¯ãåŠçã®åŒã³åºãåŽããã®ãŸãåŒã³åºãåŽããšãã£ãå ·åã«ããããã¬ãã«ã®ã€ã³ã¿ããªã¿å±€ãŸã§èªåçã«äŒæããŸããã€ã³ã¿ããªã¿å±€ã¯ãã¹ã¿ãã¯ãã¬ãŒã¹ããã¯ãšåãããŠäŸå€ããŠãŒã¶ã«å ±åããŸãã
ãšãããã C ããã°ã©ãã®å Žåããšã©ãŒãã§ãã¯ã¯åžžã«æç€ºçã«è¡ããã°ãªããŸããã Python/C API ã®å
šãŠã®é¢æ°ã¯ã颿°ã®ããã¥ã¡ã³ãã§æç¢ºã«èª¬æããªãéãäŸå€ãçºè¡ããå¯èœæ§ããããŸããäžè¬çãªè©±ãšããŠããã颿°ãäœããã®ãšã©ãŒã«ééãããšã颿°ã¯äŸå€ãèšå®ããŠã颿°å
ã«ãããåç
§ã®æææš©ãå
šãŠæŸæ£ãããšã©ãŒå€ (error indicator) ãè¿ããŸããããã¥ã¡ã³ãã«æžãããŠãªãå Žåããã®ãšã©ãŒå€ã¯é¢æ°ã®æ»ãå€ã®åã«ãã£ãŠã NULL ã -1 ã®ã©ã¡ããã«ãªããŸããããã€ãã®é¢æ°ã§ã¯ããŒã«åã§ç/åœãè¿ããåœã¯ãšã©ãŒã瀺ããŸããããããŠå°æ°ã®é¢æ°ã§ã¯æç¢ºãªãšã©ãŒææšãè¿ããªãã£ããããããŸããªæ»ãå€ãè¿ãããããã®ã§ã PyErr_Occurred() ã§æç€ºçã«ãšã©ãŒãã¹ããè¡ãå¿
èŠããããŸãããããã®äŸå€ã¯åžžã«æç€ºçã«ããã¥ã¡ã³ãåãããŸãã
äŸå€æã®ç¶æ
æ
å ± (exception state)ã¯ãã¹ã¬ããåäœã«çšæãããèšæ¶é å (per-thread storage) å
ã§ç®¡çãããŸã (ãã®èšæ¶é åã¯ãã¹ã¬ããã䜿ããªãã¢ããªã±ãŒã·ã§ã³ã§ã¯ã°ããŒãã«ãªèšæ¶é åãšåãã§ã)ãäžã€ã®ã¹ã¬ããã¯äºã€ã®ç¶æ
ã®ã©ã¡ãã: äŸå€ãçºçãããããŸã çºçããŠããªãããããšããŸãã颿° PyErr_Occurred() ã䜿ããšããã®ç¶æ
ã調ã¹ãããŸã: ãã®é¢æ°ã¯äŸå€ãçºçããéã«ã¯ãã®äŸå€åãªããžã§ã¯ãã«å¯Ÿããåçšåç
§ (borrowed reference) ãè¿ããããã§ãªããšãã«ã¯ NULL ãè¿ããŸããäŸå€ç¶æ
ãèšå®ãã颿°ã¯æ°å€ããããŸã: PyErr_SetString() ã¯ãã£ãšãããç¥ãããŠãã (ãããã£ãšãæ±çšæ§ã®ãªã) äŸå€ãèšå®ããããã®é¢æ°ã§ã PyErr_Clear() ã¯äŸå€ç¶æ
æ
å ±ãæ¶ãå»ã颿°ã§ãã
å®å
šãªäŸå€ç¶æ
æ
å ±ã¯ã3 ã€ã®ãªããžã§ã¯ã: äŸå€ã®åãäŸå€ã®å€ããããŠãã¬ãŒã¹ããã¯ããããªããŸã (ã©ã®ãªããžã§ã¯ãã NULL ãåãåŸãŸã)ããããã®æ
å ±ã¯ã Python ã® sys.exc_info() ã®çµæãšåãæå³ãæã¡ãŸã; ãšã¯ããã C ãš Python ã®äŸå€ç¶æ
æ
å ±ã¯å
šãåãã§ã¯ãããŸãã: Python ã«ãããäŸå€ãªããžã§ã¯ãã¯ãPython ã® try ... except æã§æè¿åŠçãããªããžã§ã¯ãã衚ãäžæ¹ã C ã¬ãã«ã®äŸå€ç¶æ
æ
å ±ãåç¶ããã®ã¯ãæž¡ãããäŸå€æ
å ±ã sys.exc_info() ãã®ä»ã«è»¢éããããåãèšãã Python ã®ãã€ãã³ãŒãã€ã³ã¿ããªã¿ã®ã¡ã€ã³ã«ãŒãã«å°éãããŸã§ãäŸå€ã颿°ã®éã§åãæž¡ããããŠããéã ãã§ãã
Python 1.5 ããã¯ãPython ã§æžãããã³ãŒãããäŸå€ç¶æ
æ
å ±ã«ã¢ã¯ã»ã¹ããæ¹æ³ãšããŠãæšå¥šãããŠããŠã¹ã¬ããã»ãŒããªæ¹æ³ã¯ sys.exc_info() ã«ãªã£ãŠããã®ã§æ³šæããŠãã ããããã®é¢æ°ã¯ Python ã³ãŒãã®å®è¡ãããŠããã¹ã¬ããã«ãããäŸå€ç¶æ
æ
å ±ãè¿ããŸãããŸãããããã®äŸå€ç¶æ
æ
å ±ã«å¯Ÿããã¢ã¯ã»ã¹ææ®µã¯ãäž¡æ¹ãšãæå³ã¥ã (semantics) ã倿Žããããã颿°ãäŸå€ãææãããšããã®é¢æ°ãå®è¡ããŠããã¹ã¬ããã®äŸå€ç¶æ
æ
å ±ãä¿åããŠãåŒã³åºãåŽã®äŸå€ç¶æ
æ
å ±ãç¶æããããã«ãªããŸããããã®å€æŽã«ãã£ãŠãç¡å®³ããã«èŠãã颿°ãçŸåšæ±ã£ãŠããäŸå€ãäžæžãããããšã§åŒãèµ·ãããããäŸå€åŠçã³ãŒãã§ãããããŠãããã°ãææ¢ããŠããŸã; ãŸãããã¬ãŒã¹ããã¯å
ã®ã¹ã¿ãã¯ãã¬ãŒã ã§åç
§ãããŠãããªããžã§ã¯ãããã°ãã°äžå¿
èŠã«å¯¿åœãæ°žãããŠããã®ããªãããŠããŸãã
äžè¬çãªåçãšããŠããã颿°ãå¥ã®é¢æ°ãåŒã³åºããŠäœããã®äœæ¥ãããããšããåŒã³åºãå ã®é¢æ°ãäŸå€ãéåºããŠããªãã調ã¹ãªããŠã¯ãªãããããéåºããŠããã°ããã®äŸå€ç¶æ æ å ±ã¯åŒã³åºãåŽã«æž¡ãããªããã°ãªããŸãããåŒã³åºãå ã®é¢æ°ã¯ãªããžã§ã¯ãåç §ã®æææš©ããã¹ãŠæŸæ£ãããšã©ãŒææšãè¿ããªããŠã¯ãªããŸããããäœèšã«äŸå€ãèšå®ããå¿ èŠã¯ ãããŸãã --- ãããªããšãããã°ããã£ãä»éåºãããã°ããã®äŸå€ãäžæžãããŠããŸãããšã©ãŒã®åå ãã®ãã®ã«é¢ããéèŠãªæ å ±ã倱ãããšã«ãªããŸãã
A simple example of detecting exceptions and passing them on is shown in the
sum_sequence() example above. It so happens that this example doesn't
need to clean up any owned references when it detects an error. The following
example function shows some error cleanup. First, to remind you why you like
Python, we show the equivalent Python code:
def incr_item(dict, key):
try:
item = dict[key]
except KeyError:
item = 0
dict[key] = item + 1
以äžã¯å¯Ÿå¿ããã³ãŒãã C ã§å®ç§ã«æžãããã®ã§ã:
int
incr_item(PyObject *dict, PyObject *key)
{
/* Objects all initialized to NULL for Py_XDECREF */
PyObject *item = NULL, *const_one = NULL, *incremented_item = NULL;
int rv = -1; /* Return value initialized to -1 (failure) */
item = PyObject_GetItem(dict, key);
if (item == NULL) {
/* Handle KeyError only: */
if (!PyErr_ExceptionMatches(PyExc_KeyError))
goto error;
/* Clear the error and use zero: */
PyErr_Clear();
item = PyLong_FromLong(0L);
if (item == NULL)
goto error;
}
const_one = PyLong_FromLong(1L);
if (const_one == NULL)
goto error;
incremented_item = PyNumber_Add(item, const_one);
if (incremented_item == NULL)
goto error;
if (PyObject_SetItem(dict, key, incremented_item) < 0)
goto error;
rv = 0; /* Success */
/* Continue with cleanup code */
error:
/* Cleanup code, shared by success and failure path */
/* Use Py_XDECREF() to ignore NULL references */
Py_XDECREF(item);
Py_XDECREF(const_one);
Py_XDECREF(incremented_item);
return rv; /* -1 for error, 0 for success */
}
ãªããšãã®äŸã¯ C ã§ goto æã䜿ããå§ãã®æ¹æ³ãŸã§ç€ºããŠããŸãã! ãã®äŸã§ã¯ãç¹å®ã®äŸå€ãåŠçããããã« PyErr_ExceptionMatches() ããã³ PyErr_Clear() ãã©ã䜿ããã瀺ããŠããŸãããŸããæææš©ãæã£ãŠããåç
§ã§ãå€ã NULL ã«ãªããããããªããã®ãæšãŠãããã« Py_XDECREF() ãã©ã䜿ããã瀺ããŠããŸã (颿°åã« 'X' ãä»ããŠããããšã«æ³šæããŠãã ãã; Py_DECREF() 㯠NULL åç
§ã«åºããããšã¯ã©ãã·ã¥ããŸã)ãæ£ããåäœãããããã«ã¯ãæææš©ãæã€åç
§ãä¿æããããã®å€æ°ã NULL ã§åæåããããšãéèŠã§ã; åæ§ã«ããããããæ»ãå€ãå®çŸ©ããéã«ã¯å€ã -1 (倱æ) ã§åæåããŠãããŠãæåŸã®é¢æ°åŒã³åºããŸã§ããŸããã£ãå Žåã«ã®ã¿ 0 (æå) ã«èšå®ããŸãã
Python ã®åã蟌ã¿Â¶
Python ã€ã³ã¿ããªã¿ã®åã蟌ã¿ãè¡ã人 (ããã°æ¡åŒµã¢ãžã¥ãŒã«ã®æžãæã®å¯Ÿæ¥µ) ãæ°ã«ãããªããã°ãªããªãéèŠãªã¿ã¹ã¯ã¯ãPython ã€ã³ã¿ããªã¿ã®åæååŠç (initialization)ããããŠããããã¯çµäºåŠç (finalization) ã§ããã€ã³ã¿ããªã¿ã®ã»ãšãã©ã®æ©èœã¯ãã€ã³ã¿ããªã¿ã®èµ·ååŸãã䜿ããŸããã
åºæ¬çãªåæååŠçãè¡ã颿°ã¯ Py_Initialize() ã§ãããã®é¢æ°ã¯ããŒãæžã¿ã®ã¢ãžã¥ãŒã«ãããªãããŒãã«ãäœæããåå°ãšãªãã¢ãžã¥ãŒã« builtins, __main__, ããã³ sys ãäœæããŸãããŸããã¢ãžã¥ãŒã«æ€çŽ¢ãã¹ (sys.path) ã®åæåãè¡ããŸãã
Py_Initialize() does not set the "script argument list" (sys.argv).
If this variable is needed by Python code that will be executed later, setting
PyConfig.argv and PyConfig.parse_argv must be set: see
Python Initialization Configuration.
ã»ãšãã©ã®ã·ã¹ãã ã§ã¯ (ç¹ã« Unix ãš Windows ã¯ã詳现ããããã«ç°ãªãã¯ããŸãã)ã Py_Initialize() ã¯æšæºã® Python ã€ã³ã¿ããªã¿å®è¡åœ¢åŒã®å Žæã«å¯Ÿããæšå®çµæã«åºã¥ããŠã Python ã®ã©ã€ãã©ãªã Python ã€ã³ã¿ããªã¿å®è¡åœ¢åŒããã®çžå¯Ÿãã¹ã§èŠã€ãããšããä»®å®ã®äžã«ã¢ãžã¥ãŒã«æ€çŽ¢ãã¹ãèšç®ããŸãããšããããã®æ€çŽ¢ã§ã¯ãã·ã§ã«ã³ãã³ãæ€çŽ¢ãã¹ (ç°å¢å€æ° PATH) äžã«èŠã€ãã£ã python ãšããååã®å®è¡ãã¡ã€ã«ã®çœ®ãããŠãããã£ã¬ã¯ããªã®èŠªãã£ã¬ã¯ããªããã®çžå¯Ÿã§ã lib/pythonX.Y ãšããååã®ãã£ã¬ã¯ããªãæ¢ããŸãã
äŸãã°ã Python å®è¡åœ¢åŒã /usr/local/bin/python ã§èŠã€ãã£ããšãããšãã©ã€ãã©ãªã /usr/local/lib/pythonX.Y ã«ãããã®ãšä»®å®ããŸãã (å®éã«ã¯ããã®ãã¹ã¯ "ãã©ãŒã«ãã㯠(fallback)" ã®ã©ã€ãã©ãªäœçœ®ã§ãããã python ã PATH äžã«ç¡ãå Žåã«äœ¿ãããŸãã) ãŠãŒã¶ã¯ PYTHONHOME ãèšå®ããããšã§ãã®åäœããªãŒããŒã©ã€ããããã PYTHONPATH ãèšå®ããŠè¿œå ã®ãã£ã¬ã¯ããªãæšæºã¢ãžã¥ãŒã«æ€çŽ¢ãã¹ã®åã«æ¿å
¥ãããã§ããŸãã
The embedding application can steer the search by setting
PyConfig.program_name before calling
Py_InitializeFromConfig(). Note that
PYTHONHOME still overrides this and PYTHONPATH is still
inserted in front of the standard path. An application that requires total
control has to provide its own implementation of Py_GetPath(),
Py_GetPrefix(), Py_GetExecPrefix(), and
Py_GetProgramFullPath() (all defined in Modules/getpath.c).
ããŸã«ã Python ãåæååã®ç¶æ
ã«ãã©ãããããšããããŸããäŸãã°ãããã¢ããªã±ãŒã·ã§ã³ã§ã¯å®è¡ãæåãããããªãã (start over) ããã (Py_Initialize() ãããäžåºŠåŒã³åºããã) ããã«ããããããããŸããããããã¯ãã¢ããªã±ãŒã·ã§ã³ã Python ãäžæŠäœ¿ãçµããŠãPython ã確ä¿ããã¡ã¢ãªãè§£æŸãããããããããŸããã Py_FinalizeEx() ã䜿ããšããããåŠçãå®çŸã§ããŸãããŸãã颿° Py_IsInitialized() ã¯ãPython ãçŸåšåæåæžã¿ã®ç¶æ
ã«ããå Žåã«çãè¿ããŸãããããã®é¢æ°ã«ã€ããŠã®ãããªãæ
å ±ã¯ãåŸã®ç« ã§èª¬æããŸãã Py_FinalizeEx() ãPythonã€ã³ã¿ããªã¿ã«ç¢ºä¿ãããå
šãŠã®ã¡ã¢ãªã è§£æŸããããã§ã¯ãªã ããšã«æ³šæããŠãã ãããäŸãã°ãæ¡åŒµã¢ãžã¥ãŒã«ã«ãã£ãŠç¢ºä¿ãããã¡ã¢ãªã¯ãçŸåšã®ãšããè§£æŸããäºãã§ããŸããã
ãããã°çãã«ã (Debugging Builds)¶
ã€ã³ã¿ããªã¿ãšæ¡åŒµã¢ãžã¥ãŒã«ã«å¯ŸããŠã®è¿œå ãã§ãã¯ãããããã®ããã€ãã®ãã¯ããæå¹ã«ããŠPythonããã«ãããããšãã§ããŸãããããã®ãã§ãã¯ã¯ãå®è¡æã«å€§ããªãªãŒããŒããããçããåŸåããããŸãããªã®ã§ãããã©ã«ãã§ã¯æå¹ã«ãããŠããŸããã
Pythonãããã°çãã«ãã®å
šãŠã®çš®é¡ã®ãªã¹ãããPythonãœãŒã¹é
åž(source distribution)ã®äžã® Misc/SpecialBuilds.txt ã«ãããŸããåç
§ã«ãŠã³ãã®ãã¬ãŒã¹ãã¡ã¢ãªã¢ãã±ãŒã¿ã®ãããã°ãã€ã³ã¿ããªã¿ã®ã¡ã€ã³ã«ãŒãã®äœã¬ãã«ãããã¡ã€ãªã³ã°ãå©çšå¯èœã§ãããã䜿ããããã«ãã«ã€ããŠã®ã¿ããã®ç¯ã®æ®ãã®éšåã§èª¬æããŸãã
-
Py_DEBUG¶
Compiling the interpreter with the Py_DEBUG macro defined produces
what is generally meant by a debug build of Python.
Py_DEBUG is enabled in the Unix build by adding
--with-pydebug to the ./configure command.
It is also implied by the presence of the
not-Python-specific _DEBUG macro. When Py_DEBUG is enabled
in the Unix build, compiler optimization is disabled.
In addition to the reference count debugging described below, extra checks are performed, see Python Debug Build.
Defining Py_TRACE_REFS enables reference tracing
(see the configure --with-trace-refs option).
When defined, a circular doubly linked list of active objects is maintained by adding two extra
fields to every PyObject. Total allocations are tracked as well. Upon
exit, all existing references are printed. (In interactive mode this happens
after every statement run by the interpreter.)
ãã詳ããæ
å ±ã«ã€ããŠã¯ãPythonã®ãœãŒã¹é
åž(source distribution)ã®äžã® Misc/SpecialBuilds.txt ãåç
§ããŠãã ããã
ããããã®ãµãŒãããŒãã£ããŒã«Â¶
The following third party tools offer both simpler and more sophisticated approaches to creating C, C++ and Rust extensions for Python:
Using tools such as these can help avoid writing code that is tightly bound to a particular version of CPython, avoid reference counting errors, and focus more on your own code than on using the CPython API. In general, new versions of Python can be supported by updating the tool, and your code will often use newer and more efficient APIs automatically. Some tools also support compiling for other implementations of Python from a single set of sources.
These projects are not supported by the same people who maintain Python, and issues need to be raised with the projects directly. Remember to check that the project is still maintained and supported, as the list above may become outdated.
åè
- Python Packaging User Guide: Binary Extensions
Python Packaging User Guideã¯ãã€ããªæ¡åŒµã®äœæãç°¡åã«ãªã䟿å©ãªããŒã«ãã«ããŒããŠããã ãã§ã¯ãªãããŸãå§ãã«ãªãæ¡åŒµã¢ãžã¥ãŒã«ãäœãããšãæãŸãããã®æ§ã ãªçç±ã«ã€ããŠè°è«ããŠããŸãã