diff options
author | Fred Drake <fdrake@acm.org> | 2002-04-12 19:04:17 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2002-04-12 19:04:17 (GMT) |
commit | e77e5ef2af7f0cb667c32f59bc7cb1c88aac8697 (patch) | |
tree | 9f0c1d3273a2da63ad4ef1f728e2c2e0c59ddcc4 /Doc | |
parent | 292da58a5c2cc34c6e15907017079f93c77deb5f (diff) | |
download | cpython-e77e5ef2af7f0cb667c32f59bc7cb1c88aac8697.zip cpython-e77e5ef2af7f0cb667c32f59bc7cb1c88aac8697.tar.gz cpython-e77e5ef2af7f0cb667c32f59bc7cb1c88aac8697.tar.bz2 |
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.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/ext/embedding.tex | 21 |
1 files changed, 9 insertions, 12 deletions
diff --git a/Doc/ext/embedding.tex b/Doc/ext/embedding.tex index a44b89c..77fd451 100644 --- a/Doc/ext/embedding.tex +++ b/Doc/ext/embedding.tex @@ -178,24 +178,21 @@ as its argument, which is constructed using the \cfunction{PyString_FromString()} data conversion routine. \begin{verbatim} - pDict = PyModule_GetDict(pModule); - /* pDict is a borrowed reference */ - - pFunc = PyDict_GetItemString(pDict, argv[2]); - /* pFun is a borrowed reference */ + pFunc = PyObject_GetAttrString(pModule, argv[2]); + /* pFunc is a new reference */ if (pFunc && PyCallable_Check(pFunc)) { ... } + Py_XDECREF(pFunc); \end{verbatim} -Once the script is loaded, its dictionary is retrieved with -\cfunction{PyModule_GetDict()}. The dictionary is then searched using -the normal dictionary access routines for the function name. If the -name exists, and the object retunred is callable, you can safely -assume that it is a function. The program then proceeds by -constructing a tuple of arguments as normal. The call to the python -function is then made with: +Once the script is loaded, the name we're looking for is retrieved +using \cfunction{PyObject_GetAttrString()}. If the name exists, and +the object retunred is callable, you can safely assume that it is a +function. The program then proceeds by constructing a tuple of +arguments as normal. The call to the Python function is then made +with: \begin{verbatim} pValue = PyObject_CallObject(pFunc, pArgs); |