Commit 21a663ea authored by Serhiy Storchaka's avatar Serhiy Storchaka

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

parent 131b8f8e
This diff is collapsed.
......@@ -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)) {
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_FromObject(obj);
obj2 = PyUnicode_AsEncodedString(obj, "idna", NULL);
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;
assert(PyBytes_Check(obj2));
data->obj = obj2;
data->buf = PyBytes_AS_STRING(obj2);
len = PyBytes_GET_SIZE(obj2);
}
data->obj = obj3;
data->buf = PyBytes_AS_STRING(obj3);
len = PyBytes_GET_SIZE(obj3);
}
else {
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);
......
......@@ -123,11 +123,6 @@ STRINGLIB(parse_args_finds)(const char * function_name, PyObject *args,
/*
Wraps stringlib_parse_args_finds() and additionally ensures that the
first argument is a unicode object.
Note that we receive a pointer to the pointer of the substring object,
so when we create that object in this function we don't DECREF it,
because it continues living in the caller functions (those functions,
after finishing using the substring, must DECREF it).
*/
Py_LOCAL_INLINE(int)
......@@ -135,14 +130,10 @@ STRINGLIB(parse_args_finds_unicode)(const char * function_name, PyObject *args,
PyObject **substring,
Py_ssize_t *start, Py_ssize_t *end)
{
PyObject *tmp_substring;
if(STRINGLIB(parse_args_finds)(function_name, args, &tmp_substring,
if(STRINGLIB(parse_args_finds)(function_name, args, substring,
start, end)) {
tmp_substring = PyUnicode_FromObject(tmp_substring);
if (!tmp_substring)
if (ensure_unicode(*substring) < 0)
return 0;
*substring = tmp_substring;
return 1;
}
return 0;
......
This diff is collapsed.
......@@ -1931,9 +1931,8 @@ builtin_input_impl(PyModuleDef *module, PyObject *prompt)
Py_CLEAR(stringpo);
if (po == NULL)
goto _readline_errors;
promptstr = PyBytes_AsString(po);
if (promptstr == NULL)
goto _readline_errors;
assert(PyBytes_Check(po));
promptstr = PyBytes_AS_STRING(po);
}
else {
po = NULL;
......
......@@ -1056,35 +1056,25 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
return converterr("(AsCharBuffer failed)",
arg, msgbuf, bufsize);
}
else {
PyObject *u;
/* Convert object to Unicode */
u = PyUnicode_FromObject(arg);
if (u == NULL)
return converterr(
"string or unicode or text buffer",
arg, msgbuf, bufsize);
else if (PyUnicode_Check(arg)) {
/* Encode object; use default error handling */
s = PyUnicode_AsEncodedString(u,
s = PyUnicode_AsEncodedString(arg,
encoding,
NULL);
Py_DECREF(u);
if (s == NULL)
return converterr("(encoding failed)",
arg, msgbuf, bufsize);
if (!PyBytes_Check(s)) {
Py_DECREF(s);
return converterr(
"(encoder failed to return bytes)",
arg, msgbuf, bufsize);
}
assert(PyBytes_Check(s));
size = PyBytes_GET_SIZE(s);
ptr = PyBytes_AS_STRING(s);
if (ptr == NULL)
ptr = "";
}
else {
return converterr(
recode_strings ? "str" : "str, bytes or bytearray",
arg, msgbuf, bufsize);
}
/* Write output; output is guaranteed to be 0-terminated */
if (*format == '#') {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment