-
Notifications
You must be signed in to change notification settings - Fork 0
/
side_python.cc
207 lines (169 loc) · 6.2 KB
/
side_python.cc
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
198
199
200
201
202
203
204
205
206
207
#include <Python.h>
#include <cassert>
#include <memory>
#include <string>
#include "slapo_py_update_hook.h"
#include "cc_py_obj.h"
using std::string;
using std::unique_ptr;
namespace slapo_py_update_hook {
namespace {
CCPyObj op_type;
CCPyObj mod_type;
class GilHolder {
public:
GilHolder() : state_(PyGILState_Ensure()) {}
GilHolder(const GilHolder &) = delete;
~GilHolder() { PyGILState_Release(state_); }
void operator=(const GilHolder &) = delete;
private:
PyGILState_STATE state_;
};
} // anonymous namespace
//
// ModificationOp
//
CCPyObj mod_op_to_python(ModificationOp &op) {
CCPyObj py_entry = CCPyObj::checked_steal(PyDict_New());
for (const auto &name_values : op.entry) {
CCPyObj values = CCPyObj::checked_steal(PyList_New(0));
for (const string &value : name_values.second) {
CCPyObj py_value{value};
PyList_Append(values.ref(), py_value.ref());
}
CCPyObj py_name{name_values.first};
PyDict_SetItem(py_entry.ref(), py_name.ref(), values.ref());
}
CCPyObj py_mods = CCPyObj::checked_steal(PyList_New(0));
for (const Modification &mod : op.mods) {
CCPyObj py_values = CCPyObj::checked_steal(PyList_New(0));
for (const string &value : mod.values) {
CCPyObj py_value{value};
PyList_Append(py_values.ref(), py_value.ref());
}
CCPyObj py_mod = mod_type(mod.name, py_values, mod.op, mod.flags);
PyList_Append(py_mods.ref(), py_mod.ref());
}
return op_type(op.dn, op.auth_dn, py_entry, py_mods);
}
void mod_op_from_python(ModificationOp &op, CCPyObj py_op) {
op.mods.clear();
CCPyObj mods = py_op.attr("modifications");
Py_ssize_t num_mods = mods.size();
for (Py_ssize_t i = 0; i < num_mods; i++) {
CCPyObj py_mod = mods.item(i);
if (py_mod.size() != 4) {
throw PyError{"Invalid modification (mods should only contain "
"len-4-tuples or "
"Modification namedtuples)"};
}
Modification mod;
mod.name = static_cast<string>(py_mod.item(0));
CCPyObj values = py_mod.item(1);
CCPyObj iterator =
CCPyObj::checked_steal(PyObject_GetIter(values.ref()));
CCPyObj item;
while ((item = CCPyObj::unchecked_steal(PyIter_Next(iterator.ref())))
.ref() != nullptr) {
mod.values.push_back(item);
}
mod.op = py_mod.item(2);
mod.flags = py_mod.item(3);
op.mods.push_back(mod);
}
}
//
// Misc
//
void init_python() {
Py_InitializeEx(0);
PyEval_InitThreads();
PyEval_SaveThread();
GilHolder gil_holder;
init_cc_py_obj();
// Ideally we'd use PyImport_ImportModule and PyObject_CallMethod or
// PyObject_CallMethodObjArgs, but the namedtuple function looks at the
// caller's stack frame, which wouldn't exist and would raise an exception.
CCPyObj globals = CCPyObj::checked_steal(PyDict_New());
PyDict_SetItemString(globals.ref(), "__builtins__", PyEval_GetBuiltins());
CCPyObj locals = CCPyObj::checked_steal(PyDict_New());
CCPyObj::checked_steal(
PyRun_String("import collections\n"
"op_type = collections.namedtuple(\n"
"'ModificationOp', 'dn,auth_dn,entry,modifications')\n"
"mod_type = collections.namedtuple(\n"
"'Modification', 'name,values,op,flags')\n",
Py_file_input, globals.ref(), locals.ref()));
op_type = locals.item("op_type");
mod_type = locals.item("mod_type");
}
//
// InstanceInfo
//
class InstanceInfoImpl : public InstanceInfo {
public:
InstanceInfoImpl() : function_name_("update") {}
virtual ~InstanceInfoImpl() {}
void set_filename(const std::string &name) override { filename_ = name; }
void set_function_name(const std::string &name) override {
function_name_ = name;
}
void open() override;
int update(ModificationOp &op, std::string &error) override;
private:
std::string filename_;
std::string function_name_;
CCPyObj py_module_;
};
void InstanceInfoImpl::open() {
if (filename_.empty()) {
throw PyError{"No py_filename specified in config"};
}
unique_ptr<FILE, decltype(&fclose)> fp{nullptr, &fclose};
fp.reset(fopen(filename_.c_str(), "r"));
if (!fp) {
throw PyError{"Unable to open file " + filename_ + ": " +
strerror(errno)};
}
GilHolder gil_holder;
CCPyObj mod = CCPyObj::checked_steal(PyModule_New("update_hook"));
PyModule_AddStringConstant(mod.ref(), "__file__", filename_.c_str());
for (const auto &name_value : py_consts) {
PyModule_AddIntConstant(mod.ref(), name_value.first.c_str(),
name_value.second);
}
CCPyObj builtins = CCPyObj::checked_borrow(PyEval_GetBuiltins());
PyModule_AddObject(mod.ref(), "__builtins__", builtins.new_ref());
PyModule_AddObject(mod.ref(), "Modification", mod_type.new_ref());
CCPyObj locals = CCPyObj::checked_steal(PyDict_New());
CCPyObj::checked_steal(
PyRun_FileEx(fp.get(), filename_.c_str(), Py_file_input,
PyModule_GetDict(mod.ref()), locals.ref(), 0));
PyDict_Update(PyModule_GetDict(mod.ref()), locals.ref());
if (!PyObject_HasAttrString(mod.ref(), function_name_.c_str())) {
throw PyError{"File " + filename_ + " is missing function " +
function_name_};
}
py_module_ = mod;
}
int InstanceInfoImpl::update(ModificationOp &op, string &error) {
assert(py_module_.ref());
GilHolder gil_holder;
CCPyObj py_op = mod_op_to_python(op);
CCPyObj result = py_module_.attr(function_name_)(py_op);
if (result.ref() != Py_None) {
if (result.size() != 2) {
throw PyError{"Result must be None or (int, str)"};
}
int status = result.item(0);
if (status != 0) {
error = static_cast<string>(result.item(1));
return status;
}
}
mod_op_from_python(op, py_op);
return 0; // LDAP_SUCCESS
}
// static
InstanceInfo *InstanceInfo::create() { return new InstanceInfoImpl; }
} // namespace slapo_py_update_hook