@@ -1389,6 +1389,7 @@ static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject
13891389 PyTypeObject * result ;
13901390 StgDictObject * stgdict ;
13911391 PyObject * name = PyTuple_GET_ITEM (args , 0 );
1392+ PyObject * newname ;
13921393 PyObject * swapped_args ;
13931394 static PyObject * suffix ;
13941395 Py_ssize_t i ;
@@ -1399,17 +1400,17 @@ static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject
13991400
14001401 if (suffix == NULL )
14011402#ifdef WORDS_BIGENDIAN
1402- suffix = PyString_FromString ("_le" );
1403+ suffix = PyUnicode_FromString ("_le" );
14031404#else
1404- suffix = PyString_FromString ("_be" );
1405+ suffix = PyUnicode_FromString ("_be" );
14051406#endif
14061407
1407- Py_INCREF (name );
1408- PyString_Concat (& name , suffix );
1409- if (name == NULL )
1408+ newname = PyUnicode_Concat (name , suffix );
1409+ if (newname == NULL ) {
14101410 return NULL ;
1411+ }
14111412
1412- PyTuple_SET_ITEM (swapped_args , 0 , name );
1413+ PyTuple_SET_ITEM (swapped_args , 0 , newname );
14131414 for (i = 1 ; i < PyTuple_GET_SIZE (args ); ++ i ) {
14141415 PyObject * v = PyTuple_GET_ITEM (args , i );
14151416 Py_INCREF (v );
@@ -1484,6 +1485,8 @@ SimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
14841485 PyTypeObject * result ;
14851486 StgDictObject * stgdict ;
14861487 PyObject * proto ;
1488+ const char * proto_str ;
1489+ int proto_len ;
14871490 PyMethodDef * ml ;
14881491 struct fielddesc * fmt ;
14891492
@@ -1494,24 +1497,52 @@ SimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
14941497 return NULL ;
14951498
14961499 proto = PyObject_GetAttrString ((PyObject * )result , "_type_" ); /* new ref */
1497- if (!proto
1498- || !PyString_Check (proto )
1499- || 1 != strlen (PyString_AS_STRING (proto ))
1500- || !strchr (SIMPLE_TYPE_CHARS , PyString_AS_STRING (proto )[0 ])) {
1500+ if (!proto ) {
1501+ PyErr_SetString (PyExc_AttributeError ,
1502+ "class must define a '_type_' attribute" );
1503+ error :
1504+ Py_XDECREF (proto );
1505+ Py_XDECREF (result );
1506+ return NULL ;
1507+ }
1508+ if (PyUnicode_Check (proto )) {
1509+ PyObject * v = _PyUnicode_AsDefaultEncodedString (proto , NULL );
1510+ if (!v )
1511+ goto error ;
1512+ proto_str = PyString_AS_STRING (v );
1513+ proto_len = PyString_GET_SIZE (v );
1514+ }
1515+ else if (PyString_Check (proto )) {
1516+ proto_str = PyString_AS_STRING (proto );
1517+ proto_len = PyString_GET_SIZE (proto );
1518+ }
1519+ else if (PyBytes_Check (proto )) {
1520+ proto_str = PyBytes_AS_STRING (proto );
1521+ proto_len = PyBytes_GET_SIZE (proto );
1522+ }
1523+ else {
1524+ PyErr_SetString (PyExc_TypeError ,
1525+ "class must define a '_type_' string attribute" );
1526+ goto error ;
1527+ }
1528+ if (proto_len != 1 ) {
1529+ PyErr_SetString (PyExc_ValueError ,
1530+ "class must define a '_type_' attribute "
1531+ "which must be a string of length 1" );
1532+ goto error ;
1533+ }
1534+ if (!strchr (SIMPLE_TYPE_CHARS , * proto_str )) {
15011535 PyErr_Format (PyExc_AttributeError ,
15021536 "class must define a '_type_' attribute which must be\n"
15031537 "a single character string containing one of '%s'." ,
15041538 SIMPLE_TYPE_CHARS );
1505- Py_XDECREF (proto );
1506- Py_DECREF (result );
1507- return NULL ;
1539+ goto error ;
15081540 }
1509- fmt = getentry (PyString_AS_STRING ( proto ) );
1541+ fmt = getentry (proto_str );
15101542 if (fmt == NULL ) {
15111543 Py_DECREF (result );
15121544 PyErr_Format (PyExc_ValueError ,
1513- "_type_ '%s' not supported" ,
1514- PyString_AS_STRING (proto ));
1545+ "_type_ '%s' not supported" , proto_str );
15151546 return NULL ;
15161547 }
15171548
@@ -1551,7 +1582,7 @@ SimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
15511582 Overrides the SimpleType_from_param generic method.
15521583 */
15531584 if (result -> tp_base == & Simple_Type ) {
1554- switch (PyString_AS_STRING ( proto )[ 0 ] ) {
1585+ switch (* proto_str ) {
15551586 case 'z' : /* c_char_p */
15561587 ml = & c_char_p_method ;
15571588 break ;
0 commit comments