| 26 | #endif |
| 27 | |
| 28 | static int RunModule(const char* modname) |
| 29 | { |
| 30 | PyObject *module, *runpy, *runmodule, *runargs, *result; |
| 31 | runpy = PyImport_ImportModule("runpy"); |
| 32 | if (runpy == NULL) { |
| 33 | fprintf(stderr, "Could not import runpy module\n"); |
| 34 | PyErr_Print(); |
| 35 | return -1; |
| 36 | } |
| 37 | runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main"); |
| 38 | if (runmodule == NULL) { |
| 39 | fprintf(stderr, "Could not access runpy._run_module_as_main\n"); |
| 40 | PyErr_Print(); |
| 41 | Py_DECREF(runpy); |
| 42 | return -1; |
| 43 | } |
| 44 | module = PyUnicode_FromString(modname); |
| 45 | if (module == NULL) { |
| 46 | fprintf(stderr, "Could not convert module name to unicode\n"); |
| 47 | PyErr_Print(); |
| 48 | Py_DECREF(runpy); |
| 49 | Py_DECREF(runmodule); |
| 50 | return -1; |
| 51 | } |
| 52 | runargs = Py_BuildValue("(Oi)", module, 0); |
| 53 | if (runargs == NULL) { |
| 54 | fprintf(stderr, |
| 55 | "Could not create arguments for runpy._run_module_as_main\n"); |
| 56 | PyErr_Print(); |
| 57 | Py_DECREF(runpy); |
| 58 | Py_DECREF(runmodule); |
| 59 | Py_DECREF(module); |
| 60 | return -1; |
| 61 | } |
| 62 | result = PyObject_Call(runmodule, runargs, NULL); |
| 63 | if (result == NULL) { |
| 64 | PyErr_Print(); |
| 65 | } |
| 66 | Py_DECREF(runpy); |
| 67 | Py_DECREF(runmodule); |
| 68 | Py_DECREF(module); |
| 69 | Py_DECREF(runargs); |
| 70 | if (result == NULL) { |
| 71 | return -1; |
| 72 | } |
| 73 | Py_DECREF(result); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | #ifdef MS_WINDOWS |
| 78 | static int pymain(int argc, wchar_t** argv) |