Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Lib/test/test_string/test_templatelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,25 @@ def test_template_values(self):
t = t'Hello, {name}, {age} from {country}'
self.assertEqual(t.values, ("Lys", 0, "GR"))

def test_repr(self):
self.assertEqual(repr(t''), 'Template()')
self.assertEqual(repr(t'foo'), "Template('foo')")
x = 42
self.assertEqual(
repr(t'{x}'),
"Template(Interpolation(42, 'x', None, ''))")
self.assertEqual(
repr(t'a{x!r:02}b'),
"Template('a', Interpolation(42, 'x', 'r', '02'), 'b')")

# Test a "recursive" template
x = []
t = t'a{x}b'
x.append(t)
self.assertEqual(
repr(t),
"Template('a', Interpolation([Template(...)], 'x', None, ''), 'b')")

def test_pickle_template(self):
user = 'test'
for template in (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Change :func:`repr` output of :class:`string.templatelib.Template` objects
to list the parts of the template in their original source order.
68 changes: 64 additions & 4 deletions Objects/templateobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,70 @@
template_repr(PyObject *op)
{
templateobject *self = templateobject_CAST(op);
return PyUnicode_FromFormat("%s(strings=%R, interpolations=%R)",
_PyType_Name(Py_TYPE(self)),
self->strings,
self->interpolations);

int res = Py_ReprEnter(self);

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-arm)

passing argument 1 of ‘Py_ReprEnter’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

passing argument 1 of ‘Py_ReprEnter’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Cross build Linux

passing argument 1 of ‘Py_ReprEnter’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (bolt) / build and test (ubuntu-24.04)

passing argument 1 of ‘Py_ReprEnter’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

passing argument 1 of ‘Py_ReprEnter’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04-arm)

passing argument 1 of ‘Py_ReprEnter’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, tail-call)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, tail-call)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, tail-call)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, tail-call)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

passing argument 1 of ‘Py_ReprEnter’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04)

passing argument 1 of ‘Py_ReprEnter’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [C:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [C:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 216 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]
if (res != 0) {
return (res > 0 ? PyUnicode_FromString("Template(...)") : NULL);
}

Py_ssize_t stringslen = PyTuple_GET_SIZE(self->strings);
Py_ssize_t interpolationslen = PyTuple_GET_SIZE(self->interpolations);

PyUnicodeWriter *writer = PyUnicodeWriter_Create(10);
if (writer == NULL) {
return NULL;
}

if (PyUnicodeWriter_WriteUTF8(writer, _PyType_Name(Py_TYPE(self)), -1) < 0) {
goto error;
}
if (PyUnicodeWriter_WriteChar(writer, '(') < 0) {
goto error;
}

/* Render the strings and interpolations in the order they appeared in the
constructor call, i.e. interleaved and skipping empty strings. This
matches the order produced by templateiter_next. */
int first = 1;
for (Py_ssize_t i = 0; i < stringslen; i++) {
PyObject *string = PyTuple_GET_ITEM(self->strings, i);
if (PyUnicode_GET_LENGTH(string) > 0) {
if (!first) {
if (PyUnicodeWriter_WriteASCII(writer, ", ", 2) < 0) {
goto error;
}
}
if (PyUnicodeWriter_WriteRepr(writer, string) < 0) {
goto error;
}
first = 0;
}
if (i < interpolationslen) {
PyObject *interpolation = PyTuple_GET_ITEM(self->interpolations, i);
if (!first) {
if (PyUnicodeWriter_WriteASCII(writer, ", ", 2) < 0) {
goto error;
}
}
if (PyUnicodeWriter_WriteRepr(writer, interpolation) < 0) {
goto error;
}
first = 0;
}
}

if (PyUnicodeWriter_WriteChar(writer, ')') < 0) {
goto error;
}

Py_ReprLeave(self);

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-arm)

passing argument 1 of ‘Py_ReprLeave’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

passing argument 1 of ‘Py_ReprLeave’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Cross build Linux

passing argument 1 of ‘Py_ReprLeave’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (bolt) / build and test (ubuntu-24.04)

passing argument 1 of ‘Py_ReprLeave’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

passing argument 1 of ‘Py_ReprLeave’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04-arm)

passing argument 1 of ‘Py_ReprLeave’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, tail-call)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, tail-call)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, tail-call)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, tail-call)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

passing argument 1 of ‘Py_ReprLeave’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04)

passing argument 1 of ‘Py_ReprLeave’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [C:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [C:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 271 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

return PyUnicodeWriter_Finish(writer);

error:
Py_ReprLeave(self);

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-arm)

passing argument 1 of ‘Py_ReprLeave’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

passing argument 1 of ‘Py_ReprLeave’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Cross build Linux

passing argument 1 of ‘Py_ReprLeave’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (bolt) / build and test (ubuntu-24.04)

passing argument 1 of ‘Py_ReprLeave’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

passing argument 1 of ‘Py_ReprLeave’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04-arm)

passing argument 1 of ‘Py_ReprLeave’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, tail-call)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, tail-call)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, tail-call)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, tail-call)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

passing argument 1 of ‘Py_ReprLeave’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04)

passing argument 1 of ‘Py_ReprLeave’ from incompatible pointer type [-Wincompatible-pointer-types]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [C:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [C:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 276 in Objects/templateobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64, switch-case)

'function': incompatible types - from 'templateobject *' to 'PyObject *' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]
PyUnicodeWriter_Discard(writer);
return NULL;
}

static PyObject *
Expand Down
Loading