Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e77e5ef

Browse files
committed
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.
1 parent 292da58 commit e77e5ef

1 file changed

Lines changed: 9 additions & 12 deletions

File tree

Doc/ext/embedding.tex

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,24 +178,21 @@ \section{Pure Embedding
178178
\cfunction{PyString_FromString()} data conversion routine.
179179

180180
\begin{verbatim}
181-
pDict = PyModule_GetDict(pModule);
182-
/* pDict is a borrowed reference */
183-
184-
pFunc = PyDict_GetItemString(pDict, argv[2]);
185-
/* pFun is a borrowed reference */
181+
pFunc = PyObject_GetAttrString(pModule, argv[2]);
182+
/* pFunc is a new reference */
186183
187184
if (pFunc && PyCallable_Check(pFunc)) {
188185
...
189186
}
187+
Py_XDECREF(pFunc);
190188
\end{verbatim}
191189

192-
Once the script is loaded, its dictionary is retrieved with
193-
\cfunction{PyModule_GetDict()}. The dictionary is then searched using
194-
the normal dictionary access routines for the function name. If the
195-
name exists, and the object retunred is callable, you can safely
196-
assume that it is a function. The program then proceeds by
197-
constructing a tuple of arguments as normal. The call to the python
198-
function is then made with:
190+
Once the script is loaded, the name we're looking for is retrieved
191+
using \cfunction{PyObject_GetAttrString()}. If the name exists, and
192+
the object retunred is callable, you can safely assume that it is a
193+
function. The program then proceeds by constructing a tuple of
194+
arguments as normal. The call to the Python function is then made
195+
with:
199196

200197
\begin{verbatim}
201198
pValue = PyObject_CallObject(pFunc, pArgs);

0 commit comments

Comments
 (0)