summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Andres Klode <jak@debian.org>2009-07-15 20:39:18 +0200
committerJulian Andres Klode <jak@debian.org>2009-07-15 20:39:18 +0200
commit6975079da04ae1fad51dca63b40a1aae15f45aa0 (patch)
tree58e0b55462ea926b603f074b5b1ba768c4d8dbd9
parent03d50af0b317fb6501be1cd1f5be1a1ff3526703 (diff)
downloadpython-apt-6975079da04ae1fad51dca63b40a1aae15f45aa0.tar.gz
doc/client-example.cc: Make it a module AND an application.
You can now compile this as a module or as an application. The benefit is that the explained process is more like the one required for Python 3.
-rw-r--r--doc/client-example.cc38
1 files changed, 30 insertions, 8 deletions
diff --git a/doc/client-example.cc b/doc/client-example.cc
index 7fa6672f..7f01b637 100644
--- a/doc/client-example.cc
+++ b/doc/client-example.cc
@@ -18,13 +18,14 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
+
#include <python-apt/python-apt.h>
+#include <apt-pkg/hashes.h>
-int main(int argc, char *argv[]) {
- Py_Initialize();
+// The module initialization.
+extern "C" void initclient() {
if (import_apt_pkg() < 0)
- return 1;
-
+ return;
// Initialize a module.
PyObject *Module = Py_InitModule("client", NULL);
@@ -36,11 +37,32 @@ int main(int argc, char *argv[]) {
// Another example: Add the HashString type to the module.
Py_INCREF(&PyHashString_Type);
PyModule_AddObject(Module, "HashString", (PyObject*)(&PyHashString_Type));
+}
- // Run IPython, adding the client module to the namespace.
+int main(int argc, char *argv[]) {
+ // Return value.
+ int ret = 0;
+ // Initialize python
+ Py_Initialize();
+ // Make the client module importable
+ PyImport_AppendInittab("client", &initclient);
+ // Set the commandline arguments.
PySys_SetArgv(argc, argv);
- PyRun_SimpleString("from IPython.Shell import start\n");
- PyRun_SimpleString("import client\n");
- PyRun_SimpleString("start(user_ns=dict(client=client)).mainloop()\n");
+
+ // Import the module, so the user does not have to import it.
+ if (PyRun_SimpleString("import client\n") < 0) {
+ // Failure (should never be reached)
+ ret = 1;
+ goto end;
+ }
+
+ // Run IPython if available, otherwise a normal interpreter.
+ if (PyRun_SimpleString("from IPython.Shell import start\n") == 0)
+ PyRun_SimpleString("start(user_ns=dict(client=client)).mainloop()\n");
+ else
+ Py_Main(argc, argv);
+
+end:
Py_Finalize();
+ return ret;
}