[Python-checkins] cpython (merge 3.6 -> default): Merge with 3.6

jason.coombs python-checkins at python.org
Sun Nov 6 11:27:58 EST 2016


https://hg.python.org/cpython/rev/f332032b1934
changeset:   104929:f332032b1934
parent:      104926:5fda909c722c
parent:      104928:d9eb609be2ca
user:        Jason R. Coombs <jaraco at jaraco.com>
date:        Sun Nov 06 11:27:50 2016 -0500
summary:
  Merge with 3.6

files:
  Doc/reference/lexical_analysis.rst |  19 +++++++++++------
  Lib/test/test_fstring.py           |  13 +++++++----
  2 files changed, 20 insertions(+), 12 deletions(-)


diff --git a/Doc/reference/lexical_analysis.rst b/Doc/reference/lexical_analysis.rst
--- a/Doc/reference/lexical_analysis.rst
+++ b/Doc/reference/lexical_analysis.rst
@@ -679,17 +679,22 @@
 
 A consequence of sharing the same syntax as regular string literals is
 that characters in the replacement fields must not conflict with the
-quoting used in the outer formatted string literal.  Also, escape
-sequences normally apply to the outer formatted string literal,
-rather than inner string literals::
+quoting used in the outer formatted string literal::
 
    f"abc {a["x"]} def"    # error: outer string literal ended prematurely
-   f"abc {a[\"x\"]} def"  # workaround: escape the inner quotes
    f"abc {a['x']} def"    # workaround: use different quoting
 
-   f"newline: {ord('\n')}"   # error: literal line break in inner string
-   f"newline: {ord('\\n')}"  # workaround: double escaping
-   fr"newline: {ord('\n')}"  # workaround: raw outer string
+Backslashes are not allowed in format expressions and will raise
+an error::
+
+   f"newline: {ord('\n')}"  # raises SyntaxError
+
+To include a value in which a backslash escape is required, create
+a temporary variable.
+
+   >>> newline = ord('\n')
+   >>> f"newline: {newline}"
+   'newline: 10'
 
 See also :pep:`498` for the proposal that added formatted string literals,
 and :meth:`str.format`, which uses a related format string mechanism.
diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
--- a/Lib/test/test_fstring.py
+++ b/Lib/test/test_fstring.py
@@ -382,11 +382,14 @@
                              ])
 
     def test_no_escapes_for_braces(self):
-        # \x7b is '{'.  Make sure it doesn't start an expression.
-        self.assertEqual(f'\x7b2}}', '{2}')
-        self.assertEqual(f'\x7b2', '{2')
-        self.assertEqual(f'\u007b2', '{2')
-        self.assertEqual(f'\N{LEFT CURLY BRACKET}2\N{RIGHT CURLY BRACKET}', '{2}')
+        """
+        Only literal curly braces begin an expression.
+        """
+        # \x7b is '{'.
+        self.assertEqual(f'\x7b1+1}}', '{1+1}')
+        self.assertEqual(f'\x7b1+1', '{1+1')
+        self.assertEqual(f'\u007b1+1', '{1+1')
+        self.assertEqual(f'\N{LEFT CURLY BRACKET}1+1\N{RIGHT CURLY BRACKET}', '{1+1}')
 
     def test_newlines_in_expressions(self):
         self.assertEqual(f'{0}', '0')

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list