@@ -221,10 +221,15 @@ unicode_copycharacters(PyObject *self, PyObject *args)
221
221
}
222
222
223
223
static PyObject *
224
- unicode_case_operation (PyObject * str , int (* function )(Py_UCS4 , Py_UCS4 * , int ), const char * name )
224
+ unicode_case_operation (PyObject * str , int (* function )(Py_UCS4 , Py_UCS4 * , int ))
225
225
{
226
+ if (!PyUnicode_Check (str )) {
227
+ PyErr_Format (PyExc_TypeError , "expect str type, got %T" , str );
228
+ return NULL ;
229
+ }
230
+
226
231
if (PyUnicode_GET_LENGTH (str ) != 1 ) {
227
- PyErr_Format (PyExc_ValueError , "%s only accepts 1-character strings" , name );
232
+ PyErr_SetString (PyExc_ValueError , "expecting 1-character strings only" );
228
233
return NULL ;
229
234
}
230
235
@@ -233,48 +238,39 @@ unicode_case_operation(PyObject *str, int (*function)(Py_UCS4, Py_UCS4 *, int),
233
238
Py_UCS4 buf [3 ];
234
239
int chars = function (c , buf , Py_ARRAY_LENGTH (buf ));
235
240
if (chars <= 0 ) {
236
- PyErr_BadInternalCall ();
237
241
return NULL ;
238
242
}
239
243
240
- PyUnicodeWriter * writer = PyUnicodeWriter_Create (1 );
241
- if (writer == NULL ) {
242
- return NULL ;
243
- }
244
- if (PyUnicodeWriter_WriteUCS4 (writer , buf , chars ) < 0 ) {
245
- PyUnicodeWriter_Discard (writer );
246
- return NULL ;
247
- }
248
- return PyUnicodeWriter_Finish (writer );
244
+ return PyUnicode_FromKindAndData (PyUnicode_4BYTE_KIND , buf , chars );
249
245
}
250
246
251
247
/* Test PyUnicode_ToLower() */
252
248
static PyObject *
253
249
unicode_tolower (PyObject * self , PyObject * arg )
254
250
{
255
- return unicode_case_operation (arg , PyUnicode_ToLower , "unicode_tolower" );
251
+ return unicode_case_operation (arg , PyUnicode_ToLower );
256
252
}
257
253
258
254
/* Test PyUnicode_ToUpper() */
259
255
static PyObject *
260
256
unicode_toupper (PyObject * self , PyObject * arg )
261
257
{
262
- return unicode_case_operation (arg , PyUnicode_ToUpper , "unicode_toupper" );
258
+ return unicode_case_operation (arg , PyUnicode_ToUpper );
263
259
}
264
260
265
261
266
262
/* Test PyUnicode_ToLower() */
267
263
static PyObject *
268
264
unicode_totitle (PyObject * self , PyObject * arg )
269
265
{
270
- return unicode_case_operation (arg , PyUnicode_ToTitle , "unicode_totitle" );
266
+ return unicode_case_operation (arg , PyUnicode_ToTitle );
271
267
}
272
268
273
269
/* Test PyUnicode_ToLower() */
274
270
static PyObject *
275
271
unicode_tofolded (PyObject * self , PyObject * arg )
276
272
{
277
- return unicode_case_operation (arg , PyUnicode_ToFolded , "unicode_tofolded" );
273
+ return unicode_case_operation (arg , PyUnicode_ToFolded );
278
274
}
279
275
280
276
0 commit comments