Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPythonHandle.cpp
More file actions
Latest commit
162 lines (139 loc) · 5.43 KB
/
Copy pathPythonHandle.cpp
File metadata and controls
162 lines (139 loc) · 5.43 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
// Copyright (c) 2013-2017 Josh Blum
// SPDX-License-Identifier: BSL-1.0
#include<Poco/Format.h>
#include<cassert>
#include<iostream>
#include"PythonProxy.hpp"
PythonProxyHandle::PythonProxyHandle(std::shared_ptr<PythonProxyEnvironment> env, PyObject *obj, constbool borrowed):
env(env), obj(obj)
{
PyGilStateLock lock;
ref = PyObjectRef(obj, borrowed);
}
PythonProxyHandle::~PythonProxyHandle(void)
{
PyGilStateLock lock;
ref = PyObjectRef();
}
intPythonProxyHandle::compareTo(const Pothos::Proxy &proxy) const
{
PyGilStateLock lock;
int rEq = 0, rGt = 0, rLt = 0;
rEq = PyObject_RichCompareBool(obj, env->getHandle(proxy)->obj, Py_EQ);
if (rEq == 1) return0;
if (rEq == -1) goto fail;
rGt = PyObject_RichCompareBool(obj, env->getHandle(proxy)->obj, Py_GT);
if (rGt == 1) return +1;
if (rGt == -1) goto fail;
rLt = PyObject_RichCompareBool(obj, env->getHandle(proxy)->obj, Py_LT);
if (rLt == 1) return -1;
if (rLt == -1) goto fail;
fail:
constint r = 0;
auto errorMsg = getErrorString();
if (not errorMsg.empty())
{
throwPothos::ProxyCompareError("PythonProxyHandle::compareTo()", errorMsg);
}
return r;
}
size_tPythonProxyHandle::hashCode(void) const
{
PyGilStateLock lock;
returnsize_t(PyObject_Hash(obj));
}
std::string PythonProxyHandle::toString(void) const
{
PyGilStateLock lock;
PyObjectRef str(PyObject_Str(obj), REF_NEW);
returnPyObjToStdString(str.obj);
}
std::string PythonProxyHandle::getClassName(void) const
{
PyGilStateLock lock;
PyObjectRef cls(PyObject_GetAttrString(obj, "__class__"), REF_NEW);
PyObjectRef clsName(PyObject_GetAttrString(cls.obj, "__name__"), REF_NEW);
PyObjectRef modName(PyObject_GetAttrString(cls.obj, "__module__"), REF_NEW);
constauto clsNameStr = PyObjToStdString(clsName.obj);
constauto modNameStr = PyObjToStdString(modName.obj);
#if PY_MAJOR_VERSION >= 3
if (modNameStr == "builtins") return clsNameStr;
#else
if (modNameStr == "__builtin__") return clsNameStr;
#endif
return modNameStr + "." + clsNameStr;
}
Pothos::Proxy PythonProxyHandle::call(const std::string &name, const Pothos::Proxy *args, constsize_t numArgs)
{
PyGilStateLock lock;
if (this->obj == nullptr) throwPothos::ProxyHandleCallError(
"PythonProxyHandle::call("+name+")", "cant call on a null object");
/*******************************************************************
* Step 0) handle field accessors and mutators
******************************************************************/
constauto colon = name.find(":");
if (colon != std::string::npos)
{
PyObjectRef result(Py_None, REF_BORROWED);
if (name.substr(0, colon) == "set"and numArgs == 1)
{
PyObject_SetAttrString(this->obj, name.substr(colon+1).c_str(), env->getHandle(args[0])->obj);
}
elseif (name.substr(0, colon) == "get"and numArgs == 0)
{
result = PyObjectRef(PyObject_GetAttrString(this->obj, name.substr(colon+1).c_str()), REF_NEW);
}
elsethrowPothos::ProxyHandleCallError(
"PythonProxyHandle::call("+name+")", "unknown operation");
auto errorMsg = getErrorString();
if (not errorMsg.empty())
{
throwPothos::ProxyExceptionMessage(errorMsg);
}
return env->makeHandle(result);
}
/*******************************************************************
* Step 1) locate the callable object
******************************************************************/
PyObjectRef attrObj;
if (name.empty() or name == "()") attrObj = PyObjectRef(ref);
else attrObj = PyObjectRef(PyObject_GetAttrString(this->obj, name.c_str()), REF_NEW);
if (attrObj.obj == nullptr)
{
throwPothos::ProxyHandleCallError(
"PythonProxyHandle::call("+name+")",
Poco::format("no attribute on %s", this->toString()));
}
if (notPyCallable_Check(attrObj.obj))
{
if (numArgs == 0) return env->makeHandle(attrObj); //access a field
elsethrowPothos::ProxyHandleCallError(
"PythonProxyHandle::call("+name+")",
Poco::format("cant call on %s", this->toString()));
}
/*******************************************************************
* Step 2) create tuple of arguments
******************************************************************/
PyObjectRef argsObj(PyTuple_New(numArgs), REF_NEW);
std::vector<std::shared_ptr<PythonProxyHandle>> argHandles(numArgs);
for (size_t i = 0; i < numArgs; i++)
{
argHandles[i] = env->getHandle(args[i]);
PyTuple_SetItem(argsObj.obj, i, argHandles[i]->ref.newRef());
}
/*******************************************************************
* Step 4) call into the callable object
******************************************************************/
PyObjectRef result(PyObject_CallObject(attrObj.obj, argsObj.obj), REF_NEW);
/*******************************************************************
* Step 5) exception handling and reporting
******************************************************************/
auto errorMsg = getErrorString();
if (not errorMsg.empty())
{
throwPothos::ProxyExceptionMessage(errorMsg);
}
auto x = env->makeHandle(result);
if (x.getClassName() == "PothosProxy") return x.convert<Pothos::Proxy>();
return x;
}