40 lines
1.4 KiB
Plaintext
40 lines
1.4 KiB
Plaintext
From: gmcm at hypernet.com (Gordon McMillan)
|
|
Date: Wed, 7 Apr 1999 03:12:31 GMT
|
|
Subject: Embedding python question: PyRun_String only returns None no
|
|
In-Reply-To: <dkuhlmanF9sLro.IGx@netcom.com>
|
|
References: <dkuhlmanF9sLro.IGx@netcom.com>
|
|
Message-ID: <1288667549-75228395@hypernet.com>
|
|
Content-Length: 1034
|
|
X-UID: 83
|
|
|
|
Dave Kuhlman replies to a query about PyRun_SimpleString...
|
|
|
|
> When you embed Python in an application, the application often
|
|
> exposes functions that are callable from Python scripts. You could
|
|
> provide a function named setReturnValue(value), which when called,
|
|
> passed a Python object (the value). The script calls this function,
|
|
> and then, when it exits, the embedding application (the caller of
|
|
> PyRun_String or PyRun_SimpleString) uses the Python value saved by
|
|
> this function.
|
|
|
|
With all the error checking and some app specific logic removed,
|
|
here's some code that loads a module and calls a specific function in
|
|
that module:
|
|
|
|
module = PyImport_ImportModule("mymodule");
|
|
moduledict = PyModule_GetDict(module);
|
|
func = PyDict_GetItemString(moduledict, "CheckMenu");
|
|
args = Py_BuildValue("(ss)", "spam", "eggs");
|
|
retval = PyEval_CallObjectWithKeywords(func, args, (PyObject
|
|
*)NULL)
|
|
Py_XINCREF(retval);
|
|
rc = PyArg_Parse(retval, "s", &str);
|
|
Py_XDECREF(retval);
|
|
Py_XDECREF(module);
|
|
|
|
- Gordon
|
|
|
|
|
|
|
|
|