@@ -141,6 +141,122 @@ getargs_w_star(PyObject *self, PyObject *args)
141141 return result ;
142142}
143143
144+ static PyObject *
145+ getargs_w_star_opt (PyObject * self , PyObject * args )
146+ {
147+ Py_buffer buffer ;
148+ Py_buffer buf2 ;
149+ int number = 1 ;
150+
151+ if (!PyArg_ParseTuple (args , "w*|w*i:getargs_w_star" ,
152+ & buffer , & buf2 , & number )) {
153+ return NULL ;
154+ }
155+
156+ if (2 <= buffer .len ) {
157+ char * str = buffer .buf ;
158+ str [0 ] = '[' ;
159+ str [buffer .len - 1 ] = ']' ;
160+ }
161+
162+ PyObject * result = PyBytes_FromStringAndSize (buffer .buf , buffer .len );
163+ PyBuffer_Release (& buffer );
164+ return result ;
165+ }
166+
167+ /* Test the old w and w# codes that no longer work */
168+ static PyObject *
169+ test_w_code_invalid (PyObject * self , PyObject * arg )
170+ {
171+ static const char * const keywords [] = {"a" , "b" , "c" , "d" , NULL };
172+ char * formats_3 [] = {"O|w#$O" ,
173+ "O|w$O" ,
174+ "O|w#O" ,
175+ "O|wO" ,
176+ NULL };
177+ char * formats_4 [] = {"O|w#O$O" ,
178+ "O|wO$O" ,
179+ "O|Ow#O" ,
180+ "O|OwO" ,
181+ "O|Ow#$O" ,
182+ "O|Ow$O" ,
183+ NULL };
184+ size_t n ;
185+ PyObject * args ;
186+ PyObject * kwargs ;
187+ PyObject * tmp ;
188+
189+ if (!(args = PyTuple_Pack (1 , Py_None ))) {
190+ return NULL ;
191+ }
192+
193+ kwargs = PyDict_New ();
194+ if (!kwargs ) {
195+ Py_DECREF (args );
196+ return NULL ;
197+ }
198+
199+ if (PyDict_SetItemString (kwargs , "c" , Py_None )) {
200+ Py_DECREF (args );
201+ Py_XDECREF (kwargs );
202+ return NULL ;
203+ }
204+
205+ for (n = 0 ; formats_3 [n ]; ++ n ) {
206+ if (PyArg_ParseTupleAndKeywords (args , kwargs , formats_3 [n ],
207+ (char * * ) keywords ,
208+ & tmp , & tmp , & tmp )) {
209+ Py_DECREF (args );
210+ Py_DECREF (kwargs );
211+ PyErr_Format (PyExc_AssertionError ,
212+ "test_w_code_invalid_suffix: %s" ,
213+ formats_3 [n ]);
214+ return NULL ;
215+ }
216+ else {
217+ if (!PyErr_ExceptionMatches (PyExc_SystemError )) {
218+ Py_DECREF (args );
219+ Py_DECREF (kwargs );
220+ return NULL ;
221+ }
222+ PyErr_Clear ();
223+ }
224+ }
225+
226+ if (PyDict_DelItemString (kwargs , "c" ) ||
227+ PyDict_SetItemString (kwargs , "d" , Py_None )) {
228+
229+ Py_DECREF (kwargs );
230+ Py_DECREF (args );
231+ return NULL ;
232+ }
233+
234+ for (n = 0 ; formats_4 [n ]; ++ n ) {
235+ if (PyArg_ParseTupleAndKeywords (args , kwargs , formats_4 [n ],
236+ (char * * ) keywords ,
237+ & tmp , & tmp , & tmp , & tmp )) {
238+ Py_DECREF (args );
239+ Py_DECREF (kwargs );
240+ PyErr_Format (PyExc_AssertionError ,
241+ "test_w_code_invalid_suffix: %s" ,
242+ formats_4 [n ]);
243+ return NULL ;
244+ }
245+ else {
246+ if (!PyErr_ExceptionMatches (PyExc_SystemError )) {
247+ Py_DECREF (args );
248+ Py_DECREF (kwargs );
249+ return NULL ;
250+ }
251+ PyErr_Clear ();
252+ }
253+ }
254+
255+ Py_DECREF (args );
256+ Py_DECREF (kwargs );
257+ Py_RETURN_NONE ;
258+ }
259+
144260static PyObject *
145261getargs_empty (PyObject * self , PyObject * args , PyObject * kwargs )
146262{
@@ -684,6 +800,7 @@ static PyMethodDef test_methods[] = {
684800 {"getargs_s_star" , getargs_s_star , METH_VARARGS },
685801 {"getargs_tuple" , getargs_tuple , METH_VARARGS },
686802 {"getargs_w_star" , getargs_w_star , METH_VARARGS },
803+ {"getargs_w_star_opt" , getargs_w_star_opt , METH_VARARGS },
687804 {"getargs_empty" , _PyCFunction_CAST (getargs_empty ), METH_VARARGS |METH_KEYWORDS },
688805 {"getargs_y" , getargs_y , METH_VARARGS },
689806 {"getargs_y_hash" , getargs_y_hash , METH_VARARGS },
@@ -693,6 +810,7 @@ static PyMethodDef test_methods[] = {
693810 {"getargs_z_star" , getargs_z_star , METH_VARARGS },
694811 {"parse_tuple_and_keywords" , parse_tuple_and_keywords , METH_VARARGS },
695812 {"gh_99240_clear_args" , gh_99240_clear_args , METH_VARARGS },
813+ {"test_w_code_invalid" , test_w_code_invalid , METH_NOARGS },
696814 {NULL },
697815};
698816
0 commit comments