mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
Fix warn_invalid_escape_sequence()
Issue #28691: Fix warn_invalid_escape_sequence(): handle correctly DeprecationWarning raised as an exception. First clear the current exception to replace the DeprecationWarning exception with a SyntaxError exception. Unit test written by Serhiy Storchaka.
This commit is contained in:
parent
de40e1218c
commit
f9cca365c7
2 changed files with 27 additions and 1 deletions
|
@ -111,6 +111,7 @@ class TestLiterals(unittest.TestCase):
|
||||||
continue
|
continue
|
||||||
with self.assertWarns(DeprecationWarning):
|
with self.assertWarns(DeprecationWarning):
|
||||||
self.assertEqual(eval(r"'\%c'" % b), '\\' + chr(b))
|
self.assertEqual(eval(r"'\%c'" % b), '\\' + chr(b))
|
||||||
|
|
||||||
with warnings.catch_warnings(record=True) as w:
|
with warnings.catch_warnings(record=True) as w:
|
||||||
warnings.simplefilter('always', category=DeprecationWarning)
|
warnings.simplefilter('always', category=DeprecationWarning)
|
||||||
eval("'''\n\\z'''")
|
eval("'''\n\\z'''")
|
||||||
|
@ -118,6 +119,15 @@ class TestLiterals(unittest.TestCase):
|
||||||
self.assertEqual(w[0].filename, '<string>')
|
self.assertEqual(w[0].filename, '<string>')
|
||||||
self.assertEqual(w[0].lineno, 2)
|
self.assertEqual(w[0].lineno, 2)
|
||||||
|
|
||||||
|
with warnings.catch_warnings(record=True) as w:
|
||||||
|
warnings.simplefilter('error', category=DeprecationWarning)
|
||||||
|
with self.assertRaises(SyntaxError) as cm:
|
||||||
|
eval("'''\n\\z'''")
|
||||||
|
exc = cm.exception
|
||||||
|
self.assertEqual(w, [])
|
||||||
|
self.assertEqual(exc.filename, '<string>')
|
||||||
|
self.assertEqual(exc.lineno, 2)
|
||||||
|
|
||||||
def test_eval_str_raw(self):
|
def test_eval_str_raw(self):
|
||||||
self.assertEqual(eval(""" r'x' """), 'x')
|
self.assertEqual(eval(""" r'x' """), 'x')
|
||||||
self.assertEqual(eval(r""" r'\x01' """), '\\' + 'x01')
|
self.assertEqual(eval(r""" r'\x01' """), '\\' + 'x01')
|
||||||
|
@ -150,6 +160,7 @@ class TestLiterals(unittest.TestCase):
|
||||||
continue
|
continue
|
||||||
with self.assertWarns(DeprecationWarning):
|
with self.assertWarns(DeprecationWarning):
|
||||||
self.assertEqual(eval(r"b'\%c'" % b), b'\\' + bytes([b]))
|
self.assertEqual(eval(r"b'\%c'" % b), b'\\' + bytes([b]))
|
||||||
|
|
||||||
with warnings.catch_warnings(record=True) as w:
|
with warnings.catch_warnings(record=True) as w:
|
||||||
warnings.simplefilter('always', category=DeprecationWarning)
|
warnings.simplefilter('always', category=DeprecationWarning)
|
||||||
eval("b'''\n\\z'''")
|
eval("b'''\n\\z'''")
|
||||||
|
@ -157,6 +168,15 @@ class TestLiterals(unittest.TestCase):
|
||||||
self.assertEqual(w[0].filename, '<string>')
|
self.assertEqual(w[0].filename, '<string>')
|
||||||
self.assertEqual(w[0].lineno, 2)
|
self.assertEqual(w[0].lineno, 2)
|
||||||
|
|
||||||
|
with warnings.catch_warnings(record=True) as w:
|
||||||
|
warnings.simplefilter('error', category=DeprecationWarning)
|
||||||
|
with self.assertRaises(SyntaxError) as cm:
|
||||||
|
eval("b'''\n\\z'''")
|
||||||
|
exc = cm.exception
|
||||||
|
self.assertEqual(w, [])
|
||||||
|
self.assertEqual(exc.filename, '<string>')
|
||||||
|
self.assertEqual(exc.lineno, 2)
|
||||||
|
|
||||||
def test_eval_bytes_raw(self):
|
def test_eval_bytes_raw(self):
|
||||||
self.assertEqual(eval(""" br'x' """), b'x')
|
self.assertEqual(eval(""" br'x' """), b'x')
|
||||||
self.assertEqual(eval(""" rb'x' """), b'x')
|
self.assertEqual(eval(""" rb'x' """), b'x')
|
||||||
|
|
|
@ -4129,7 +4129,13 @@ warn_invalid_escape_sequence(struct compiling *c, const node *n,
|
||||||
NULL, NULL) < 0 &&
|
NULL, NULL) < 0 &&
|
||||||
PyErr_ExceptionMatches(PyExc_DeprecationWarning))
|
PyErr_ExceptionMatches(PyExc_DeprecationWarning))
|
||||||
{
|
{
|
||||||
const char *s = PyUnicode_AsUTF8(msg);
|
const char *s;
|
||||||
|
|
||||||
|
/* Replace the DeprecationWarning exception with a SyntaxError
|
||||||
|
to get a more accurate error report */
|
||||||
|
PyErr_Clear();
|
||||||
|
|
||||||
|
s = PyUnicode_AsUTF8(msg);
|
||||||
if (s != NULL) {
|
if (s != NULL) {
|
||||||
ast_error(c, n, s);
|
ast_error(c, n, s);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue