Issue #26057: Got rid of nonneeded use of PyUnicode_FromObject().

This commit is contained in:
Serhiy Storchaka 2016-04-13 15:37:23 +03:00
parent 131b8f8eee
commit 21a663ea28
6 changed files with 240 additions and 641 deletions

View file

@ -1401,7 +1401,7 @@ static int
idna_converter(PyObject *obj, struct maybe_idna *data)
{
size_t len;
PyObject *obj2, *obj3;
PyObject *obj2;
if (obj == NULL) {
idna_cleanup(data);
return 1;
@ -1416,31 +1416,27 @@ idna_converter(PyObject *obj, struct maybe_idna *data)
data->buf = PyByteArray_AsString(obj);
len = PyByteArray_Size(obj);
}
else if (PyUnicode_Check(obj) && PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) {
data->buf = PyUnicode_DATA(obj);
len = PyUnicode_GET_LENGTH(obj);
else if (PyUnicode_Check(obj)) {
if (PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) {
data->buf = PyUnicode_DATA(obj);
len = PyUnicode_GET_LENGTH(obj);
}
else {
obj2 = PyUnicode_AsEncodedString(obj, "idna", NULL);
if (!obj2) {
PyErr_SetString(PyExc_TypeError, "encoding of hostname failed");
return 0;
}
assert(PyBytes_Check(obj2));
data->obj = obj2;
data->buf = PyBytes_AS_STRING(obj2);
len = PyBytes_GET_SIZE(obj2);
}
}
else {
obj2 = PyUnicode_FromObject(obj);
if (!obj2) {
PyErr_Format(PyExc_TypeError, "string or unicode text buffer expected, not %s",
obj->ob_type->tp_name);
return 0;
}
obj3 = PyUnicode_AsEncodedString(obj2, "idna", NULL);
Py_DECREF(obj2);
if (!obj3) {
PyErr_SetString(PyExc_TypeError, "encoding of hostname failed");
return 0;
}
if (!PyBytes_Check(obj3)) {
Py_DECREF(obj3);
PyErr_SetString(PyExc_TypeError, "encoding of hostname failed to return bytes");
return 0;
}
data->obj = obj3;
data->buf = PyBytes_AS_STRING(obj3);
len = PyBytes_GET_SIZE(obj3);
PyErr_Format(PyExc_TypeError, "str, bytes or bytearray expected, not %s",
obj->ob_type->tp_name);
return 0;
}
if (strlen(data->buf) != len) {
Py_CLEAR(data->obj);