summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMonadChains <monadchains@gmail.com>2022-10-20 13:59:10 (GMT)
committerGitHub <noreply@github.com>2022-10-20 13:59:10 (GMT)
commitc60b3b3cca9cab5f5dab7217660c45ebfdb1d7b8 (patch)
tree23eeb32958ffcd5544d826d2cc0e3fa25ccea50b
parente48f9b2b7e73f4a89a9b9c287f3b93dc13a60460 (diff)
downloadcpython-c60b3b3cca9cab5f5dab7217660c45ebfdb1d7b8.zip
cpython-c60b3b3cca9cab5f5dab7217660c45ebfdb1d7b8.tar.gz
cpython-c60b3b3cca9cab5f5dab7217660c45ebfdb1d7b8.tar.bz2
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.
-rw-r--r--Objects/object.c25
1 files changed, 8 insertions, 17 deletions
diff --git a/Objects/object.c b/Objects/object.c
index 26cdcef..837f0a1 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -282,31 +282,22 @@ PyObject_Print(PyObject *op, FILE *fp, int flags)
s = PyObject_Str(op);
else
s = PyObject_Repr(op);
- if (s == NULL)
+ if (s == NULL) {
ret = -1;
- else if (PyBytes_Check(s)) {
- fwrite(PyBytes_AS_STRING(s), 1,
- PyBytes_GET_SIZE(s), fp);
}
- else if (PyUnicode_Check(s)) {
- PyObject *t;
- t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
+ else {
+ assert(PyUnicode_Check(s));
+ const char *t;
+ Py_ssize_t len;
+ t = PyUnicode_AsUTF8AndSize(s, &len);
if (t == NULL) {
ret = -1;
}
else {
- fwrite(PyBytes_AS_STRING(t), 1,
- PyBytes_GET_SIZE(t), fp);
- Py_DECREF(t);
+ fwrite(t, 1, len, fp);
}
+ 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) {