Skip to content

Commit 8842739

Browse files
authored
t-strings: Various changes (python#74)
1 parent 4d1f2a5 commit 8842739

File tree

11 files changed

+25
-24
lines changed

11 files changed

+25
-24
lines changed

Doc/library/token.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ The token constants are:
133133

134134
.. data:: TSTRING_START
135135

136-
Token value used to indicate the beginning of a t-string literal.
136+
Token value used to indicate the beginning of a template string literal.
137137

138138
.. impl-detail::
139139

@@ -144,7 +144,7 @@ The token constants are:
144144

145145
.. data:: TSTRING_MIDDLE
146146

147-
Token value used for literal text inside a t-string literal
147+
Token value used for literal text inside a template string literal
148148
including format specifications.
149149

150150
.. impl-detail::
@@ -158,7 +158,7 @@ The token constants are:
158158

159159
.. data:: TSTRING_END
160160

161-
Token value used to indicate the end of a t-string.
161+
Token value used to indicate the end of a template string literal.
162162

163163
.. impl-detail::
164164

Doc/whatsnew/3.14.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ safe shell operations, improve logging, tackle modern ideas in web development
160160
See :pep:`750` for more details.
161161

162162
(Contributed by Jim Baker, Guido van Rossum, Paul Everitt, Koudai Aono,
163-
Lysandros Nikolaou, and Dave Peck in :gh:`132661`.)
163+
Lysandros Nikolaou, Dave Peck, Adam Turner, Jelle Zijlstra, Bénédikt Tran,
164+
and Pablo Galindo Salgado in :gh:`132661`.)
164165

165166
.. _whatsnew314-pep768:
166167

Include/internal/pycore_interpolation.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ extern "C" {
99
# error "this header requires Py_BUILD_CORE define"
1010
#endif
1111

12-
#include "pycore_stackref.h" // _PyStackRef
13-
1412
extern PyTypeObject _PyInterpolation_Type;
1513

1614
#define _PyInterpolation_CheckExact(op) Py_IS_TYPE((op), &_PyInterpolation_Type)

Lib/_ast_unparse.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -573,11 +573,11 @@ def _write_str_avoiding_backslashes(self, string, *, quote_types=_ALL_QUOTES):
573573
quote_type = quote_types[0]
574574
self.write(f"{quote_type}{string}{quote_type}")
575575

576-
def _ftstring_helper(self, ftstring_parts):
577-
new_ftstring_parts = []
576+
def _ftstring_helper(self, parts):
577+
new_parts = []
578578
quote_types = list(_ALL_QUOTES)
579579
fallback_to_repr = False
580-
for value, is_constant in ftstring_parts:
580+
for value, is_constant in parts:
581581
if is_constant:
582582
value, new_quote_types = self._str_literal_helper(
583583
value,
@@ -596,22 +596,22 @@ def _ftstring_helper(self, ftstring_parts):
596596
new_quote_types = [q for q in quote_types if q not in value]
597597
if new_quote_types:
598598
quote_types = new_quote_types
599-
new_ftstring_parts.append(value)
599+
new_parts.append(value)
600600

601601
if fallback_to_repr:
602602
# If we weren't able to find a quote type that works for all parts
603603
# of the JoinedStr, fallback to using repr and triple single quotes.
604604
quote_types = ["'''"]
605-
new_ftstring_parts.clear()
606-
for value, is_constant in ftstring_parts:
605+
new_parts.clear()
606+
for value, is_constant in parts:
607607
if is_constant:
608608
value = repr('"' + value) # force repr to use single quotes
609609
expected_prefix = "'\""
610610
assert value.startswith(expected_prefix), repr(value)
611611
value = value[len(expected_prefix):-1]
612-
new_ftstring_parts.append(value)
612+
new_parts.append(value)
613613

614-
value = "".join(new_ftstring_parts)
614+
value = "".join(new_parts)
615615
quote_type = quote_types[0]
616616
self.write(f"{quote_type}{value}{quote_type}")
617617

Lib/tokenize.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def untokenize(self, iterable):
251251
self.tokens.append(indent)
252252
self.prev_col = len(indent)
253253
startline = False
254-
elif tok_type in (FSTRING_MIDDLE, TSTRING_MIDDLE):
254+
elif tok_type in {FSTRING_MIDDLE, TSTRING_MIDDLE}:
255255
if '{' in token or '}' in token:
256256
token = self.escape_brackets(token)
257257
last_line = token.splitlines()[-1]
@@ -308,7 +308,7 @@ def compat(self, token, iterable):
308308
elif startline and indents:
309309
toks_append(indents[-1])
310310
startline = False
311-
elif toknum in (FSTRING_MIDDLE, TSTRING_MIDDLE):
311+
elif toknum in {FSTRING_MIDDLE, TSTRING_MIDDLE}:
312312
tokval = self.escape_brackets(tokval)
313313

314314
# Insert a space between two consecutive brackets if we are in an f-string

Objects/interpolationobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/* t-string Interpolation object implementation */
22

33
#include "Python.h"
4-
#include "pycore_initconfig.h" // _PyStatus_OK
4+
#include "pycore_initconfig.h" // _PyStatus_OK
55
#include "pycore_interpolation.h"
6+
#include "pycore_typeobject.h" // _PyType_GetDict
67

78
static int
89
_conversion_converter(PyObject *arg, PyObject **conversion)

Objects/templateobject.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "Python.h"
44
#include "pycore_interpolation.h" // _PyInterpolation_CheckExact()
5+
#include "pycore_runtime.h" // _Py_STR()
56
#include "pycore_template.h"
67

78
typedef struct {
@@ -155,13 +156,15 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
155156
}
156157
else if (_PyInterpolation_CheckExact(item)) {
157158
if (!last_was_str) {
159+
_Py_DECLARE_STR(empty, "");
158160
PyTuple_SET_ITEM(strings, stringsidx++, &_Py_STR(empty));
159161
}
160162
PyTuple_SET_ITEM(interpolations, interpolationsidx++, Py_NewRef(item));
161163
last_was_str = 0;
162164
}
163165
}
164166
if (!last_was_str) {
167+
_Py_DECLARE_STR(empty, "");
165168
PyTuple_SET_ITEM(strings, stringsidx++, &_Py_STR(empty));
166169
}
167170

Objects/unicodeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
5656
#include "pycore_pyhash.h" // _Py_HashSecret_t
5757
#include "pycore_pylifecycle.h" // _Py_SetFileSystemEncoding()
5858
#include "pycore_pystate.h" // _PyInterpreterState_GET()
59-
#include "pycore_template.h" // _PyTemplate_Type
59+
#include "pycore_template.h" // _PyTemplate_Concat()
6060
#include "pycore_tuple.h" // _PyTuple_FromArray()
6161
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI
6262
#include "pycore_unicodeobject.h" // struct _Py_unicode_state

Python/bytecodes.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "pycore_emscripten_signal.h" // _Py_CHECK_EMSCRIPTEN_SIGNALS
1717
#include "pycore_function.h"
1818
#include "pycore_instruments.h"
19-
#include "pycore_interpolation.h" // _PyInterpolation_FromStackRefStealOnSuccess()
19+
#include "pycore_interpolation.h" // _PyInterpolation_Build()
2020
#include "pycore_intrinsics.h"
2121
#include "pycore_long.h" // _PyLong_GetZero()
2222
#include "pycore_moduleobject.h" // PyModuleObject
@@ -31,7 +31,7 @@
3131
#include "pycore_setobject.h" // _PySet_NextEntry()
3232
#include "pycore_sliceobject.h" // _PyBuildSlice_ConsumeRefs
3333
#include "pycore_stackref.h"
34-
#include "pycore_template.h" // _PyTemplate_From{List,Values}()
34+
#include "pycore_template.h" // _PyTemplate_Build()
3535
#include "pycore_tuple.h" // _PyTuple_ITEMS()
3636
#include "pycore_typeobject.h" // _PySuper_Lookup()
3737

Python/ceval.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "pycore_import.h" // _PyImport_IsDefaultImportFunc()
2020
#include "pycore_instruments.h"
2121
#include "pycore_interpframe.h" // _PyFrame_SetStackPointer()
22-
#include "pycore_interpolation.h" // _PyInterpolation_FromStackRefStealOnSuccess()
22+
#include "pycore_interpolation.h" // _PyInterpolation_Build()
2323
#include "pycore_intrinsics.h"
2424
#include "pycore_jit.h"
2525
#include "pycore_list.h" // _PyList_GetItemRef()
@@ -37,7 +37,7 @@
3737
#include "pycore_setobject.h" // _PySet_Update()
3838
#include "pycore_sliceobject.h" // _PyBuildSlice_ConsumeRefs
3939
#include "pycore_sysmodule.h" // _PySys_GetOptionalAttrString()
40-
#include "pycore_template.h" // _PyTemplate_From{List,Values}()
40+
#include "pycore_template.h" // _PyTemplate_Build()
4141
#include "pycore_traceback.h" // _PyTraceBack_FromFrame
4242
#include "pycore_tuple.h" // _PyTuple_ITEMS()
4343
#include "pycore_uop_ids.h" // Uops

Tools/cases_generator/analyzer.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,6 @@ def has_error_without_pop(op: parser.CodeDef) -> bool:
630630
"_PyFrame_StackPush",
631631
"_PyFunction_SetVersion",
632632
"_PyGen_GetGeneratorFromFrame",
633-
"_PyInterpolation_FromStackRefStealOnSuccess",
634633
"_PyInterpreterState_GET",
635634
"_PyList_AppendTakeRef",
636635
"_PyList_ITEMS",
@@ -649,7 +648,6 @@ def has_error_without_pop(op: parser.CodeDef) -> bool:
649648
"_PyObject_InlineValues",
650649
"_PyObject_IsUniquelyReferenced",
651650
"_PyObject_ManagedDictPointer",
652-
"_PyTemplate_FromList",
653651
"_PyThreadState_HasStackSpace",
654652
"_PyTuple_FromStackRefStealOnSuccess",
655653
"_PyTuple_ITEMS",

0 commit comments

Comments
 (0)