-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProxyEnvironmentType.cpp
More file actions
197 lines (173 loc) · 6.46 KB
/
ProxyEnvironmentType.cpp
File metadata and controls
197 lines (173 loc) · 6.46 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright (c) 2014-2014 Josh Blum
// SPDX-License-Identifier: BSL-1.0
#include "PothosModule.hpp"
#include <cassert>
static PyTypeObject ProxyEnvironmentType = {
PyObject_HEAD_INIT(NULL)
};
static void ProxyEnvironment_dealloc(ProxyEnvironmentObject *self)
{
delete self->env;
Py_TYPE(self)->tp_free((PyObject*)self);
}
static int ProxyEnvironment_init(ProxyEnvironmentObject *self, PyObject *args, PyObject *kwds)
{
//convert args, should not throw
Pothos::ProxyVector proxyArgs;
Pothos::ProxyMap proxyKwargs;
if (args != nullptr) proxyArgs = PyObjectToProxy(args).convert<Pothos::ProxyVector>();
if (kwds != nullptr) proxyKwargs = PyObjectToProxy(kwds).convert<Pothos::ProxyMap>();
//allocate environment sptr
self->env = new Pothos::ProxyEnvironment::Sptr();
//no args, thats ok, its a null env
if (proxyArgs.empty()) return 0;
//check the input
if (proxyArgs.size() != 1)
{
PyErr_SetString(PyExc_RuntimeError, "expects one arg for proxy name");
return -1;
}
try
{
Pothos::ProxyEnvironmentArgs envArgs;
for (const auto &pair : proxyKwargs)
{
envArgs[pair.first.convert<std::string>()] = pair.second.convert<std::string>();
}
const auto name = proxyArgs[0].convert<std::string>();
*(self->env) = Pothos::ProxyEnvironment::make(name, envArgs);
}
catch (const Pothos::Exception &ex)
{
PyErr_SetString(PyExc_RuntimeError, ex.displayText().c_str());
return -1;
}
return 0;
}
static PyObject *ProxyEnvironment_findProxy(ProxyEnvironmentObject *self, PyObject *args)
{
//convert args, should not throw
Pothos::ProxyVector proxyArgs;
if (args != nullptr) proxyArgs = PyObjectToProxy(args).convert<Pothos::ProxyVector>();
//check the input
if (proxyArgs.size() != 1)
{
PyErr_SetString(PyExc_RuntimeError, "expects one arg for proxy name");
return nullptr;
}
try
{
const auto name = proxyArgs[0].convert<std::string>();
auto proxy = (*self->env)->findProxy(name);
return makeProxyObject(proxy);
}
catch (const Pothos::Exception &ex)
{
PyErr_SetString(PyExc_RuntimeError, ex.displayText().c_str());
return nullptr;
}
}
static PyObject *ProxyEnvironment_getName(ProxyEnvironmentObject *self)
{
const auto name = (*self->env)->getName();
auto proxy = getPythonProxyEnv()->makeProxy(name);
return ProxyToPyObject(proxy);
}
static PyObject *ProxyEnvironment_convertObjectToProxy(ProxyEnvironmentObject *self, PyObject *args)
{
//check the input
if (not args or PyTuple_Size(args) != 1)
{
PyErr_SetString(PyExc_RuntimeError, "expects one arg for input object");
return nullptr;
}
auto arg0 = PyTuple_GetItem(args, 0);
if (isProxyObject(arg0))
{
PyErr_SetString(PyExc_RuntimeError, "input should not be of type ProxyObject");
return nullptr;
}
//convert into the environment in self
try
{
auto proxy = PyObjectToProxy(arg0);
return makeProxyObject(proxyEnvTranslate(proxy, *self->env));
}
catch (const Pothos::Exception &ex)
{
PyErr_SetString(PyExc_RuntimeError, ex.displayText().c_str());
return nullptr;
}
}
static PyObject *ProxyEnvironment_convertProxyToObject(ProxyEnvironmentObject *, PyObject *args)
{
//check the input
if (not args or PyTuple_Size(args) != 1)
{
PyErr_SetString(PyExc_RuntimeError, "expects one arg for input proxy");
return nullptr;
}
auto arg0 = PyTuple_GetItem(args, 0);
if (not isProxyObject(arg0))
{
PyErr_SetString(PyExc_RuntimeError, "input is not of type ProxyObject");
return nullptr;
}
//convert proxy into a proxy holding a python object
try
{
auto proxy = *reinterpret_cast<ProxyObject *>(arg0)->proxy;
return ProxyToPyObject(proxyEnvTranslate(proxy, getPythonProxyEnv()));
}
catch (const Pothos::Exception &ex)
{
PyErr_SetString(PyExc_RuntimeError, ex.displayText().c_str());
return nullptr;
}
}
static PyMethodDef ProxyEnvironment_methods[] = {
{"findProxy", (PyCFunction)ProxyEnvironment_findProxy, METH_VARARGS, "Pothos::ProxyEnvironment::findProxy(name)"},
{"getName", (PyCFunction)ProxyEnvironment_getName, METH_NOARGS, "Pothos::ProxyEnvironment::getName()"},
{"convertObjectToProxy", (PyCFunction)ProxyEnvironment_convertObjectToProxy, METH_VARARGS, "Pothos::ProxyEnvironment::convertObjectToProxy(obj)"},
{"convertProxyToObject", (PyCFunction)ProxyEnvironment_convertProxyToObject, METH_VARARGS, "Pothos::ProxyEnvironment::convertProxyToObject(proxy)"},
{nullptr} /* Sentinel */
};
PyObject* ProxyEnvironment_Compare(PyObject *o1, PyObject *o2, int opid)
{
if (not isProxyEnvironmentObject(o1) or not isProxyEnvironmentObject(o2))
{
PyErr_SetString(PyExc_RuntimeError, "ProxyEnvironment must compare to another ProxyEnvironment");
return nullptr;
}
auto s1 = *reinterpret_cast<ProxyEnvironmentObject*>(o1)->env;
auto s2 = *reinterpret_cast<ProxyEnvironmentObject*>(o2)->env;
const int cmp = (s1 < s2)? -1 : ((s1 > s2)? +1 : 0);
return richCompareFromSimple(cmp, opid);
}
PyObject *makeProxyEnvironmentObject(const Pothos::ProxyEnvironment::Sptr &env)
{
PyObject *o = PyObject_CallObject((PyObject *)&ProxyEnvironmentType, nullptr);
auto proxyObject = reinterpret_cast<ProxyEnvironmentObject *>(o);
*(proxyObject->env) = env;
return o;
}
bool isProxyEnvironmentObject(PyObject *obj)
{
if (obj == nullptr) return false;
return Py_TYPE(obj) == &ProxyEnvironmentType;
}
void registerProxyEnvironmentType(PyObject *m)
{
ProxyEnvironmentType.tp_new = PyType_GenericNew;
ProxyEnvironmentType.tp_name = "PothosProxyEnvironment";
ProxyEnvironmentType.tp_basicsize = sizeof(ProxyEnvironmentObject);
ProxyEnvironmentType.tp_dealloc = (destructor)ProxyEnvironment_dealloc;
ProxyEnvironmentType.tp_richcompare = (richcmpfunc)ProxyEnvironment_Compare;
ProxyEnvironmentType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
ProxyEnvironmentType.tp_doc = "Pothos ProxyEnvironment binding";
ProxyEnvironmentType.tp_methods = ProxyEnvironment_methods;
ProxyEnvironmentType.tp_init = (initproc)ProxyEnvironment_init;
if (PyType_Ready(&ProxyEnvironmentType) < 0) return;
Py_INCREF(&ProxyEnvironmentType);
PyModule_AddObject(m, "ProxyEnvironment", (PyObject *)&ProxyEnvironmentType);
}