mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
gh-98421: Clean Up PyObject_Print (GH-98422)
Work on test coverage for `PyObject_Print` made it clear that some lines can't get executed. Simplify the function by excluding the checks for non-string types. Also eliminate creating a temporary bytes object.
This commit is contained in:
parent
e48f9b2b7e
commit
c60b3b3cca
1 changed files with 8 additions and 17 deletions
|
@ -282,31 +282,22 @@ PyObject_Print(PyObject *op, FILE *fp, int flags)
|
||||||
s = PyObject_Str(op);
|
s = PyObject_Str(op);
|
||||||
else
|
else
|
||||||
s = PyObject_Repr(op);
|
s = PyObject_Repr(op);
|
||||||
if (s == NULL)
|
if (s == NULL) {
|
||||||
ret = -1;
|
ret = -1;
|
||||||
else if (PyBytes_Check(s)) {
|
|
||||||
fwrite(PyBytes_AS_STRING(s), 1,
|
|
||||||
PyBytes_GET_SIZE(s), fp);
|
|
||||||
}
|
}
|
||||||
else if (PyUnicode_Check(s)) {
|
else {
|
||||||
PyObject *t;
|
assert(PyUnicode_Check(s));
|
||||||
t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
|
const char *t;
|
||||||
|
Py_ssize_t len;
|
||||||
|
t = PyUnicode_AsUTF8AndSize(s, &len);
|
||||||
if (t == NULL) {
|
if (t == NULL) {
|
||||||
ret = -1;
|
ret = -1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
fwrite(PyBytes_AS_STRING(t), 1,
|
fwrite(t, 1, len, fp);
|
||||||
PyBytes_GET_SIZE(t), fp);
|
|
||||||
Py_DECREF(t);
|
|
||||||
}
|
}
|
||||||
|
Py_DECREF(s);
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
PyErr_Format(PyExc_TypeError,
|
|
||||||
"str() or repr() returned '%.100s'",
|
|
||||||
Py_TYPE(s)->tp_name);
|
|
||||||
ret = -1;
|
|
||||||
}
|
|
||||||
Py_XDECREF(s);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue