From a7c1a00f695a2cadab82de4834a1767a1a1d86aa Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 20 Jun 2024 21:30:35 +0200 Subject: [PATCH] gh-119182: Use public PyUnicodeWriter in contextvar_tp_repr() The public PyUnicodeWriter API enables overallocation by default and so is more efficient. It also makes the code simpler and shorter. --- Python/context.c | 47 +++++++++++++++-------------------------------- 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/Python/context.c b/Python/context.c index 3937819b3c386c..42000b13038834 100644 --- a/Python/context.c +++ b/Python/context.c @@ -893,56 +893,39 @@ contextvar_tp_hash(PyContextVar *self) static PyObject * contextvar_tp_repr(PyContextVar *self) { - _PyUnicodeWriter writer; - - _PyUnicodeWriter_Init(&writer); - - if (_PyUnicodeWriter_WriteASCIIString( - &writer, "" + // "" + Py_ssize_t estimate = self->var_default ? 53 : 43; + PyUnicodeWriter *writer = PyUnicodeWriter_Create(estimate); + if (writer == NULL) { + return NULL; } - PyObject *name = PyObject_Repr(self->var_name); - if (name == NULL) { + if (PyUnicodeWriter_WriteUTF8(writer, "", self); - if (addr == NULL) { - goto error; - } - if (_PyUnicodeWriter_WriteStr(&writer, addr) < 0) { - Py_DECREF(addr); + if (PyUnicodeWriter_Format(writer, " at %p>", self) < 0) { goto error; } - Py_DECREF(addr); - - return _PyUnicodeWriter_Finish(&writer); + return PyUnicodeWriter_Finish(writer); error: - _PyUnicodeWriter_Dealloc(&writer); + PyUnicodeWriter_Discard(writer); return NULL; } 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