aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/runtime/main/main.c
blob: 69dd80e5583698d28f4bc7185e8530117d013c6b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <Python.h>

#include <stdlib.h>
#include <string.h>

void Py_InitArgcArgv(int argc, char** argv);

static const char* env_entry_point = "Y_PYTHON_ENTRY_POINT";

#ifdef _MSC_VER
extern char** environ;

void unsetenv(const char* name) {
    const int n = strlen(name);
    char** dst = environ;
    for (char** src = environ; *src; src++)
        if (strncmp(*src, name, n) || (*src)[n] != '=')
            *dst++ = *src;
    *dst = NULL;
}
#endif

static int pymain(int argc, char** argv) {
    const char* entry_point = getenv(env_entry_point);
    if (entry_point && !strcmp(entry_point, ":main")) {
        unsetenv(env_entry_point);
        return Py_Main(argc, argv);
    }
    Py_InitArgcArgv(argc, argv);
    Py_SetProgramName(argv[0]);
    Py_Initialize();
    PySys_SetArgv(argc, argv);
    int rc = PyRun_SimpleString(
        "from library.python.runtime import entry_points\n"
        "entry_points.run_constructors()\n"
        "import __res\n"
        "__res.importer.run_main()\n");
    Py_Finalize();
    return rc == 0 ? 0 : 1;
}

int (*mainptr)(int argc, char** argv) = pymain;

int main(int argc, char** argv) {
    return mainptr(argc, argv);
}