source: christine.c @ 0bc1d62

Revision 0bc1d62, 3.0 KB checked in by Marco Antonio Islas Cruz <markuz@…>, 19 months ago (diff)

Added error tracking if cannot import libchristine.Christine

  • Property mode set to 100644
Line 
1#include <Python.h>
2#include <import.h>
3#include <graminit.h>
4#include <pythonrun.h>
5
6#ifdef WINDOWS
7    #include <direct.h>
8    #define GetCurrentDir _getcwd
9#else
10    #include <unistd.h>
11    #define GetCurrentDir getcwd
12 #endif
13
14void error(char *msg) {
15        PyErr_Print();
16        printf("%s\n", msg);
17        exit(1);
18}
19
20int
21main(int argc, char *argv[]){
22        PyObject *t,*main_module, *main_dict, *main_dict_copy;
23        PyObject *builtinMod,*c_argv, *runChristine,*name;
24        PyObject *path,* os_getcwd, *osname;
25        PyObject *sys_argv, *sys_path;
26        PyObject *christine_module, *christine_dict;
27        char cCurrentPath[FILENAME_MAX];
28        int i;
29        Py_Initialize();
30        // Get a reference to the main module
31        main_module = PyImport_AddModule("__main__");
32        if (main_module == NULL)
33                error ("Could not import __main__ module");
34        main_dict = PyModule_GetDict(main_module);
35        if (main_dict == NULL)
36                error ("Could not get the __main__ module dictionary");
37        if (PyDict_GetItemString(main_dict, "__import__") == NULL){
38                builtinMod = PyImport_ImportModule("__builtin__");
39                        PyObject *sys = PyImport_ImportModule("sys");
40                        if (sys == NULL)
41                                error("");
42                        PyObject *bdict = PyModule_GetDict(sys);
43                if (builtinMod == NULL)
44                                error("Could not import the __builtins__ module");
45                        if (PyDict_SetItemString(main_dict, "__builtins__", builtinMod) != 0)
46                                error("Could not assign the new dictionary");
47                        if (PyDict_SetItemString(main_dict,"sys", sys) !=0)
48                                error("");
49                        if (PyDict_GetItemString(main_dict, "__builtins__") == NULL)
50                                error ("Still there is no __builtins__!!!!");
51                        PyObject  *keys = PyDict_Keys(bdict);
52                        PyList_Sort(keys);
53        }
54        //Get the reference of sys.argv
55        sys_argv = PySys_GetObject("argv");
56        if (sys_argv == NULL)
57                sys_argv = PyList_New(0);
58        for (i = 0; i < argc ;i++)
59                if (PyList_Append(sys_argv, PyString_FromString(argv[i]))!=0)
60                                        error("Cannot append to sys_argv");
61        PySys_SetObject("argv",sys_argv);
62        main_dict_copy = PyDict_Copy(main_dict);
63        if (main_dict_copy == NULL)
64                error ("Can't get the main_dict _copy");
65        if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
66             error("Can't get the current path");
67        //Get the reference os sys.path
68        sys_path = PySys_GetObject("path");
69        if (PyList_Append(sys_path,  PyString_FromString(cCurrentPath)) < 0)
70                error ("Cannot append the current path to sys");
71        PySys_SetObject("path", sys_path);
72        name = PyString_FromString("libchristine.Christine");
73        christine_module= PyImport_Import(name);
74        if (christine_module == NULL){
75                error ("Cannot import libchristine.Christine.runChristine");
76                return NULL;
77        }
78        //getting the module dict
79        christine_dict =  PyModule_GetDict(christine_module);
80        //Get the runChristine function
81        runChristine = PyDict_GetItemString(christine_dict, "runChristine");
82        if (!PyCallable_Check(runChristine)) {
83                                error("runChristine is not callable");
84                    return NULL;
85                }
86        Py_XINCREF(runChristine);
87        PyObject_CallObject(runChristine,  PyTuple_New(0));
88        Py_XDECREF(runChristine);
89        if (t == NULL){
90                error("Error while trying to run christine");
91        }
92        Py_Finalize();
93        return 0;
94}
95
96
Note: See TracBrowser for help on using the repository browser.