@@ -233,13 +233,20 @@ static int
233233PyCurses_ConvertToChtype (PyCursesWindowObject * win , PyObject * obj , chtype * ch )
234234{
235235 long value ;
236- if (PyBytes_Check (obj ) && PyBytes_Size (obj ) == 1 ) {
236+ if (PyBytes_Check (obj )) {
237+ if (PyBytes_GET_SIZE (obj ) != 1 ) {
238+ PyErr_Format (PyExc_TypeError ,
239+ "expect int or bytes or str of length 1, "
240+ "got a bytes of length %zd" ,
241+ PyBytes_GET_SIZE (obj ));
242+ return 0 ;
243+ }
237244 value = (unsigned char )PyBytes_AsString (obj )[0 ];
238245 }
239246 else if (PyUnicode_Check (obj )) {
240- if (PyUnicode_GetLength (obj ) != 1 ) {
247+ if (PyUnicode_GET_LENGTH (obj ) != 1 ) {
241248 PyErr_Format (PyExc_TypeError ,
242- "expect bytes or str of length 1, or int , "
249+ "expect int or bytes or str of length 1, "
243250 "got a str of length %zi" ,
244251 PyUnicode_GET_LENGTH (obj ));
245252 return 0 ;
@@ -272,7 +279,7 @@ PyCurses_ConvertToChtype(PyCursesWindowObject *win, PyObject *obj, chtype *ch)
272279 }
273280 else {
274281 PyErr_Format (PyExc_TypeError ,
275- "expect bytes or str of length 1, or int , got %s" ,
282+ "expect int or bytes or str of length 1, got %s" ,
276283 Py_TYPE (obj )-> tp_name );
277284 return 0 ;
278285 }
@@ -315,7 +322,7 @@ PyCurses_ConvertToCchar_t(PyCursesWindowObject *win, PyObject *obj,
315322#ifdef HAVE_NCURSESW
316323 if (PyUnicode_AsWideChar (obj , buffer , 2 ) != 1 ) {
317324 PyErr_Format (PyExc_TypeError ,
318- "expect bytes or str of length 1, or int , "
325+ "expect int or bytes or str of length 1, "
319326 "got a str of length %zi" ,
320327 PyUnicode_GET_LENGTH (obj ));
321328 return 0 ;
@@ -326,7 +333,14 @@ PyCurses_ConvertToCchar_t(PyCursesWindowObject *win, PyObject *obj,
326333 return PyCurses_ConvertToChtype (win , obj , ch );
327334#endif
328335 }
329- else if (PyBytes_Check (obj ) && PyBytes_Size (obj ) == 1 ) {
336+ else if (PyBytes_Check (obj )) {
337+ if (PyBytes_GET_SIZE (obj ) != 1 ) {
338+ PyErr_Format (PyExc_TypeError ,
339+ "expect int or bytes or str of length 1, "
340+ "got a bytes of length %zd" ,
341+ PyBytes_GET_SIZE (obj ));
342+ return 0 ;
343+ }
330344 value = (unsigned char )PyBytes_AsString (obj )[0 ];
331345 }
332346 else if (PyLong_CheckExact (obj )) {
@@ -340,7 +354,7 @@ PyCurses_ConvertToCchar_t(PyCursesWindowObject *win, PyObject *obj,
340354 }
341355 else {
342356 PyErr_Format (PyExc_TypeError ,
343- "expect bytes or str of length 1, or int , got %s" ,
357+ "expect int or bytes or str of length 1, got %s" ,
344358 Py_TYPE (obj )-> tp_name );
345359 return 0 ;
346360 }
@@ -4443,7 +4457,7 @@ PyCurses_ConvertToWchar_t(PyObject *obj,
44434457 wchar_t buffer [2 ];
44444458 if (PyUnicode_AsWideChar (obj , buffer , 2 ) != 1 ) {
44454459 PyErr_Format (PyExc_TypeError ,
4446- "expect str of length 1 or int , "
4460+ "expect int or str of length 1, "
44474461 "got a str of length %zi" ,
44484462 PyUnicode_GET_LENGTH (obj ));
44494463 return 0 ;
@@ -4470,7 +4484,7 @@ PyCurses_ConvertToWchar_t(PyObject *obj,
44704484 }
44714485 else {
44724486 PyErr_Format (PyExc_TypeError ,
4473- "expect str of length 1 or int , got %s" ,
4487+ "expect int or str of length 1, got %s" ,
44744488 Py_TYPE (obj )-> tp_name );
44754489 return 0 ;
44764490 }
0 commit comments