File: test_eof.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (90 lines) | stat: -rw-r--r-- 3,625 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""test script for a few new invalid token catches"""

import sys
from test import support
from test.support import os_helper
from test.support import script_helper
import unittest

if sys.implementation.name == 'pypy':
    eof_msg = "unexpected end of file (EOF)"
else:
    eof_msg = "unexpected EOF"

class EOFTestCase(unittest.TestCase):
    def test_EOF_single_quote(self):
        expect = "unterminated string literal (detected at line 1) (<string>, line 1)"
        for quote in ("'", "\""):
            try:
                eval(f"""{quote}this is a test\
                """)
            except SyntaxError as msg:
                self.assertEqual(str(msg), expect)
                self.assertEqual(msg.offset, 1)
            else:
                raise support.TestFailed

    def test_EOFS(self):
        expect = ("unterminated triple-quoted string literal (detected at line 1) (<string>, line 1)")
        try:
            eval("""'''this is a test""")
        except SyntaxError as msg:
            self.assertEqual(str(msg), expect)
            self.assertEqual(msg.offset, 1)
        else:
            raise support.TestFailed

    def test_EOFS_with_file(self):
        expect = ("(<string>, line 1)")
        with os_helper.temp_dir() as temp_dir:
            file_name = script_helper.make_script(temp_dir, 'foo', """'''this is \na \ntest""")
            rc, out, err = script_helper.assert_python_failure(file_name)
        self.assertIn(b'unterminated triple-quoted string literal (detected at line 3)', err)

    def test_eof_with_line_continuation(self):
        if sys.implementation.name == 'pypy':
            expect = f"{eof_msg} in multi-line statement (<string>, line 1)"
        else:
            expect = f"{eof_msg} while parsing (<string>, line 1)"
        try:
            compile('"\\xhh" \\',  '<string>', 'exec', dont_inherit=True)
        except SyntaxError as msg:
            self.assertEqual(str(msg), expect)
        else:
            raise support.TestFailed

    def test_line_continuation_EOF(self):
        """A continuation at the end of input must be an error; bpo2180."""
        if sys.implementation.name == 'pypy':
            expect = f"{eof_msg} in multi-line statement (<string>, line 1)"
        else:
            expect = f"{eof_msg} while parsing (<string>, line 1)"
        with self.assertRaises(SyntaxError) as excinfo:
            exec('x = 5\\')
        self.assertEqual(str(excinfo.exception), expect)
        with self.assertRaises(SyntaxError) as excinfo:
            exec('\\')
        self.assertEqual(str(excinfo.exception), expect)

    @unittest.skipIf(not sys.executable, "sys.executable required")
    def test_line_continuation_EOF_from_file_bpo2180(self):
        """Ensure tok_nextc() does not add too many ending newlines."""
        if sys.implementation.name == 'pypy':
            expect = f'{eof_msg} in multi-line statement'.encode()
        else:
            expect = f'{eof_msg} while parsing'.encode()
        with os_helper.temp_dir() as temp_dir:
            file_name = script_helper.make_script(temp_dir, 'foo', '\\')
            rc, out, err = script_helper.assert_python_failure(file_name)
            self.assertIn(expect, err)
            self.assertIn(b'line 1', err)
            self.assertIn(b'\\', err)

            file_name = script_helper.make_script(temp_dir, 'foo', 'y = 6\\')
            rc, out, err = script_helper.assert_python_failure(file_name)
            self.assertIn(expect, err)
            self.assertIn(b'line 1', err)
            self.assertIn(b'y = 6\\', err)

if __name__ == "__main__":
    unittest.main()