-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpython_common.cpp
More file actions
177 lines (133 loc) · 4.9 KB
/
python_common.cpp
File metadata and controls
177 lines (133 loc) · 4.9 KB
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
#include "python_common.h"
namespace TbApiImpl {
namespace Python {
bool getStringValue(PyObject *field_value, std::string &ret_value, bool &type_mismatch) {
type_mismatch = false;
if (field_value == Py_None || field_value == NULL)
return false;
#if PY_MAJOR_VERSION >= 3
if (!PyUnicode_Check(field_value)) {
#else
if (!PyString_Check(field_value)) {
#endif
type_mismatch = true;
return false;
}
#if PY_MAJOR_VERSION >= 3
PyObject* str_object = PyUnicode_AsUTF8String(field_value); // TODO: check UTF8?
ret_value = PyBytes_AsString(str_object);
Py_XDECREF(str_object);
#else
ret_value = PyString_AsString(field_value); // TODO: check UTF8?
#endif
return true;
}
bool getInt32Value(PyObject *field_value, int32_t &ret_value, bool &type_mismatch) {
type_mismatch = false;
if (field_value == Py_None || field_value == NULL)
return false;
ret_value = PyLong_AsLong(field_value);
if (ret_value == -1 && PyErr_Occurred()) {
type_mismatch = true;
return false;
}
return true;
}
bool getInt64Value(PyObject *field_value, int64_t &ret_value, bool &type_mismatch) {
type_mismatch = false;
if (field_value == Py_None || field_value == NULL)
return false;
ret_value = PyLong_AsLongLong(field_value);
if (ret_value == -1 && PyErr_Occurred()) {
type_mismatch = true;
return false;
}
return true;
}
bool getDoubleValue(PyObject *field_value, double &ret_value, bool &type_mismatch) {
type_mismatch = false;
if (field_value == Py_None || field_value == NULL)
return false;
ret_value = PyFloat_AsDouble(field_value);
if (ret_value == -1.0 && PyErr_Occurred()) {
type_mismatch = true;
return false;
}
return true;
}
bool getBooleanValue(PyObject *field_value, bool &ret_value) {
if (field_value == Py_None || field_value == NULL)
return false;
if (!PyBool_Check(field_value))
return false;
ret_value = PyObject_Not(field_value) ? false : true;
return true;
}
bool getStringValue(PyObject *message, const std::string &field_name, PyObject *field_key, std::string &ret_value) {
if (message == NULL)
return false;
if (!PyObject_HasAttr(message, field_key))
return false;
PythonRefHolder object(PyObject_GetAttr(message, field_key));
if (object.getReference() == Py_None)
return false;
bool type_mismatch = false;
bool not_null = getStringValue(object.getReference(), ret_value, type_mismatch);
if (type_mismatch)
THROW_EXCEPTION("Wrong type of field '%s'. Required: STRING.", field_name.c_str());
return not_null;
}
bool getInt32Value(PyObject *message, const std::string &field_name, PyObject *field_key, int32_t &ret_value) {
if (message == NULL)
return false;
if (!PyObject_HasAttr(message, field_key))
return false;
PythonRefHolder object(PyObject_GetAttr(message, field_key));
if (object.getReference() == Py_None)
return false;
bool type_mismatch = false;
bool not_null = getInt32Value(object.getReference(), ret_value, type_mismatch);
if (type_mismatch)
THROW_EXCEPTION("Wrong type of field '%s'. Required: INTEGER.", field_name.c_str());
return not_null;
}
bool getInt64Value(PyObject *message, const std::string &field_name, PyObject *field_key, int64_t &ret_value) {
if (message == NULL)
return false;
if (!PyObject_HasAttr(message, field_key))
return false;
PythonRefHolder object(PyObject_GetAttr(message, field_key));
if (object.getReference() == Py_None)
return false;
bool type_mismatch = false;
bool not_null = getInt64Value(object.getReference(), ret_value, type_mismatch);
if (type_mismatch)
THROW_EXCEPTION("Wrong type of field '%s'. Required: INTEGER.", field_name.c_str());
return not_null;
}
bool getDoubleValue(PyObject *message, const std::string &field_name, PyObject *field_key, double &ret_value) {
if (message == NULL)
return false;
if (!PyObject_HasAttr(message, field_key))
return false;
PythonRefHolder object(PyObject_GetAttr(message, field_key));
if (object.getReference() == Py_None)
return false;
bool type_mismatch = false;
bool not_null = getDoubleValue(object.getReference(), ret_value, type_mismatch);
if (type_mismatch)
THROW_EXCEPTION("Wrong type of field '%s'. Required: FLOAT.", field_name.c_str());
return not_null;
}
bool getBooleanValue(PyObject *message, const std::string &field_name, PyObject *field_key, bool &ret_value) {
if (message == NULL)
return false;
if (!PyObject_HasAttr(message, field_key))
return false;
PythonRefHolder object(PyObject_GetAttr(message, field_key));
if (object.getReference() == Py_None)
return false;
return getBooleanValue(object.getReference(), ret_value);
}
}
}