-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProxyCallType.cpp
More file actions
77 lines (65 loc) · 2.3 KB
/
ProxyCallType.cpp
File metadata and controls
77 lines (65 loc) · 2.3 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
// Copyright (c) 2014 Josh Blum
// SPDX-License-Identifier: BSL-1.0
#include "PothosModule.hpp"
#include <cassert>
static PyTypeObject ProxyCallType = {
PyObject_HEAD_INIT(NULL)
};
static void ProxyCall_dealloc(ProxyCallObject *self)
{
delete self->proxy;
delete self->name;
Py_TYPE(self)->tp_free((PyObject*)self);
}
static int ProxyCall_init(ProxyCallObject *self, PyObject *args, PyObject *)
{
self->proxy = new PyObjectRef(PyTuple_GetItem(args, 0), REF_BORROWED);
self->name = new PyObjectRef(PyTuple_GetItem(args, 1), REF_BORROWED);
return 0;
}
PyObject* ProxyCall_call(ProxyCallObject *self, PyObject *args, PyObject *)
{
try
{
//extract string name
const auto name = PyObjectToProxy(self->name->obj).convert<std::string>();
//create call args
Pothos::ProxyVector proxyArgs;
for (int i = 0; i < PyTuple_Size(args); i++)
{
proxyArgs.push_back(PyObjectToProxy(PyTuple_GetItem(args, i)));
}
//make proxy call
auto handle = ((ProxyObject *)self->proxy->obj)->proxy->getHandle();
Pothos::Proxy proxy;
{
PyThreadStateLock lock; //proxy call could be potentially blocking
proxy = handle->call(name, proxyArgs.data(), proxyArgs.size());
}
//convert the result into a pyobject
return ProxyToPyObject(proxyEnvTranslate(proxy, getPythonProxyEnv()));
}
catch (const Pothos::Exception &ex)
{
PyErr_SetString(PyExc_RuntimeError, ex.displayText().c_str());
return nullptr;
}
}
PyObject *makeProxyCallObject(PyObject *args)
{
return PyObject_CallObject((PyObject *)&ProxyCallType, args);
}
void registerProxyCallType(PyObject *m)
{
ProxyCallType.tp_new = PyType_GenericNew;
ProxyCallType.tp_name = "PothosProxyCall";
ProxyCallType.tp_basicsize = sizeof(ProxyCallObject);
ProxyCallType.tp_dealloc = (destructor)ProxyCall_dealloc;
ProxyCallType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
ProxyCallType.tp_doc = "Pothos Proxy Call binding";
ProxyCallType.tp_init = (initproc)ProxyCall_init;
ProxyCallType.tp_call = (ternaryfunc)ProxyCall_call;
if (PyType_Ready(&ProxyCallType) < 0) return;
Py_INCREF(&ProxyCallType);
PyModule_AddObject(m, "ProxyCall", (PyObject *)&ProxyCallType);
}