@@ -1100,25 +1100,45 @@ O_set(void *ptr, PyObject *value, Py_ssize_t size)
11001100static PyObject *
11011101c_set (void * ptr , PyObject * value , Py_ssize_t size )
11021102{
1103- if (PyBytes_Check (value ) && PyBytes_GET_SIZE (value ) == 1 ) {
1103+ if (PyBytes_Check (value )) {
1104+ if (PyBytes_GET_SIZE (value ) != 1 ) {
1105+ PyErr_Format (PyExc_TypeError ,
1106+ "one character bytes, bytearray or integer "
1107+ "in range(256) expected, not bytes of length %zd" ,
1108+ PyBytes_GET_SIZE (value ));
1109+ return NULL ;
1110+ }
11041111 * (char * )ptr = PyBytes_AS_STRING (value )[0 ];
11051112 _RET (value );
11061113 }
1107- if (PyByteArray_Check (value ) && PyByteArray_GET_SIZE (value ) == 1 ) {
1114+ if (PyByteArray_Check (value )) {
1115+ if (PyByteArray_GET_SIZE (value ) != 1 ) {
1116+ PyErr_Format (PyExc_TypeError ,
1117+ "one character bytes, bytearray or integer "
1118+ "in range(256) expected, not bytearray of length %zd" ,
1119+ PyByteArray_GET_SIZE (value ));
1120+ return NULL ;
1121+ }
11081122 * (char * )ptr = PyByteArray_AS_STRING (value )[0 ];
11091123 _RET (value );
11101124 }
1111- if (PyLong_Check (value ))
1112- {
1113- long longval = PyLong_AsLong (value );
1114- if (longval < 0 || longval >= 256 )
1115- goto error ;
1125+ if (PyLong_Check (value )) {
1126+ int overflow ;
1127+ long longval = PyLong_AsLongAndOverflow (value , & overflow );
1128+ if (longval == -1 && PyErr_Occurred ()) {
1129+ return NULL ;
1130+ }
1131+ if (overflow || longval < 0 || longval >= 256 ) {
1132+ PyErr_SetString (PyExc_TypeError , "integer not in range(256)" );
1133+ return NULL ;
1134+ }
11161135 * (char * )ptr = (char )longval ;
11171136 _RET (value );
11181137 }
1119- error :
11201138 PyErr_Format (PyExc_TypeError ,
1121- "one character bytes, bytearray or integer expected" );
1139+ "one character bytes, bytearray or integer "
1140+ "in range(256) expected, not %s" ,
1141+ Py_TYPE (value )-> tp_name );
11221142 return NULL ;
11231143}
11241144
@@ -1137,22 +1157,27 @@ u_set(void *ptr, PyObject *value, Py_ssize_t size)
11371157 wchar_t chars [2 ];
11381158 if (!PyUnicode_Check (value )) {
11391159 PyErr_Format (PyExc_TypeError ,
1140- "unicode string expected instead of %s instance" ,
1141- Py_TYPE (value )-> tp_name );
1160+ "unicode character expected instead of %s instance" ,
1161+ Py_TYPE (value )-> tp_name );
11421162 return NULL ;
1143- } else
1144- Py_INCREF (value );
1163+ }
11451164
11461165 len = PyUnicode_AsWideChar (value , chars , 2 );
11471166 if (len != 1 ) {
1148- Py_DECREF (value );
1149- PyErr_SetString (PyExc_TypeError ,
1150- "one character unicode string expected" );
1167+ if (PyUnicode_GET_LENGTH (value ) != 1 ) {
1168+ PyErr_Format (PyExc_TypeError ,
1169+ "unicode character expected instead of string of length %zd" ,
1170+ PyUnicode_GET_LENGTH (value ));
1171+ }
1172+ else {
1173+ PyErr_Format (PyExc_TypeError ,
1174+ "string %A cannot be converted to a single wchar_t character" ,
1175+ value );
1176+ }
11511177 return NULL ;
11521178 }
11531179
11541180 * (wchar_t * )ptr = chars [0 ];
1155- Py_DECREF (value );
11561181
11571182 _RET (value );
11581183}
0 commit comments