mirror of
https://github.com/python/cpython.git
synced 2025-11-12 07:02:33 +00:00
Change example of retrieving & calling a Python function to not use
PyModule_GetDict(), which is also more flexible: it does not assume that the "module" is a real module.
This commit is contained in:
parent
292da58a5c
commit
e77e5ef2af
1 changed files with 9 additions and 12 deletions
|
|
@ -178,24 +178,21 @@ as its argument, which is constructed using the
|
||||||
\cfunction{PyString_FromString()} data conversion routine.
|
\cfunction{PyString_FromString()} data conversion routine.
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
pDict = PyModule_GetDict(pModule);
|
pFunc = PyObject_GetAttrString(pModule, argv[2]);
|
||||||
/* pDict is a borrowed reference */
|
/* pFunc is a new reference */
|
||||||
|
|
||||||
pFunc = PyDict_GetItemString(pDict, argv[2]);
|
|
||||||
/* pFun is a borrowed reference */
|
|
||||||
|
|
||||||
if (pFunc && PyCallable_Check(pFunc)) {
|
if (pFunc && PyCallable_Check(pFunc)) {
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
|
Py_XDECREF(pFunc);
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
Once the script is loaded, its dictionary is retrieved with
|
Once the script is loaded, the name we're looking for is retrieved
|
||||||
\cfunction{PyModule_GetDict()}. The dictionary is then searched using
|
using \cfunction{PyObject_GetAttrString()}. If the name exists, and
|
||||||
the normal dictionary access routines for the function name. If the
|
the object retunred is callable, you can safely assume that it is a
|
||||||
name exists, and the object retunred is callable, you can safely
|
function. The program then proceeds by constructing a tuple of
|
||||||
assume that it is a function. The program then proceeds by
|
arguments as normal. The call to the Python function is then made
|
||||||
constructing a tuple of arguments as normal. The call to the python
|
with:
|
||||||
function is then made with:
|
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
pValue = PyObject_CallObject(pFunc, pArgs);
|
pValue = PyObject_CallObject(pFunc, pArgs);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue