Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 1 | /* |
| 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 Sukegawa | f7a8d94 | 2016-03-01 01:41:47 +0900 | [diff] [blame^] | 24 | #include <limits> |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 25 | #include <stdint.h> |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 26 | |
| 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. */ |
| 37 | PyObject* INTERN_STRING(TFrozenDict); |
| 38 | PyObject* INTERN_STRING(cstringio_buf); |
| 39 | PyObject* INTERN_STRING(cstringio_refill); |
| 40 | static PyObject* INTERN_STRING(string_length_limit); |
| 41 | static PyObject* INTERN_STRING(container_length_limit); |
| 42 | static PyObject* INTERN_STRING(trans); |
| 43 | |
| 44 | namespace apache { |
| 45 | namespace thrift { |
| 46 | namespace py { |
| 47 | |
| 48 | template <typename T> |
| 49 | static 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 | |
| 70 | static 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 | |
| 80 | template <typename T> |
| 81 | static 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 Sukegawa | f7a8d94 | 2016-03-01 01:41:47 +0900 | [diff] [blame^] | 90 | #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 Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 96 | protocol.setStringLengthLimit( |
Nobuaki Sukegawa | f7a8d94 | 2016-03-01 01:41:47 +0900 | [diff] [blame^] | 97 | as_long_then_delete(PyObject_GetAttr(oprot, INTERN_STRING(string_length_limit)), |
| 98 | default_limit)); |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 99 | protocol.setContainerLengthLimit( |
| 100 | as_long_then_delete(PyObject_GetAttr(oprot, INTERN_STRING(container_length_limit)), |
Nobuaki Sukegawa | f7a8d94 | 2016-03-01 01:41:47 +0900 | [diff] [blame^] | 101 | default_limit)); |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 102 | 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 | |
| 122 | using namespace apache::thrift::py; |
| 123 | |
| 124 | /* -- PYTHON MODULE SETUP STUFF --- */ |
| 125 | |
| 126 | extern "C" { |
| 127 | |
| 128 | static PyObject* encode_binary(PyObject*, PyObject* args) { |
| 129 | return encode_impl<BinaryProtocol>(args); |
| 130 | } |
| 131 | |
| 132 | static PyObject* decode_binary(PyObject*, PyObject* args) { |
| 133 | return decode_impl<BinaryProtocol>(args); |
| 134 | } |
| 135 | |
| 136 | static PyObject* encode_compact(PyObject*, PyObject* args) { |
| 137 | return encode_impl<CompactProtocol>(args); |
| 138 | } |
| 139 | |
| 140 | static PyObject* decode_compact(PyObject*, PyObject* args) { |
| 141 | return decode_impl<CompactProtocol>(args); |
| 142 | } |
| 143 | |
| 144 | static 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 Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 152 | #if PY_MAJOR_VERSION >= 3 |
| 153 | |
| 154 | static 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 | |
| 166 | PyObject* PyInit_fastbinary() { |
| 167 | |
| 168 | #else |
| 169 | |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 170 | #define INITERROR return; |
| 171 | |
| 172 | void initfastbinary() { |
| 173 | |
| 174 | PycString_IMPORT; |
| 175 | if (PycStringIO == NULL) |
| 176 | INITERROR |
| 177 | |
Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 178 | #endif |
| 179 | |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 180 | #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 Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 196 | #if PY_MAJOR_VERSION >= 3 |
| 197 | PyModule_Create(&ThriftFastBinaryDef); |
| 198 | #else |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 199 | Py_InitModule("thrift.protocol.fastbinary", ThriftFastBinaryMethods); |
Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 200 | #endif |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 201 | if (module == NULL) |
| 202 | INITERROR; |
| 203 | |
Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 204 | #if PY_MAJOR_VERSION >= 3 |
| 205 | return module; |
| 206 | #endif |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 207 | } |
| 208 | } |