-
Notifications
You must be signed in to change notification settings - Fork 0
/
PythonHacks.cpp
197 lines (167 loc) · 4.99 KB
/
PythonHacks.cpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "Python.h"
#include <unordered_map>
static std::unordered_map<PyFunctionObject*, PyObject*> repr_tracker;
static char module_docstring[] =
"Module to try out different Python backend hacks";
static PyObject* changeFunctionName(PyObject *self, PyObject *args) {
PyObject *type = NULL,
*old_name = NULL,
*new_name = NULL,
*dict = NULL,
*func_obj = NULL;
if (!PyArg_ParseTuple(args, "OOO", &type, &old_name, &new_name)) {
PyErr_SetString(PyExc_ValueError, "Unexpected number of args");
goto FAIL;
}
#if PY_MAJOR_VERSION >= 3
if (!PyUnicode_Check(old_name) || !PyUnicode_Check(new_name))
#else
if (!PyString_Check(old_name) || !PyString_Check(new_name))
#endif
{
PyErr_SetString(PyExc_TypeError, "'old_name' and 'new_name' need to be strings");
goto FAIL;
}
if (PyType_Check(type)) {
PyTypeObject *pytype = (PyTypeObject *)type;
dict = pytype->tp_dict;
func_obj = PyDict_GetItem(dict, old_name);
if (func_obj == NULL) {
PyErr_SetString(PyExc_ValueError, "object definition does not exist in the given type!");
goto FAIL;
}
}
else if ( PyModule_Check(type)) {
dict = PyModule_GetDict(type);
func_obj = PyDict_GetItem(dict, old_name);
if (func_obj == NULL) {
PyErr_SetString(PyExc_ValueError, "object definition does not exist in the given module!");
goto FAIL;
}
}
else {
PyErr_SetString(PyExc_ValueError, "'type' is neither a module or a type");
goto FAIL;
}
if (PyDict_GetItem(dict, new_name))
{
PyErr_SetString(PyExc_ValueError, "new_name already exists");
goto FAIL;
}
PyDict_SetItem(dict, new_name, func_obj);
PyDict_DelItem(dict, old_name);
Py_RETURN_NONE;
FAIL:
return NULL;
}
// definition from CPython funcobject.c
static PyObject*
func_repr(PyFunctionObject *op)
{
#if PY_MAJOR_VERSION >= 3
return PyUnicode_FromFormat("<function %s at %p>",
(char*)PyUnicode_DATA(op->func_name), op);
#else
return PyString_FromFormat("<function %s at %p>",
PyString_AsString(op->func_name), op);
#endif
}
static PyObject* test_repr(PyFunctionObject* self) {
if (repr_tracker.find(self) != repr_tracker.end()) {
PyObject* result = PyObject_CallFunctionObjArgs(repr_tracker[self], self, NULL);
if (!result) {
return func_repr(self);
}
#if PY_MAJOR_VERSION >= 3
return PyUnicode_FromFormat("%s",
(char*)PyUnicode_DATA(result));
#else
return PyString_FromFormat("%s",
PyString_AsString(result));
#endif
}
return func_repr(self);
}
static PyObject* changeFunctionReprToLambda(PyObject *self, PyObject *args) {
PyObject *function = NULL,
*new_repr = NULL,
*test_result = NULL;
PyTypeObject* function_type = NULL;
PyFunctionObject* function_obj = NULL;
if (!PyArg_ParseTuple(args, "OO", &function, &new_repr)) {
PyErr_SetString(PyExc_ValueError, "Unexpected number of args");
goto FAIL;
}
if (!PyFunction_Check(function)) {
PyErr_SetString(PyExc_TypeError, "expected a function");
goto FAIL;
}
function_obj = (PyFunctionObject*) function;
if (new_repr == Py_None) {
if (repr_tracker.find(function_obj) != repr_tracker.end()) {
Py_DECREF(repr_tracker[function_obj]);
repr_tracker.erase(function_obj);
}
Py_TYPE(function)->tp_repr = (reprfunc)func_repr;
Py_RETURN_NONE;
}
if (!PyFunction_Check(new_repr)) {
PyErr_SetString(PyExc_TypeError, "expected 'new_repr' to be a function");
goto FAIL;
}
test_result = PyObject_CallFunctionObjArgs(new_repr, function_obj, NULL);
if (!test_result){
PyErr_SetString(PyExc_TypeError, "Wrong function signature. Expected something like lambda x: x");
goto FAIL;
}
#if PY_MAJOR_VERSION >= 3
if (!PyUnicode_Check(test_result))
#else
if (!PyString_Check(test_result))
#endif
{
PyErr_SetString(PyExc_TypeError, "'new_repr' is expected to return a Python string");
goto FAIL;
}
if (repr_tracker.find(function_obj) != repr_tracker.end())
Py_XDECREF(repr_tracker[(PyFunctionObject*) function]);
function_type = Py_TYPE(function_type);
Py_INCREF(new_repr);
repr_tracker[function_obj] = new_repr;
Py_TYPE(function)->tp_repr = (reprfunc)test_repr;
Py_RETURN_NONE;
FAIL:
return NULL;
}
static PyMethodDef module_methods[] = {
{"change_function_name", changeFunctionName, METH_VARARGS, "change function name"},
{"change_function_repr", changeFunctionReprToLambda, METH_VARARGS, "replace __repr__ slot with a lambda"},
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef cModPyDef =
{
PyModuleDef_HEAD_INIT,
"python_hacks", /* name of module */
module_docstring, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
module_methods
};
#define INITERROR return NULL
PyMODINIT_FUNC PyInit_python_hacks(void)
#else
#define INITERROR return
PyMODINIT_FUNC initpython_hacks(void)
#endif
{
#if PY_MAJOR_VERSION >= 3
PyObject *m = PyModule_Create(&cModPyDef);
#else
PyObject *m = Py_InitModule3("python_hacks", module_methods, module_docstring);
#endif
if (!m)
INITERROR;
#if PY_MAJOR_VERSION >= 3
return m;
#endif
}