Panda3D
 All Classes Functions Variables Enumerations
ppython.cxx
00001 ///////////////////////////////////////////////////////////////////////
00002 //
00003 // This is a little wrapper to make it easy to run a python
00004 // program from the command line. Basically, it just interfaces
00005 // to the Python API and imports the module that was specified
00006 // by the IMPORT_MODULE preprocessor definition when it was compiled.
00007 //
00008 ///////////////////////////////////////////////////////////////////////
00009 
00010 #include <Python.h>
00011 
00012 #ifndef IMPORT_MODULE
00013 #error IMPORT_MODULE must be defined when compiling ppython.cxx !
00014 #endif
00015 
00016 #define _STRINGIFY(s) #s
00017 #define STRINGIFY(s) _STRINGIFY(s)
00018 #define IMPORT_MODULE_STR STRINGIFY(IMPORT_MODULE)
00019 
00020 int main(int argc, char **argv) {
00021   int sts = 0;
00022 
00023   Py_SetProgramName(argv[0]);
00024   
00025   // On windows, we need to set pythonhome correctly. We'll try to
00026   // find ppython.exe on the path and set pythonhome to its location.
00027 #ifdef _WIN32
00028   char *path = getenv("PATH");
00029   char *result = strtok(path, ";");
00030   while (result != NULL) {
00031     struct stat st;       
00032     char *ppython = (char*) malloc(strlen(result) + 13);
00033     strcpy(ppython, result);
00034     strcat(ppython, "\\ppython.exe");
00035     if (stat(ppython, &st) == 0) {
00036         Py_SetPythonHome(result);
00037         free(ppython);
00038         break;
00039     }                                
00040     result = strtok(NULL, ";");
00041     free(ppython);
00042   }
00043 #endif
00044   
00045   Py_Initialize();
00046 
00047   if (Py_VerboseFlag) {
00048     fprintf(stderr, "Python %s\\n%s\\n", Py_GetVersion(), Py_GetCopyright());
00049   }
00050 
00051   PySys_SetArgv(argc, argv);
00052 
00053   PyObject* m = PyImport_ImportModule(IMPORT_MODULE_STR);
00054   if (m <= 0) {
00055     PyErr_Print();
00056     sts = 1;
00057   }
00058 
00059   Py_Finalize();
00060   return sts;
00061 }
 All Classes Functions Variables Enumerations