-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPythonHelpers.cpp
More file actions
111 lines (96 loc) · 4.02 KB
/
PythonHelpers.cpp
File metadata and controls
111 lines (96 loc) · 4.02 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
//********************************************************************************//
// MIT License //
// //
// Copyright (c) 2022 TeamViewer Germany GmbH //
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy //
// of this software and associated documentation files (the "Software"), to deal //
// in the Software without restriction, including without limitation the rights //
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell //
// copies of the Software, and to permit persons to whom the Software is //
// furnished to do so, subject to the following conditions: //
// //
// The above copyright notice and this permission notice shall be included in all //
// copies or substantial portions of the Software. //
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE //
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, //
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE //
// SOFTWARE. //
//********************************************************************************//
#include "PythonHelpers.h"
namespace tvagentapipy
{
const char* const InternalError = "Internal call has failed";
PyObject* NoneOrInternalError(bool success)
{
if (success)
{
Py_RETURN_NONE;
}
PyErr_SetString(PyExc_RuntimeError, InternalError);
return nullptr;
}
PyTypeObject* CreateEnumType(
const std::string& enumName,
const std::map<std::string, long>& enumValues)
{
PyObject* pyModuleEnum = PyImport_ImportModule("enum");
if (pyModuleEnum == nullptr)
{
PyErr_SetString(PyExc_ImportError, "enum");
return nullptr;
}
PyObject* pyTypeEnum = PyObject_GetAttrString(pyModuleEnum, "Enum");
if (pyTypeEnum == nullptr)
{
Py_XDECREF(pyModuleEnum);
PyErr_SetString(PyExc_ImportError, "enum.Enum");
return nullptr;
}
PyObject* pyAttrs = PyDict_New();
for (const auto& enumValue : enumValues)
{
PyObject* pyValue = PyLong_FromLong(enumValue.second);
PyDict_SetItemString(pyAttrs, enumValue.first.c_str(), pyValue);
Py_XDECREF(pyValue);
}
PyObject* pyArgs = Py_BuildValue("sO", enumName.c_str(), pyAttrs);
Py_XDECREF(pyAttrs);
PyObject* pyNewEnumType = PyObject_CallObject(pyTypeEnum, pyArgs);
Py_XDECREF(pyArgs);
Py_XDECREF(pyTypeEnum);
Py_XDECREF(pyModuleEnum);
return reinterpret_cast<PyTypeObject*>(pyNewEnumType);
}
PyObject* PyEnumValue(
PyTypeObject* pyEnumType,
const std::string& enumValueName)
{
PyObject* pyEnumValue = PyObject_GetAttrString(
reinterpret_cast<PyObject*>(pyEnumType),
enumValueName.c_str());
return pyEnumValue;
}
PyTypeObject PyTypeObjectInitialized()
{
// We'r not able to use designated initializers (available only in C99 and C++20) to
// partially initialize PyTypeObject with PyVarObject_HEAD_INIT macro and have the
// rest of it being zero-initialized. Partially initialized PyTypeObject causes a
// compilation warning, therefore we use this helper function with initalization via
// Head struct, which replicates PyTypeObject header fields.
struct Head
{
PyObject_VAR_HEAD
};
static const Head head = {
PyVarObject_HEAD_INIT(nullptr, 0)
};
PyTypeObject typeObject{};
typeObject.ob_base = head.ob_base;
return typeObject;
}
} // namespace tvagentapipy