Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
3a5d4cb9
Commit
3a5d4cb9
authored
Jan 12, 2012
by
Antoine Pitrou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #13748: Raw bytes literals can now be written with the `rb` prefix as well as `br`.
parent
b63a450c
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
54 additions
and
19 deletions
+54
-19
Doc/reference/lexical_analysis.rst
Doc/reference/lexical_analysis.rst
+5
-1
Lib/test/test_strlit.py
Lib/test/test_strlit.py
+17
-4
Lib/test/tokenize_tests.txt
Lib/test/tokenize_tests.txt
+8
-0
Misc/NEWS
Misc/NEWS
+3
-0
Parser/tokenizer.c
Parser/tokenizer.c
+9
-7
Python/ast.c
Python/ast.c
+12
-7
No files found.
Doc/reference/lexical_analysis.rst
View file @
3a5d4cb9
...
...
@@ -412,7 +412,7 @@ String literals are described by the following lexical definitions:
.. productionlist::
bytesliteral: `bytesprefix`(`shortbytes` | `longbytes`)
bytesprefix: "b" | "B" | "br" | "Br" | "bR" | "BR"
bytesprefix: "b" | "B" | "br" | "Br" | "bR" | "BR"
| "rb" | "rB" | "Rb" | "RB"
shortbytes: "'" `shortbytesitem`* "'" | '"' `shortbytesitem`* '"'
longbytes: "'''" `longbytesitem`* "'''" | '"""' `longbytesitem`* '"""'
shortbytesitem: `shortbyteschar` | `bytesescapeseq`
...
...
@@ -446,6 +446,10 @@ or ``'R'``; such strings are called :dfn:`raw strings` and treat backslashes as
literal characters. As a result, in string literals, ``'\U'`` and ``'\u'``
escapes in raw strings are not treated specially.
.. versionadded:: 3.3
The ``'rb'`` prefix of raw bytes literals has been added as a synonym
of ``'br'``.
In triple-quoted strings, unescaped newlines and quotes are allowed (and are
retained), except that three unescaped quotes in a row terminate the string. (A
"quote" is the character used to open the string, i.e. either ``'`` or ``"``.)
...
...
Lib/test/test_strlit.py
View file @
3a5d4cb9
...
...
@@ -2,10 +2,10 @@ r"""Test correct treatment of various string literals by the parser.
There are four types of string literals:
'abc' -- normal str
r'abc' -- raw str
b'xyz' -- normal bytes
br'xyz' -- raw bytes
'abc'
-- normal str
r'abc'
-- raw str
b'xyz'
-- normal bytes
br'xyz'
| rb'xyz'
-- raw bytes
The difference between normal and raw strings is of course that in a
raw string, \
esc
apes (while still used to determine the end of the
...
...
@@ -103,12 +103,25 @@ class TestLiterals(unittest.TestCase):
def
test_eval_bytes_raw
(
self
):
self
.
assertEqual
(
eval
(
""" br'x' """
),
b'x'
)
self
.
assertEqual
(
eval
(
""" rb'x' """
),
b'x'
)
self
.
assertEqual
(
eval
(
r""" br'\x01' """
),
b'
\
\
'
+
b'x01'
)
self
.
assertEqual
(
eval
(
r""" rb'\x01' """
),
b'
\
\
'
+
b'x01'
)
self
.
assertEqual
(
eval
(
""" br'
\
x01
' """
),
byte
(
1
))
self
.
assertEqual
(
eval
(
""" rb'
\
x01
' """
),
byte
(
1
))
self
.
assertEqual
(
eval
(
r""" br'\x81' """
),
b"
\
\
"
+
b"x81"
)
self
.
assertEqual
(
eval
(
r""" rb'\x81' """
),
b"
\
\
"
+
b"x81"
)
self
.
assertRaises
(
SyntaxError
,
eval
,
""" br'
\
x81
' """
)
self
.
assertRaises
(
SyntaxError
,
eval
,
""" rb'
\
x81
' """
)
self
.
assertEqual
(
eval
(
r""" br'\u1881' """
),
b"
\
\
"
+
b"u1881"
)
self
.
assertEqual
(
eval
(
r""" rb'\u1881' """
),
b"
\
\
"
+
b"u1881"
)
self
.
assertRaises
(
SyntaxError
,
eval
,
""" br'
\
u1881
' """
)
self
.
assertRaises
(
SyntaxError
,
eval
,
""" rb'
\
u1881
' """
)
self
.
assertRaises
(
SyntaxError
,
eval
,
""" bb'' """
)
self
.
assertRaises
(
SyntaxError
,
eval
,
""" rr'' """
)
self
.
assertRaises
(
SyntaxError
,
eval
,
""" brr'' """
)
self
.
assertRaises
(
SyntaxError
,
eval
,
""" bbr'' """
)
self
.
assertRaises
(
SyntaxError
,
eval
,
""" rrb'' """
)
self
.
assertRaises
(
SyntaxError
,
eval
,
""" rbb'' """
)
def
check_encoding
(
self
,
encoding
,
extra
=
""
):
modname
=
"xx_"
+
encoding
.
replace
(
"-"
,
"_"
)
...
...
Lib/test/tokenize_tests.txt
View file @
3a5d4cb9
...
...
@@ -114,8 +114,12 @@ x = b'abc' + B'ABC'
y = b"abc" + B"ABC"
x = br'abc' + Br'ABC' + bR'ABC' + BR'ABC'
y = br"abc" + Br"ABC" + bR"ABC" + BR"ABC"
x = rb'abc' + rB'ABC' + Rb'ABC' + RB'ABC'
y = rb"abc" + rB"ABC" + Rb"ABC" + RB"ABC"
x = br'\\' + BR'\\'
x = rb'\\' + RB'\\'
x = br'\'' + ''
x = rb'\'' + ''
y = br'''
foo bar \\
baz''' + BR'''
...
...
@@ -124,6 +128,10 @@ y = Br"""foo
bar \\ baz
""" + bR'''spam
'''
y = rB"""foo
bar \\ baz
""" + Rb'''spam
'''
# Indentation
if 1:
...
...
Misc/NEWS
View file @
3a5d4cb9
...
...
@@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1?
Core and Builtins
-----------------
- Issue #13748: Raw bytes literals can now be written with the ``rb`` prefix
as well as ``br``.
- Issue #12736: Use full unicode case mappings for upper, lower, and title case.
- Issue #12760: Add a create mode to open(). Patch by David Townshend.
...
...
Parser/tokenizer.c
View file @
3a5d4cb9
...
...
@@ -1412,13 +1412,15 @@ tok_get(register struct tok_state *tok, char **p_start, char **p_end)
/* Identifier (most frequent token!) */
nonascii
=
0
;
if
(
is_potential_identifier_start
(
c
))
{
/* Process b"", r"" and br"" */
if
(
c
==
'b'
||
c
==
'B'
)
{
c
=
tok_nextc
(
tok
);
if
(
c
==
'"'
||
c
==
'\''
)
goto
letter_quote
;
}
if
(
c
==
'r'
||
c
==
'R'
)
{
/* Process b"", r"", br"" and rb"" */
int
saw_b
=
0
,
saw_r
=
0
;
while
(
1
)
{
if
(
!
saw_b
&&
(
c
==
'b'
||
c
==
'B'
))
saw_b
=
1
;
else
if
(
!
saw_r
&&
(
c
==
'r'
||
c
==
'R'
))
saw_r
=
1
;
else
break
;
c
=
tok_nextc
(
tok
);
if
(
c
==
'"'
||
c
==
'\''
)
goto
letter_quote
;
...
...
Python/ast.c
View file @
3a5d4cb9
...
...
@@ -3744,13 +3744,18 @@ parsestr(struct compiling *c, const node *n, int *bytesmode)
int
rawmode
=
0
;
int
need_encoding
;
if
(
isalpha
(
quote
))
{
if
(
quote
==
'b'
||
quote
==
'B'
)
{
quote
=
*++
s
;
*
bytesmode
=
1
;
}
if
(
quote
==
'r'
||
quote
==
'R'
)
{
quote
=
*++
s
;
rawmode
=
1
;
while
(
!*
bytesmode
||
!
rawmode
)
{
if
(
quote
==
'b'
||
quote
==
'B'
)
{
quote
=
*++
s
;
*
bytesmode
=
1
;
}
else
if
(
quote
==
'r'
||
quote
==
'R'
)
{
quote
=
*++
s
;
rawmode
=
1
;
}
else
{
break
;
}
}
}
if
(
quote
!=
'\''
&&
quote
!=
'\"'
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment