@@ -307,6 +307,53 @@ test_L_code(PyObject *self, PyObject *args)
307307
308308#endif /* ifdef HAVE_LONG_LONG */
309309
310+ #ifdef Py_USING_UNICODE
311+
312+ /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
313+ of an error.
314+ */
315+ static PyObject *
316+ test_u_code (PyObject * self , PyObject * args )
317+ {
318+ PyObject * tuple , * obj ;
319+ Py_UNICODE * value ;
320+ int len ;
321+
322+ if (!PyArg_ParseTuple (args , ":test_u_code" ))
323+ return NULL ;
324+
325+ tuple = PyTuple_New (1 );
326+ if (tuple == NULL )
327+ return NULL ;
328+
329+ obj = PyUnicode_Decode ("test" , strlen ("test" ),
330+ "ascii" , NULL );
331+ if (obj == NULL )
332+ return NULL ;
333+
334+ PyTuple_SET_ITEM (tuple , 0 , obj );
335+
336+ value = 0 ;
337+ if (PyArg_ParseTuple (tuple , "u:test_u_code" , & value ) < 0 )
338+ return NULL ;
339+ if (value != PyUnicode_AS_UNICODE (obj ))
340+ return raiseTestError ("test_u_code" ,
341+ "u code returned wrong value for u'test'" );
342+ value = 0 ;
343+ if (PyArg_ParseTuple (tuple , "u#:test_u_code" , & value , & len ) < 0 )
344+ return NULL ;
345+ if (value != PyUnicode_AS_UNICODE (obj ) ||
346+ len != PyUnicode_GET_SIZE (obj ))
347+ return raiseTestError ("test_u_code" ,
348+ "u# code returned wrong values for u'test'" );
349+
350+ Py_DECREF (tuple );
351+ Py_INCREF (Py_None );
352+ return Py_None ;
353+ }
354+
355+ #endif
356+
310357static PyObject *
311358raise_exception (PyObject * self , PyObject * args )
312359{
@@ -342,6 +389,9 @@ static PyMethodDef TestMethods[] = {
342389#ifdef HAVE_LONG_LONG
343390 {"test_longlong_api" , test_longlong_api , METH_VARARGS },
344391 {"test_L_code" , test_L_code , METH_VARARGS },
392+ #endif
393+ #ifdef Py_USING_UNICODE
394+ {"test_u_code" , test_u_code , METH_VARARGS },
345395#endif
346396 {NULL , NULL } /* sentinel */
347397};
0 commit comments