home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 4 / AACD04.ISO / AACD / Programming / Python / Source / Embed / test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  2.2 KB  |  93 lines

  1. /* Example of embedding Python in another program */
  2.  
  3. #include "Python.h"
  4.  
  5. static char *argv0;
  6.  
  7. static const char *ver = "$VER: embed demo " __AMIGADATE__ " Embedded Python\0";
  8.  
  9. extern void InitAmigaPython(int argc, char **argv);
  10.  
  11. void initxyzzy(Py_PROTO(void)); /* Forward */
  12.  
  13. int main(int argc, char **argv)
  14. {
  15.     FILE *fh;
  16.  
  17.     /* Save a copy of argv0 */
  18.     argv0 = argv[0];
  19.  
  20.     /* Init Embedded AmigaPython. Required. */
  21.     InitAmigaPython(argc, argv);
  22.  
  23.     /* Optionally, set the program name.  Default is the name of the   */
  24.     /*   executable, which should be fine.  */
  25.     /* Py_SetProgramName("test"); */
  26.  
  27.  
  28.     /* Initialize the Python interpreter.  Required. */
  29.     Py_Initialize();
  30.  
  31.     /* Add a static module */
  32.     initxyzzy();
  33.  
  34.     /* Define sys.argv.  It is up to the application if you
  35.        want this; you can also let it undefined (since the Python 
  36.        code is generally not a main program it has no business
  37.        touching sys.argv...) */
  38.     PySys_SetArgv(argc, argv);
  39.  
  40.     /* Do some application specific code */
  41.     printf("Hello, brave new world\n\n");
  42.  
  43.     /* Execute some Python statements (in module __main__) */
  44.     PyRun_SimpleString("import sys\n");
  45.     PyRun_SimpleString("print 'BUILTIN MODULES:',sys.builtin_module_names\n");
  46.     PyRun_SimpleString("print 'IMPORTED MODULES:',sys.modules.keys()\n");
  47.     PyRun_SimpleString("print 'sys.argv:',sys.argv\n");
  48.  
  49.     fh = fopen("testscript.py","r");
  50.     if(fh)
  51.     {
  52.         PyRun_SimpleFile(fh,"testscript.py");
  53.         PyRun_SimpleString("print '>> testscript has set result to',result\n");
  54.         fclose(fh);
  55.     }
  56.  
  57.     /* Note that you can call any public function of the Python
  58.        interpreter here, e.g. call_object(). */
  59.  
  60.     /* Some more application specific code */
  61.     printf("\nGoodbye, cruel world\n");
  62.  
  63.     /* Exit, cleaning up the interpreter */
  64.     Py_Exit(0);
  65.     /*NOTREACHED*/
  66. }
  67.  
  68. /* A static module */
  69.  
  70. static PyObject *xyzzy_foo Py_PROTO((PyObject *, PyObject *));
  71.  
  72. static PyObject *
  73. xyzzy_foo(self, args)
  74.     PyObject *self; /* Not used */
  75.     PyObject *args;
  76. {
  77.     if (!PyArg_ParseTuple(args, ""))
  78.         return NULL;
  79.     return PyInt_FromLong(42L);
  80. }
  81.  
  82. static PyMethodDef xyzzy_methods[] = {
  83.     {"foo",        xyzzy_foo,    1},
  84.     {NULL,        NULL}        /* sentinel */
  85. };
  86.  
  87. void
  88. initxyzzy Py_PROTO((void))
  89. {
  90.     PyImport_AddModule("xyzzy");
  91.     Py_InitModule("xyzzy", xyzzy_methods);
  92. }
  93.