| 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 | |
| Csaba Ringhofer | 37c7910 | 2025-09-15 14:40:17 +0200 | [diff] [blame] | 20 | #define PY_SSIZE_T_CLEAN |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 21 | #include <Python.h> |
| 22 | #include "types.h" |
| 23 | #include "binary.h" |
| 24 | #include "compact.h" |
| Nobuaki Sukegawa | f7a8d94 | 2016-03-01 01:41:47 +0900 | [diff] [blame] | 25 | #include <limits> |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 26 | #include <stdint.h> |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 27 | |
| 28 | // TODO(dreiss): defval appears to be unused. Look into removing it. |
| 29 | // TODO(dreiss): Make parse_spec_args recursive, and cache the output |
| 30 | // permanently in the object. (Malloc and orphan.) |
| 31 | // TODO(dreiss): Why do we need cStringIO for reading, why not just char*? |
| 32 | // Can cStringIO let us work with a BufferedTransport? |
| 33 | // TODO(dreiss): Don't ignore the rv from cwrite (maybe). |
| 34 | |
| 35 | // Doing a benchmark shows that interning actually makes a difference, amazingly. |
| 36 | |
| 37 | /** Pointer to interned string to speed up attribute lookup. */ |
| 38 | PyObject* INTERN_STRING(TFrozenDict); |
| 39 | PyObject* INTERN_STRING(cstringio_buf); |
| 40 | PyObject* INTERN_STRING(cstringio_refill); |
| 41 | static PyObject* INTERN_STRING(string_length_limit); |
| 42 | static PyObject* INTERN_STRING(container_length_limit); |
| 43 | static PyObject* INTERN_STRING(trans); |
| 44 | |
| 45 | namespace apache { |
| 46 | namespace thrift { |
| 47 | namespace py { |
| 48 | |
| 49 | template <typename T> |
| 50 | static PyObject* encode_impl(PyObject* args) { |
| 51 | if (!args) |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 52 | return nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 53 | |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 54 | PyObject* enc_obj = nullptr; |
| 55 | PyObject* type_args = nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 56 | if (!PyArg_ParseTuple(args, "OO", &enc_obj, &type_args)) { |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 57 | return nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 58 | } |
| 59 | if (!enc_obj || !type_args) { |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 60 | return nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | T protocol; |
| 64 | if (!protocol.prepareEncodeBuffer() || !protocol.encodeValue(enc_obj, T_STRUCT, type_args)) { |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 65 | return nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | return protocol.getEncodedValue(); |
| 69 | } |
| 70 | |
| 71 | static inline long as_long_then_delete(PyObject* value, long default_value) { |
| 72 | ScopedPyObject scope(value); |
| 73 | long v = PyInt_AsLong(value); |
| 74 | if (INT_CONV_ERROR_OCCURRED(v)) { |
| 75 | PyErr_Clear(); |
| 76 | return default_value; |
| 77 | } |
| 78 | return v; |
| 79 | } |
| 80 | |
| 81 | template <typename T> |
| 82 | static PyObject* decode_impl(PyObject* args) { |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 83 | PyObject* output_obj = nullptr; |
| 84 | PyObject* oprot = nullptr; |
| 85 | PyObject* typeargs = nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 86 | if (!PyArg_ParseTuple(args, "OOO", &output_obj, &oprot, &typeargs)) { |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 87 | return nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | T protocol; |
| James E. King, III | 07f5997 | 2017-03-10 06:18:33 -0500 | [diff] [blame] | 91 | int32_t default_limit = (std::numeric_limits<int32_t>::max)(); |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 92 | protocol.setStringLengthLimit( |
| Nobuaki Sukegawa | f7a8d94 | 2016-03-01 01:41:47 +0900 | [diff] [blame] | 93 | as_long_then_delete(PyObject_GetAttr(oprot, INTERN_STRING(string_length_limit)), |
| 94 | default_limit)); |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 95 | protocol.setContainerLengthLimit( |
| 96 | as_long_then_delete(PyObject_GetAttr(oprot, INTERN_STRING(container_length_limit)), |
| Nobuaki Sukegawa | f7a8d94 | 2016-03-01 01:41:47 +0900 | [diff] [blame] | 97 | default_limit)); |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 98 | ScopedPyObject transport(PyObject_GetAttr(oprot, INTERN_STRING(trans))); |
| 99 | if (!transport) { |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 100 | return nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | StructTypeArgs parsedargs; |
| 104 | if (!parse_struct_args(&parsedargs, typeargs)) { |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 105 | return nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | if (!protocol.prepareDecodeBufferFromTransport(transport.get())) { |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 109 | return nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | return protocol.readStruct(output_obj, parsedargs.klass, parsedargs.spec); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | using namespace apache::thrift::py; |
| 119 | |
| 120 | /* -- PYTHON MODULE SETUP STUFF --- */ |
| 121 | |
| 122 | extern "C" { |
| 123 | |
| 124 | static PyObject* encode_binary(PyObject*, PyObject* args) { |
| 125 | return encode_impl<BinaryProtocol>(args); |
| 126 | } |
| 127 | |
| 128 | static PyObject* decode_binary(PyObject*, PyObject* args) { |
| 129 | return decode_impl<BinaryProtocol>(args); |
| 130 | } |
| 131 | |
| 132 | static PyObject* encode_compact(PyObject*, PyObject* args) { |
| 133 | return encode_impl<CompactProtocol>(args); |
| 134 | } |
| 135 | |
| 136 | static PyObject* decode_compact(PyObject*, PyObject* args) { |
| 137 | return decode_impl<CompactProtocol>(args); |
| 138 | } |
| 139 | |
| 140 | static PyMethodDef ThriftFastBinaryMethods[] = { |
| 141 | {"encode_binary", encode_binary, METH_VARARGS, ""}, |
| 142 | {"decode_binary", decode_binary, METH_VARARGS, ""}, |
| 143 | {"encode_compact", encode_compact, METH_VARARGS, ""}, |
| 144 | {"decode_compact", decode_compact, METH_VARARGS, ""}, |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 145 | {nullptr, nullptr, 0, nullptr} /* Sentinel */ |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 146 | }; |
| 147 | |
| Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 148 | #if PY_MAJOR_VERSION >= 3 |
| 149 | |
| 150 | static struct PyModuleDef ThriftFastBinaryDef = {PyModuleDef_HEAD_INIT, |
| 151 | "thrift.protocol.fastbinary", |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 152 | nullptr, |
| Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 153 | 0, |
| 154 | ThriftFastBinaryMethods, |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 155 | nullptr, |
| 156 | nullptr, |
| 157 | nullptr, |
| 158 | nullptr}; |
| Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 159 | |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 160 | #define INITERROR return nullptr; |
| Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 161 | |
| 162 | PyObject* PyInit_fastbinary() { |
| 163 | |
| 164 | #else |
| 165 | |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 166 | #define INITERROR return; |
| 167 | |
| 168 | void initfastbinary() { |
| 169 | |
| 170 | PycString_IMPORT; |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 171 | if (PycStringIO == nullptr) |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 172 | INITERROR |
| 173 | |
| Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 174 | #endif |
| 175 | |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 176 | #define INIT_INTERN_STRING(value) \ |
| 177 | do { \ |
| 178 | INTERN_STRING(value) = PyString_InternFromString(#value); \ |
| 179 | if (!INTERN_STRING(value)) \ |
| 180 | INITERROR \ |
| 181 | } while (0) |
| 182 | |
| 183 | INIT_INTERN_STRING(TFrozenDict); |
| 184 | INIT_INTERN_STRING(cstringio_buf); |
| 185 | INIT_INTERN_STRING(cstringio_refill); |
| 186 | INIT_INTERN_STRING(string_length_limit); |
| 187 | INIT_INTERN_STRING(container_length_limit); |
| 188 | INIT_INTERN_STRING(trans); |
| 189 | #undef INIT_INTERN_STRING |
| 190 | |
| 191 | PyObject* module = |
| Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 192 | #if PY_MAJOR_VERSION >= 3 |
| 193 | PyModule_Create(&ThriftFastBinaryDef); |
| 194 | #else |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 195 | Py_InitModule("thrift.protocol.fastbinary", ThriftFastBinaryMethods); |
| Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 196 | #endif |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 197 | if (module == nullptr) |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 198 | INITERROR; |
| 199 | |
| Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 200 | #if PY_MAJOR_VERSION >= 3 |
| 201 | return module; |
| 202 | #endif |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 203 | } |
| 204 | } |