-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpython.h
More file actions
81 lines (63 loc) · 2.6 KB
/
python.h
File metadata and controls
81 lines (63 loc) · 2.6 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef _JAUNCH_PYTHON_H
#define _JAUNCH_PYTHON_H
#include <stddef.h> // for NULL, size_t
#include "logging.h"
#include "common.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. Path to the runtime executable launcher (python).
// 3. List of arguments to the Python runtime, one per line.
// =======================================================================
if (argc < 1) {
FAIL(ERROR_ARGC_OUT_OF_BOUNDS, "Too few PYTHON directive arguments: %d", argc);
}
char **ptr = (char **)argv;
const char *libpython_path = *ptr++;
LOG_INFO("PYTHON", "libpython_path = %s", libpython_path);
const int python_argc = argc - 1;
const char **python_argv = (const char **)ptr;
const char *python_exe_path = *ptr++;
LOG_INFO("PYTHON", "python_exe_path = %s", python_exe_path);
// =======================================================================
// Load the Python runtime.
// =======================================================================
// Load libpython.
LOG_DEBUG("PYTHON", "Loading libpython");
void *python_library = lib_open(libpython_path);
if (python_library == NULL) {
FAIL(ERROR_DLOPEN, "Failed to load libpython: %s", lib_error());
}
// Load Py_BytesMain function.
LOG_DEBUG("PYTHON", "Loading Py_BytesMain");
static int (*Py_BytesMain)(int, char **);
Py_BytesMain = lib_sym(python_library, "Py_BytesMain");
if (Py_BytesMain == NULL) {
LOG_ERROR("Failed to locate Py_BytesMain function: %s", lib_error());
lib_close(python_library);
return ERROR_DLSYM;
}
// Invoke Python main routine with the specified arguments.
int result = Py_BytesMain(python_argc, (char **)python_argv);
if (result != 0) {
LOG_ERROR("Failed to run Python script: %d", result);
lib_close(python_library);
return result;
}
// =======================================================================
// Clean up.
// =======================================================================
LOG_DEBUG("PYTHON", "Closing libpython");
lib_close(python_library);
LOG_INFO("PYTHON", "Python cleanup complete");
return SUCCESS;
}
static void cleanup_python() {}
#endif