-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpy.c
More file actions
183 lines (157 loc) · 4.82 KB
/
py.c
File metadata and controls
183 lines (157 loc) · 4.82 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <stdio.h>
#include "py.h"
static lookup_func_callback lookup_func_cb = NULL;
typedef struct {
const char* func_name;
void** func_ptr;
} FuncEntry;
void (*PyGILState_Ensure)(void) = NULL;
void (*PyGILState_Release)(void*) = NULL;
void* (*PyDict_New)(void) = NULL;
void* (*PyRun_String)(const char*, int, void*, void*) = NULL;
void* (*PyErr_Occurred)(void) = NULL;
void (*PyErr_Clear)(void) = NULL;
void (*Py_DecRef)(void*) = NULL;
void (*PyErr_Print)(void) = NULL;
void* (*PyDict_GetItemString)(void*, const char*) = NULL;
void* (*PyObject_CallObject)(void*, void*) = NULL;
long (*PyLong_AsLong)(void*) = NULL;
double (*PyFloat_AsDouble)(void*) = NULL;
int (*PyObject_IsTrue)(void*) = NULL;
const char* (*PyUnicode_AsUTF8)(void*, Py_ssize_t*) = NULL;
void* (*PyObject_Type)(void*) = NULL;
void* (*PyObject_Str)(void*) = NULL;
void* PyBool_Type = NULL;
void* PyLong_Type = NULL;
void* PyFloat_Type = NULL;
void* PyUnicode_Type = NULL;
int store_lookup_callback(void* callback) {
lookup_func_cb = callback;
FuncEntry entries[] = {
// functions
{"PyGILState_Ensure", (void*)&PyGILState_Ensure},
{"PyGILState_Release", (void*)&PyGILState_Release},
{"PyDict_New", (void*)&PyDict_New},
{"PyRun_String", (void*)&PyRun_String},
{"PyErr_Occurred", (void*)&PyErr_Occurred},
{"PyErr_Clear", (void*)&PyErr_Clear},
{"Py_DecRef", (void*)&Py_DecRef},
{"PyErr_Print", (void*)&PyErr_Print},
{"PyDict_GetItemString", (void*)&PyDict_GetItemString},
{"PyObject_CallObject", (void*)&PyObject_CallObject},
{"PyLong_AsLong", (void*)&PyLong_AsLong},
{"PyFloat_AsDouble", (void*)&PyFloat_AsDouble},
{"PyObject_IsTrue", (void*)&PyObject_IsTrue},
{"PyUnicode_AsUTF8", (void*)&PyUnicode_AsUTF8},
{"PyObject_Str", (void*)&PyObject_Str},
// types
{"PyObject_Type", (void*)&PyObject_Type},
{"PyFloat_Type", (void*)&PyFloat_Type},
{"PyUnicode_Type", (void*)&PyUnicode_Type},
{"PyBool_Type", (void*)&PyBool_Type},
{"PyLong_Type", (void*)&PyLong_Type},
};
int num_entries = sizeof(entries) / sizeof(entries[0]);
for (int i = 0; i < num_entries; ++i) {
*entries[i].func_ptr = (void*)lookup_func_cb(entries[i].func_name);;
if (*entries[i].func_ptr == NULL) {
printf("actrun: failed to load '%s'\n", entries[i].func_name);
return -1;
}
}
return 0;
}
void* C_PyDict_New(void) {
return PyDict_New();
}
void* C_PyRun_String(const char* str, int start, void* globals, void* locals) {
return PyRun_String(str, start, globals, locals);
}
void* C_PyErr_Occurred(void) {
return PyErr_Occurred();
}
void C_PyErr_Clear(void) {
PyErr_Clear();
}
void C_Py_DecRef(void* obj) {
Py_DecRef(obj);
}
void C_PyErr_Print(void) {
PyErr_Print();
}
void* C_PyDict_GetItemString(void* p, const char* key) {
return PyDict_GetItemString(p, key);
}
void* C_PyObject_CallObject(void* callable, void* args) {
return PyObject_CallObject(callable, args);
}
void* C_PyObject_Str(void* obj) {
return PyObject_Str(obj);
}
long C_PyLong_AsLong(void* obj) {
return PyLong_AsLong(obj);
}
double C_PyFloat_AsDouble(void* obj) {
return PyFloat_AsDouble(obj);
}
int C_PyObject_IsTrue(void* obj) {
return PyObject_IsTrue(obj);
}
const char* C_PyUnicode_AsUTF8(void* obj) {
return PyUnicode_AsUTF8(obj, NULL);
}
void* C_PyObject_Type(void* obj) {
return PyObject_Type(obj);
}
int C_IsBool(void* obj) {
void* type = C_PyObject_Type(obj);
if (type == PyBool_Type) {
C_Py_DecRef(type);
return 1; // is a boolean
}
C_Py_DecRef(type);
return 0; // not a boolean
}
int C_IsLong(void* obj) {
void* type = C_PyObject_Type(obj);
if (type == PyLong_Type) {
C_Py_DecRef(type);
return 1; // is a boolean
}
C_Py_DecRef(type);
return 0; // not a boolean
}
int C_IsFloat(void* obj) {
void* type = C_PyObject_Type(obj);
if (type == PyFloat_Type) {
C_Py_DecRef(type);
return 1; // is a boolean
}
C_Py_DecRef(type);
return 0; // not a boolean
}
int C_IsUtf8String(void* obj) {
void* type = C_PyObject_Type(obj);
if (type == PyUnicode_Type) {
C_Py_DecRef(type);
return 1; // is a boolean
}
C_Py_DecRef(type);
return 0; // not a boolean
}
const char* C_PyType_GetName(void* obj) {
void* type = C_PyObject_Type(obj);
if (type != NULL) {
void* typeStr = C_PyObject_Str(type);
if (typeStr != NULL) {
// potential mem leak since name is not cleaned up?
const char* name = C_PyUnicode_AsUTF8(typeStr);
C_Py_DecRef(typeStr);
C_Py_DecRef(type);
return name; // is a boolean
}
C_Py_DecRef(type);
}
C_Py_DecRef(type);
return "unknown";
}