@@ -14,7 +14,7 @@ static PyObject *Reader_getmaxbuf(libvalkey_ReaderObject *self);
1414static PyObject * Reader_len (libvalkey_ReaderObject * self );
1515static PyObject * Reader_has_data (libvalkey_ReaderObject * self );
1616static PyObject * Reader_set_encoding (libvalkey_ReaderObject * self , PyObject * args , PyObject * kwds );
17- static PyObject * Reader_listOnly (PyObject * self , void * closure );
17+ static PyObject * Reader_convertSetsToLists (PyObject * self , void * closure );
1818
1919static PyMethodDef libvalkey_ReaderMethods [] = {
2020 {"feed" , (PyCFunction )Reader_feed , METH_VARARGS , NULL },
@@ -28,7 +28,7 @@ static PyMethodDef libvalkey_ReaderMethods[] = {
2828};
2929
3030static PyGetSetDef libvalkey_ReaderGetSet [] = {
31- {"listOnly " , (getter )Reader_listOnly , NULL , NULL , NULL },
31+ {"convertSetsToLists " , (getter )Reader_convertSetsToLists , NULL , NULL , NULL },
3232 {NULL } /* Sentinel */
3333};
3434
@@ -73,88 +73,46 @@ PyTypeObject libvalkey_ReaderType = {
7373 Reader_new , /*tp_new */
7474};
7575
76- static int tryParentize_impl (const valkeyReadTask * task , PyObject * obj ,
77- PyObject * parent , libvalkey_ReaderObject * self ) {
78- switch (task -> parent -> type ) {
79- case VALKEY_REPLY_MAP :
80- if (task -> idx % 2 == 0 ) {
81- /* Save the object as a key. */
82- self -> pendingObject = obj ;
83- } else {
84- if (self -> pendingObject == NULL ) {
85- Py_DECREF (obj );
86- return -1 ;
87- }
88- if (PyDict_SetItem (parent , self -> pendingObject , obj ) < 0 ) {
89- Py_DECREF (obj );
90- Py_DECREF (self -> pendingObject );
76+ static void * tryParentize (const valkeyReadTask * task , PyObject * obj ) {
77+ libvalkey_ReaderObject * self = (libvalkey_ReaderObject * )task -> privdata ;
78+ if (task && task -> parent ) {
79+ PyObject * parent = (PyObject * )task -> parent -> obj ;
80+ switch (task -> parent -> type ) {
81+ case VALKEY_REPLY_MAP :
82+ if (task -> idx % 2 == 0 ) {
83+ /* Save the object as a key. */
84+ self -> pendingObject = obj ;
85+ } else {
86+ if (self -> pendingObject == NULL ) {
87+ Py_DECREF (obj );
88+ return NULL ;
89+ }
90+ if (PyDict_SetItem (parent , self -> pendingObject , obj ) < 0 ) {
91+ Py_DECREF (obj );
92+ Py_DECREF (self -> pendingObject );
93+ self -> pendingObject = NULL ;
94+ return NULL ;
95+ }
9196 self -> pendingObject = NULL ;
92- return -1 ;
9397 }
94- self -> pendingObject = NULL ;
95- }
96- break ;
97- case VALKEY_REPLY_SET :
98- assert (PyAnySet_CheckExact (parent ));
99- if (PySet_Add (parent , obj ) < 0 ) {
100- Py_DECREF (obj );
101- return -1 ;
102- }
103- break ;
104- default :
105- assert (PyList_CheckExact (parent ));
106- if (PyList_SetItem (parent , task -> idx , obj ) < 0 ) {
107- Py_DECREF (obj );
108- return -1 ;
109- }
110- }
111- return 0 ;
112- }
113-
114- static int tryParentize_ListOnly (const valkeyReadTask * task , PyObject * obj ,
115- PyObject * parent , libvalkey_ReaderObject * self ) {
116- switch (task -> parent -> type ) {
117- case VALKEY_REPLY_MAP :
118- if (task -> idx % 2 == 0 ) {
119- /* Set a temporary item to save the object as a key. */
120- self -> pendingObject = PyTuple_New (2 );
121- if (self -> pendingObject == NULL ) {
122- Py_DECREF (obj );
123- return -1 ;
98+ break ;
99+ case VALKEY_REPLY_SET :
100+ if (!self -> convertSetsToLists ) {
101+ assert (PyAnySet_CheckExact (parent ));
102+ if (PySet_Add (parent , obj ) < 0 ) {
103+ Py_DECREF (obj );
104+ return NULL ;
105+ }
106+ break ;
124107 }
125- PyTuple_SET_ITEM (self -> pendingObject , 0 , obj );
126- } else {
127- if (self -> pendingObject == NULL ) {
108+ /* fallthrough */
109+ default :
110+ assert (PyList_CheckExact (parent ));
111+ if (PyList_SetItem (parent , task -> idx , obj ) < 0 ) {
128112 Py_DECREF (obj );
129- return -1 ;
113+ return NULL ;
130114 }
131- PyTuple_SET_ITEM (self -> pendingObject , 1 , obj );
132- int res = PyList_Append (parent , self -> pendingObject );
133- Py_DECREF (self -> pendingObject );
134- self -> pendingObject = NULL ;
135- if (res < 0 )
136- return -1 ;
137- }
138- break ;
139- default :
140- assert (PyList_CheckExact (parent ));
141- if (PyList_SetItem (parent , task -> idx , obj ) < 0 ) {
142- Py_DECREF (obj );
143- return -1 ;
144- }
145- }
146- return 0 ;
147- }
148-
149- static void * tryParentize (const valkeyReadTask * task , PyObject * obj ) {
150- libvalkey_ReaderObject * self = (libvalkey_ReaderObject * )task -> privdata ;
151- if (task && task -> parent ) {
152- PyObject * parent = (PyObject * )task -> parent -> obj ;
153- int res = self -> listOnly
154- ? tryParentize_ListOnly (task , obj , parent , self )
155- : tryParentize_impl (task , obj , parent , self );
156- if (res < 0 )
157- return NULL ;
115+ }
158116 }
159117 return obj ;
160118}
@@ -224,22 +182,18 @@ static void *createStringObject(const valkeyReadTask *task, char *str, size_t le
224182static void * createArrayObject (const valkeyReadTask * task , size_t elements ) {
225183 libvalkey_ReaderObject * self = (libvalkey_ReaderObject * )task -> privdata ;
226184 PyObject * obj ;
227- if (self -> listOnly ) {
228- /* For map, don't preallocate listand use append later. */
229- if (task -> type == VALKEY_REPLY_MAP )
230- elements = 0 ;
231- obj = PyList_New (elements );
232- } else {
233- switch (task -> type ) {
234- case VALKEY_REPLY_MAP :
235- obj = PyDict_New ();
236- break ;
237- case VALKEY_REPLY_SET :
185+ switch (task -> type ) {
186+ case VALKEY_REPLY_MAP :
187+ obj = PyDict_New ();
188+ break ;
189+ case VALKEY_REPLY_SET :
190+ if (!self -> convertSetsToLists ) {
238191 obj = PySet_New (NULL );
239192 break ;
240- default :
241- obj = PyList_New (elements );
242- }
193+ }
194+ /* fallthrough */
195+ default :
196+ obj = PyList_New (elements );
243197 }
244198 return tryParentize (task , obj );
245199}
@@ -357,18 +311,18 @@ static int Reader_init(libvalkey_ReaderObject *self, PyObject *args, PyObject *k
357311 "encoding" ,
358312 "errors" ,
359313 "notEnoughData" ,
360- "listOnly " ,
314+ "convertSetsToLists " ,
361315 NULL ,
362316 };
363317 PyObject * protocolErrorClass = NULL ;
364318 PyObject * replyErrorClass = NULL ;
365319 PyObject * notEnoughData = NULL ;
366320 char * encoding = NULL ;
367321 char * errors = NULL ;
368- int listOnly = 0 ;
322+ int convertSetsToLists = 0 ;
369323
370324 if (!PyArg_ParseTupleAndKeywords (args , kwds , "|OOzzOp" , kwlist ,
371- & protocolErrorClass , & replyErrorClass , & encoding , & errors , & notEnoughData , & listOnly ))
325+ & protocolErrorClass , & replyErrorClass , & encoding , & errors , & notEnoughData , & convertSetsToLists ))
372326 return -1 ;
373327
374328 if (protocolErrorClass )
@@ -386,7 +340,7 @@ static int Reader_init(libvalkey_ReaderObject *self, PyObject *args, PyObject *k
386340 Py_INCREF (self -> notEnoughDataObject );
387341 }
388342
389- self -> listOnly = listOnly ;
343+ self -> convertSetsToLists = convertSetsToLists ;
390344
391345 return _Reader_set_encoding (self , encoding , errors );
392346}
@@ -406,7 +360,7 @@ static PyObject *Reader_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
406360 self -> protocolErrorClass = LIBVALKEY_STATE -> VkErr_ProtocolError ;
407361 self -> replyErrorClass = LIBVALKEY_STATE -> VkErr_ReplyError ;
408362 self -> pendingObject = NULL ;
409- self -> listOnly = 0 ;
363+ self -> convertSetsToLists = 0 ;
410364 Py_INCREF (self -> protocolErrorClass );
411365 Py_INCREF (self -> replyErrorClass );
412366 Py_INCREF (self -> notEnoughDataObject );
@@ -549,9 +503,9 @@ static PyObject *Reader_set_encoding(libvalkey_ReaderObject *self, PyObject *arg
549503
550504}
551505
552- static PyObject * Reader_listOnly (PyObject * obj , void * closure ) {
506+ static PyObject * Reader_convertSetsToLists (PyObject * obj , void * closure ) {
553507 libvalkey_ReaderObject * self = (libvalkey_ReaderObject * )obj ;
554- PyObject * result = PyBool_FromLong (self -> listOnly );
508+ PyObject * result = PyBool_FromLong (self -> convertSetsToLists );
555509 Py_INCREF (result );
556510 return result ;
557511}
0 commit comments