blob: 34ec7f624e7c580d85f5d0e52078e3a0e5139eb1 [file] [log] [blame]
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +09001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#include <Python.h>
21#include "types.h"
22#include "binary.h"
23#include "compact.h"
Nobuaki Sukegawaf7a8d942016-03-01 01:41:47 +090024#include <limits>
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090025#include <stdint.h>
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090026
27// TODO(dreiss): defval appears to be unused. Look into removing it.
28// TODO(dreiss): Make parse_spec_args recursive, and cache the output
29// permanently in the object. (Malloc and orphan.)
30// TODO(dreiss): Why do we need cStringIO for reading, why not just char*?
31// Can cStringIO let us work with a BufferedTransport?
32// TODO(dreiss): Don't ignore the rv from cwrite (maybe).
33
34// Doing a benchmark shows that interning actually makes a difference, amazingly.
35
36/** Pointer to interned string to speed up attribute lookup. */
37PyObject* INTERN_STRING(TFrozenDict);
38PyObject* INTERN_STRING(cstringio_buf);
39PyObject* INTERN_STRING(cstringio_refill);
40static PyObject* INTERN_STRING(string_length_limit);
41static PyObject* INTERN_STRING(container_length_limit);
42static PyObject* INTERN_STRING(trans);
43
44namespace apache {
45namespace thrift {
46namespace py {
47
48template <typename T>
49static PyObject* encode_impl(PyObject* args) {
50 if (!args)
51 return NULL;
52
53 PyObject* enc_obj = NULL;
54 PyObject* type_args = NULL;
55 if (!PyArg_ParseTuple(args, "OO", &enc_obj, &type_args)) {
56 return NULL;
57 }
58 if (!enc_obj || !type_args) {
59 return NULL;
60 }
61
62 T protocol;
63 if (!protocol.prepareEncodeBuffer() || !protocol.encodeValue(enc_obj, T_STRUCT, type_args)) {
64 return NULL;
65 }
66
67 return protocol.getEncodedValue();
68}
69
70static inline long as_long_then_delete(PyObject* value, long default_value) {
71 ScopedPyObject scope(value);
72 long v = PyInt_AsLong(value);
73 if (INT_CONV_ERROR_OCCURRED(v)) {
74 PyErr_Clear();
75 return default_value;
76 }
77 return v;
78}
79
80template <typename T>
81static PyObject* decode_impl(PyObject* args) {
82 PyObject* output_obj = NULL;
83 PyObject* oprot = NULL;
84 PyObject* typeargs = NULL;
85 if (!PyArg_ParseTuple(args, "OOO", &output_obj, &oprot, &typeargs)) {
86 return NULL;
87 }
88
89 T protocol;
Nobuaki Sukegawaf7a8d942016-03-01 01:41:47 +090090#ifdef _MSC_VER
91 // workaround strange VC++ 2015 bug where #else path does not compile
92 int32_t default_limit = INT32_MAX;
93#else
94 int32_t default_limit = std::numeric_limits<int32_t>::max();
95#endif
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090096 protocol.setStringLengthLimit(
Nobuaki Sukegawaf7a8d942016-03-01 01:41:47 +090097 as_long_then_delete(PyObject_GetAttr(oprot, INTERN_STRING(string_length_limit)),
98 default_limit));
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090099 protocol.setContainerLengthLimit(
100 as_long_then_delete(PyObject_GetAttr(oprot, INTERN_STRING(container_length_limit)),
Nobuaki Sukegawaf7a8d942016-03-01 01:41:47 +0900101 default_limit));
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900102 ScopedPyObject transport(PyObject_GetAttr(oprot, INTERN_STRING(trans)));
103 if (!transport) {
104 return NULL;
105 }
106
107 StructTypeArgs parsedargs;
108 if (!parse_struct_args(&parsedargs, typeargs)) {
109 return NULL;
110 }
111
112 if (!protocol.prepareDecodeBufferFromTransport(transport.get())) {
113 return NULL;
114 }
115
116 return protocol.readStruct(output_obj, parsedargs.klass, parsedargs.spec);
117}
118}
119}
120}
121
122using namespace apache::thrift::py;
123
124/* -- PYTHON MODULE SETUP STUFF --- */
125
126extern "C" {
127
128static PyObject* encode_binary(PyObject*, PyObject* args) {
129 return encode_impl<BinaryProtocol>(args);
130}
131
132static PyObject* decode_binary(PyObject*, PyObject* args) {
133 return decode_impl<BinaryProtocol>(args);
134}
135
136static PyObject* encode_compact(PyObject*, PyObject* args) {
137 return encode_impl<CompactProtocol>(args);
138}
139
140static PyObject* decode_compact(PyObject*, PyObject* args) {
141 return decode_impl<CompactProtocol>(args);
142}
143
144static PyMethodDef ThriftFastBinaryMethods[] = {
145 {"encode_binary", encode_binary, METH_VARARGS, ""},
146 {"decode_binary", decode_binary, METH_VARARGS, ""},
147 {"encode_compact", encode_compact, METH_VARARGS, ""},
148 {"decode_compact", decode_compact, METH_VARARGS, ""},
149 {NULL, NULL, 0, NULL} /* Sentinel */
150};
151
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +0900152#if PY_MAJOR_VERSION >= 3
153
154static struct PyModuleDef ThriftFastBinaryDef = {PyModuleDef_HEAD_INIT,
155 "thrift.protocol.fastbinary",
156 NULL,
157 0,
158 ThriftFastBinaryMethods,
159 NULL,
160 NULL,
161 NULL,
162 NULL};
163
164#define INITERROR return NULL;
165
166PyObject* PyInit_fastbinary() {
167
168#else
169
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900170#define INITERROR return;
171
172void initfastbinary() {
173
174 PycString_IMPORT;
175 if (PycStringIO == NULL)
176 INITERROR
177
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +0900178#endif
179
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900180#define INIT_INTERN_STRING(value) \
181 do { \
182 INTERN_STRING(value) = PyString_InternFromString(#value); \
183 if (!INTERN_STRING(value)) \
184 INITERROR \
185 } while (0)
186
187 INIT_INTERN_STRING(TFrozenDict);
188 INIT_INTERN_STRING(cstringio_buf);
189 INIT_INTERN_STRING(cstringio_refill);
190 INIT_INTERN_STRING(string_length_limit);
191 INIT_INTERN_STRING(container_length_limit);
192 INIT_INTERN_STRING(trans);
193#undef INIT_INTERN_STRING
194
195 PyObject* module =
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +0900196#if PY_MAJOR_VERSION >= 3
197 PyModule_Create(&ThriftFastBinaryDef);
198#else
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900199 Py_InitModule("thrift.protocol.fastbinary", ThriftFastBinaryMethods);
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +0900200#endif
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900201 if (module == NULL)
202 INITERROR;
203
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +0900204#if PY_MAJOR_VERSION >= 3
205 return module;
206#endif
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900207}
208}