mirror of
https://github.com/python/cpython.git
synced 2025-10-14 10:53:40 +00:00
gh-125196: Use PyUnicodeWriter for repr(contextvars.Token) (#125220)
Replace the private _PyUnicodeWriter with the public PyUnicodeWriter.
This commit is contained in:
parent
1b2a5485f9
commit
942916378a
2 changed files with 19 additions and 28 deletions
|
@ -60,6 +60,14 @@ class ContextTest(unittest.TestCase):
|
||||||
c.reset(t)
|
c.reset(t)
|
||||||
self.assertIn(' used ', repr(t))
|
self.assertIn(' used ', repr(t))
|
||||||
|
|
||||||
|
@isolated_context
|
||||||
|
def test_token_repr_1(self):
|
||||||
|
c = contextvars.ContextVar('a')
|
||||||
|
tok = c.set(1)
|
||||||
|
self.assertRegex(repr(tok),
|
||||||
|
r"^<Token var=<ContextVar name='a' "
|
||||||
|
r"at 0x[0-9a-fA-F]+> at 0x[0-9a-fA-F]+>$")
|
||||||
|
|
||||||
def test_context_subclassing_1(self):
|
def test_context_subclassing_1(self):
|
||||||
with self.assertRaisesRegex(TypeError, 'not an acceptable base type'):
|
with self.assertRaisesRegex(TypeError, 'not an acceptable base type'):
|
||||||
class MyContextVar(contextvars.ContextVar):
|
class MyContextVar(contextvars.ContextVar):
|
||||||
|
|
|
@ -1154,48 +1154,31 @@ token_tp_dealloc(PyContextToken *self)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
token_tp_repr(PyContextToken *self)
|
token_tp_repr(PyContextToken *self)
|
||||||
{
|
{
|
||||||
_PyUnicodeWriter writer;
|
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
|
||||||
|
if (writer == NULL) {
|
||||||
_PyUnicodeWriter_Init(&writer);
|
return NULL;
|
||||||
|
}
|
||||||
if (_PyUnicodeWriter_WriteASCIIString(&writer, "<Token", 6) < 0) {
|
if (PyUnicodeWriter_WriteUTF8(writer, "<Token", 6) < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self->tok_used) {
|
if (self->tok_used) {
|
||||||
if (_PyUnicodeWriter_WriteASCIIString(&writer, " used", 5) < 0) {
|
if (PyUnicodeWriter_WriteUTF8(writer, " used", 5) < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (PyUnicodeWriter_WriteUTF8(writer, " var=", 5) < 0) {
|
||||||
if (_PyUnicodeWriter_WriteASCIIString(&writer, " var=", 5) < 0) {
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
if (PyUnicodeWriter_WriteRepr(writer, (PyObject *)self->tok_var) < 0) {
|
||||||
PyObject *var = PyObject_Repr((PyObject *)self->tok_var);
|
|
||||||
if (var == NULL) {
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if (_PyUnicodeWriter_WriteStr(&writer, var) < 0) {
|
if (PyUnicodeWriter_Format(writer, " at %p>", self) < 0) {
|
||||||
Py_DECREF(var);
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
Py_DECREF(var);
|
return PyUnicodeWriter_Finish(writer);
|
||||||
|
|
||||||
PyObject *addr = PyUnicode_FromFormat(" at %p>", self);
|
|
||||||
if (addr == NULL) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
if (_PyUnicodeWriter_WriteStr(&writer, addr) < 0) {
|
|
||||||
Py_DECREF(addr);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
Py_DECREF(addr);
|
|
||||||
|
|
||||||
return _PyUnicodeWriter_Finish(&writer);
|
|
||||||
|
|
||||||
error:
|
error:
|
||||||
_PyUnicodeWriter_Dealloc(&writer);
|
PyUnicodeWriter_Discard(writer);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue