File: pyutils.h

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 (230 lines) | stat: -rw-r--r-- 6,910 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
/*
 * SPDX-FileCopyrightText: All Contributors to the PyTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#pragma once

#include <boost/python.hpp>
#include <tango/tango.h>
#include <omnithread.h>

#define arg_(a) bopy::arg(a)

inline void raise_(PyObject *type, const char *message)
{
    PyErr_SetString(type, message);
    bopy::throw_error_already_set();
}

inline PyObject *EncodeAsLatin1(PyObject *in)
{
    PyObject *bytes_out = PyUnicode_AsLatin1String(in);
    if(!bytes_out)
    {
        PyObject *bytes_replaced = PyUnicode_AsEncodedString(in, "latin-1", "replace");
        const char *string_replaced = PyBytes_AsString(bytes_replaced);
        std::string err_msg = "Can't encode ";
        if(!string_replaced)
        {
            err_msg += "unknown Unicode string as Latin-1";
        }
        else
        {
            err_msg += "'";
            err_msg += string_replaced;
            err_msg += "' Unicode string as Latin-1 (bad chars replaced with ?)";
        }
        Py_XDECREF(bytes_replaced);
        raise_(PyExc_UnicodeError, err_msg.c_str());
    }

    return bytes_out;
}

inline PyObject *PyObject_GetAttrString_(PyObject *o, const std::string &attr_name)
{
    const char *attr = attr_name.c_str();
    return PyObject_GetAttrString(o, attr);
}

inline PyObject *PyImport_ImportModule_(const std::string &name)
{
    const char *attr = name.c_str();
    return PyImport_ImportModule(attr);
}

// Bytes interface
#include <bytesobject.h>

PyObject *from_char_to_python_str(const char *in,
                                  Py_ssize_t size = -1,
                                  const char *encoding = NULL, /* defaults to latin-1 */
                                  const char *errors = "strict");

PyObject *from_char_to_python_str(const std::string &in,
                                  const char *encoding = NULL, /* defaults to latin-1 */
                                  const char *errors = "strict");

bopy::object from_char_to_boost_str(const char *in,
                                    Py_ssize_t size = -1,
                                    const char *encoding = NULL, /* defaults to latin-1 */
                                    const char *errors = "strict");

bopy::object from_char_to_boost_str(const std::string &in,
                                    const char *encoding = NULL, /* defaults to latin-1 */
                                    const char *errors = "strict");

void throw_bad_type(const char *type, const char *source);

char *from_str_to_char(PyObject *in, Py_ssize_t *size_out, const bool utf_encoding = false /* defaults to latin-1 */);
char *from_str_to_char(const bopy::object &in,
                       Py_ssize_t *size_out,
                       const bool utf_encoding = false /* defaults to latin-1 */);
char *from_str_to_char(PyObject *in);
char *from_str_to_char(const bopy::object &in);

void view_pybytes_as_char_array(const bopy::object &py_value, Tango::DevVarCharArray &out_array);

/// You should run any I/O intensive operations (like requesting data through
/// the network) in the context of an object like this.
class AutoPythonAllowThreads
{
    PyThreadState *m_save;

  public:
    inline void giveup()
    {
        if(m_save)
        {
            PyEval_RestoreThread(m_save);
            m_save = 0;
        }
    }

    inline AutoPythonAllowThreads()
    {
        m_save = PyEval_SaveThread();
    }

    inline ~AutoPythonAllowThreads()
    {
        giveup();
    }
};

// Delete a pointer for a CppTango class with Python GIL released.
// Typically used by boost::shared_ptr constructors as the function
// to call when the object is deleted.
struct DeleterWithoutGIL
{
    template <typename T>
    void operator()(T *ptr)
    {
        AutoPythonAllowThreads guard;
        delete ptr;
    }
};

/// The following class ensures usage in a non-omniORB thread will
/// still get a dummy omniORB thread ID - cppTango requires threads to
/// be identifiable in this way.  It should only be acquired once for the
/// lifetime of the thread, and must be released before the thread is
/// cleaned up.
/// See https://github.com/tango-controls/pytango/issues/307
class EnsureOmniThread
{
    omni_thread::ensure_self *ensure_self;

  public:
    inline EnsureOmniThread()
    {
        ensure_self = NULL;
    }

    inline void acquire()
    {
        if(ensure_self == NULL)
        {
            ensure_self = new omni_thread::ensure_self;
        }
    }

    inline void release()
    {
        if(ensure_self != NULL)
        {
            delete ensure_self;
            ensure_self = NULL;
        }
    }

    inline ~EnsureOmniThread()
    {
        release();
    }
};

/**
 * Determines if the calling thread is (or looks like) an omniORB thread.
 *
 * @return returns true if the calling thread has an omniORB thread ID or false otherwise
 */
inline bool is_omni_thread()
{
    omni_thread *thread_id = omni_thread::self();
    return (thread_id != NULL);
}

/**
 * Determines if the given method name exists and is callable
 * within the python class
 *
 * @param[in] obj object to search for the method
 * @param[in] method_name the name of the method
 *
 * @return returns true is the method exists or false otherwise
 */
bool is_method_defined(bopy::object &obj, const std::string &method_name);

/**
 * Determines if the given method name exists and is callable
 * within the python class
 *
 * @param[in] obj object to search for the method
 * @param[in] method_name the name of the method
 *
 * @return returns true is the method exists or false otherwise
 */
bool is_method_defined(PyObject *obj, const std::string &method_name);

/**
 * Determines if the given method name exists and is callable
 * within the python class
 *
 * @param[in] obj object to search for the method
 * @param[in] method_name the name of the method
 * @param[out] exists set to true if the symbol exists or false otherwise
 * @param[out] is_method set to true if the symbol exists and is a method
 *             or false otherwise
 */
void is_method_defined(PyObject *obj, const std::string &method_name, bool &exists, bool &is_method);

/**
 * Determines if the given method name exists and is callable
 * within the python class
 *
 * @param[in] obj object to search for the method
 * @param[in] method_name the name of the method
 * @param[out] exists set to true if the symbol exists or false otherwise
 * @param[out] is_method set to true if the symbol exists and is a method
 *             or false otherwise
 */
void is_method_defined(bopy::object &obj, const std::string &method_name, bool &exists, bool &is_method);

#define PYTANGO_MOD bopy::object pytango((bopy::handle<>(bopy::borrowed(PyImport_AddModule("tango")))));

#define CALL_METHOD(retType, self, name, ...) bopy::call_method<retType>(self, name, __VA_ARGS__);

bool hasattr(bopy::object &, const std::string &);