| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8973de5 commit a5a5590
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -85,6 +85,19 @@ typedef struct { char c; long long x; } s_long_long; | |||
| 85 | 85 | #pragma options align=reset | |
| 86 | 86 | #endif | |
| 87 | 87 | ||
| 88 | + /*[python input] | ||
| 89 | + class cache_struct_converter(CConverter): | ||
| 90 | + type = 'PyStructObject *' | ||
| 91 | + converter = 'cache_struct_converter' | ||
| 92 | + c_default = "NULL" | ||
| 93 | + | ||
| 94 | + def cleanup(self): | ||
| 95 | + return "Py_XDECREF(%s);\n" % self.name | ||
| 96 | + [python start generated code]*/ | ||
| 97 | + /*[python end generated code: output=da39a3ee5e6b4b0d input=49957cca130ffb63]*/ | ||
| 98 | + | ||
| 99 | + static int cache_struct_converter(PyObject *, PyObject **); | ||
| 100 | + | ||
| 88 | 101 | #include "clinic/_struct.c.h" | |
| 89 | 102 | ||
| 90 | 103 | /* Helper for integer format codes: converts an arbitrary Python object to a | |
@@ -2037,21 +2050,27 @@ PyTypeObject PyStructType = { | |||
| 2037 | 2050 | #define MAXCACHE 100 | |
| 2038 | 2051 | static PyObject *cache = NULL; | |
| 2039 | 2052 | ||
| 2040 | - static PyStructObject * | ||
| 2041 | - cache_struct(PyObject *fmt) | ||
| 2053 | + static int | ||
| 2054 | + cache_struct_converter(PyObject *fmt, PyObject **ptr) | ||
| 2042 | 2055 | { | |
| 2043 | 2056 | PyObject * s_object; | |
| 2044 | 2057 | ||
| 2058 | + if (fmt == NULL) { | ||
| 2059 | + Py_DECREF(*ptr); | ||
| 2060 | + return 1; | ||
| 2061 | + } | ||
| 2062 | + | ||
| 2045 | 2063 | if (cache == NULL) { | |
| 2046 | 2064 | cache = PyDict_New(); | |
| 2047 | 2065 | if (cache == NULL) | |
| 2048 | - return NULL; | ||
| 2066 | + return 0; | ||
| 2049 | 2067 | } | |
| 2050 | 2068 | ||
| 2051 | 2069 | s_object = PyDict_GetItem(cache, fmt); | |
| 2052 | 2070 | if (s_object != NULL) { | |
| 2053 | 2071 | Py_INCREF(s_object); | |
| 2054 | - return (PyStructObject *)s_object; | ||
| 2072 | + *ptr = s_object; | ||
| 2073 | + return Py_CLEANUP_SUPPORTED; | ||
| 2055 | 2074 | } | |
| 2056 | 2075 | ||
| 2057 | 2076 | s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL); | |
@@ -2061,8 +2080,10 @@ cache_struct(PyObject *fmt) | |||
| 2061 | 2080 | /* Attempt to cache the result */ | |
| 2062 | 2081 | if (PyDict_SetItem(cache, fmt, s_object) == -1) | |
| 2063 | 2082 | PyErr_Clear(); | |
| 2083 | + *ptr = s_object; | ||
| 2084 | + return Py_CLEANUP_SUPPORTED; | ||
| 2064 | 2085 | } | |
| 2065 | - return (PyStructObject *)s_object; | ||
| 2086 | + return 0; | ||
| 2066 | 2087 | } | |
| 2067 | 2088 | ||
| 2068 | 2089 | /*[clinic input] | |
@@ -2081,25 +2102,19 @@ _clearcache_impl(PyObject *module) | |||
| 2081 | 2102 | ||
| 2082 | 2103 | ||
| 2083 | 2104 | /*[clinic input] | |
| 2084 | - calcsize | ||
| 2105 | + calcsize -> Py_ssize_t | ||
| 2085 | 2106 | ||
| 2086 | - format: object | ||
| 2107 | + format as s_object: cache_struct | ||
| 2087 | 2108 | / | |
| 2088 | 2109 | ||
| 2089 | 2110 | Return size in bytes of the struct described by the format string. | |
| 2090 | 2111 | [clinic start generated code]*/ | |
| 2091 | 2112 | ||
| 2092 | - static PyObject * | ||
| 2093 | - calcsize(PyObject *module, PyObject *format) | ||
| 2094 | - /*[clinic end generated code: output=90fbcf191fe9470a input=55488303a06777fa]*/ | ||
| 2113 | + static Py_ssize_t | ||
| 2114 | + calcsize_impl(PyObject *module, PyStructObject *s_object) | ||
| 2115 | + /*[clinic end generated code: output=db7d23d09c6932c4 input=96a6a590c7717ecd]*/ | ||
| 2095 | 2116 | { | |
| 2096 | - Py_ssize_t n; | ||
| 2097 | - PyStructObject *s_object = cache_struct(format); | ||
| 2098 | - if (s_object == NULL) | ||
| 2099 | - return NULL; | ||
| 2100 | - n = s_object->s_size; | ||
| 2101 | - Py_DECREF(s_object); | ||
| 2102 | - return PyLong_FromSsize_t(n); | ||
| 2117 | + return s_object->s_size; | ||
| 2103 | 2118 | } | |
| 2104 | 2119 | ||
| 2105 | 2120 | PyDoc_STRVAR(pack_doc, | |
@@ -2111,7 +2126,7 @@ to the format string. See help(struct) for more on format strings."); | |||
| 2111 | 2126 | static PyObject * | |
| 2112 | 2127 | pack(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) | |
| 2113 | 2128 | { | |
| 2114 | - PyStructObject *s_object; | ||
| 2129 | + PyObject *s_object = NULL; | ||
| 2115 | 2130 | PyObject *format, *result; | |
| 2116 | 2131 | ||
| 2117 | 2132 | if (nargs == 0) { | |
@@ -2120,11 +2135,10 @@ pack(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) | |||
| 2120 | 2135 | } | |
| 2121 | 2136 | format = args[0]; | |
| 2122 | 2137 | ||
| 2123 | - s_object = cache_struct(format); | ||
| 2124 | - if (s_object == NULL) { | ||
| 2138 | + if (!cache_struct_converter(format, &s_object)) { | ||
| 2125 | 2139 | return NULL; | |
| 2126 | 2140 | } | |
| 2127 | - result = s_pack((PyObject *)s_object, args + 1, nargs - 1, kwnames); | ||
| 2141 | + result = s_pack(s_object, args + 1, nargs - 1, kwnames); | ||
| 2128 | 2142 | Py_DECREF(s_object); | |
| 2129 | 2143 | return result; | |
| 2130 | 2144 | } | |
@@ -2140,7 +2154,7 @@ on format strings."); | |||
| 2140 | 2154 | static PyObject * | |
| 2141 | 2155 | pack_into(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) | |
| 2142 | 2156 | { | |
| 2143 | - PyStructObject *s_object; | ||
| 2157 | + PyObject *s_object = NULL; | ||
| 2144 | 2158 | PyObject *format, *result; | |
| 2145 | 2159 | ||
| 2146 | 2160 | if (nargs == 0) { | |
@@ -2149,19 +2163,18 @@ pack_into(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) | |||
| 2149 | 2163 | } | |
| 2150 | 2164 | format = args[0]; | |
| 2151 | 2165 | ||
| 2152 | - s_object = cache_struct(format); | ||
| 2153 | - if (s_object == NULL) { | ||
| 2166 | + if (!cache_struct_converter(format, &s_object)) { | ||
| 2154 | 2167 | return NULL; | |
| 2155 | 2168 | } | |
| 2156 | - result = s_pack_into((PyObject *)s_object, args + 1, nargs - 1, kwnames); | ||
| 2169 | + result = s_pack_into(s_object, args + 1, nargs - 1, kwnames); | ||
| 2157 | 2170 | Py_DECREF(s_object); | |
| 2158 | 2171 | return result; | |
| 2159 | 2172 | } | |
| 2160 | 2173 | ||
| 2161 | 2174 | /*[clinic input] | |
| 2162 | 2175 | unpack | |
| 2163 | 2176 | ||
| 2164 | - format: object | ||
| 2177 | + format as s_object: cache_struct | ||
| 2165 | 2178 | buffer: Py_buffer | |
| 2166 | 2179 | / | |
| 2167 | 2180 | ||
@@ -2173,24 +2186,16 @@ See help(struct) for more on format strings. | |||
| 2173 | 2186 | [clinic start generated code]*/ | |
| 2174 | 2187 | ||
| 2175 | 2188 | static PyObject * | |
| 2176 | - unpack_impl(PyObject *module, PyObject *format, Py_buffer *buffer) | ||
| 2177 | - /*[clinic end generated code: output=f75ada02aaa33b3b input=654078e6660c2df0]*/ | ||
| 2189 | + unpack_impl(PyObject *module, PyStructObject *s_object, Py_buffer *buffer) | ||
| 2190 | + /*[clinic end generated code: output=48ddd4d88eca8551 input=05fa3b91678da727]*/ | ||
| 2178 | 2191 | { | |
| 2179 | - PyStructObject *s_object; | ||
| 2180 | - PyObject *result; | ||
| 2181 | - | ||
| 2182 | - s_object = cache_struct(format); | ||
| 2183 | - if (s_object == NULL) | ||
| 2184 | - return NULL; | ||
| 2185 | - result = Struct_unpack_impl(s_object, buffer); | ||
| 2186 | - Py_DECREF(s_object); | ||
| 2187 | - return result; | ||
| 2192 | + return Struct_unpack_impl(s_object, buffer); | ||
| 2188 | 2193 | } | |
| 2189 | 2194 | ||
| 2190 | 2195 | /*[clinic input] | |
| 2191 | 2196 | unpack_from | |
| 2192 | 2197 | ||
| 2193 | - format: object | ||
| 2198 | + format as s_object: cache_struct | ||
| 2194 | 2199 | / | |
| 2195 | 2200 | buffer: Py_buffer | |
| 2196 | 2201 | offset: Py_ssize_t = 0 | |
@@ -2203,27 +2208,17 @@ See help(struct) for more on format strings. | |||
| 2203 | 2208 | [clinic start generated code]*/ | |
| 2204 | 2209 | ||
| 2205 | 2210 | static PyObject * | |
| 2206 | - unpack_from_impl(PyObject *module, PyObject *format, Py_buffer *buffer, | ||
| 2207 | - Py_ssize_t offset) | ||
| 2208 | - /*[clinic end generated code: output=2492f0c3a0b82577 input=9ead76c6ac7164f7]*/ | ||
| 2211 | + unpack_from_impl(PyObject *module, PyStructObject *s_object, | ||
| 2212 | + Py_buffer *buffer, Py_ssize_t offset) | ||
| 2213 | + /*[clinic end generated code: output=1042631674c6e0d3 input=6e80a5398e985025]*/ | ||
| 2209 | 2214 | { | |
| 2210 | - PyStructObject *s_object; | ||
| 2211 | - PyObject *result; | ||
| 2212 | - | ||
| 2213 | - s_object = cache_struct(format); | ||
| 2214 | - if (s_object == NULL) { | ||
| 2215 | - return NULL; | ||
| 2216 | - } | ||
| 2217 | - result = Struct_unpack_from_impl(s_object, buffer, offset); | ||
| 2218 | - | ||
| 2219 | - Py_DECREF(s_object); | ||
| 2220 | - return result; | ||
| 2215 | + return Struct_unpack_from_impl(s_object, buffer, offset); | ||
| 2221 | 2216 | } | |
| 2222 | 2217 | ||
| 2223 | 2218 | /*[clinic input] | |
| 2224 | 2219 | iter_unpack | |
| 2225 | 2220 | ||
| 2226 | - format: object | ||
| 2221 | + format as s_object: cache_struct | ||
| 2227 | 2222 | buffer: object | |
| 2228 | 2223 | / | |
| 2229 | 2224 | ||
@@ -2236,19 +2231,11 @@ Requires that the bytes length be a multiple of the format struct size. | |||
| 2236 | 2231 | [clinic start generated code]*/ | |
| 2237 | 2232 | ||
| 2238 | 2233 | static PyObject * | |
| 2239 | - iter_unpack_impl(PyObject *module, PyObject *format, PyObject *buffer) | ||
| 2240 | - /*[clinic end generated code: output=b1291e97a6d4cf3c input=8674dfd2f0dae416]*/ | ||
| 2234 | + iter_unpack_impl(PyObject *module, PyStructObject *s_object, | ||
| 2235 | + PyObject *buffer) | ||
| 2236 | + /*[clinic end generated code: output=0ae50e250d20e74d input=b214a58869a3c98d]*/ | ||
| 2241 | 2237 | { | |
| 2242 | - PyStructObject *s_object; | ||
| 2243 | - PyObject *result; | ||
| 2244 | - | ||
| 2245 | - s_object = cache_struct(format); | ||
| 2246 | - if (s_object == NULL) | ||
| 2247 | - return NULL; | ||
| 2248 | - | ||
| 2249 | - result = Struct_iter_unpack(s_object, buffer); | ||
| 2250 | - Py_DECREF(s_object); | ||
| 2251 | - return result; | ||
| 2238 | + return Struct_iter_unpack(s_object, buffer); | ||
| 2252 | 2239 | } | |
| 2253 | 2240 | ||
| 2254 | 2241 | static struct PyMethodDef module_functions[] = { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -155,6 +155,32 @@ PyDoc_STRVAR(calcsize__doc__, | |||
| 155 | 155 | #define CALCSIZE_METHODDEF \ | |
| 156 | 156 | {"calcsize", (PyCFunction)calcsize, METH_O, calcsize__doc__}, | |
| 157 | 157 | ||
| 158 | + static Py_ssize_t | ||
| 159 | + calcsize_impl(PyObject *module, PyStructObject *s_object); | ||
| 160 | + | ||
| 161 | + static PyObject * | ||
| 162 | + calcsize(PyObject *module, PyObject *arg) | ||
| 163 | + { | ||
| 164 | + PyObject *return_value = NULL; | ||
| 165 | + PyStructObject *s_object = NULL; | ||
| 166 | + Py_ssize_t _return_value; | ||
| 167 | + | ||
| 168 | + if (!PyArg_Parse(arg, "O&:calcsize", cache_struct_converter, &s_object)) { | ||
| 169 | + goto exit; | ||
| 170 | + } | ||
| 171 | + _return_value = calcsize_impl(module, s_object); | ||
| 172 | + if ((_return_value == -1) && PyErr_Occurred()) { | ||
| 173 | + goto exit; | ||
| 174 | + } | ||
| 175 | + return_value = PyLong_FromSsize_t(_return_value); | ||
| 176 | + | ||
| 177 | + exit: | ||
| 178 | + /* Cleanup for s_object */ | ||
| 179 | + Py_XDECREF(s_object); | ||
| 180 | + | ||
| 181 | + return return_value; | ||
| 182 | + } | ||
| 183 | + | ||
| 158 | 184 | PyDoc_STRVAR(unpack__doc__, | |
| 159 | 185 | "unpack($module, format, buffer, /)\n" | |
| 160 | 186 | "--\n" | |
@@ -169,26 +195,28 @@ PyDoc_STRVAR(unpack__doc__, | |||
| 169 | 195 | {"unpack", (PyCFunction)unpack, METH_FASTCALL, unpack__doc__}, | |
| 170 | 196 | ||
| 171 | 197 | static PyObject * | |
| 172 | - unpack_impl(PyObject *module, PyObject *format, Py_buffer *buffer); | ||
| 198 | + unpack_impl(PyObject *module, PyStructObject *s_object, Py_buffer *buffer); | ||
| 173 | 199 | ||
| 174 | 200 | static PyObject * | |
| 175 | 201 | unpack(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) | |
| 176 | 202 | { | |
| 177 | 203 | PyObject *return_value = NULL; | |
| 178 | - PyObject *format; | ||
| 204 | + PyStructObject *s_object = NULL; | ||
| 179 | 205 | Py_buffer buffer = {NULL, NULL}; | |
| 180 | 206 | ||
| 181 | - if (!_PyArg_ParseStack(args, nargs, "Oy*:unpack", | ||
| 182 | - &format, &buffer)) { | ||
| 207 | + if (!_PyArg_ParseStack(args, nargs, "O&y*:unpack", | ||
| 208 | + cache_struct_converter, &s_object, &buffer)) { | ||
| 183 | 209 | goto exit; | |
| 184 | 210 | } | |
| 185 | 211 | ||
| 186 | 212 | if (!_PyArg_NoStackKeywords("unpack", kwnames)) { | |
| 187 | 213 | goto exit; | |
| 188 | 214 | } | |
| 189 | - return_value = unpack_impl(module, format, &buffer); | ||
| 215 | + return_value = unpack_impl(module, s_object, &buffer); | ||
| 190 | 216 | ||
| 191 | 217 | exit: | |
| 218 | + /* Cleanup for s_object */ | ||
| 219 | + Py_XDECREF(s_object); | ||
| 192 | 220 | /* Cleanup for buffer */ | |
| 193 | 221 | if (buffer.obj) { | |
| 194 | 222 | PyBuffer_Release(&buffer); | |
@@ -211,26 +239,28 @@ PyDoc_STRVAR(unpack_from__doc__, | |||
| 211 | 239 | {"unpack_from", (PyCFunction)unpack_from, METH_FASTCALL, unpack_from__doc__}, | |
| 212 | 240 | ||
| 213 | 241 | static PyObject * | |
| 214 | - unpack_from_impl(PyObject *module, PyObject *format, Py_buffer *buffer, | ||
| 215 | - Py_ssize_t offset); | ||
| 242 | + unpack_from_impl(PyObject *module, PyStructObject *s_object, | ||
| 243 | + Py_buffer *buffer, Py_ssize_t offset); | ||
| 216 | 244 | ||
| 217 | 245 | static PyObject * | |
| 218 | 246 | unpack_from(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) | |
| 219 | 247 | { | |
| 220 | 248 | PyObject *return_value = NULL; | |
| 221 | 249 | static const char * const _keywords[] = {"", "buffer", "offset", NULL}; | |
| 222 | - static _PyArg_Parser _parser = {"Oy*|n:unpack_from", _keywords, 0}; | ||
| 223 | - PyObject *format; | ||
| 250 | + static _PyArg_Parser _parser = {"O&y*|n:unpack_from", _keywords, 0}; | ||
| 251 | + PyStructObject *s_object = NULL; | ||
| 224 | 252 | Py_buffer buffer = {NULL, NULL}; | |
| 225 | 253 | Py_ssize_t offset = 0; | |
| 226 | 254 | ||
| 227 | 255 | if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser, | |
| 228 | - &format, &buffer, &offset)) { | ||
| 256 | + cache_struct_converter, &s_object, &buffer, &offset)) { | ||
| 229 | 257 | goto exit; | |
| 230 | 258 | } | |
| 231 | - return_value = unpack_from_impl(module, format, &buffer, offset); | ||
| 259 | + return_value = unpack_from_impl(module, s_object, &buffer, offset); | ||
| 232 | 260 | ||
| 233 | 261 | exit: | |
| 262 | + /* Cleanup for s_object */ | ||
| 263 | + Py_XDECREF(s_object); | ||
| 234 | 264 | /* Cleanup for buffer */ | |
| 235 | 265 | if (buffer.obj) { | |
| 236 | 266 | PyBuffer_Release(&buffer); | |
@@ -254,27 +284,30 @@ PyDoc_STRVAR(iter_unpack__doc__, | |||
| 254 | 284 | {"iter_unpack", (PyCFunction)iter_unpack, METH_FASTCALL, iter_unpack__doc__}, | |
| 255 | 285 | ||
| 256 | 286 | static PyObject * | |
| 257 | - iter_unpack_impl(PyObject *module, PyObject *format, PyObject *buffer); | ||
| 287 | + iter_unpack_impl(PyObject *module, PyStructObject *s_object, | ||
| 288 | + PyObject *buffer); | ||
| 258 | 289 | ||
| 259 | 290 | static PyObject * | |
| 260 | 291 | iter_unpack(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) | |
| 261 | 292 | { | |
| 262 | 293 | PyObject *return_value = NULL; | |
| 263 | - PyObject *format; | ||
| 294 | + PyStructObject *s_object = NULL; | ||
| 264 | 295 | PyObject *buffer; | |
| 265 | 296 | ||
| 266 | - if (!_PyArg_UnpackStack(args, nargs, "iter_unpack", | ||
| 267 | - 2, 2, | ||
| 268 | - &format, &buffer)) { | ||
| 297 | + if (!_PyArg_ParseStack(args, nargs, "O&O:iter_unpack", | ||
| 298 | + cache_struct_converter, &s_object, &buffer)) { | ||
| 269 | 299 | goto exit; | |
| 270 | 300 | } | |
| 271 | 301 | ||
| 272 | 302 | if (!_PyArg_NoStackKeywords("iter_unpack", kwnames)) { | |
| 273 | 303 | goto exit; | |
| 274 | 304 | } | |
| 275 | - return_value = iter_unpack_impl(module, format, buffer); | ||
| 305 | + return_value = iter_unpack_impl(module, s_object, buffer); | ||
| 276 | 306 | ||
| 277 | 307 | exit: | |
| 308 | + /* Cleanup for s_object */ | ||
| 309 | + Py_XDECREF(s_object); | ||
| 310 | + | ||
| 278 | 311 | return return_value; | |
| 279 | 312 | } | |
| 280 | - /*[clinic end generated code: output=0714090a5d0ea8ce input=a9049054013a1b77]*/ | ||
| 313 | + /*[clinic end generated code: output=03e0d193ab1983f9 input=a9049054013a1b77]*/ | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments