forked from vortec/vfs_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_importer.c
More file actions
105 lines (84 loc) · 2.81 KB
/
python_importer.c
File metadata and controls
105 lines (84 loc) · 2.81 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "python_importer.h"
PyObject *py_handler = NULL;
bool always_import = false;
void debug(int level, const char *text)
{
DEBUG(level, ("vfs_python: %s\n", text));
}
static PyObject* py_debug(PyObject* self, PyObject* args) {
int level = 5;
const char* text;
if (!PyArg_ParseTuple(args, "|is", &level, &text)) {
// If parsing with an integer fails, try parsing just the string
PyErr_Clear();
if (!PyArg_ParseTuple(args, "s", &text)) {
return NULL;
}
}
debug(level, text);
Py_RETURN_NONE;
}
static PyMethodDef debug_method = {
"debug", (PyCFunction)py_debug, METH_VARARGS, "Log debug message"
};
void add_debug_function_to_globals() {
PyObject *module_dict = PyImport_GetModuleDict();
PyObject *builtins = PyDict_GetItemString(module_dict, "builtins");
if (builtins != NULL) {
PyObject *py_func = PyCFunction_NewEx(&debug_method, NULL, NULL);
PyDict_SetItemString(PyModule_GetDict(builtins), "debug", py_func);
Py_DECREF(py_func);
}
}
const char *get_conf(vfs_handle_struct *handle, const char *name)
{
return lp_parm_const_string(SNUM(handle->conn), "python", name, NULL);
}
PyObject *get_py_mod(const char *script_path)
{
PyObject *py_mod;
PyObject *py_imp_str, *py_imp_handle, *py_imp_dict;
PyObject *py_imp_load_source, *py_args_tuple;
Py_Initialize();
add_debug_function_to_globals();
py_imp_str = PyUnicode_FromString("imp");
py_imp_handle = PyImport_Import(py_imp_str);
py_imp_dict = PyModule_GetDict(py_imp_handle);
py_imp_load_source = PyDict_GetItemString(py_imp_dict, "load_source");
py_args_tuple = PyTuple_New(2);
PyTuple_SetItem(py_args_tuple, 0, PyUnicode_FromString("handler"));
PyTuple_SetItem(py_args_tuple, 1, PyUnicode_FromString(script_path));
py_mod = PyObject_CallObject(py_imp_load_source, py_args_tuple);
Py_DECREF(py_args_tuple);
Py_DECREF(py_imp_str);
Py_DECREF(py_imp_handle);
Py_DECREF(py_imp_dict);
Py_DECREF(py_imp_load_source);
return py_mod;
}
PyObject *get_py_func(PyObject *py_mod, const char *func_name)
{
PyObject *py_func_name = PyUnicode_FromString(func_name);
PyObject *py_func = PyObject_GetAttr(py_mod, py_func_name);
Py_DECREF(py_func_name);
if (py_func != NULL)
{
if (PyCallable_Check(py_func) == 1)
{
return py_func;
}
}
debug(5, "something went wrong get_py_func.");
return NULL;
}
PyObject *get_func(vfs_handle_struct *handle, const char *func_name)
{
debug(5, "get_func");
if ((py_handler == NULL) || (always_import == true))
{
debug(5, "importing module.");
const char *script_path = get_conf(handle, "script");
py_handler = get_py_mod(script_path);
}
return get_py_func(py_handler, func_name);
}