Issue #26057: Got rid of nonneeded use of PyUnicode_FromObject().
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index bcff004..ec35fb9 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -1401,7 +1401,7 @@
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 @@
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);