-
Notifications
You must be signed in to change notification settings - Fork 5
/
PythonProxy.hpp
102 lines (84 loc) · 2.95 KB
/
PythonProxy.hpp
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
// Copyright (c) 2013-2014 Josh Blum
// SPDX-License-Identifier: BSL-1.0
#pragma once
#include "PyObjectUtils.hpp"
#include <Pothos/Config.hpp>
#include <Pothos/Proxy.hpp>
#include <Pothos/Callable.hpp>
#include <string>
class PythonProxyHandle;
inline std::string PyObjToStdString(PyObject *o)
{
#if PY_MAJOR_VERSION >= 3
assert(PyUnicode_Check(o));
Py_ssize_t size = 0;
const char *c = PyUnicode_AsUTF8AndSize(o, &size);
return std::string(c, size);
#else
assert(PyString_Check(o));
return std::string(PyString_AsString(o), PyString_Size(o));
#endif
}
inline PyObject *StdStringToPyObject(const std::string &s)
{
#if PY_MAJOR_VERSION >= 3
return PyUnicode_DecodeLocaleAndSize(s.c_str(), s.size(), nullptr);
#else
return PyString_FromStringAndSize(s.c_str(), s.size());
#endif
}
inline std::string getErrorString(void)
{
if (not PyErr_Occurred()) return "";
PyObject *type = nullptr, *value = nullptr, *traceback = nullptr;
PyErr_Fetch(&type, &value, &traceback);
assert(value != nullptr);
std::string errorMsg = PyObjToStdString(PyObjectRef(PyObject_Str(value), REF_NEW).obj);
Py_XDECREF(type);
Py_XDECREF(value);
Py_XDECREF(traceback);
PyErr_Clear();
return errorMsg;
}
/***********************************************************************
* custom Python environment overload
**********************************************************************/
class PythonProxyEnvironment :
public Pothos::ProxyEnvironment
{
public:
PythonProxyEnvironment(const Pothos::ProxyEnvironmentArgs &);
Pothos::Proxy makeHandle(PyObject *obj, const bool borrowed);
Pothos::Proxy makeHandle(const PyObjectRef &ref);
std::shared_ptr<PythonProxyHandle> getHandle(const Pothos::Proxy &proxy);
std::string getName(void) const
{
return "python";
}
Pothos::Proxy findProxy(const std::string &name);
Pothos::Proxy convertObjectToProxy(const Pothos::Object &local);
Pothos::Object convertProxyToObject(const Pothos::Proxy &proxy);
void serialize(const Pothos::Proxy &, std::ostream &);
Pothos::Proxy deserialize(std::istream &);
};
/***********************************************************************
* custom Python class handler overload
**********************************************************************/
class PythonProxyHandle : public Pothos::ProxyHandle
{
public:
PythonProxyHandle(std::shared_ptr<PythonProxyEnvironment> env, PyObject *obj, const bool borrowed);
~PythonProxyHandle(void);
Pothos::ProxyEnvironment::Sptr getEnvironment(void) const
{
return env;
}
Pothos::Proxy call(const std::string &name, const Pothos::Proxy *args, const size_t numArgs);
int compareTo(const Pothos::Proxy &proxy) const;
size_t hashCode(void) const;
std::string toString(void) const;
std::string getClassName(void) const;
std::shared_ptr<PythonProxyEnvironment> env;
PyObject *obj;
PyObjectRef ref;
};