|
8 | 8 | import pickle
|
9 | 9 | import weakref
|
10 | 10 | import errno
|
| 11 | +from textwrap import dedent |
11 | 12 |
|
12 | 13 | from test.support import (captured_stderr, check_impl_detail,
|
13 | 14 | cpython_only, gc_collect,
|
@@ -1833,6 +1834,130 @@ def test_copy_pickle(self):
|
1833 | 1834 | self.assertEqual(exc.name, orig.name)
|
1834 | 1835 | self.assertEqual(exc.path, orig.path)
|
1835 | 1836 |
|
| 1837 | +class SyntaxErrorTests(unittest.TestCase): |
| 1838 | + def test_range_of_offsets(self): |
| 1839 | + cases = [ |
| 1840 | + # Basic range from 2->7 |
| 1841 | + (("bad.py", 1, 2, "abcdefg", 1, 7), |
| 1842 | + dedent( |
| 1843 | + """ |
| 1844 | + File "bad.py", line 1 |
| 1845 | + abcdefg |
| 1846 | + ^^^^^ |
| 1847 | + SyntaxError: bad bad |
| 1848 | + """)), |
| 1849 | + # end_offset = start_offset + 1 |
| 1850 | + (("bad.py", 1, 2, "abcdefg", 1, 3), |
| 1851 | + dedent( |
| 1852 | + """ |
| 1853 | + File "bad.py", line 1 |
| 1854 | + abcdefg |
| 1855 | + ^ |
| 1856 | + SyntaxError: bad bad |
| 1857 | + """)), |
| 1858 | + # Negative end offset |
| 1859 | + (("bad.py", 1, 2, "abcdefg", 1, -2), |
| 1860 | + dedent( |
| 1861 | + """ |
| 1862 | + File "bad.py", line 1 |
| 1863 | + abcdefg |
| 1864 | + ^ |
| 1865 | + SyntaxError: bad bad |
| 1866 | + """)), |
| 1867 | + # end offset before starting offset |
| 1868 | + (("bad.py", 1, 4, "abcdefg", 1, 2), |
| 1869 | + dedent( |
| 1870 | + """ |
| 1871 | + File "bad.py", line 1 |
| 1872 | + abcdefg |
| 1873 | + ^ |
| 1874 | + SyntaxError: bad bad |
| 1875 | + """)), |
| 1876 | + # Both offsets negative |
| 1877 | + (("bad.py", 1, -4, "abcdefg", 1, -2), |
| 1878 | + dedent( |
| 1879 | + """ |
| 1880 | + File "bad.py", line 1 |
| 1881 | + abcdefg |
| 1882 | + SyntaxError: bad bad |
| 1883 | + """)), |
| 1884 | + # Both offsets negative and the end more negatibe |
| 1885 | + (("bad.py", 1, -4, "abcdefg", 1, -5), |
| 1886 | + dedent( |
| 1887 | + """ |
| 1888 | + File "bad.py", line 1 |
| 1889 | + abcdefg |
| 1890 | + SyntaxError: bad bad |
| 1891 | + """)), |
| 1892 | + # Both offsets 0 |
| 1893 | + (("bad.py", 1, 0, "abcdefg", 1, 0), |
| 1894 | + dedent( |
| 1895 | + """ |
| 1896 | + File "bad.py", line 1 |
| 1897 | + abcdefg |
| 1898 | + SyntaxError: bad bad |
| 1899 | + """)), |
| 1900 | + # Start offset 0 and end offset not 0 |
| 1901 | + (("bad.py", 1, 0, "abcdefg", 1, 5), |
| 1902 | + dedent( |
| 1903 | + """ |
| 1904 | + File "bad.py", line 1 |
| 1905 | + abcdefg |
| 1906 | + SyntaxError: bad bad |
| 1907 | + """)), |
| 1908 | + # End offset pass the source lenght |
| 1909 | + (("bad.py", 1, 2, "abcdefg", 1, 100), |
| 1910 | + dedent( |
| 1911 | + """ |
| 1912 | + File "bad.py", line 1 |
| 1913 | + abcdefg |
| 1914 | + ^^^^^^ |
| 1915 | + SyntaxError: bad bad |
| 1916 | + """)), |
| 1917 | + ] |
| 1918 | + for args, expected in cases: |
| 1919 | + with self.subTest(args=args): |
| 1920 | + try: |
| 1921 | + raise SyntaxError("bad bad", args) |
| 1922 | + except SyntaxError as exc: |
| 1923 | + with support.captured_stderr() as err: |
| 1924 | + sys.__excepthook__(*sys.exc_info()) |
| 1925 | + the_exception = exc |
| 1926 | + |
| 1927 | + def test_attributes_new_constructor(self): |
| 1928 | + args = ("bad.py", 1, 2, "abcdefg", 1, 100) |
| 1929 | + the_exception = SyntaxError("bad bad", args) |
| 1930 | + filename, lineno, offset, error, end_lineno, end_offset = args |
| 1931 | + self.assertEqual(filename, the_exception.filename) |
| 1932 | + self.assertEqual(lineno, the_exception.lineno) |
| 1933 | + self.assertEqual(end_lineno, the_exception.end_lineno) |
| 1934 | + self.assertEqual(offset, the_exception.offset) |
| 1935 | + self.assertEqual(end_offset, the_exception.end_offset) |
| 1936 | + self.assertEqual(error, the_exception.text) |
| 1937 | + self.assertEqual("bad bad", the_exception.msg) |
| 1938 | + |
| 1939 | + def test_attributes_old_constructor(self): |
| 1940 | + args = ("bad.py", 1, 2, "abcdefg") |
| 1941 | + the_exception = SyntaxError("bad bad", args) |
| 1942 | + filename, lineno, offset, error = args |
| 1943 | + self.assertEqual(filename, the_exception.filename) |
| 1944 | + self.assertEqual(lineno, the_exception.lineno) |
| 1945 | + self.assertEqual(None, the_exception.end_lineno) |
| 1946 | + self.assertEqual(offset, the_exception.offset) |
| 1947 | + self.assertEqual(None, the_exception.end_offset) |
| 1948 | + self.assertEqual(error, the_exception.text) |
| 1949 | + self.assertEqual("bad bad", the_exception.msg) |
| 1950 | + |
| 1951 | + def test_incorrect_constructor(self): |
| 1952 | + args = ("bad.py", 1, 2) |
| 1953 | + self.assertRaises(TypeError, SyntaxError, "bad bad", args) |
| 1954 | + |
| 1955 | + args = ("bad.py", 1, 2, 4, 5, 6, 7) |
| 1956 | + self.assertRaises(TypeError, SyntaxError, "bad bad", args) |
| 1957 | + |
| 1958 | + args = ("bad.py", 1, 2, "abcdefg", 1) |
| 1959 | + self.assertRaises(TypeError, SyntaxError, "bad bad", args) |
| 1960 | + |
1836 | 1961 |
|
1837 | 1962 | class PEP626Tests(unittest.TestCase):
|
1838 | 1963 |
|
|
0 commit comments