This repository was archived by the owner on Oct 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathpython.c
More file actions
64 lines (58 loc) · 1.79 KB
/
python.c
File metadata and controls
64 lines (58 loc) · 1.79 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* See LICENSE for licensing information
*/
/* Needs to be first */
#include <Python.h>
#include <stdio.h>
#include <stdlib.h>
#include "python-global-lock.h"
#include "python-interpreter.h"
/*
* This is actually a pretty dirty hack: The argv0 required here
* cannot be the argv[0] of the plugin as it is in the wrong path.
* Then Python will not search in the correct path for the modules.
* Thus, we make this a static string based on the prefix set during
* compilation of the plugin. Shouldn't cause too much harm but would
* like to see a better solution.
*/
#if PY_MAJOR_VERSION >= 3
#define Lstr2(s) L ## s
#define Lstr(s) Lstr2(s)
#define ARGV0 Lstr(INSTALL_PREFIX) L"/bin/shadow-python3"
#else
#define ARGV0 INSTALL_PREFIX "/bin/shadow-python"
#endif
/* The used sub-interpreter */
PyThreadState *shd_py_interpreter = NULL;
int main(int argc, char *argv[]) {
int ret = 0;
/* Must be the first thing we do to get everything else started
* We can do this here even though Py_Main will call those again.
* However, we must run them before creating our sub-interpreter.
*
* See comments on ARGV0 as to why we set it like this.
*/
Py_SetProgramName(ARGV0);
Py_Initialize();
/* Create sub-interpreter */
#ifdef DEBUG
fprintf(stderr, "starting new interpreter\n");
#endif
shd_py_interpreter = Py_NewInterpreter();
if(!shd_py_interpreter) {
PyErr_Print();
return 1;
}
PyThreadState *old = PyThreadState_Swap(shd_py_interpreter);
ret = python_main(argc, argv);
#ifdef DEBUG
fprintf(stderr, "shutting down interpreter\n");
#endif
PyThreadState_Swap(shd_py_interpreter);
Py_EndInterpreter(shd_py_interpreter);
shd_py_interpreter = NULL;
PyThreadState_Swap(old);
Py_Finalize();
return ret;
#undef PYERR
}