@@ -85,6 +85,19 @@ typedef struct { char c; long long x; } s_long_long;
8585#pragma options align=reset
8686#endif
8787
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+
88101#include "clinic/_struct.c.h"
89102
90103/* Helper for integer format codes: converts an arbitrary Python object to a
@@ -2037,21 +2050,27 @@ PyTypeObject PyStructType = {
20372050#define MAXCACHE 100
20382051static PyObject * cache = NULL ;
20392052
2040- static PyStructObject *
2041- cache_struct (PyObject * fmt )
2053+ static int
2054+ cache_struct_converter (PyObject * fmt , PyObject * * ptr )
20422055{
20432056 PyObject * s_object ;
20442057
2058+ if (fmt == NULL ) {
2059+ Py_DECREF (* ptr );
2060+ return 1 ;
2061+ }
2062+
20452063 if (cache == NULL ) {
20462064 cache = PyDict_New ();
20472065 if (cache == NULL )
2048- return NULL ;
2066+ return 0 ;
20492067 }
20502068
20512069 s_object = PyDict_GetItem (cache , fmt );
20522070 if (s_object != NULL ) {
20532071 Py_INCREF (s_object );
2054- return (PyStructObject * )s_object ;
2072+ * ptr = s_object ;
2073+ return Py_CLEANUP_SUPPORTED ;
20552074 }
20562075
20572076 s_object = PyObject_CallFunctionObjArgs ((PyObject * )(& PyStructType ), fmt , NULL );
@@ -2061,8 +2080,10 @@ cache_struct(PyObject *fmt)
20612080 /* Attempt to cache the result */
20622081 if (PyDict_SetItem (cache , fmt , s_object ) == -1 )
20632082 PyErr_Clear ();
2083+ * ptr = s_object ;
2084+ return Py_CLEANUP_SUPPORTED ;
20642085 }
2065- return ( PyStructObject * ) s_object ;
2086+ return 0 ;
20662087}
20672088
20682089/*[clinic input]
@@ -2081,25 +2102,19 @@ _clearcache_impl(PyObject *module)
20812102
20822103
20832104/*[clinic input]
2084- calcsize
2105+ calcsize -> Py_ssize_t
20852106
2086- format: object
2107+ format as s_object: cache_struct
20872108 /
20882109
20892110Return size in bytes of the struct described by the format string.
20902111[clinic start generated code]*/
20912112
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 ]*/
20952116{
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 ;
21032118}
21042119
21052120PyDoc_STRVAR (pack_doc ,
@@ -2111,7 +2126,7 @@ to the format string. See help(struct) for more on format strings.");
21112126static PyObject *
21122127pack (PyObject * self , PyObject * * args , Py_ssize_t nargs , PyObject * kwnames )
21132128{
2114- PyStructObject * s_object ;
2129+ PyObject * s_object = NULL ;
21152130 PyObject * format , * result ;
21162131
21172132 if (nargs == 0 ) {
@@ -2120,11 +2135,10 @@ pack(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
21202135 }
21212136 format = args [0 ];
21222137
2123- s_object = cache_struct (format );
2124- if (s_object == NULL ) {
2138+ if (!cache_struct_converter (format , & s_object )) {
21252139 return NULL ;
21262140 }
2127- result = s_pack (( PyObject * ) s_object , args + 1 , nargs - 1 , kwnames );
2141+ result = s_pack (s_object , args + 1 , nargs - 1 , kwnames );
21282142 Py_DECREF (s_object );
21292143 return result ;
21302144}
@@ -2140,7 +2154,7 @@ on format strings.");
21402154static PyObject *
21412155pack_into (PyObject * self , PyObject * * args , Py_ssize_t nargs , PyObject * kwnames )
21422156{
2143- PyStructObject * s_object ;
2157+ PyObject * s_object = NULL ;
21442158 PyObject * format , * result ;
21452159
21462160 if (nargs == 0 ) {
@@ -2149,19 +2163,18 @@ pack_into(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
21492163 }
21502164 format = args [0 ];
21512165
2152- s_object = cache_struct (format );
2153- if (s_object == NULL ) {
2166+ if (!cache_struct_converter (format , & s_object )) {
21542167 return NULL ;
21552168 }
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 );
21572170 Py_DECREF (s_object );
21582171 return result ;
21592172}
21602173
21612174/*[clinic input]
21622175unpack
21632176
2164- format: object
2177+ format as s_object: cache_struct
21652178 buffer: Py_buffer
21662179 /
21672180
@@ -2173,24 +2186,16 @@ See help(struct) for more on format strings.
21732186[clinic start generated code]*/
21742187
21752188static 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 ]*/
21782191{
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 );
21882193}
21892194
21902195/*[clinic input]
21912196unpack_from
21922197
2193- format: object
2198+ format as s_object: cache_struct
21942199 /
21952200 buffer: Py_buffer
21962201 offset: Py_ssize_t = 0
@@ -2203,27 +2208,17 @@ See help(struct) for more on format strings.
22032208[clinic start generated code]*/
22042209
22052210static 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 ]*/
22092214{
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 );
22212216}
22222217
22232218/*[clinic input]
22242219iter_unpack
22252220
2226- format: object
2221+ format as s_object: cache_struct
22272222 buffer: object
22282223 /
22292224
@@ -2236,19 +2231,11 @@ Requires that the bytes length be a multiple of the format struct size.
22362231[clinic start generated code]*/
22372232
22382233static 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]*/
22412237{
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 );
22522239}
22532240
22542241static struct PyMethodDef module_functions [] = {
0 commit comments