From db0181e4044f9f6828fbff94895df9c2505e6fcb Mon Sep 17 00:00:00 2001 From: monadchains Date: Wed, 19 Oct 2022 01:18:11 +0200 Subject: [PATCH 1/2] clean up PyObject_Print --- Objects/object.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/Objects/object.c b/Objects/object.c index 26cdcef59dddbd..a905bb3077ef32 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -282,13 +282,10 @@ 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)) { + else { PyObject *t; t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace"); if (t == NULL) { @@ -300,12 +297,6 @@ PyObject_Print(PyObject *op, FILE *fp, int flags) Py_DECREF(t); } } - else { - PyErr_Format(PyExc_TypeError, - "str() or repr() returned '%.100s'", - Py_TYPE(s)->tp_name); - ret = -1; - } Py_XDECREF(s); } } From 9192ae2f484a34f53c7e8a23f67eaf1939a65f80 Mon Sep 17 00:00:00 2001 From: monadchains Date: Thu, 20 Oct 2022 01:56:47 +0200 Subject: [PATCH 2/2] address reviews --- Objects/object.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Objects/object.c b/Objects/object.c index a905bb3077ef32..837f0a1a578ce6 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -286,18 +286,18 @@ PyObject_Print(PyObject *op, FILE *fp, int flags) ret = -1; } else { - PyObject *t; - t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace"); + 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); } - Py_XDECREF(s); } } if (ret == 0) { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy