Author: Ronan Lamy <[email protected]>
Branch: py3.6
Changeset: r97528:5dbba3873910
Date: 2019-09-18 20:47 +0100
http://bitbucket.org/pypy/pypy/changeset/5dbba3873910/
Log: Raise SyntaxError instead of DeprecationWarning when treating
invalid escapes in bytes as errors (bpo-28691)
diff --git a/pypy/interpreter/pyparser/parsestring.py
b/pypy/interpreter/pyparser/parsestring.py
--- a/pypy/interpreter/pyparser/parsestring.py
+++ b/pypy/interpreter/pyparser/parsestring.py
@@ -249,10 +249,14 @@
buf = builder.build()
if first_escape_error_char != '':
- space.warn(
- space.newtext("invalid escape sequence '\\%s'"
- % first_escape_error_char),
- space.w_DeprecationWarning)
+ try:
+ msg = "invalid escape sequence '\\%s'" % first_escape_error_char
+ space.warn(space.newtext(msg), space.w_DeprecationWarning)
+ except OperationError as e:
+ if e.match(space, space.w_DeprecationWarning):
+ raise oefmt(space.w_SyntaxError, msg)
+ else:
+ raise
return buf, first_escape_error_char
diff --git a/pypy/interpreter/pyparser/test/apptest_parsestring.py
b/pypy/interpreter/pyparser/test/apptest_parsestring.py
new file mode 100644
--- /dev/null
+++ b/pypy/interpreter/pyparser/test/apptest_parsestring.py
@@ -0,0 +1,10 @@
+from pytest import raises
+import warnings
+
+def test_bytes_invalid_escape():
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('error', category=DeprecationWarning)
+ with raises(SyntaxError) as excinfo:
+ eval("b'''\n\\z'''")
+ assert not w
+ assert excinfo.value.filename == '<string>'
diff --git a/pypy/interpreter/pyparser/test/test_parsestring.py
b/pypy/interpreter/pyparser/test/test_parsestring.py
--- a/pypy/interpreter/pyparser/test/test_parsestring.py
+++ b/pypy/interpreter/pyparser/test/test_parsestring.py
@@ -47,9 +47,7 @@
parsestring.parsestr, space, None, "b'\xe9'")
self.parse_and_compare(r"b'\xe9'", chr(0xE9))
-
def test_unicode(self):
- space = self.space
for s in ['hello world', 'hello\n world']:
self.parse_and_compare(repr(s), unicode(s))
@@ -106,7 +104,7 @@
s = s.decode("koi8-u").encode("utf8")
w_ret = parsestring.parsestr(self.space, 'koi8-u', s)
ret = space.unwrap(w_ret)
- assert ret == eval("# -*- coding: koi8-u -*-\nu'\x81\\t'")
+ assert ret == eval("# -*- coding: koi8-u -*-\nu'\x81\\t'")
def test_multiline_unicode_strings_with_backslash(self):
space = self.space
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit