Skip to content

Commit 95baa28

Browse files
[3.14] gh-132661: Add default value (of "") for Interpolation.expression (GH-136441) (#136511)
Co-authored-by: Dave Peck <[email protected]>
1 parent da8bcfd commit 95baa28

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
@@ -3,33 +3,45 @@
33

44

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

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

3446

3547
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)