-
Notifications
You must be signed in to change notification settings - Fork 27
/
PyOperation.h
187 lines (151 loc) · 6.1 KB
/
PyOperation.h
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//=========================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//=========================================================================
#ifndef smtk_model_PyOperation_h
#define smtk_model_PyOperation_h
#include "smtk/common/CompilerInformation.h"
SMTK_THIRDPARTY_PRE_INCLUDE
#include <pybind11/pybind11.h>
SMTK_THIRDPARTY_POST_INCLUDE
#include <type_traits>
#include "smtk/io/Logger.h"
#include "smtk/operation/Operation.h"
#include "smtk/common/PythonInterpreter.h"
namespace pybind11
{
namespace detail
{
template <>
struct type_caster<std::shared_ptr<smtk::operation::Operation> >
{
PYBIND11_TYPE_CASTER(std::shared_ptr<smtk::operation::Operation>, _("Operation"));
using OperationCaster = copyable_holder_caster<smtk::operation::Operation,
std::shared_ptr<smtk::operation::Operation> >;
bool load(handle src, bool b)
{
OperationCaster oc;
bool success = oc.load(src, b);
if (!success)
{
return false;
}
auto py_obj = reinterpret_borrow<object> (src);
auto base_ptr = static_cast<std::shared_ptr<smtk::operation::Operation> > (oc);
// Construct a shared_ptr to the object
auto py_obj_ptr = std::shared_ptr<object>{
new object{py_obj},
[](object* py_object_ptr)
{
// It's possible that when the shared_ptr dies we won't have the
// gil (if the last holder is in a non-Python thread), so we
// make sure to acquire it in the deleter.
gil_scoped_acquire gil;
delete py_object_ptr;
}
};
value = std::shared_ptr<smtk::operation::Operation> (py_obj_ptr, base_ptr.get());
return true;
}
static handle cast (std::shared_ptr<smtk::operation::Operation> base,
return_value_policy rvp,
handle h)
{
return OperationCaster::cast (base, rvp, h);
}
};
template <>
struct is_holder_type<smtk::operation::Operation,
std::shared_ptr<smtk::operation::Operation> > : std::true_type {};
}
}
namespace smtk
{
namespace operation
{
class PyOperation : public Operation
{
public:
PyOperation() = default;
~PyOperation() override = default;
static std::shared_ptr<smtk::operation::Operation> create(std::string modulename,
std::string className,
smtk::operation::Operation::Index index)
{
// Import the module containing our operation
pybind11::module module = pybind11::module::import(modulename.c_str());
// Create an instance of our operation
pybind11::object obj = module.attr(className.c_str())();
// For C++ operations, index() is a compile-time intrinsic of the
// operation class. Python operations only come into existence at runtime,
// though, so we need to manually set a python operation's index.
obj.cast<std::shared_ptr<smtk::operation::PyOperation> >()->setIndex(index);
// The precedent for python operation names is estabilished in
// ImportPythonOperation to be the modulename.className
// We follow that convention here.
obj.cast<std::shared_ptr<smtk::operation::PyOperation> >()->setTypeName(
modulename + "." + className);
return obj.cast<std::shared_ptr<smtk::operation::Operation> >();
}
Index index() const override { return m_index; }
bool ableToOperate() override
{
// When a python operation is constructed on one thread and called on
// another, we need to temporarily cut the relationship between
// PyThreadState and the original thread before calling this method on a
// new thread. Otherwise, we result in deadlock. Pybind11's
// gil_scoped_release provides this functionality.
pybind11::gil_scoped_release thread_state(true);
PYBIND11_OVERLOAD(bool, Operation, ableToOperate, );
}
std::string typeName() const override { return m_typeName; }
smtk::io::Logger& log() const override
{
// When a python operation is constructed on one thread and called on
// another, we need to temporarily cut the relationship between
// PyThreadState and the original thread before calling this method on a
// new thread. Otherwise, we result in deadlock. Pybind11's
// gil_scoped_release provides this functionality.
pybind11::gil_scoped_release thread_state(true);
PYBIND11_OVERLOAD(smtk::io::Logger&, Operation, log, );
}
Result operateInternal() override
{
// Python operations often raise exceptions. If they are uncaught, they
// become C++ exceptions. We convert these exceptions to a failed
// execution.
try
{
return this->operateInternalPy();
}
catch(std::exception& e)
{
this->log().addRecord(smtk::io::Logger::Error, e.what());
return this->createResult(smtk::operation::Operation::Outcome::FAILED);
}
}
void postProcessResult(Result& res) override
{ PYBIND11_OVERLOAD(void, Operation, postProcessResult, res); }
void generateSummary(Result& res) override
{ PYBIND11_OVERLOAD(void, Operation, generateSummary, res); }
// We incorporate the base class's method with a different access modifier
using Operation::createBaseSpecification;
/// Avoid PyGILState_Check() failures by running on the main thread by default.
bool threadSafe() const override { return false; }
private:
Specification createSpecification() override
{ PYBIND11_OVERLOAD_PURE(Specification, Operation, createSpecification); }
void setIndex(Index index) { m_index = index; }
void setTypeName(const std::string& typeName) { m_typeName = typeName; }
Result operateInternalPy() { PYBIND11_OVERLOAD_PURE(Result, Operation, operateInternal, ); }
Index m_index;
std::string m_typeName;
};
}
}
#endif