[Python-checkins] cpython (merge 3.6 -> default): Issue #28512: Fixed setting the offset attribute of SyntaxError by

serhiy.storchaka python-checkins at python.org
Sun Dec 11 07:44:50 EST 2016


https://hg.python.org/cpython/rev/72aaef2d144b
changeset:   105589:72aaef2d144b
parent:      105585:b11850871300
parent:      105588:df59faf7fa59
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sun Dec 11 14:44:21 2016 +0200
summary:
  Issue #28512: Fixed setting the offset attribute of SyntaxError by
PyErr_SyntaxLocationEx() and PyErr_SyntaxLocationObject().

files:
  Lib/test/support/__init__.py |  13 +++-
  Lib/test/test_future.py      |  63 ++++++++---------------
  Lib/test/test_global.py      |   6 +-
  Lib/test/test_support.py     |   2 +-
  Lib/test/test_symtable.py    |   8 +-
  Lib/test/test_syntax.py      |   9 ++-
  Misc/NEWS                    |   3 +
  Python/errors.c              |  12 +---
  8 files changed, 56 insertions(+), 60 deletions(-)


diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -1060,9 +1060,16 @@
         file.close()
         unlink(TESTFN)
 
-def check_syntax_error(testcase, statement):
-    testcase.assertRaises(SyntaxError, compile, statement,
-                          '<test string>', 'exec')
+def check_syntax_error(testcase, statement, *, lineno=None, offset=None):
+    with testcase.assertRaises(SyntaxError) as cm:
+        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):
     import urllib.request, urllib.parse
diff --git a/Lib/test/test_future.py b/Lib/test/test_future.py
--- a/Lib/test/test_future.py
+++ b/Lib/test/test_future.py
@@ -2,6 +2,7 @@
 
 import unittest
 from test import support
+import os
 import re
 
 rx = re.compile(r'\((\S+).py, line (\d+)')
@@ -12,6 +13,12 @@
 
 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):
         with support.CleanImport('future_test1'):
             from test import future_test1
@@ -27,68 +34,44 @@
             from test import test_future3
 
     def test_badfuture3(self):
-        try:
+        with self.assertRaises(SyntaxError) as cm:
             from test import badsyntax_future3
-        except SyntaxError as msg:
-            self.assertEqual(get_error_location(msg), ("badsyntax_future3", '3'))
-        else:
-            self.fail("expected exception didn't occur")
+        self.check_syntax_error(cm.exception, "badsyntax_future3", 3)
 
     def test_badfuture4(self):
-        try:
+        with self.assertRaises(SyntaxError) as cm:
             from test import badsyntax_future4
-        except SyntaxError as msg:
-            self.assertEqual(get_error_location(msg), ("badsyntax_future4", '3'))
-        else:
-            self.fail("expected exception didn't occur")
+        self.check_syntax_error(cm.exception, "badsyntax_future4", 3)
 
     def test_badfuture5(self):
-        try:
+        with self.assertRaises(SyntaxError) as cm:
             from test import badsyntax_future5
-        except SyntaxError as msg:
-            self.assertEqual(get_error_location(msg), ("badsyntax_future5", '4'))
-        else:
-            self.fail("expected exception didn't occur")
+        self.check_syntax_error(cm.exception, "badsyntax_future5", 4)
 
     def test_badfuture6(self):
-        try:
+        with self.assertRaises(SyntaxError) as cm:
             from test import badsyntax_future6
-        except SyntaxError as msg:
-            self.assertEqual(get_error_location(msg), ("badsyntax_future6", '3'))
-        else:
-            self.fail("expected exception didn't occur")
+        self.check_syntax_error(cm.exception, "badsyntax_future6", 3)
 
     def test_badfuture7(self):
-        try:
+        with self.assertRaises(SyntaxError) as cm:
             from test import badsyntax_future7
-        except SyntaxError as msg:
-            self.assertEqual(get_error_location(msg), ("badsyntax_future7", '3'))
-        else:
-            self.fail("expected exception didn't occur")
+        self.check_syntax_error(cm.exception, "badsyntax_future7", 3, 53)
 
     def test_badfuture8(self):
-        try:
+        with self.assertRaises(SyntaxError) as cm:
             from test import badsyntax_future8
-        except SyntaxError as msg:
-            self.assertEqual(get_error_location(msg), ("badsyntax_future8", '3'))
-        else:
-            self.fail("expected exception didn't occur")
+        self.check_syntax_error(cm.exception, "badsyntax_future8", 3)
 
     def test_badfuture9(self):
-        try:
+        with self.assertRaises(SyntaxError) as cm:
             from test import badsyntax_future9
-        except SyntaxError as msg:
-            self.assertEqual(get_error_location(msg), ("badsyntax_future9", '3'))
-        else:
-            self.fail("expected exception didn't occur")
+        self.check_syntax_error(cm.exception, "badsyntax_future9", 3, 0)
 
     def test_badfuture10(self):
-        try:
+        with self.assertRaises(SyntaxError) as cm:
             from test import badsyntax_future10
-        except SyntaxError as msg:
-            self.assertEqual(get_error_location(msg), ("badsyntax_future10", '3'))
-        else:
-            self.fail("expected exception didn't occur")
+        self.check_syntax_error(cm.exception, "badsyntax_future10", 3, 0)
 
     def test_parserhack(self):
         # test that the parser.c::future_hack function works as expected
diff --git a/Lib/test/test_global.py b/Lib/test/test_global.py
--- a/Lib/test/test_global.py
+++ b/Lib/test/test_global.py
@@ -24,7 +24,7 @@
     global a
     global b
 """
-        check_syntax_error(self, prog_text_1)
+        check_syntax_error(self, prog_text_1, lineno=4, offset=4)
 
     def test2(self):
         prog_text_2 = """\
@@ -32,7 +32,7 @@
     print(x)
     global x
 """
-        check_syntax_error(self, prog_text_2)
+        check_syntax_error(self, prog_text_2, lineno=3, offset=4)
 
     def test3(self):
         prog_text_3 = """\
@@ -41,7 +41,7 @@
     x = 2
     global x
 """
-        check_syntax_error(self, prog_text_3)
+        check_syntax_error(self, prog_text_3, lineno=4, offset=4)
 
     def test4(self):
         prog_text_4 = """\
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -240,7 +240,7 @@
         self.assertEqual(cm.exception.errno, errno.EBADF)
 
     def test_check_syntax_error(self):
-        support.check_syntax_error(self, "def class")
+        support.check_syntax_error(self, "def class", lineno=1, offset=9)
         with self.assertRaises(AssertionError):
             support.check_syntax_error(self, "x=1")
 
diff --git a/Lib/test/test_symtable.py b/Lib/test/test_symtable.py
--- a/Lib/test/test_symtable.py
+++ b/Lib/test/test_symtable.py
@@ -159,15 +159,17 @@
     def test_filename_correct(self):
         ### Bug tickler: SyntaxError file name correct whether error raised
         ### while parsing or building symbol table.
-        def checkfilename(brokencode):
+        def checkfilename(brokencode, offset):
             try:
                 symtable.symtable(brokencode, "spam", "exec")
             except SyntaxError as e:
                 self.assertEqual(e.filename, "spam")
+                self.assertEqual(e.lineno, 1)
+                self.assertEqual(e.offset, offset)
             else:
                 self.fail("no SyntaxError for %r" % (brokencode,))
-        checkfilename("def f(x): foo)(")  # parse-time
-        checkfilename("def f(x): global x")  # symtable-build-time
+        checkfilename("def f(x): foo)(", 14)  # parse-time
+        checkfilename("def f(x): global x", 10)  # symtable-build-time
         symtable.symtable("pass", b"spam", "exec")
         with self.assertWarns(DeprecationWarning), \
              self.assertRaises(TypeError):
diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py
--- a/Lib/test/test_syntax.py
+++ b/Lib/test/test_syntax.py
@@ -548,7 +548,7 @@
 class SyntaxTestCase(unittest.TestCase):
 
     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.
 
         errtest is a regular expression that must be present in the
@@ -563,6 +563,11 @@
             mo = re.search(errtext, str(err))
             if mo is None:
                 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:
             self.fail("compile() did not raise SyntaxError")
 
@@ -573,7 +578,7 @@
         self._check_error("del f()", "delete")
 
     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.
         source = """if 1:
             def error(a):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #28512: Fixed setting the offset attribute of SyntaxError by
+  PyErr_SyntaxLocationEx() and PyErr_SyntaxLocationObject().
+
 - Issue #28918: Fix the cross compilation of xxlimited when Python has been
   built with Py_DEBUG defined.
 
diff --git a/Python/errors.c b/Python/errors.c
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -1053,16 +1053,15 @@
             PyErr_Clear();
         Py_DECREF(tmp);
     }
+    tmp = NULL;
     if (col_offset >= 0) {
         tmp = PyLong_FromLong(col_offset);
         if (tmp == NULL)
             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 (_PyObject_SetAttrId(v, &PyId_filename, filename))
             PyErr_Clear();
@@ -1074,9 +1073,6 @@
             Py_DECREF(tmp);
         }
     }
-    if (_PyObject_SetAttrId(v, &PyId_offset, Py_None)) {
-        PyErr_Clear();
-    }
     if (exc != PyExc_SyntaxError) {
         if (!_PyObject_HasAttrId(v, &PyId_msg)) {
             tmp = PyObject_Str(v);

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list