This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathpython_loader.c
100 lines (88 loc) · 3.18 KB
/
python_loader.c
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
/* python_loader.c
*
* Pyreshark Plugin for Wireshark. (https://github.com/ashdnazg/pyreshark)
*
* Copyright (c) 2013 by Eshed Shaham
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "python_loader.h"
#include <glib.h>
#include <gmodule.h>
gboolean load_symbol(GModule *handle, const char *function_name, void ** dest)
{
gpointer gp;
if (g_module_symbol(handle, function_name, &gp))
{
*dest = gp;
return TRUE;
} else {
return FALSE;
}
}
python_lib_t * load_python_lib(const char * python_lib_name)
{
GModule *handle = NULL;
int * _Py_NoSiteFlag = NULL;
python_lib_t *python_lib = NULL;
void *site_module;
if ((handle = g_module_open(python_lib_name, (GModuleFlags)0)) == NULL)
{
return NULL;
}
python_lib = (python_lib_t *) g_malloc(sizeof(python_lib_t));
if (!load_symbol(handle, "Py_Initialize", (void **) &(python_lib->Py_Initialize)) ||
!load_symbol(handle, "PyRun_SimpleStringFlags", (void **) &(python_lib->PyRun_SimpleStringFlags)) ||
!load_symbol(handle, "PyFile_FromString", (void **) &(python_lib->PyFile_FromString)) ||
!load_symbol(handle, "PyFile_AsFile", (void **) &(python_lib->PyFile_AsFile)) ||
!load_symbol(handle, "PyRun_SimpleFileExFlags", (void **) &(python_lib->PyRun_SimpleFileExFlags)) ||
!load_symbol(handle, "Py_DecRef", (void **) &(python_lib->Py_DecRef)) ||
!load_symbol(handle, "PyImport_ImportModule", (void **) &(python_lib->PyImport_ImportModule)))
{
g_free(python_lib);
g_module_close(handle);
return NULL;
}
load_symbol(handle, "Py_NoSiteFlag", (void **) &(_Py_NoSiteFlag));
++(*_Py_NoSiteFlag);
python_lib->Py_Initialize();
site_module = python_lib->PyImport_ImportModule("site");
if (site_module == NULL)
{
g_free(python_lib);
g_module_close(handle);
return NULL;
}
python_lib->Py_DecRef(site_module);
return python_lib;
}
python_lib_t * load_python(python_version_t *out_version)
{
python_lib_t *python_lib = NULL;
python_lib = load_python_lib(PYTHON_27);
if (python_lib != NULL)
{
*out_version = PYTHON_VERSION_27;
return python_lib;
}
python_lib = load_python_lib(PYTHON_26);
if (python_lib != NULL)
{
*out_version = PYTHON_VERSION_26;
return python_lib;
}
return NULL;
}