File: device_proxy.cpp

package info (click to toggle)
pytango 10.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,216 kB
  • sloc: python: 28,206; cpp: 16,380; sql: 255; sh: 82; makefile: 43
file content (1067 lines) | stat: -rw-r--r-- 39,393 bytes parent folder | download | duplicates (3)
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
/*
 * SPDX-FileCopyrightText: All Contributors to the PyTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#include "precompiled_header.hpp"
#include "device_attribute.h"
#include "device_pipe.h"
#include "callback.h"
#include "defs.h"
#include "pytgutils.h"
#include <boost/python/stl_iterator.hpp>

#include "server/pipe.h"
#include "fast_from_py.h"

extern const char *param_must_be_seq;
extern const char *unreachable_code;
extern const char *non_string_seq;

BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(lock_overloads, Tango::DeviceProxy::lock, 0, 1);
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(unlock_overloads, Tango::DeviceProxy::unlock, 0, 1);

namespace PyDeviceProxy
{
struct PickleSuite : bopy::pickle_suite
{
    static bopy::tuple getinitargs(Tango::DeviceProxy &self)
    {
        std::string ret = self.get_db_host() + ":" + self.get_db_port() + "/" + self.dev_name();
        return bopy::make_tuple(ret);
    }
};

static inline Tango::DevState state(Tango::DeviceProxy &self)
{
    AutoPythonAllowThreads guard;
    return self.state();
}

static inline std::string status(Tango::DeviceProxy &self)
{
    AutoPythonAllowThreads guard;
    return self.status();
}

static inline int ping(Tango::DeviceProxy &self)
{
    AutoPythonAllowThreads guard;
    return self.ping();
}

static inline void
    pylist_to_devattrs(Tango::DeviceProxy &self, bopy::object &py_list, std::vector<Tango::DeviceAttribute> &dev_attrs)
{
    std::vector<std::string> attr_names;
    std::vector<bopy::object> py_values;
    Py_ssize_t size = len(py_list);

    // Fill attr_names and py_values
    for(Py_ssize_t n = 0; n < size; ++n)
    {
        bopy::object tup = py_list[n];
        std::string attr_name = bopy::extract<std::string>(tup[0]);
        attr_names.push_back(attr_name);
        py_values.push_back(tup[1]);
    }

    // Get attr_info for all the attr_names
    unique_pointer<Tango::AttributeInfoListEx> attr_infos;
    {
        AutoPythonAllowThreads guard;
        attr_infos.reset(self.get_attribute_config_ex(attr_names));
    }

    // Now prepare dev_attrs with attr_infos and py_values
    dev_attrs.resize(size);
    for(long n = 0; n < size; ++n)
    {
        PyDeviceAttribute::reset(dev_attrs[n], (*attr_infos)[n], py_values[n]);
    }
}

Tango::DevicePipe read_pipe(Tango::DeviceProxy &self, const std::string &pipe_name)
{
    AutoPythonAllowThreads guard;
    return self.read_pipe(pipe_name);
}

//------------------------- copy from pipe.cpp -------------------------------------------------------
static void throw_wrong_python_data_type(const std::string &name, const char *origin)
{
    TangoSys_OMemStream o;
    o << "Wrong Python type for pipe " << name << std::ends;
    Tango::Except::throw_exception("PyDs_WrongPythonDataTypeForPipe", o.str(), origin);
}

template <typename T>
void append_scalar_encoded(T &obj, const std::string &name, bopy::object &py_value)
{
    bopy::object p0 = py_value[0];
    bopy::object p1 = py_value[1];

    const char *encoded_format = bopy::extract<const char *>(p0.ptr());

    PyObject *data_ptr = p1.ptr();
    Py_buffer view;

    if(PyObject_GetBuffer(data_ptr, &view, PyBUF_FULL_RO) < 0)
    {
        throw_wrong_python_data_type(obj.get_name(), "append_scalar_encoded");
    }
    CORBA::ULong nb = static_cast<CORBA::ULong>(view.len);
    Tango::DevVarCharArray arr(nb, nb, (CORBA::Octet *) view.buf, false);
    Tango::DevEncoded value;
    value.encoded_format = CORBA::string_dup(encoded_format);
    value.encoded_data = arr;
    obj << value;
    PyBuffer_Release(&view);
}

template <typename T, long tangoTypeConst>
void __append_scalar(T &obj, const std::string &name, bopy::object &py_value)
{
    typedef typename TANGO_const2type(tangoTypeConst) TangoScalarType;
    TangoScalarType tg_value;
    from_py<tangoTypeConst>::convert(py_value, tg_value);
    Tango::DataElement<TangoScalarType> data_elt(name, tg_value);
    obj << data_elt;
}

template <long tangoTypeConst>
void append_scalar(Tango::DevicePipe &pipe, const std::string &name, bopy::object &py_value)
{
    __append_scalar<Tango::DevicePipe, tangoTypeConst>(pipe, name, py_value);
}

template <>
void append_scalar<Tango::DEV_VOID>(Tango::DevicePipe &pipe, const std::string &name, bopy::object &py_value)
{
    throw_wrong_python_data_type(pipe.get_name(), "append_scalar");
}

template <>
void append_scalar<Tango::DEV_PIPE_BLOB>(Tango::DevicePipe &pipe, const std::string &name, bopy::object &py_value)
{
    throw_wrong_python_data_type(pipe.get_name(), "append_scalar");
}

template <>
void append_scalar<Tango::DEV_ENCODED>(Tango::DevicePipe &pipe, const std::string &name, bopy::object &py_value)
{
    append_scalar_encoded<Tango::DevicePipe>(pipe, name, py_value);
}

template <long tangoTypeConst>
void append_scalar(Tango::DevicePipeBlob &blob, const std::string &name, bopy::object &py_value)
{
    __append_scalar<Tango::DevicePipeBlob, tangoTypeConst>(blob, name, py_value);
}

template <>
void append_scalar<Tango::DEV_VOID>(Tango::DevicePipeBlob &blob, const std::string &name, bopy::object &py_value)
{
    throw_wrong_python_data_type(blob.get_name(), "append_scalar");
}

template <>
void append_scalar<Tango::DEV_PIPE_BLOB>(Tango::DevicePipeBlob &blob, const std::string &name, bopy::object &py_value)
{
    throw_wrong_python_data_type(blob.get_name(), "append_scalar");
}

template <>
void append_scalar<Tango::DEV_ENCODED>(Tango::DevicePipeBlob &blob, const std::string &name, bopy::object &py_value)
{
    append_scalar_encoded<Tango::DevicePipeBlob>(blob, name, py_value);
}

// -------------
// Array version
// -------------

template <typename T, long tangoArrayTypeConst>
void __append_array(T &obj, const std::string &name, bopy::object &py_value)
{
    typedef typename TANGO_const2type(tangoArrayTypeConst) TangoArrayType;
    TangoArrayType *value = fast_convert2array<tangoArrayTypeConst>(py_value);
    obj << value;
}

template <long tangoArrayTypeConst>
void append_array(Tango::DevicePipe &pipe, const std::string &name, bopy::object &py_value)
{
    __append_array<Tango::DevicePipe, tangoArrayTypeConst>(pipe, name, py_value);
}

template <>
void append_array<Tango::DEV_VOID>(Tango::DevicePipe &pipe, const std::string &name, bopy::object &py_value)
{
    throw_wrong_python_data_type(pipe.get_name(), "append_array");
}

template <>
void append_array<Tango::DEV_PIPE_BLOB>(Tango::DevicePipe &pipe, const std::string &name, bopy::object &py_value)
{
    throw_wrong_python_data_type(pipe.get_name(), "append_array");
}

template <>
void append_array<Tango::DEVVAR_LONGSTRINGARRAY>(Tango::DevicePipe &pipe,
                                                 const std::string &name,
                                                 bopy::object &py_value)
{
    throw_wrong_python_data_type(pipe.get_name(), "append_array");
}

template <>
void append_array<Tango::DEVVAR_DOUBLESTRINGARRAY>(Tango::DevicePipe &pipe,
                                                   const std::string &name,
                                                   bopy::object &py_value)
{
    throw_wrong_python_data_type(pipe.get_name(), "append_array");
}

template <long tangoArrayTypeConst>
void append_array(Tango::DevicePipeBlob &blob, const std::string &name, bopy::object &py_value)
{
    __append_array<Tango::DevicePipeBlob, tangoArrayTypeConst>(blob, name, py_value);
}

template <>
void append_array<Tango::DEV_VOID>(Tango::DevicePipeBlob &blob, const std::string &name, bopy::object &py_value)
{
    throw_wrong_python_data_type(blob.get_name(), "append_array");
}

template <>
void append_array<Tango::DEV_PIPE_BLOB>(Tango::DevicePipeBlob &blob, const std::string &name, bopy::object &py_value)
{
    throw_wrong_python_data_type(blob.get_name(), "append_array");
}

template <>
void append_array<Tango::DEVVAR_LONGSTRINGARRAY>(Tango::DevicePipeBlob &blob,
                                                 const std::string &name,
                                                 bopy::object &py_value)
{
    throw_wrong_python_data_type(blob.get_name(), "append_array");
}

template <>
void append_array<Tango::DEVVAR_DOUBLESTRINGARRAY>(Tango::DevicePipeBlob &blob,
                                                   const std::string &name,
                                                   bopy::object &py_value)
{
    throw_wrong_python_data_type(blob.get_name(), "append_array");
}

template <typename T>
void __append(T &obj, const std::string &name, bopy::object &py_value, const Tango::CmdArgType dtype)
{
    TANGO_DO_ON_DEVICE_DATA_TYPE_ID(dtype, append_scalar<tangoTypeConst>(obj, name, py_value);
                                    , append_array<tangoTypeConst>(obj, name, py_value););
}

template <typename T>
void __set_value(T &obj, bopy::object &py_value)
{
    // need to fill item names first because in case it is a sub-blob,
    // the Tango C++ API doesnt't provide a way to do it
    Py_ssize_t n = bopy::len(py_value);
    std::vector<std::string> elem_names;
    for(Py_ssize_t i = 0; i < n; ++i)
    {
        std::string s = bopy::extract<std::string>(py_value[i]["name"]);
        elem_names.push_back(bopy::extract<std::string>(py_value[i]["name"]));
    }
    obj.set_data_elt_names(elem_names);

    for(Py_ssize_t i = 0; i < n; ++i)
    {
        bopy::object item = py_value[i];
        std::string item_name = bopy::extract<std::string>(item["name"]);
        bopy::object py_item_data = item["value"];
        Tango::CmdArgType item_dtype = bopy::extract<Tango::CmdArgType>(item["dtype"]);
        if(item_dtype == Tango::DEV_PIPE_BLOB) // a sub-blob
        {
            std::string blob_name = bopy::extract<std::string>(py_item_data[0]);
            bopy::object py_blob_data = py_item_data[1];
            Tango::DevicePipeBlob blob(blob_name);
            __set_value(blob, py_blob_data);
            obj << blob;
        }
        else
        {
            __append(obj, item_name, py_item_data, item_dtype);
        }
    }
}

void set_value(Tango::DevicePipe &pipe, bopy::object &py_value)
{
    __set_value<Tango::DevicePipe>(pipe, py_value);
}

//---------------------- end of copy from pipe.cpp -------------------------------------------------

static void write_pipe(Tango::DeviceProxy &self,
                       const std::string &pipe_name,
                       const std::string &root_blob_name,
                       bopy::object py_value)
{
    Tango::DevicePipe device_pipe(pipe_name, root_blob_name);
    set_value(device_pipe, py_value);
    // interface to c++ method
    AutoPythonAllowThreads guard;
    self.write_pipe(device_pipe);
}

static bopy::object
    read_attribute(Tango::DeviceProxy &self, const std::string &attr_name, PyTango::ExtractAs extract_as)
{
    // Even if there's an exception in convert_to_python, the
    // DeviceAttribute will be deleted there, so we don't need to worry.
    Tango::DeviceAttribute *dev_attr = 0;
    {
        AutoPythonAllowThreads guard;
        dev_attr = new Tango::DeviceAttribute(self.read_attribute(attr_name.c_str()));
    }
    return PyDeviceAttribute::convert_to_python(dev_attr, self, extract_as);
}

static inline bopy::object
    read_attributes(Tango::DeviceProxy &self, bopy::object py_attr_names, PyTango::ExtractAs extract_as)
{
    CSequenceFromPython<StdStringVector> attr_names(py_attr_names);

    PyDeviceAttribute::AutoDevAttrVector dev_attr_vec;
    {
        AutoPythonAllowThreads guard;
        dev_attr_vec.reset(self.read_attributes(*attr_names));
    }

    return PyDeviceAttribute::convert_to_python(dev_attr_vec, self, extract_as);
}

static inline void
    write_attribute(Tango::DeviceProxy &self, const Tango::AttributeInfo &attr_info, bopy::object py_value)
{
    Tango::DeviceAttribute da;
    PyDeviceAttribute::reset(da, attr_info, py_value);

    AutoPythonAllowThreads guard;
    self.write_attribute(da);
}

static inline void write_attribute(Tango::DeviceProxy &self, const std::string &attr_name, bopy::object py_value)
{
    Tango::DeviceAttribute dev_attr;
    PyDeviceAttribute::reset(dev_attr, attr_name, self, py_value);

    AutoPythonAllowThreads guard;
    self.write_attribute(dev_attr);
}

static inline void write_attributes(Tango::DeviceProxy &self, bopy::object py_list)
{
    std::vector<Tango::DeviceAttribute> dev_attrs;
    pylist_to_devattrs(self, py_list, dev_attrs);

    AutoPythonAllowThreads guard;
    self.write_attributes(dev_attrs);
}

static inline bopy::object write_read_attribute(Tango::DeviceProxy &self,
                                                const std::string &attr_name,
                                                bopy::object py_value,
                                                PyTango::ExtractAs extract_as)
{
    Tango::DeviceAttribute w_dev_attr;
    unique_pointer<Tango::DeviceAttribute> r_dev_attr;

    // Prepare dev_attr structure
    PyDeviceAttribute::reset(w_dev_attr, attr_name, self, py_value);

    // Do the actual write_read_attribute thing...
    {
        AutoPythonAllowThreads guard;
        Tango::DeviceAttribute da = self.write_read_attribute(w_dev_attr);
        r_dev_attr.reset(new Tango::DeviceAttribute(da));
    }

    // Convert the result back to python
    return PyDeviceAttribute::convert_to_python(r_dev_attr.release(), self, extract_as);
}

static bopy::object write_read_attributes(Tango::DeviceProxy &self,
                                          bopy::object py_name_val_list,
                                          bopy::object py_attr_names,
                                          PyTango::ExtractAs extract_as)
{
    // Convert write
    std::vector<Tango::DeviceAttribute> dev_attrs;
    pylist_to_devattrs(self, py_name_val_list, dev_attrs);

    // Convert read
    CSequenceFromPython<StdStringVector> attr_names(py_attr_names);
    PyDeviceAttribute::AutoDevAttrVector dev_attr_vec;

    // Do the actual write_read_attributes thing...
    {
        AutoPythonAllowThreads guard;
        dev_attr_vec.reset(self.write_read_attributes(dev_attrs, *attr_names));
    }

    // Convert the result back to python
    return PyDeviceAttribute::convert_to_python(dev_attr_vec, self, extract_as);
}

static bopy::object command_history(Tango::DeviceProxy &self, const std::string &cmd_name, int depth)
{
    std::vector<Tango::DeviceDataHistory> *device_data_hist = NULL;
    bopy::list ret;
    {
        AutoPythonAllowThreads guard;
        device_data_hist = self.command_history(const_cast<std::string &>(cmd_name), depth);
    }
    std::vector<Tango::DeviceDataHistory>::iterator it = device_data_hist->begin();
    for(; it != device_data_hist->end(); ++it)
    {
        Tango::DeviceDataHistory &hist = *it;
        ret.append(hist);
    }
    delete device_data_hist;
    return ret;
}

static inline bopy::object
    attribute_history(Tango::DeviceProxy &self, const std::string &attr_name, int depth, PyTango::ExtractAs extract_as)
{
    unique_pointer<std::vector<Tango::DeviceAttributeHistory>> att_hist;
    {
        AutoPythonAllowThreads guard;
        att_hist.reset(self.attribute_history(const_cast<std::string &>(attr_name), depth));
    }
    return PyDeviceAttribute::convert_to_python(att_hist, self, extract_as);
}

static inline long read_attributes_asynch(Tango::DeviceProxy &self, bopy::object py_attr_names)
{
    CSequenceFromPython<StdStringVector> attr_names(py_attr_names);

    AutoPythonAllowThreads guard;
    return self.read_attributes_asynch(*attr_names);
}

static inline bopy::object read_attributes_reply(Tango::DeviceProxy &self, long id, PyTango::ExtractAs extract_as)
{
    PyDeviceAttribute::AutoDevAttrVector dev_attr_vec;
    {
        AutoPythonAllowThreads guard;
        dev_attr_vec.reset(self.read_attributes_reply(id));
    }
    return PyDeviceAttribute::convert_to_python(dev_attr_vec, self, extract_as);
}

static inline bopy::object
    read_attributes_reply(Tango::DeviceProxy &self, long id, long timeout, PyTango::ExtractAs extract_as)
{
    PyDeviceAttribute::AutoDevAttrVector dev_attr_vec;
    {
        AutoPythonAllowThreads guard;
        dev_attr_vec.reset(self.read_attributes_reply(id, timeout));
    }
    return PyDeviceAttribute::convert_to_python(dev_attr_vec, self, extract_as);
}

static inline long write_attributes_asynch(Tango::DeviceProxy &self, bopy::object py_list)
{
    std::vector<Tango::DeviceAttribute> dev_attrs;
    pylist_to_devattrs(self, py_list, dev_attrs);

    AutoPythonAllowThreads guard;
    return self.write_attributes_asynch(dev_attrs);
}

static inline void write_attributes_reply(Tango::DeviceProxy &self, long id, long timestamp)
{
    AutoPythonAllowThreads guard;
    self.write_attributes_reply(id, timestamp);
}

static inline void write_attributes_reply(Tango::DeviceProxy &self, long id)
{
    AutoPythonAllowThreads guard;
    self.write_attributes_reply(id);
}

static inline void read_attributes_asynch(bopy::object py_self,
                                          bopy::object py_attr_names,
                                          bopy::object py_cb,
                                          PyTango::ExtractAs extract_as)
{
    Tango::DeviceProxy *self = bopy::extract<Tango::DeviceProxy *>(py_self);
    CSequenceFromPython<StdStringVector> attr_names(py_attr_names);

    PyCallBackAutoDie *cb = bopy::extract<PyCallBackAutoDie *>(py_cb);
    cb->set_autokill_references(py_cb, py_self);
    cb->set_extract_as(extract_as);

    try
    {
        AutoPythonAllowThreads guard;
        self->read_attributes_asynch(*attr_names, *cb);
    }
    catch(...)
    {
        cb->unset_autokill_references();
        throw;
    }
}

static inline void write_attributes_asynch(bopy::object py_self, bopy::object py_list, bopy::object py_cb)
{
    Tango::DeviceProxy *self = bopy::extract<Tango::DeviceProxy *>(py_self);
    std::vector<Tango::DeviceAttribute> dev_attrs;
    pylist_to_devattrs(*self, py_list, dev_attrs);

    PyCallBackAutoDie *cb = bopy::extract<PyCallBackAutoDie *>(py_cb);
    cb->set_autokill_references(py_cb, py_self);

    try
    {
        AutoPythonAllowThreads guard;
        self->write_attributes_asynch(dev_attrs, *cb);
    }
    catch(...)
    {
        cb->unset_autokill_references();
        throw;
    }
}

static int subscribe_event_global(bopy::object py_self, Tango::EventType event, bopy::object py_cb, bool stateless)
{
    Tango::DeviceProxy &self = bopy::extract<Tango::DeviceProxy &>(py_self);

    PyCallBackPushEvent *cb = 0;
    if(bopy::extract<PyCallBackPushEvent &>(py_cb).check())
    {
        cb = bopy::extract<PyCallBackPushEvent *>(py_cb);
        cb->set_device(py_self);

        AutoPythonAllowThreads guard;
        return self.subscribe_event(event, cb, stateless);
    }

    Tango::Except::throw_exception("PyDs_UnexpectedFailure", "Unexpected failure", "subscribe_event_global");
}

static int subscribe_event_attrib(bopy::object py_self,
                                  const std::string &attr_name,
                                  Tango::EventType event,
                                  bopy::object py_cb_or_queuesize,
                                  bopy::object &py_filters,
                                  bool stateless,
                                  PyTango::ExtractAs extract_as)
{
    Tango::DeviceProxy &self = bopy::extract<Tango::DeviceProxy &>(py_self);
    CSequenceFromPython<StdStringVector> filters(py_filters);

    PyCallBackPushEvent *cb = 0;
    int event_queue_size = 0;
    if(bopy::extract<PyCallBackPushEvent &>(py_cb_or_queuesize).check())
    {
        cb = bopy::extract<PyCallBackPushEvent *>(py_cb_or_queuesize);

        cb->set_device(py_self);
        cb->set_extract_as(extract_as);

        AutoPythonAllowThreads guard;
        return self.subscribe_event(attr_name, event, cb, *filters, stateless);
    }
    else
    {
        event_queue_size = bopy::extract<int>(py_cb_or_queuesize);
        AutoPythonAllowThreads guard;
        return self.subscribe_event(attr_name, event, event_queue_size, *filters, stateless);
    }
}

static void unsubscribe_event(Tango::DeviceProxy &self, int event)
{
    // If the callback is running, unsubscribe_event will lock
    // until it finishes. So we MUST release GIL to avoid a deadlock
    AutoPythonAllowThreads guard;
    self.unsubscribe_event(event);
}

template <typename ED, typename EDList>
static bopy::object
    get_events__aux(bopy::object py_self, int event_id, PyTango::ExtractAs extract_as = PyTango::ExtractAsNumpy)
{
    Tango::DeviceProxy &self = bopy::extract<Tango::DeviceProxy &>(py_self);

    EDList event_list;
    self.get_events(event_id, event_list);

    bopy::list r;

    for(size_t i = 0; i < event_list.size(); ++i)
    {
        ED *event_data = event_list[i];

        bopy::object py_ev(
            bopy::handle<>(bopy::to_python_indirect<ED *, bopy::detail::make_owning_holder>()(event_data)));

        // EventDataList deletes EventData's on destructor, so once
        // we are handling it somewhere else (as an bopy::object) we must
        // unset the reference
        event_list[i] = 0;

        PyCallBackPushEvent::fill_py_event(event_data, py_ev, py_self, extract_as);

        r.append(py_ev);
    }
    return r;
}

static void
    get_events__callback(bopy::object py_self, int event_id, PyCallBackPushEvent *cb, PyTango::ExtractAs extract_as)
{
    Tango::DeviceProxy &self = bopy::extract<Tango::DeviceProxy &>(py_self);

    cb->set_device(py_self);
    cb->set_extract_as(extract_as);

    self.get_events(event_id, cb);
}

static bopy::object get_events__attr_conf(bopy::object py_self, int event_id)
{
    return get_events__aux<Tango::AttrConfEventData, Tango::AttrConfEventDataList>(py_self, event_id);
}

static bopy::object get_events__data(bopy::object py_self, int event_id, PyTango::ExtractAs extract_as)
{
    return get_events__aux<Tango::EventData, Tango::EventDataList>(py_self, event_id, extract_as);
}

static bopy::object get_events__data_ready(bopy::object py_self, int event_id)
{
    return get_events__aux<Tango::DataReadyEventData, Tango::DataReadyEventDataList>(py_self, event_id);
}

static bopy::object get_events__pipe_data(bopy::object py_self, int event_id, PyTango::ExtractAs extract_as)
{
    return get_events__aux<Tango::PipeEventData, Tango::PipeEventDataList>(py_self, event_id, extract_as);
}

static bopy::object get_events__devintr_change_data(bopy::object py_self, int event_id, PyTango::ExtractAs extract_as)
{
    return get_events__aux<Tango::DevIntrChangeEventData, Tango::DevIntrChangeEventDataList>(
        py_self, event_id, extract_as);
}

static boost::shared_ptr<Tango::DeviceProxy> makeDeviceProxy1(const std::string &name)
{
    AutoPythonAllowThreads guard;
    return boost::shared_ptr<Tango::DeviceProxy>(new Tango::DeviceProxy(name.c_str()), DeleterWithoutGIL());
}

static boost::shared_ptr<Tango::DeviceProxy> makeDeviceProxy2(const std::string &name, bool b)
{
    AutoPythonAllowThreads guard;
    return boost::shared_ptr<Tango::DeviceProxy>(new Tango::DeviceProxy(name.c_str(), b), DeleterWithoutGIL());
}
}; // namespace PyDeviceProxy

void export_device_proxy()
{
    bopy::class_<Tango::DeviceProxy, bopy::bases<Tango::Connection>> DeviceProxy("DeviceProxy", bopy::init<>());

    DeviceProxy.def(bopy::init<const char *>())
        .def(bopy::init<const char *, bool>())
        .def(bopy::init<const Tango::DeviceProxy &>())
        .def("__init__", bopy::make_constructor(PyDeviceProxy::makeDeviceProxy1))
        .def("__init__", bopy::make_constructor(PyDeviceProxy::makeDeviceProxy2))

        //
        // Pickle
        //
        .def_pickle(PyDeviceProxy::PickleSuite())

        //
        // general methods
        //
        .def("dev_name", &Tango::DeviceProxy::dev_name)

        .def("info", &Tango::DeviceProxy::info, (arg_("self")), bopy::return_internal_reference<1>())

        .def("get_device_db",
             &Tango::DeviceProxy::get_device_db,
             bopy::return_value_policy<bopy::reference_existing_object>())

        .def("_status", &PyDeviceProxy::status, (arg_("self")))

        .def("_state", &PyDeviceProxy::state, (arg_("self")))

        .def("adm_name", &Tango::DeviceProxy::adm_name, (arg_("self")))

        .def("description", &Tango::DeviceProxy::description, (arg_("self")))

        .def("name", &Tango::DeviceProxy::name, (arg_("self")))

        .def("alias", &Tango::DeviceProxy::alias, (arg_("self")))

        .def("get_tango_lib_version", &Tango::DeviceProxy::get_tango_lib_version, (arg_("self")))

        .def("_ping", &PyDeviceProxy::ping, (arg_("self")))

        .def("black_box",
             &Tango::DeviceProxy::black_box,
             (arg_("self"), arg_("n")),
             bopy::return_value_policy<bopy::manage_new_object>())

        //
        // command methods
        //
        .def("get_command_list",
             &Tango::DeviceProxy::get_command_list,
             (arg_("self")),
             bopy::return_value_policy<bopy::manage_new_object>())

        .def("_get_command_config",
             (Tango::CommandInfoList * (Tango::DeviceProxy::*) (StdStringVector &) ) &
                 Tango::DeviceProxy::get_command_config,
             (arg_("self"), arg_("attr_names")),
             bopy::return_value_policy<bopy::manage_new_object>())

        .def("_get_command_config",
             (Tango::CommandInfo(Tango::DeviceProxy::*)(const std::string &)) & Tango::DeviceProxy::get_command_config,
             (arg_("self"), arg_("attr_name")))

        .def("command_query", &Tango::DeviceProxy::command_query, (arg_("self"), arg_("command")))

        .def("command_list_query",
             &Tango::DeviceProxy::command_list_query,
             (arg_("self")),
             bopy::return_value_policy<bopy::manage_new_object>())

        .def("import_info", &Tango::DeviceProxy::import_info, (arg_("self")))

        //
        // property methods
        //
        .def("_get_property",
             (void(Tango::DeviceProxy::*)(const std::string &, Tango::DbData &)) & Tango::DeviceProxy::get_property,
             (arg_("self"), arg_("propname"), arg_("propdata")))

        .def("_get_property",
             (void(Tango::DeviceProxy::*)(const std::vector<std::string> &, Tango::DbData &)) &
                 Tango::DeviceProxy::get_property,
             (arg_("self"), arg_("propnames"), arg_("propdata")))

        .def("_get_property",
             (void(Tango::DeviceProxy::*)(Tango::DbData &)) & Tango::DeviceProxy::get_property,
             (arg_("self"), arg_("propdata")))

        .def("_put_property", &Tango::DeviceProxy::put_property, (arg_("self"), arg_("propdata")))

        .def("_delete_property",
             (void(Tango::DeviceProxy::*)(const std::string &)) & Tango::DeviceProxy::delete_property,
             (arg_("self"), arg_("propname")))

        .def("_delete_property",
             (void(Tango::DeviceProxy::*)(const StdStringVector &)) & Tango::DeviceProxy::delete_property,
             (arg_("self"), arg_("propnames")))

        .def("_delete_property",
             (void(Tango::DeviceProxy::*)(const Tango::DbData &)) & Tango::DeviceProxy::delete_property,
             (arg_("self"), arg_("propdata")))

        .def(
            "_get_property_list", &Tango::DeviceProxy::get_property_list, (arg_("self"), arg_("filter"), arg_("array")))

        //
        // pipe methods
        //

        .def("_get_pipe_list",
             &Tango::DeviceProxy::get_pipe_list,
             (arg_("self")),
             bopy::return_value_policy<bopy::manage_new_object>())

        .def("_get_pipe_config",
             (Tango::PipeInfoList * (Tango::DeviceProxy::*) (const StdStringVector &) ) &
                 Tango::DeviceProxy::get_pipe_config,
             bopy::return_value_policy<bopy::manage_new_object>())

        .def("_get_pipe_config",
             (Tango::PipeInfo(Tango::DeviceProxy::*)(const std::string &)) & Tango::DeviceProxy::get_pipe_config,
             (arg_("self"), arg_("pipe_name")))

        .def("_set_pipe_config",
             (void(Tango::DeviceProxy::*)(Tango::PipeInfoList &)) & Tango::DeviceProxy::set_pipe_config,
             (arg_("self"), arg_("seq")))

        //
        // this should define the c++ signature    Tango::DevicePipe read_pipe(Tango::DeviceProxy& self, const
        // std::string & pipe_name)
        //
        .def("__read_pipe", &PyDeviceProxy::read_pipe, (arg_("self"), arg_("pipe_name")))
        //
        // this should define the c++ signature   write_pipe(Tango::DeviceProxy& self, Tango::DevicePipe& pipe_data)
        //
        .def("__write_pipe", &PyDeviceProxy::write_pipe, (arg_("self"), arg_("pipe_data")))

        //
        // attribute methods
        //

        .def("get_attribute_list",
             &Tango::DeviceProxy::get_attribute_list,
             (arg_("self")),
             bopy::return_value_policy<bopy::manage_new_object>())

        .def("_get_attribute_config",
             (Tango::AttributeInfoList * (Tango::DeviceProxy::*) (const StdStringVector &) ) &
                 Tango::DeviceProxy::get_attribute_config,
             (arg_("self"), arg_("attr_names")),
             bopy::return_value_policy<bopy::manage_new_object>())

        .def("_get_attribute_config",
             (Tango::AttributeInfoEx(Tango::DeviceProxy::*)(const std::string &)) &
                 Tango::DeviceProxy::get_attribute_config,
             (arg_("self"), arg_("attr_name")))

        .def("_get_attribute_config_ex",
             &Tango::DeviceProxy::get_attribute_config_ex,
             (arg_("self"), arg_("attr_names")),
             bopy::return_value_policy<bopy::manage_new_object>())

        .def("attribute_query", &Tango::DeviceProxy::attribute_query, (arg_("self"), arg_("attr_name")))

        .def("attribute_list_query",
             &Tango::DeviceProxy::attribute_list_query,
             (arg_("self")),
             bopy::return_value_policy<bopy::manage_new_object>())

        .def("attribute_list_query_ex",
             &Tango::DeviceProxy::attribute_list_query_ex,
             (arg_("self")),
             bopy::return_value_policy<bopy::manage_new_object>())

        .def("_set_attribute_config",
             (void(Tango::DeviceProxy::*)(const Tango::AttributeInfoList &)) & Tango::DeviceProxy::set_attribute_config,
             (arg_("self"), arg_("seq")))

        .def("_set_attribute_config",
             (void(Tango::DeviceProxy::*)(const Tango::AttributeInfoListEx &)) &
                 Tango::DeviceProxy::set_attribute_config,
             (arg_("self"), arg_("seq")))

        .def("_read_attribute",
             &PyDeviceProxy::read_attribute,
             (arg_("self"), arg_("attr_name"), arg_("extract_as") = PyTango::ExtractAsNumpy))

        .def("_read_attributes",
             &PyDeviceProxy::read_attributes,
             (arg_("self"), arg_("attr_names"), arg_("extract_as") = PyTango::ExtractAsNumpy))

        .def("_write_attribute",
             (void (*)(Tango::DeviceProxy &, const std::string &, bopy::object)) & PyDeviceProxy::write_attribute,
             (arg_("self"), arg_("attr_name"), arg_("value")))

        .def("_write_attribute",
             (void (*)(Tango::DeviceProxy &, const Tango::AttributeInfo &, bopy::object)) &
                 PyDeviceProxy::write_attribute,
             (arg_("self"), arg_("attr_info"), arg_("value")))

        .def("_write_attributes", &PyDeviceProxy::write_attributes, (arg_("self"), arg_("name_val")))

        .def("_write_read_attribute",
             &PyDeviceProxy::write_read_attribute,
             (arg_("self"), arg_("attr_name"), arg_("value"), arg_("extract_as") = PyTango::ExtractAsNumpy))

        .def("_write_read_attributes",
             &PyDeviceProxy::write_read_attributes,
             (arg_("self"), arg_("attr_in"), arg_("attr_read_names"), arg_("extract_as") = PyTango::ExtractAsNumpy))

        //
        // history methods
        //

        .def("command_history", &PyDeviceProxy::command_history, (arg_("self"), arg_("cmd_name"), arg_("depth")))

        .def("attribute_history",
             &PyDeviceProxy::attribute_history,
             (arg_("self"), arg_("attr_name"), arg_("depth"), arg_("extract_as") = PyTango::ExtractAsNumpy))

        //
        // Polling administration methods
        //

        .def("polling_status",
             &Tango::DeviceProxy::polling_status,
             (arg_("self")),
             bopy::return_value_policy<bopy::manage_new_object>())

        .def("poll_command",
             (void(Tango::DeviceProxy::*)(const char *, int)) & Tango::DeviceProxy::poll_command,
             (arg_("self"), arg_("cmd_name"), arg_("period")))

        .def("poll_attribute",
             (void(Tango::DeviceProxy::*)(const char *, int)) & Tango::DeviceProxy::poll_attribute,
             (arg_("self"), arg_("attr_name"), arg_("period")))

        .def("get_command_poll_period",
             (int(Tango::DeviceProxy::*)(const char *)) & Tango::DeviceProxy::get_command_poll_period,
             (arg_("self"), arg_("cmd_name")))

        .def("get_attribute_poll_period",
             (int(Tango::DeviceProxy::*)(const char *)) & Tango::DeviceProxy::get_attribute_poll_period,
             (arg_("self"), arg_("attr_name")))

        .def("is_command_polled",
             (bool(Tango::DeviceProxy::*)(const char *)) & Tango::DeviceProxy::is_command_polled,
             (arg_("self"), arg_("cmd_name")))

        .def("is_attribute_polled",
             (bool(Tango::DeviceProxy::*)(const char *)) & Tango::DeviceProxy::is_attribute_polled,
             (arg_("self"), arg_("attr_name")))

        .def("stop_poll_command",
             (void(Tango::DeviceProxy::*)(const char *)) & Tango::DeviceProxy::stop_poll_command,
             (arg_("self"), arg_("cmd_name")))

        .def("stop_poll_attribute",
             (void(Tango::DeviceProxy::*)(const char *)) & Tango::DeviceProxy::stop_poll_attribute,
             (arg_("self"), arg_("attr_name")))

        //
        // Asynchronous methods
        //
        .def("__read_attributes_asynch",
             (long (*)(Tango::DeviceProxy &, bopy::object)) & PyDeviceProxy::read_attributes_asynch,
             (arg_("self"), arg_("attr_names")))

        .def("__read_attributes_reply",
             (bopy::object(*)(Tango::DeviceProxy &, long, PyTango::ExtractAs)) & PyDeviceProxy::read_attributes_reply,
             (arg_("self"), arg_("id"), arg_("extract_as") = PyTango::ExtractAsNumpy))

        .def("__read_attributes_reply",
             (bopy::object(*)(Tango::DeviceProxy &, long, long, PyTango::ExtractAs)) &
                 PyDeviceProxy::read_attributes_reply,
             (arg_("self"), arg_("id"), arg_("timeout"), arg_("extract_as") = PyTango::ExtractAsNumpy))

        .def("pending_asynch_call", &Tango::DeviceProxy::pending_asynch_call, (arg_("self"), arg_("req_type")))

        .def("__write_attributes_asynch",
             (long (*)(Tango::DeviceProxy &, bopy::object)) & PyDeviceProxy::write_attributes_asynch,
             (arg_("self"), arg_("values")))

        .def("__write_attributes_reply",
             (void (*)(Tango::DeviceProxy &, long)) & PyDeviceProxy::write_attributes_reply,
             (arg_("self"), arg_("id")))

        .def("__write_attributes_reply",
             (void (*)(Tango::DeviceProxy &, long, long)) & PyDeviceProxy::write_attributes_reply,
             (arg_("self"), arg_("id"), arg_("timeout")))

        .def("__read_attributes_asynch",
             (void (*)(bopy::object, bopy::object, bopy::object, PyTango::ExtractAs)) &
                 PyDeviceProxy::read_attributes_asynch,
             (arg_("self"), arg_("attr_names"), arg_("callback"), arg_("extract_as") = PyTango::ExtractAsNumpy))

        .def("__write_attributes_asynch",
             (void (*)(bopy::object, bopy::object, bopy::object)) & PyDeviceProxy::write_attributes_asynch,
             (arg_("self"), arg_("values"), arg_("callback")))

        //
        // Logging administration methods
        //

        .def("add_logging_target",
             (void(Tango::DeviceProxy::*)(const std::string &)) & Tango::DeviceProxy::add_logging_target,
             (arg_("self"), arg_("target_type_target_name")))

        .def("remove_logging_target",
             (void(Tango::DeviceProxy::*)(const std::string &)) & Tango::DeviceProxy::remove_logging_target,
             (arg_("self"), arg_("target_type_target_name")))

        .def("get_logging_target", &Tango::DeviceProxy::get_logging_target, (arg_("self")))

        .def("get_logging_level", &Tango::DeviceProxy::get_logging_level, (arg_("self")))

        .def("set_logging_level", &Tango::DeviceProxy::set_logging_level, (arg_("self"), arg_("level")))

        //
        // Event methods
        //

        .def("__subscribe_event",
             &PyDeviceProxy::subscribe_event_global,
             (arg_("self"), arg_("event"), arg_("cb_or_queuesize")))

        .def("__subscribe_event",
             &PyDeviceProxy::subscribe_event_attrib,
             (arg_("self"),
              arg_("attr_name"),
              arg_("event"),
              arg_("cb_or_queuesize"),
              arg_("filters") = bopy::list(),
              arg_("stateless") = false,
              arg_("extract_as") = PyTango::ExtractAsNumpy))

        .def("__unsubscribe_event", &PyDeviceProxy::unsubscribe_event)

        .def("__get_callback_events",
             PyDeviceProxy::get_events__callback,
             (arg_("self"), arg_("event_id"), arg_("callback"), arg_("extract_as") = PyTango::ExtractAsNumpy))

        .def("__get_attr_conf_events", PyDeviceProxy::get_events__attr_conf, (arg_("self"), arg_("event_id")))

        .def("__get_data_events",
             PyDeviceProxy::get_events__data,
             (arg_("self"), arg_("event_id"), arg_("extract_as") = PyTango::ExtractAsNumpy))

        .def("__get_data_ready_events", PyDeviceProxy::get_events__data_ready, (arg_("self"), arg_("event_id")))

        .def("__get_pipe_events",
             PyDeviceProxy::get_events__pipe_data,
             (arg_("self"), arg_("event_id"), arg_("extract_as") = PyTango::ExtractAsNumpy))

        .def("__get_devintr_change_events",
             PyDeviceProxy::get_events__devintr_change_data,
             (arg_("self"), arg_("event_id")))

        // methods to access data in event queues
        .def("event_queue_size", &Tango::DeviceProxy::event_queue_size, (arg_("self"), arg_("event_id")))

        .def("get_last_event_date", &Tango::DeviceProxy::get_last_event_date, (arg_("self"), arg_("event_id")))

        .def("is_event_queue_empty", &Tango::DeviceProxy::is_event_queue_empty, (arg_("self"), arg_("event_id")))

        //
        // Locking methods
        //
        .def("lock", &Tango::DeviceProxy::lock, lock_overloads((arg_("lock_validity"))))

        .def("unlock", &Tango::DeviceProxy::unlock, unlock_overloads(arg_("force")))

        .def("locking_status", &Tango::DeviceProxy::locking_status, (arg_("self")))

        .def("is_locked", &Tango::DeviceProxy::is_locked, (arg_("self")))

        .def("is_locked_by_me", &Tango::DeviceProxy::is_locked_by_me, (arg_("self")))

        .def("get_locker", &Tango::DeviceProxy::get_locker, (arg_("self"), arg_("lockinfo")))

        /// This is to be used by the python layer of this api...
        //.setattr("__subscribed_events", bopy::dict())
        ;
}