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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
|
/*
* SPDX-FileCopyrightText: All Contributors to the PyTango project
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "precompiled_header.hpp"
#include "pytgutils.h"
#include "callback.h"
#include "device_attribute.h"
#include "exception.h"
#include "device_pipe.h"
struct PyCmdDoneEvent
{
bopy::object device;
bopy::object cmd_name;
bopy::object argout;
bopy::object argout_raw;
bopy::object err;
bopy::object errors;
bopy::object ext;
};
struct PyAttrReadEvent
{
bopy::object device;
bopy::object attr_names;
bopy::object argout;
bopy::object err;
bopy::object errors;
bopy::object ext;
};
struct PyAttrWrittenEvent
{
bopy::object device;
bopy::object attr_names;
bopy::object err;
bopy::object errors;
bopy::object ext;
};
static void copy_most_fields(PyCallBackAutoDie *_self, const Tango::CmdDoneEvent *ev, PyCmdDoneEvent *py_ev)
{
// py_ev->device
py_ev->cmd_name = bopy::object(ev->cmd_name);
py_ev->argout_raw = bopy::object(ev->argout);
py_ev->err = bopy::object(ev->err);
py_ev->errors = bopy::object(ev->errors);
// py_ev->ext = bopy::object(ev->ext);
}
static void copy_most_fields(PyCallBackAutoDie *_self, const Tango::AttrReadEvent *ev, PyAttrReadEvent *py_ev)
{
// py_ev->device
py_ev->attr_names = bopy::object(ev->attr_names);
PyDeviceAttribute::AutoDevAttrVector dev_attr_vec(ev->argout);
py_ev->argout = PyDeviceAttribute::convert_to_python(dev_attr_vec, *ev->device, _self->m_extract_as);
py_ev->err = bopy::object(ev->err);
py_ev->errors = bopy::object(ev->errors);
// py_ev->ext = bopy::object(ev->ext);
}
static void copy_most_fields(PyCallBackAutoDie *_self, const Tango::AttrWrittenEvent *ev, PyAttrWrittenEvent *py_ev)
{
// py_ev->device
py_ev->attr_names = bopy::object(ev->attr_names);
py_ev->err = bopy::object(ev->err);
py_ev->errors = bopy::object(ev->errors);
// py_ev->ext = bopy::object(ev->ext);
}
/*static*/ bopy::object PyCallBackAutoDie::py_on_callback_parent_fades;
/*static*/ std::map<PyObject *, PyObject *> PyCallBackAutoDie::s_weak2ob;
PyCallBackAutoDie::~PyCallBackAutoDie()
{
if(this->m_weak_parent)
{
PyCallBackAutoDie::s_weak2ob.erase(this->m_weak_parent);
bopy::xdecref(this->m_weak_parent);
}
}
/*static*/ void PyCallBackAutoDie::init()
{
bopy::object py_scope = bopy::scope();
bopy::def("__on_callback_parent_fades", on_callback_parent_fades);
PyCallBackAutoDie::py_on_callback_parent_fades = py_scope.attr("__on_callback_parent_fades");
}
void PyCallBackAutoDie::on_callback_parent_fades(PyObject *weakobj)
{
PyObject *ob = PyCallBackAutoDie::s_weak2ob[weakobj];
if(!ob)
{
return;
}
// while (ob->ob_refcnt)
bopy::xdecref(ob);
}
void PyCallBackAutoDie::set_autokill_references(bopy::object &py_self, bopy::object &py_parent)
{
if(m_self == 0)
{
m_self = py_self.ptr();
}
assert(m_self == py_self.ptr());
PyObject *recb = PyCallBackAutoDie::py_on_callback_parent_fades.ptr();
this->m_weak_parent = PyWeakref_NewRef(py_parent.ptr(), recb);
if(!this->m_weak_parent)
{
bopy::throw_error_already_set();
}
bopy::incref(this->m_self);
PyCallBackAutoDie::s_weak2ob[this->m_weak_parent] = py_self.ptr();
}
void PyCallBackAutoDie::unset_autokill_references()
{
bopy::decref(m_self);
}
template <typename OriginalT, typename CopyT>
static void _run_virtual_once(PyCallBackAutoDie *_self, OriginalT *ev, const char *virt_fn_name)
{
AutoPythonGIL gil;
try
{
CopyT *py_ev = new CopyT();
bopy::object py_value =
bopy::object(bopy::handle<>(bopy::to_python_indirect<CopyT *, bopy::detail::make_owning_holder>()(py_ev)));
// - py_ev->device = bopy::object(ev->device); No, we use m_weak_parent
// so we get exactly the same python bopy::object...
if(_self->m_weak_parent)
{
PyObject *parent = PyWeakref_GET_OBJECT(_self->m_weak_parent);
if(parent && parent != Py_None)
{
py_ev->device = bopy::object(bopy::handle<>(bopy::borrowed(parent)));
}
}
copy_most_fields(_self, ev, py_ev);
_self->get_override(virt_fn_name)(py_value);
}
SAFE_CATCH_INFORM(virt_fn_name)
_self->unset_autokill_references();
}
/*virtual*/ void PyCallBackAutoDie::cmd_ended(Tango::CmdDoneEvent *ev)
{
_run_virtual_once<Tango::CmdDoneEvent, PyCmdDoneEvent>(this, ev, "cmd_ended");
}
/*virtual*/ void PyCallBackAutoDie::attr_read(Tango::AttrReadEvent *ev)
{
_run_virtual_once<Tango::AttrReadEvent, PyAttrReadEvent>(this, ev, "attr_read");
}
/*virtual*/ void PyCallBackAutoDie::attr_written(Tango::AttrWrittenEvent *ev)
{
_run_virtual_once<Tango::AttrWrittenEvent, PyAttrWrittenEvent>(this, ev, "attr_written");
}
PyCallBackPushEvent::~PyCallBackPushEvent()
{
bopy::xdecref(this->m_weak_device);
}
void PyCallBackPushEvent::set_device(bopy::object &py_device)
{
this->m_weak_device = PyWeakref_NewRef(py_device.ptr(), 0);
if(!this->m_weak_device)
{
bopy::throw_error_already_set();
}
}
namespace
{
template <typename OriginalT>
void copy_device(OriginalT *ev, bopy::object py_ev, bopy::object py_device)
{
if(py_device.ptr() != Py_None)
{
py_ev.attr("device") = py_device;
}
else
{
py_ev.attr("device") = bopy::object(ev->device);
}
}
template <typename OriginalT>
static void _push_event(PyCallBackPushEvent *self, OriginalT *ev)
{
// If the event is received after python dies but before the process
// finishes then discard the event
if(!Py_IsInitialized())
{
TANGO_LOG_DEBUG << "Tango event (" << ev->event << ") received for after python shutdown. "
<< "Event will be ignored" << std::endl;
return;
}
AutoPythonGIL gil;
// Make a copy of ev in python
// (the original will be deleted by TangoC++ on return)
bopy::object py_ev(ev);
OriginalT *ev_copy = bopy::extract<OriginalT *>(py_ev);
// If possible, reuse the original DeviceProxy
bopy::object py_device;
if(self->m_weak_device)
{
PyObject *py_c_device = PyWeakref_GET_OBJECT(self->m_weak_device);
if(py_c_device && py_c_device != Py_None)
{
py_device = bopy::object(bopy::handle<>(bopy::borrowed(py_c_device)));
}
}
try
{
PyCallBackPushEvent::fill_py_event(ev_copy, py_ev, py_device, self->m_extract_as);
}
SAFE_CATCH_REPORT("PyCallBackPushEvent::fill_py_event")
try
{
self->get_override("push_event")(py_ev);
}
SAFE_CATCH_INFORM("push_event")
}
} // namespace
bopy::object PyCallBackPushEvent::get_override(const char *name)
{
return bopy::wrapper<Tango::CallBack>::get_override(name);
}
void PyCallBackPushEvent::fill_py_event(Tango::EventData *ev,
bopy::object &py_ev,
bopy::object py_device,
PyTango::ExtractAs extract_as)
{
copy_device(ev, py_ev, py_device);
/// @todo on error extracting, we could save the error in DeviceData
/// instead of throwing it...?
// Save a copy of attr_value, so we can still access it after
// the execution of the callback (Tango will delete the original!)
// I originally was 'stealing' the reference to TangoC++: I got
// attr_value and set it to 0... But now TangoC++ is not deleting
// attr_value pointer but its own copy, so my efforts are useless.
if(ev->attr_value)
{
Tango::DeviceAttribute *attr = new Tango::DeviceAttribute;
(*attr) = std::move(*ev->attr_value);
py_ev.attr("attr_value") = PyDeviceAttribute::convert_to_python(attr, *ev->device, extract_as);
}
// ev->attr_value = 0; // Do not delete, python will.
}
void PyCallBackPushEvent::fill_py_event(Tango::AttrConfEventData *ev,
bopy::object &py_ev,
bopy::object py_device,
PyTango::ExtractAs extract_as)
{
copy_device(ev, py_ev, py_device);
if(ev->attr_conf)
{
py_ev.attr("attr_conf") = *ev->attr_conf;
}
}
void PyCallBackPushEvent::fill_py_event(Tango::DataReadyEventData *ev,
bopy::object &py_ev,
bopy::object py_device,
PyTango::ExtractAs extract_as)
{
copy_device(ev, py_ev, py_device);
}
void PyCallBackPushEvent::fill_py_event(Tango::PipeEventData *ev,
bopy::object &py_ev,
bopy::object py_device,
PyTango::ExtractAs extract_as)
{
copy_device(ev, py_ev, py_device);
if(ev->pipe_value)
{
Tango::DevicePipe *pipe_value = new Tango::DevicePipe;
(*pipe_value) = std::move(*ev->pipe_value);
py_ev.attr("pipe_value") = PyTango::DevicePipe::convert_to_python(pipe_value, extract_as);
}
}
void PyCallBackPushEvent::fill_py_event(Tango::DevIntrChangeEventData *ev,
bopy::object &py_ev,
bopy::object py_device,
PyTango::ExtractAs extract_as)
{
copy_device(ev, py_ev, py_device);
py_ev.attr("cmd_list") = ev->cmd_list;
py_ev.attr("att_list") = ev->att_list;
}
/*virtual*/ void PyCallBackPushEvent::push_event(Tango::EventData *ev)
{
_push_event(this, ev);
}
/*virtual*/ void PyCallBackPushEvent::push_event(Tango::AttrConfEventData *ev)
{
_push_event(this, ev);
}
/*virtual*/ void PyCallBackPushEvent::push_event(Tango::DataReadyEventData *ev)
{
_push_event(this, ev);
}
/*virtual*/ void PyCallBackPushEvent::push_event(Tango::PipeEventData *ev)
{
_push_event(this, ev);
}
/*virtual*/ void PyCallBackPushEvent::push_event(Tango::DevIntrChangeEventData *ev)
{
_push_event(this, ev);
}
void export_callback()
{
PyCallBackAutoDie::init();
/// @todo move somewhere else, another file i tal...
bopy::class_<PyCmdDoneEvent> CmdDoneEvent("CmdDoneEvent", bopy::no_init);
CmdDoneEvent.def_readonly("device", &PyCmdDoneEvent::device)
.def_readonly("cmd_name", &PyCmdDoneEvent::cmd_name)
.def_readonly("argout_raw", &PyCmdDoneEvent::argout_raw)
.def_readonly("err", &PyCmdDoneEvent::err)
.def_readonly("errors", &PyCmdDoneEvent::errors)
.def_readonly("ext", &PyCmdDoneEvent::ext)
.def_readwrite("argout", &PyCmdDoneEvent::argout);
bopy::class_<PyAttrReadEvent> AttrReadEvent("AttrReadEvent", bopy::no_init);
AttrReadEvent.def_readonly("device", &PyAttrReadEvent::device)
.def_readonly("attr_names", &PyAttrReadEvent::attr_names)
.def_readonly("argout", &PyAttrReadEvent::argout)
.def_readonly("err", &PyAttrReadEvent::err)
.def_readonly("errors", &PyAttrReadEvent::errors)
.def_readonly("ext", &PyAttrReadEvent::ext);
bopy::class_<PyAttrWrittenEvent> AttrWrittenEvent("AttrWrittenEvent", bopy::no_init);
AttrWrittenEvent.def_readonly("device", &PyAttrWrittenEvent::device)
.def_readonly("attr_names", &PyAttrWrittenEvent::attr_names)
.def_readonly("err", &PyAttrWrittenEvent::err)
.def_readonly("errors", &PyAttrWrittenEvent::errors)
.def_readonly("ext", &PyAttrWrittenEvent::ext);
bopy::class_<PyCallBackAutoDie, boost::noncopyable> CallBackAutoDie(
"__CallBackAutoDie", "INTERNAL CLASS - DO NOT USE IT", bopy::init<>());
CallBackAutoDie
.def("cmd_ended",
&PyCallBackAutoDie::cmd_ended,
"This method is defined as being empty and must be overloaded by the user when the asynchronous callback "
"model is used. This is the method which will be executed when the server reply from a command_inout is "
"received in both push and pull sub-mode.")
.def("attr_read",
&PyCallBackAutoDie::attr_read,
"This method is defined as being empty and must be overloaded by the user when the asynchronous callback "
"model is used. This is the method which will be executed when the server reply from a read_attribute(s) "
"is received in both push and pull sub-mode.")
.def("attr_written",
&PyCallBackAutoDie::attr_written,
"This method is defined as being empty and must be overloaded by the user when the asynchronous callback "
"model is used. This is the method which will be executed when the server reply from a write_attribute(s) "
"is received in both push and pull sub-mode. ");
bopy::class_<PyCallBackPushEvent, boost::noncopyable> CallBackPushEvent(
"__CallBackPushEvent", "INTERNAL CLASS - DO NOT USE IT", bopy::init<>());
CallBackPushEvent
.def("push_event",
(void(PyCallBackAutoDie::*)(Tango::EventData *)) & PyCallBackAutoDie::push_event,
"This method is defined as being empty and must be overloaded by the user when events are used. This is "
"the method which will be executed when the server send event(s) to the client. ")
.def("push_event",
(void(PyCallBackAutoDie::*)(Tango::AttrConfEventData *)) & PyCallBackAutoDie::push_event,
"This method is defined as being empty and must be overloaded by the user when events are used. This is "
"the method which will be executed when the server send attribute configuration change event(s) to the "
"client. ")
.def("push_event",
(void(PyCallBackAutoDie::*)(Tango::DataReadyEventData *)) & PyCallBackAutoDie::push_event,
"This method is defined as being empty and must be overloaded by the user when events are used. This is "
"the method which will be executed when the server send attribute data ready event(s) to the client. ")
.def("push_event",
(void(PyCallBackAutoDie::*)(Tango::PipeEventData *)) & PyCallBackAutoDie::push_event,
"This method is defined as being empty and must be overloaded by the user when events are used. This is "
"the method which will be executed when the server send pipe event(s) to the client. ")
.def("push_event",
(void(PyCallBackAutoDie::*)(Tango::DevIntrChangeEventData *)) & PyCallBackAutoDie::push_event,
"This method is defined as being empty and must be overloaded by the user when events are used. This is "
"the method which will be executed when the server send device interface change event(s) to the client. ");
}
|