The Python/C API, 发布 3.9.1rc1
(续上页)
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 */
}
This example represents an endorsed use of the goto statement in C! It illustrates the use of
PyErr_ExceptionMatches() and PyErr_Clear() to handle specific exceptions, and the use of
Py_XDECREF() to dispose of owned references that may be NULL (note the 'X' in the name; Py_DECREF()
would crash when confronted with a NULL reference). It is important that the variables used to hold owned references
are initialized to NULL for this to work; likewise, the proposed return value is initialized to -1 (failure) and only set to
success after the final call made is successful.
1.6 嵌入 Python
The one important task that only embedders (as opposed to extension writers) of the Python interpreter have to worry
about is the initialization, and possibly the finalization, of the Python interpreter. Most functionality of the interpreter
can only be used after the interpreter has been initialized.
The basic initialization function is Py_Initialize(). This initializes the table of loaded modules, and creates the
fundamental modules builtins, __main__, and sys. It also initializes the module search path (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, it must be set explicitly with a call to PySys_SetArgvEx(argc, argv, updatepath)
after the call to Py_Initialize().
On most systems (in particular, on Unix and Windows, although the details are slightly different), Py_Initialize()
calculates the module search path based upon its best guess for the location of the standard Python interpreter executable,
assuming that the Python library is found in a fixed location relative to the Python interpreter executable. In particular, it
looks for a directory named lib/pythonX.Y relative to the parent directory where the executable named python is
found on the shell command search path (the environment variable PATH).
For instance, if the Python executable is found in /usr/local/bin/python, it will assume that the libraries are in /
usr/local/lib/pythonX.Y. (In fact, this particular path is also the ”fallback” location, used when no executable
file named python is found along PATH.) The user can override this behavior by setting the environment variable
PYTHONHOME, or insert additional directories in front of the standard path by setting PYTHONPATH.
The embedding application can steer the search by calling Py_SetProgramName(file) before calling
Py_Initialize(). 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).
Sometimes, it is desirable to ”uninitialize” Python. For instance, the application may want to start over (make another call
1.6. 嵌入 Python 11