Skip to content

Commit

Permalink
[Python] Fix termination of raw-strings
Browse files Browse the repository at this point in the history
Fixes #3954

This commit extends escape pattern of raw strings by `\\`
to prevent unwanted consuming of `\"` in `\\"`.
  • Loading branch information
deathaxe committed Apr 7, 2024
1 parent 2805af6 commit 83ea918
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Python/Python.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -4051,7 +4051,7 @@ contexts:

escaped-raw-quotes:
# consume escaped quotes in raw-strings to preven them terminating strings
- match: \\['"]
- match: \\[\\'"]
scope: constant.character.escape.python

escaped-string-braces:
Expand Down
156 changes: 132 additions & 24 deletions Python/tests/syntax_test_python_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,23 @@
#^^^^^^^^ comment.block.documentation
# ^^ constant.character.escape.python

raw = r'foo\'' + r'foo\"'
r'''\\'''
#^^^^^^^^ comment.block.documentation
# ^^ constant.character.escape.python

r"""\\"""
#^^^^^^^^ comment.block.documentation
# ^^ constant.character.escape.python

R'''\\'''
#^^^^^^^^ comment.block.documentation
# ^^ constant.character.escape.python

R"""\\"""
#^^^^^^^^ comment.block.documentation
# ^^ constant.character.escape.python

raw = r'foo\'' + r'foo\"' + r'foo\\'
# ^^^^^^^ meta.string.python string.quoted.single.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
Expand All @@ -301,8 +317,12 @@
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^ punctuation.definition.string.end.python
# ^^^^^^^ meta.string.python string.quoted.single.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^ punctuation.definition.string.end.python

raw = r"foo\"" + r"foo\'"
raw = r"foo\"" + r"foo\'" + r"foo\\"
# ^^^^^^^ meta.string.python string.quoted.double.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
Expand All @@ -311,8 +331,12 @@
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^ punctuation.definition.string.end.python
# ^^^^^^^ meta.string.python string.quoted.double.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^ punctuation.definition.string.end.python

raw = rb'foo\'' + rb'foo\"'
raw = rb'foo\'' + rb'foo\"' + rb'foo\\'
# ^^^^^^^ meta.string.python string.quoted.single.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
Expand All @@ -321,8 +345,12 @@
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^ punctuation.definition.string.end.python
# ^^^^^^^ meta.string.python string.quoted.single.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^ punctuation.definition.string.end.python

raw = rb"foo\"" + rb"foo\'"
raw = rb"foo\"" + rb"foo\'" + rb"foo\\"
# ^^^^^^^ meta.string.python string.quoted.double.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
Expand All @@ -332,7 +360,7 @@
# ^^ constant.character.escape
# ^ punctuation.definition.string.end.python

raw = rf'foo\'' + rf'foo\"'
raw = rf'foo\'' + rf'foo\"' + rf'foo\\'
# ^^^^^^^ meta.string.python string.quoted.single.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
Expand All @@ -341,8 +369,12 @@
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^ punctuation.definition.string.end.python
# ^^^^^^^ meta.string.python string.quoted.single.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^ punctuation.definition.string.end.python

raw = rf"foo\"" + rf"foo\'"
raw = rf"foo\"" + rf"foo\'" + rf"foo\\"
# ^^^^^^^ meta.string.python string.quoted.double.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
Expand All @@ -351,8 +383,12 @@
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^ punctuation.definition.string.end.python
# ^^^^^^^ meta.string.python string.quoted.double.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^ punctuation.definition.string.end.python

raw = R'foo\'' + R'foo\"'
raw = R'foo\'' + R'foo\"' + R'foo\\'
# ^^^^^^^ meta.string.python string.quoted.single.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
Expand All @@ -361,8 +397,12 @@
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^ punctuation.definition.string.end.python
# ^^^^^^^ meta.string.python string.quoted.single.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^ punctuation.definition.string.end.python

raw = R"foo\"" + R"foo\'"
raw = R"foo\"" + R"foo\'" + R"foo\\"
# ^^^^^^^ meta.string.python string.quoted.double.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
Expand All @@ -371,8 +411,12 @@
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^ punctuation.definition.string.end.python
# ^^^^^^^ meta.string.python string.quoted.double.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^ punctuation.definition.string.end.python

raw = RB'foo\'' + RB'foo\"'
raw = RB'foo\'' + RB'foo\"' + RB'foo\\'
# ^^^^^^^ meta.string.python string.quoted.single.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
Expand All @@ -381,8 +425,12 @@
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^ punctuation.definition.string.end.python
# ^^^^^^^ meta.string.python string.quoted.single.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^ punctuation.definition.string.end.python

raw = RB"foo\"" + RB"foo\'"
raw = RB"foo\"" + RB"foo\'" + RB"foo\\"
# ^^^^^^^ meta.string.python string.quoted.double.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
Expand All @@ -391,8 +439,12 @@
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^ punctuation.definition.string.end.python
# ^^^^^^^ meta.string.python string.quoted.double.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^ punctuation.definition.string.end.python

raw = RF'foo\'' + RF'foo\"'
raw = RF'foo\'' + RF'foo\"' + RF'foo\\'
# ^^^^^^^ meta.string.python string.quoted.single.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
Expand All @@ -401,8 +453,12 @@
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^ punctuation.definition.string.end.python
# ^^^^^^^ meta.string.python string.quoted.single.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^ punctuation.definition.string.end.python

raw = RF"foo\"" + RF"foo\'"
raw = RF"foo\"" + RF"foo\'" + RF"foo\\"
# ^^^^^^^ meta.string.python string.quoted.double.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
Expand All @@ -411,8 +467,12 @@
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^ punctuation.definition.string.end.python
# ^^^^^^^ meta.string.python string.quoted.double.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^ punctuation.definition.string.end.python

raw = r'''foo\'''' + r'''foo\"'''
raw = r'''foo\'''' + r'''foo\"''' + r'''foo\\'''
# ^^^^^^^^^^^ meta.string.python string.quoted.single.block.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
Expand All @@ -421,8 +481,12 @@
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^^^ punctuation.definition.string.end.python
# ^^^^^^^^^^^ meta.string.python string.quoted.single.block.python
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^^^ punctuation.definition.string.end.python

raw = r"""foo\"""" + r"""foo\'"""
raw = r"""foo\"""" + r"""foo\'""" + r"""foo\\"""
# ^^^^^^^^^^^ meta.string.python string.quoted.double.block.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
Expand All @@ -431,8 +495,12 @@
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^^^ punctuation.definition.string.end.python
# ^^^^^^^^^^^ meta.string.python string.quoted.double.block.python
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^^^ punctuation.definition.string.end.python

raw = rb'''foo\'''' + rb'''foo\"'''
raw = rb'''foo\'''' + rb'''foo\"''' + rb'''foo\\'''
# ^^^^^^^^^^^ meta.string.python string.quoted.single.block.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
Expand All @@ -441,8 +509,12 @@
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^^^ punctuation.definition.string.end.python
# ^^^^^^^^^^^ meta.string.python string.quoted.single.block.python
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^^^ punctuation.definition.string.end.python

raw = rb"""foo\"""" + rb"""foo\'"""
raw = rb"""foo\"""" + rb"""foo\'""" + rb"""foo\\"""
# ^^^^^^^^^^^ meta.string.python string.quoted.double.block.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
Expand All @@ -451,8 +523,12 @@
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^^^ punctuation.definition.string.end.python
# ^^^^^^^^^^^ meta.string.python string.quoted.double.block.python
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^^^ punctuation.definition.string.end.python

raw = rf'''foo\'''' + rf'''foo\"'''
raw = rf'''foo\'''' + rf'''foo\"''' + rf'''foo\\'''
# ^^^^^^^^^^^ meta.string.python string.quoted.single.block.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
Expand All @@ -461,8 +537,12 @@
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^^^ punctuation.definition.string.end.python
# ^^^^^^^^^^^ meta.string.python string.quoted.single.block.python
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^^^ punctuation.definition.string.end.python

raw = rf"""foo\"""" + rf"""foo\'"""
raw = rf"""foo\"""" + rf"""foo\'""" + rf"""foo\\"""
# ^^^^^^^^^^^ meta.string.python string.quoted.double.block.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
Expand All @@ -471,8 +551,12 @@
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^^^ punctuation.definition.string.end.python
# ^^^^^^^^^^^ meta.string.python string.quoted.double.block.python
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape
# ^^^ punctuation.definition.string.end.python

raw = R'''foo\'''' + R'''foo\"'''
raw = R'''foo\'''' + R'''foo\"''' + R'''foo\\'''
# ^^^^^^^^^^^ meta.string.python string.quoted.single.block.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
Expand All @@ -481,8 +565,12 @@
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^^^ punctuation.definition.string.end.python
# ^^^^^^^^^^^ meta.string.python string.quoted.single.block.python
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^^^ punctuation.definition.string.end.python

raw = R"""foo\"""" + R"""foo\'"""
raw = R"""foo\"""" + R"""foo\'""" + R"""foo\\"""
# ^^^^^^^^^^^ meta.string.python string.quoted.double.block.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
Expand All @@ -491,8 +579,12 @@
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^^^ punctuation.definition.string.end.python
# ^^^^^^^^^^^ meta.string.python string.quoted.double.block.python
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^^^ punctuation.definition.string.end.python

raw = RB'''foo\'''' + RB'''foo\"'''
raw = RB'''foo\'''' + RB'''foo\"''' + RB'''foo\\'''
# ^^^^^^^^^^^ meta.string.python string.quoted.single.block.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
Expand All @@ -501,8 +593,12 @@
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^^^ punctuation.definition.string.end.python
# ^^^^^^^^^^^ meta.string.python string.quoted.single.block.python
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^^^ punctuation.definition.string.end.python

raw = RB"""foo\"""" + RB"""foo\'"""
raw = RB"""foo\"""" + RB"""foo\'""" + RB"""foo\\"""
# ^^^^^^^^^^^ meta.string.python string.quoted.double.block.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
Expand All @@ -511,8 +607,12 @@
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^^^ punctuation.definition.string.end.python
# ^^^^^^^^^^^ meta.string.python string.quoted.double.block.python
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^^^ punctuation.definition.string.end.python

raw = RF'''foo\'''' + RF'''foo\"'''
raw = RF'''foo\'''' + RF'''foo\"''' + RF'''foo\\'''
# ^^^^^^^^^^^ meta.string.python string.quoted.single.block.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
Expand All @@ -521,8 +621,12 @@
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^^^ punctuation.definition.string.end.python
# ^^^^^^^^^^^ meta.string.python string.quoted.single.block.python
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^^^ punctuation.definition.string.end.python

raw = RF"""foo\"""" + RF"""foo\'"""
raw = RF"""foo\"""" + RF"""foo\'""" + RF"""foo\\"""
# ^^^^^^^^^^^ meta.string.python string.quoted.double.block.python
# ^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
Expand All @@ -531,6 +635,10 @@
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^^^ punctuation.definition.string.end.python
# ^^^^^^^^^^^ meta.string.python string.quoted.double.block.python
# ^^^ punctuation.definition.string.begin.python
# ^^ constant.character.escape.python
# ^^^ punctuation.definition.string.end.python

###################################################
# There are many variations of making a byte string
Expand Down

0 comments on commit 83ea918

Please sign in to comment.