Skip to content

Commit f1b8d01

Browse files
authored
gh-132661: Add default value (of "") for Interpolation.expression (#136441)
1 parent f519918 commit f1b8d01

File tree

5 files changed

+58
-27
lines changed

5 files changed

+58
-27
lines changed

Lib/test/test_string/_support.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,45 @@
22

33

44
class TStringBaseCase:
5+
def assertInterpolationEqual(self, i, exp):
6+
"""Test Interpolation equality.
7+
8+
The *i* argument must be an Interpolation instance.
9+
10+
The *exp* argument must be a tuple of the form
11+
(value, expression, conversion, format_spec) where the final three
12+
items may be omitted and are assumed to be '', None and '' respectively.
13+
"""
14+
if len(exp) == 4:
15+
actual = (i.value, i.expression, i.conversion, i.format_spec)
16+
self.assertEqual(actual, exp)
17+
elif len(exp) == 3:
18+
self.assertEqual((i.value, i.expression, i.conversion), exp)
19+
self.assertEqual(i.format_spec, "")
20+
elif len(exp) == 2:
21+
self.assertEqual((i.value, i.expression), exp)
22+
self.assertEqual(i.conversion, None)
23+
self.assertEqual(i.format_spec, "")
24+
elif len(exp) == 1:
25+
self.assertEqual((i.value,), exp)
26+
self.assertEqual(i.expression, "")
27+
self.assertEqual(i.conversion, None)
28+
self.assertEqual(i.format_spec, "")
29+
530
def assertTStringEqual(self, t, strings, interpolations):
631
"""Test template string literal equality.
732
833
The *strings* argument must be a tuple of strings equal to *t.strings*.
934
1035
The *interpolations* argument must be a sequence of tuples which are
11-
compared against *t.interpolations*. Each tuple consists of
12-
(value, expression, conversion, format_spec), though the final two
13-
items may be omitted, and are assumed to be None and '' respectively.
36+
compared against *t.interpolations*. Each tuple must match the form
37+
described in the `assertInterpolationEqual` method.
1438
"""
1539
self.assertEqual(t.strings, strings)
1640
self.assertEqual(len(t.interpolations), len(interpolations))
1741

1842
for i, exp in zip(t.interpolations, interpolations, strict=True):
19-
if len(exp) == 4:
20-
actual = (i.value, i.expression, i.conversion, i.format_spec)
21-
self.assertEqual(actual, exp)
22-
continue
23-
24-
if len(exp) == 3:
25-
self.assertEqual((i.value, i.expression, i.conversion), exp)
26-
self.assertEqual(i.format_spec, '')
27-
continue
28-
29-
self.assertEqual((i.value, i.expression), exp)
30-
self.assertEqual(i.format_spec, '')
31-
self.assertIsNone(i.conversion)
43+
self.assertInterpolationEqual(i, exp)
3244

3345

3446
def convert(value, conversion):

Lib/test/test_string/test_templatelib.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ def test_basic_creation(self):
4545
self.assertEqual(len(t.interpolations), 0)
4646
self.assertEqual(fstring(t), 'Hello,\nworld')
4747

48+
def test_interpolation_creation(self):
49+
i = Interpolation('Maria', 'name', 'a', 'fmt')
50+
self.assertInterpolationEqual(i, ('Maria', 'name', 'a', 'fmt'))
51+
52+
i = Interpolation('Maria', 'name', 'a')
53+
self.assertInterpolationEqual(i, ('Maria', 'name', 'a'))
54+
55+
i = Interpolation('Maria', 'name')
56+
self.assertInterpolationEqual(i, ('Maria', 'name'))
57+
58+
i = Interpolation('Maria')
59+
self.assertInterpolationEqual(i, ('Maria',))
60+
4861
def test_creation_interleaving(self):
4962
# Should add strings on either side
5063
t = Template(Interpolation('Maria', 'name', None, ''))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``Interpolation.expression`` now has a default, the empty string.

Objects/clinic/interpolationobject.c.h

Lines changed: 14 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/interpolationobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ typedef struct {
5454
Interpolation.__new__ as interpolation_new
5555
5656
value: object
57-
expression: object(subclass_of='&PyUnicode_Type')
57+
expression: object(subclass_of='&PyUnicode_Type', c_default='&_Py_STR(empty)') = ""
5858
conversion: object(converter='_conversion_converter') = None
5959
format_spec: object(subclass_of='&PyUnicode_Type', c_default='&_Py_STR(empty)') = ""
6060
[clinic start generated code]*/
@@ -63,7 +63,7 @@ static PyObject *
6363
interpolation_new_impl(PyTypeObject *type, PyObject *value,
6464
PyObject *expression, PyObject *conversion,
6565
PyObject *format_spec)
66-
/*[clinic end generated code: output=6488e288765bc1a9 input=d91711024068528c]*/
66+
/*[clinic end generated code: output=6488e288765bc1a9 input=fc5c285c1dd23d36]*/
6767
{
6868
interpolationobject *self = PyObject_GC_New(interpolationobject, type);
6969
if (!self) {

0 commit comments

Comments
 (0)