-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathutils.hpp
142 lines (121 loc) · 4.51 KB
/
utils.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
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
//*****************************************************************************
// Copyright 2023 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#pragma once
#include <memory>
#include <string>
#pragma warning(push)
#pragma warning(disable : 6326 28182 6011 28020)
#include <pybind11/embed.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#pragma warning(pop)
#include "../logging.hpp"
namespace py = pybind11;
using namespace py::literals;
namespace ovms {
template <class T>
class PyObjectWrapper {
std::unique_ptr<T> obj;
public:
PyObjectWrapper() = delete;
PyObjectWrapper(const PyObjectWrapper& other) = delete;
PyObjectWrapper(const T& other) {
py::gil_scoped_acquire acquire;
SPDLOG_TRACE("PyObjectWrapper constructor start");
obj = std::make_unique<T>(other);
SPDLOG_TRACE("PyObjectWrapper constructor end");
}
~PyObjectWrapper() {
py::gil_scoped_acquire acquire;
SPDLOG_TRACE("PyObjectWrapper destructor start ");
obj.reset();
SPDLOG_TRACE("PyObjectWrapper destructor end ");
}
T& getObject() const {
py::gil_scoped_acquire acquire;
if (obj) {
return *obj;
} else {
throw std::exception();
}
}
template <typename U>
inline U getProperty(const std::string& name) const {
py::gil_scoped_acquire acquire;
try {
U property = obj->attr(name.c_str()).template cast<U>();
return property;
} catch (const pybind11::error_already_set& e) {
SPDLOG_DEBUG("PyObjectWrapper::getProperty failed: {}", e.what());
throw e;
} catch (std::exception& e) {
SPDLOG_DEBUG("PyObjectWrapper::getProperty failed: {}", e.what());
throw e;
}
}
};
class UnexpectedPythonObjectError : public std::exception {
protected:
std::string message;
public:
UnexpectedPythonObjectError() = delete;
UnexpectedPythonObjectError(const UnexpectedPythonObjectError& exception) {
this->message = std::string(exception.what());
}
UnexpectedPythonObjectError(const py::object& obj, const std::string& expectedType) {
py::gil_scoped_acquire acquire;
std::string objectType = obj.attr("__class__").attr("__name__").cast<std::string>();
this->message = "Unexpected Python object type. Expected: " + expectedType + ". Received: " + objectType;
}
const char* what() const throw() override {
return message.c_str();
}
};
class UnexpectedInputPythonObjectError : UnexpectedPythonObjectError {
public:
UnexpectedInputPythonObjectError(const UnexpectedPythonObjectError& exception) :
UnexpectedPythonObjectError(exception) {}
const char* what() const throw() override { return UnexpectedPythonObjectError::what(); }
};
class UnexpectedOutputPythonObjectError : UnexpectedPythonObjectError {
public:
UnexpectedOutputPythonObjectError(const UnexpectedPythonObjectError& exception) :
UnexpectedPythonObjectError(exception) {}
const char* what() const throw() override { return UnexpectedPythonObjectError::what(); }
};
class BadPythonNodeConfigurationError : public std::exception {
std::string message;
public:
BadPythonNodeConfigurationError() = delete;
BadPythonNodeConfigurationError(const std::string& message) {
this->message = "Bad python node configuration. " + message;
}
const char* what() const throw() override {
return message.c_str();
}
};
class UnexpectedOutputTensorError : public std::exception {
std::string message;
public:
UnexpectedOutputTensorError() = delete;
UnexpectedOutputTensorError(const std::string& outputName) {
this->message = "Unexpected Tensor found in the outputs. Tensor name: " + outputName + " is not a valid node output";
}
const char* what() const throw() override {
return message.c_str();
}
};
} // namespace ovms