-
Notifications
You must be signed in to change notification settings - Fork 5
/
PythonHandle.cpp
162 lines (139 loc) · 5.43 KB
/
PythonHandle.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
// 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, const bool borrowed):
env(env), obj(obj)
{
PyGilStateLock lock;
ref = PyObjectRef(obj, borrowed);
}
PythonProxyHandle::~PythonProxyHandle(void)
{
PyGilStateLock lock;
ref = PyObjectRef();
}
int PythonProxyHandle::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) return 0;
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:
const int r = 0;
auto errorMsg = getErrorString();
if (not errorMsg.empty())
{
throw Pothos::ProxyCompareError("PythonProxyHandle::compareTo()", errorMsg);
}
return r;
}
size_t PythonProxyHandle::hashCode(void) const
{
PyGilStateLock lock;
return size_t(PyObject_Hash(obj));
}
std::string PythonProxyHandle::toString(void) const
{
PyGilStateLock lock;
PyObjectRef str(PyObject_Str(obj), REF_NEW);
return PyObjToStdString(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);
const auto clsNameStr = PyObjToStdString(clsName.obj);
const auto 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, const size_t numArgs)
{
PyGilStateLock lock;
if (this->obj == nullptr) throw Pothos::ProxyHandleCallError(
"PythonProxyHandle::call("+name+")", "cant call on a null object");
/*******************************************************************
* Step 0) handle field accessors and mutators
******************************************************************/
const auto 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);
}
else if (name.substr(0, colon) == "get" and numArgs == 0)
{
result = PyObjectRef(PyObject_GetAttrString(this->obj, name.substr(colon+1).c_str()), REF_NEW);
}
else throw Pothos::ProxyHandleCallError(
"PythonProxyHandle::call("+name+")", "unknown operation");
auto errorMsg = getErrorString();
if (not errorMsg.empty())
{
throw Pothos::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)
{
throw Pothos::ProxyHandleCallError(
"PythonProxyHandle::call("+name+")",
Poco::format("no attribute on %s", this->toString()));
}
if (not PyCallable_Check(attrObj.obj))
{
if (numArgs == 0) return env->makeHandle(attrObj); //access a field
else throw Pothos::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())
{
throw Pothos::ProxyExceptionMessage(errorMsg);
}
auto x = env->makeHandle(result);
if (x.getClassName() == "PothosProxy") return x.convert<Pothos::Proxy>();
return x;
}