| 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); |
| Carel Combrink | a715bdf | 2025-10-30 07:44:21 +0100 | [diff] [blame^] | 41 | PyObject* INTERN_STRING(UUID); |
| 42 | PyObject* INTERN_STRING(bytes); |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 43 | static PyObject* INTERN_STRING(string_length_limit); |
| 44 | static PyObject* INTERN_STRING(container_length_limit); |
| 45 | static PyObject* INTERN_STRING(trans); |
| 46 | |
| 47 | namespace apache { |
| 48 | namespace thrift { |
| 49 | namespace py { |
| 50 | |
| 51 | template <typename T> |
| 52 | static PyObject* encode_impl(PyObject* args) { |
| 53 | if (!args) |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 54 | return nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 55 | |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 56 | PyObject* enc_obj = nullptr; |
| 57 | PyObject* type_args = nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 58 | if (!PyArg_ParseTuple(args, "OO", &enc_obj, &type_args)) { |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 59 | return nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 60 | } |
| 61 | if (!enc_obj || !type_args) { |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 62 | return nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | T protocol; |
| 66 | if (!protocol.prepareEncodeBuffer() || !protocol.encodeValue(enc_obj, T_STRUCT, type_args)) { |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 67 | return nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | return protocol.getEncodedValue(); |
| 71 | } |
| 72 | |
| 73 | static inline long as_long_then_delete(PyObject* value, long default_value) { |
| 74 | ScopedPyObject scope(value); |
| 75 | long v = PyInt_AsLong(value); |
| 76 | if (INT_CONV_ERROR_OCCURRED(v)) { |
| 77 | PyErr_Clear(); |
| 78 | return default_value; |
| 79 | } |
| 80 | return v; |
| 81 | } |
| 82 | |
| 83 | template <typename T> |
| 84 | static PyObject* decode_impl(PyObject* args) { |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 85 | PyObject* output_obj = nullptr; |
| 86 | PyObject* oprot = nullptr; |
| 87 | PyObject* typeargs = nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 88 | if (!PyArg_ParseTuple(args, "OOO", &output_obj, &oprot, &typeargs)) { |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 89 | return nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | T protocol; |
| James E. King, III | 07f5997 | 2017-03-10 06:18:33 -0500 | [diff] [blame] | 93 | int32_t default_limit = (std::numeric_limits<int32_t>::max)(); |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 94 | protocol.setStringLengthLimit( |
| Nobuaki Sukegawa | f7a8d94 | 2016-03-01 01:41:47 +0900 | [diff] [blame] | 95 | as_long_then_delete(PyObject_GetAttr(oprot, INTERN_STRING(string_length_limit)), |
| 96 | default_limit)); |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 97 | protocol.setContainerLengthLimit( |
| 98 | as_long_then_delete(PyObject_GetAttr(oprot, INTERN_STRING(container_length_limit)), |
| Nobuaki Sukegawa | f7a8d94 | 2016-03-01 01:41:47 +0900 | [diff] [blame] | 99 | default_limit)); |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 100 | ScopedPyObject transport(PyObject_GetAttr(oprot, INTERN_STRING(trans))); |
| 101 | if (!transport) { |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 102 | return nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | StructTypeArgs parsedargs; |
| 106 | if (!parse_struct_args(&parsedargs, typeargs)) { |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 107 | return nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | if (!protocol.prepareDecodeBufferFromTransport(transport.get())) { |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 111 | return nullptr; |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | return protocol.readStruct(output_obj, parsedargs.klass, parsedargs.spec); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | using namespace apache::thrift::py; |
| 121 | |
| 122 | /* -- PYTHON MODULE SETUP STUFF --- */ |
| 123 | |
| 124 | extern "C" { |
| 125 | |
| 126 | static PyObject* encode_binary(PyObject*, PyObject* args) { |
| 127 | return encode_impl<BinaryProtocol>(args); |
| 128 | } |
| 129 | |
| 130 | static PyObject* decode_binary(PyObject*, PyObject* args) { |
| 131 | return decode_impl<BinaryProtocol>(args); |
| 132 | } |
| 133 | |
| 134 | static PyObject* encode_compact(PyObject*, PyObject* args) { |
| 135 | return encode_impl<CompactProtocol>(args); |
| 136 | } |
| 137 | |
| 138 | static PyObject* decode_compact(PyObject*, PyObject* args) { |
| 139 | return decode_impl<CompactProtocol>(args); |
| 140 | } |
| 141 | |
| 142 | static PyMethodDef ThriftFastBinaryMethods[] = { |
| 143 | {"encode_binary", encode_binary, METH_VARARGS, ""}, |
| 144 | {"decode_binary", decode_binary, METH_VARARGS, ""}, |
| 145 | {"encode_compact", encode_compact, METH_VARARGS, ""}, |
| 146 | {"decode_compact", decode_compact, METH_VARARGS, ""}, |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 147 | {nullptr, nullptr, 0, nullptr} /* Sentinel */ |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 148 | }; |
| 149 | |
| Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 150 | #if PY_MAJOR_VERSION >= 3 |
| 151 | |
| 152 | static struct PyModuleDef ThriftFastBinaryDef = {PyModuleDef_HEAD_INIT, |
| 153 | "thrift.protocol.fastbinary", |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 154 | nullptr, |
| Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 155 | 0, |
| 156 | ThriftFastBinaryMethods, |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 157 | nullptr, |
| 158 | nullptr, |
| 159 | nullptr, |
| 160 | nullptr}; |
| Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 161 | |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 162 | #define INITERROR return nullptr; |
| Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 163 | |
| 164 | PyObject* PyInit_fastbinary() { |
| 165 | |
| 166 | #else |
| 167 | |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 168 | #define INITERROR return; |
| 169 | |
| 170 | void initfastbinary() { |
| 171 | |
| 172 | PycString_IMPORT; |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 173 | if (PycStringIO == nullptr) |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 174 | INITERROR |
| 175 | |
| Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 176 | #endif |
| 177 | |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 178 | #define INIT_INTERN_STRING(value) \ |
| 179 | do { \ |
| 180 | INTERN_STRING(value) = PyString_InternFromString(#value); \ |
| 181 | if (!INTERN_STRING(value)) \ |
| 182 | INITERROR \ |
| 183 | } while (0) |
| 184 | |
| 185 | INIT_INTERN_STRING(TFrozenDict); |
| 186 | INIT_INTERN_STRING(cstringio_buf); |
| 187 | INIT_INTERN_STRING(cstringio_refill); |
| 188 | INIT_INTERN_STRING(string_length_limit); |
| 189 | INIT_INTERN_STRING(container_length_limit); |
| 190 | INIT_INTERN_STRING(trans); |
| Carel Combrink | a715bdf | 2025-10-30 07:44:21 +0100 | [diff] [blame^] | 191 | INIT_INTERN_STRING(UUID); |
| 192 | INIT_INTERN_STRING(bytes); |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 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 |
| zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 201 | if (module == nullptr) |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 202 | INITERROR; |
| 203 | |
| Lysandros Nikolaou | c5a3712 | 2025-07-25 15:11:44 +0200 | [diff] [blame] | 204 | #ifdef Py_GIL_DISABLED |
| 205 | PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED); |
| 206 | #endif |
| 207 | |
| Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 208 | #if PY_MAJOR_VERSION >= 3 |
| 209 | return module; |
| 210 | #endif |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 211 | } |
| 212 | } |