-
Notifications
You must be signed in to change notification settings - Fork 2
/
python.h
59 lines (48 loc) · 2.04 KB
/
python.h
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
#ifndef _JAUNCH_PYTHON_H
#define _JAUNCH_PYTHON_H
/*
* This is the logic implementing Jaunch's PYTHON directive.
*
* It dynamically loads libpython and calls Py_BytesMain with the given args.
*/
static int launch_python(const size_t argc, const char **argv) {
// =======================================================================
// Parse the arguments, which must conform to the following structure:
//
// 1. Path to the runtime native library (libpython).
// 2. List of arguments to the Python runtime, one per line.
// =======================================================================
const char *libpython_path = argv[0];
debug("[JAUNCH-PYTHON] libpython_path = %s", libpython_path);
// =======================================================================
// Load the Python runtime.
// =======================================================================
// Load libpython.
debug("[JAUNCH-PYTHON] LOADING LIBPYTHON");
void *python_library = lib_open(libpython_path);
if (!python_library) { error("Error loading libpython: %s", lib_error()); return ERROR_DLOPEN; }
// Load Py_BytesMain function.
debug("[JAUNCH-PYTHON] LOADING Py_BytesMain");
static int (*Py_BytesMain)(int, char **);
Py_BytesMain = lib_sym(python_library, "Py_BytesMain");
if (!Py_BytesMain) {
error("Error finding Py_BytesMain function: %s", lib_error());
lib_close(python_library);
return 1;
}
// Invoke Python main routine with the specified arguments.
int result = Py_BytesMain(argc, (char **)argv);
if (result != 0) {
error("Error running Python script: %d", result);
lib_close(python_library);
return result;
}
// =======================================================================
// Clean up.
// =======================================================================
debug("[JAUNCH-PYTHON] CLOSING LIBPYTHON");
lib_close(python_library);
debug("[JAUNCH-PYTHON] GOODBYE");
return SUCCESS;
}
#endif