mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
Issue #28512: Fixed setting the offset attribute of SyntaxError by
PyErr_SyntaxLocationEx() and PyErr_SyntaxLocationObject().
This commit is contained in:
parent
49c14d8e8f
commit
8b58339eb2
7 changed files with 56 additions and 58 deletions
|
@ -1041,9 +1041,16 @@ def make_bad_fd():
|
||||||
file.close()
|
file.close()
|
||||||
unlink(TESTFN)
|
unlink(TESTFN)
|
||||||
|
|
||||||
def check_syntax_error(testcase, statement):
|
def check_syntax_error(testcase, statement, *, lineno=None, offset=None):
|
||||||
testcase.assertRaises(SyntaxError, compile, statement,
|
with testcase.assertRaises(SyntaxError) as cm:
|
||||||
'<test string>', 'exec')
|
compile(statement, '<test string>', 'exec')
|
||||||
|
err = cm.exception
|
||||||
|
testcase.assertIsNotNone(err.lineno)
|
||||||
|
if lineno is not None:
|
||||||
|
testcase.assertEqual(err.lineno, lineno)
|
||||||
|
testcase.assertIsNotNone(err.offset)
|
||||||
|
if offset is not None:
|
||||||
|
testcase.assertEqual(err.offset, offset)
|
||||||
|
|
||||||
def open_urlresource(url, *args, **kw):
|
def open_urlresource(url, *args, **kw):
|
||||||
import urllib.request, urllib.parse
|
import urllib.request, urllib.parse
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
from test import support
|
from test import support
|
||||||
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
rx = re.compile('\((\S+).py, line (\d+)')
|
rx = re.compile('\((\S+).py, line (\d+)')
|
||||||
|
@ -12,6 +13,12 @@ def get_error_location(msg):
|
||||||
|
|
||||||
class FutureTest(unittest.TestCase):
|
class FutureTest(unittest.TestCase):
|
||||||
|
|
||||||
|
def check_syntax_error(self, err, basename, lineno, offset=0):
|
||||||
|
self.assertIn('%s.py, line %d' % (basename, lineno), str(err))
|
||||||
|
self.assertEqual(os.path.basename(err.filename), basename + '.py')
|
||||||
|
self.assertEqual(err.lineno, lineno)
|
||||||
|
self.assertEqual(err.offset, offset)
|
||||||
|
|
||||||
def test_future1(self):
|
def test_future1(self):
|
||||||
with support.CleanImport('future_test1'):
|
with support.CleanImport('future_test1'):
|
||||||
from test import future_test1
|
from test import future_test1
|
||||||
|
@ -27,68 +34,44 @@ class FutureTest(unittest.TestCase):
|
||||||
from test import test_future3
|
from test import test_future3
|
||||||
|
|
||||||
def test_badfuture3(self):
|
def test_badfuture3(self):
|
||||||
try:
|
with self.assertRaises(SyntaxError) as cm:
|
||||||
from test import badsyntax_future3
|
from test import badsyntax_future3
|
||||||
except SyntaxError as msg:
|
self.check_syntax_error(cm.exception, "badsyntax_future3", 3)
|
||||||
self.assertEqual(get_error_location(msg), ("badsyntax_future3", '3'))
|
|
||||||
else:
|
|
||||||
self.fail("expected exception didn't occur")
|
|
||||||
|
|
||||||
def test_badfuture4(self):
|
def test_badfuture4(self):
|
||||||
try:
|
with self.assertRaises(SyntaxError) as cm:
|
||||||
from test import badsyntax_future4
|
from test import badsyntax_future4
|
||||||
except SyntaxError as msg:
|
self.check_syntax_error(cm.exception, "badsyntax_future4", 3)
|
||||||
self.assertEqual(get_error_location(msg), ("badsyntax_future4", '3'))
|
|
||||||
else:
|
|
||||||
self.fail("expected exception didn't occur")
|
|
||||||
|
|
||||||
def test_badfuture5(self):
|
def test_badfuture5(self):
|
||||||
try:
|
with self.assertRaises(SyntaxError) as cm:
|
||||||
from test import badsyntax_future5
|
from test import badsyntax_future5
|
||||||
except SyntaxError as msg:
|
self.check_syntax_error(cm.exception, "badsyntax_future5", 4)
|
||||||
self.assertEqual(get_error_location(msg), ("badsyntax_future5", '4'))
|
|
||||||
else:
|
|
||||||
self.fail("expected exception didn't occur")
|
|
||||||
|
|
||||||
def test_badfuture6(self):
|
def test_badfuture6(self):
|
||||||
try:
|
with self.assertRaises(SyntaxError) as cm:
|
||||||
from test import badsyntax_future6
|
from test import badsyntax_future6
|
||||||
except SyntaxError as msg:
|
self.check_syntax_error(cm.exception, "badsyntax_future6", 3)
|
||||||
self.assertEqual(get_error_location(msg), ("badsyntax_future6", '3'))
|
|
||||||
else:
|
|
||||||
self.fail("expected exception didn't occur")
|
|
||||||
|
|
||||||
def test_badfuture7(self):
|
def test_badfuture7(self):
|
||||||
try:
|
with self.assertRaises(SyntaxError) as cm:
|
||||||
from test import badsyntax_future7
|
from test import badsyntax_future7
|
||||||
except SyntaxError as msg:
|
self.check_syntax_error(cm.exception, "badsyntax_future7", 3, 53)
|
||||||
self.assertEqual(get_error_location(msg), ("badsyntax_future7", '3'))
|
|
||||||
else:
|
|
||||||
self.fail("expected exception didn't occur")
|
|
||||||
|
|
||||||
def test_badfuture8(self):
|
def test_badfuture8(self):
|
||||||
try:
|
with self.assertRaises(SyntaxError) as cm:
|
||||||
from test import badsyntax_future8
|
from test import badsyntax_future8
|
||||||
except SyntaxError as msg:
|
self.check_syntax_error(cm.exception, "badsyntax_future8", 3)
|
||||||
self.assertEqual(get_error_location(msg), ("badsyntax_future8", '3'))
|
|
||||||
else:
|
|
||||||
self.fail("expected exception didn't occur")
|
|
||||||
|
|
||||||
def test_badfuture9(self):
|
def test_badfuture9(self):
|
||||||
try:
|
with self.assertRaises(SyntaxError) as cm:
|
||||||
from test import badsyntax_future9
|
from test import badsyntax_future9
|
||||||
except SyntaxError as msg:
|
self.check_syntax_error(cm.exception, "badsyntax_future9", 3, 0)
|
||||||
self.assertEqual(get_error_location(msg), ("badsyntax_future9", '3'))
|
|
||||||
else:
|
|
||||||
self.fail("expected exception didn't occur")
|
|
||||||
|
|
||||||
def test_badfuture10(self):
|
def test_badfuture10(self):
|
||||||
try:
|
with self.assertRaises(SyntaxError) as cm:
|
||||||
from test import badsyntax_future10
|
from test import badsyntax_future10
|
||||||
except SyntaxError as msg:
|
self.check_syntax_error(cm.exception, "badsyntax_future10", 3, 0)
|
||||||
self.assertEqual(get_error_location(msg), ("badsyntax_future10", '3'))
|
|
||||||
else:
|
|
||||||
self.fail("expected exception didn't occur")
|
|
||||||
|
|
||||||
def test_parserhack(self):
|
def test_parserhack(self):
|
||||||
# test that the parser.c::future_hack function works as expected
|
# test that the parser.c::future_hack function works as expected
|
||||||
|
|
|
@ -240,8 +240,10 @@ class TestSupport(unittest.TestCase):
|
||||||
self.assertEqual(cm.exception.errno, errno.EBADF)
|
self.assertEqual(cm.exception.errno, errno.EBADF)
|
||||||
|
|
||||||
def test_check_syntax_error(self):
|
def test_check_syntax_error(self):
|
||||||
support.check_syntax_error(self, "def class")
|
support.check_syntax_error(self, "def class", lineno=1, offset=9)
|
||||||
self.assertRaises(AssertionError, support.check_syntax_error, self, "1")
|
self.assertRaises(AssertionError, support.check_syntax_error, self, "1")
|
||||||
|
#with self.assertRaises(AssertionError):
|
||||||
|
#support.check_syntax_error(self, "x=1")
|
||||||
|
|
||||||
def test_CleanImport(self):
|
def test_CleanImport(self):
|
||||||
import importlib
|
import importlib
|
||||||
|
|
|
@ -148,15 +148,17 @@ class SymtableTest(unittest.TestCase):
|
||||||
def test_filename_correct(self):
|
def test_filename_correct(self):
|
||||||
### Bug tickler: SyntaxError file name correct whether error raised
|
### Bug tickler: SyntaxError file name correct whether error raised
|
||||||
### while parsing or building symbol table.
|
### while parsing or building symbol table.
|
||||||
def checkfilename(brokencode):
|
def checkfilename(brokencode, offset):
|
||||||
try:
|
try:
|
||||||
symtable.symtable(brokencode, "spam", "exec")
|
symtable.symtable(brokencode, "spam", "exec")
|
||||||
except SyntaxError as e:
|
except SyntaxError as e:
|
||||||
self.assertEqual(e.filename, "spam")
|
self.assertEqual(e.filename, "spam")
|
||||||
|
self.assertEqual(e.lineno, 1)
|
||||||
|
self.assertEqual(e.offset, offset)
|
||||||
else:
|
else:
|
||||||
self.fail("no SyntaxError for %r" % (brokencode,))
|
self.fail("no SyntaxError for %r" % (brokencode,))
|
||||||
checkfilename("def f(x): foo)(") # parse-time
|
checkfilename("def f(x): foo)(", 14) # parse-time
|
||||||
checkfilename("def f(x): global x") # symtable-build-time
|
checkfilename("def f(x): global x", 10) # symtable-build-time
|
||||||
symtable.symtable("pass", b"spam", "exec")
|
symtable.symtable("pass", b"spam", "exec")
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
symtable.symtable("pass", bytearray(b"spam"), "exec")
|
symtable.symtable("pass", bytearray(b"spam"), "exec")
|
||||||
|
|
|
@ -540,7 +540,7 @@ from test import support
|
||||||
class SyntaxTestCase(unittest.TestCase):
|
class SyntaxTestCase(unittest.TestCase):
|
||||||
|
|
||||||
def _check_error(self, code, errtext,
|
def _check_error(self, code, errtext,
|
||||||
filename="<testcase>", mode="exec", subclass=None):
|
filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
|
||||||
"""Check that compiling code raises SyntaxError with errtext.
|
"""Check that compiling code raises SyntaxError with errtext.
|
||||||
|
|
||||||
errtest is a regular expression that must be present in the
|
errtest is a regular expression that must be present in the
|
||||||
|
@ -555,6 +555,11 @@ class SyntaxTestCase(unittest.TestCase):
|
||||||
mo = re.search(errtext, str(err))
|
mo = re.search(errtext, str(err))
|
||||||
if mo is None:
|
if mo is None:
|
||||||
self.fail("SyntaxError did not contain '%r'" % (errtext,))
|
self.fail("SyntaxError did not contain '%r'" % (errtext,))
|
||||||
|
self.assertEqual(err.filename, filename)
|
||||||
|
if lineno is not None:
|
||||||
|
self.assertEqual(err.lineno, lineno)
|
||||||
|
if offset is not None:
|
||||||
|
self.assertEqual(err.offset, offset)
|
||||||
else:
|
else:
|
||||||
self.fail("compile() did not raise SyntaxError")
|
self.fail("compile() did not raise SyntaxError")
|
||||||
|
|
||||||
|
@ -565,7 +570,7 @@ class SyntaxTestCase(unittest.TestCase):
|
||||||
self._check_error("del f()", "delete")
|
self._check_error("del f()", "delete")
|
||||||
|
|
||||||
def test_global_err_then_warn(self):
|
def test_global_err_then_warn(self):
|
||||||
# Bug tickler: The SyntaxError raised for one global statement
|
# Bug #763201: The SyntaxError raised for one global statement
|
||||||
# shouldn't be clobbered by a SyntaxWarning issued for a later one.
|
# shouldn't be clobbered by a SyntaxWarning issued for a later one.
|
||||||
source = """if 1:
|
source = """if 1:
|
||||||
def error(a):
|
def error(a):
|
||||||
|
@ -575,7 +580,7 @@ class SyntaxTestCase(unittest.TestCase):
|
||||||
global b # SyntaxWarning
|
global b # SyntaxWarning
|
||||||
"""
|
"""
|
||||||
warnings.filterwarnings(action='ignore', category=SyntaxWarning)
|
warnings.filterwarnings(action='ignore', category=SyntaxWarning)
|
||||||
self._check_error(source, "global")
|
self._check_error(source, "global", lineno=3, offset=16)
|
||||||
warnings.filters.pop(0)
|
warnings.filters.pop(0)
|
||||||
|
|
||||||
def test_break_outside_loop(self):
|
def test_break_outside_loop(self):
|
||||||
|
|
|
@ -10,6 +10,9 @@ Release date: TBA
|
||||||
Core and Builtins
|
Core and Builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- Issue #28512: Fixed setting the offset attribute of SyntaxError by
|
||||||
|
PyErr_SyntaxLocationEx() and PyErr_SyntaxLocationObject().
|
||||||
|
|
||||||
- Issue #5322: Fixed setting __new__ to a PyCFunction inside Python code.
|
- Issue #5322: Fixed setting __new__ to a PyCFunction inside Python code.
|
||||||
Original patch by Andreas Stührk.
|
Original patch by Andreas Stührk.
|
||||||
|
|
||||||
|
|
|
@ -1009,16 +1009,15 @@ PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
Py_DECREF(tmp);
|
Py_DECREF(tmp);
|
||||||
}
|
}
|
||||||
|
tmp = NULL;
|
||||||
if (col_offset >= 0) {
|
if (col_offset >= 0) {
|
||||||
tmp = PyLong_FromLong(col_offset);
|
tmp = PyLong_FromLong(col_offset);
|
||||||
if (tmp == NULL)
|
if (tmp == NULL)
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
else {
|
|
||||||
if (_PyObject_SetAttrId(v, &PyId_offset, tmp))
|
|
||||||
PyErr_Clear();
|
|
||||||
Py_DECREF(tmp);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if (_PyObject_SetAttrId(v, &PyId_offset, tmp ? tmp : Py_None))
|
||||||
|
PyErr_Clear();
|
||||||
|
Py_XDECREF(tmp);
|
||||||
if (filename != NULL) {
|
if (filename != NULL) {
|
||||||
if (_PyObject_SetAttrId(v, &PyId_filename, filename))
|
if (_PyObject_SetAttrId(v, &PyId_filename, filename))
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
|
@ -1030,9 +1029,6 @@ PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
|
||||||
Py_DECREF(tmp);
|
Py_DECREF(tmp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (_PyObject_SetAttrId(v, &PyId_offset, Py_None)) {
|
|
||||||
PyErr_Clear();
|
|
||||||
}
|
|
||||||
if (exc != PyExc_SyntaxError) {
|
if (exc != PyExc_SyntaxError) {
|
||||||
if (!_PyObject_HasAttrId(v, &PyId_msg)) {
|
if (!_PyObject_HasAttrId(v, &PyId_msg)) {
|
||||||
tmp = PyObject_Str(v);
|
tmp = PyObject_Str(v);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue