blob: 03bf8d8b5483fb1488d3868aeffdfb1f9d1dbd9f [file] [log] [blame]
xdegayee0582a32017-11-12 16:50:48 +01001"""Test the interactive interpreter."""
2
3import sys
4import os
5import unittest
6import subprocess
7from textwrap import dedent
8from test.support import cpython_only, SuppressCrashReport
9from test.support.script_helper import kill_python
10
11def spawn_repl(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kw):
12 """Run the Python REPL with the given arguments.
13
14 kw is extra keyword args to pass to subprocess.Popen. Returns a Popen
15 object.
16 """
17
18 # To run the REPL without using a terminal, spawn python with the command
19 # line option '-i' and the process name set to '<stdin>'.
20 # The directory of argv[0] must match the directory of the Python
21 # executable for the Popen() call to python to succeed as the directory
22 # path may be used by Py_GetPath() to build the default module search
23 # path.
24 stdin_fname = os.path.join(os.path.dirname(sys.executable), "<stdin>")
25 cmd_line = [stdin_fname, '-E', '-i']
26 cmd_line.extend(args)
27
28 # Set TERM=vt100, for the rationale see the comments in spawn_python() of
29 # test.support.script_helper.
30 env = kw.setdefault('env', dict(os.environ))
31 env['TERM'] = 'vt100'
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +020032 return subprocess.Popen(cmd_line,
33 executable=sys.executable,
34 text=True,
xdegayee0582a32017-11-12 16:50:48 +010035 stdin=subprocess.PIPE,
36 stdout=stdout, stderr=stderr,
37 **kw)
38
39class TestInteractiveInterpreter(unittest.TestCase):
40
41 @cpython_only
42 def test_no_memory(self):
43 # Issue #30696: Fix the interactive interpreter looping endlessly when
44 # no memory. Check also that the fix does not break the interactive
45 # loop when an exception is raised.
46 user_input = """
47 import sys, _testcapi
48 1/0
49 print('After the exception.')
50 _testcapi.set_nomemory(0)
51 sys.exit(0)
52 """
53 user_input = dedent(user_input)
xdegayee0582a32017-11-12 16:50:48 +010054 p = spawn_repl()
55 with SuppressCrashReport():
56 p.stdin.write(user_input)
57 output = kill_python(p)
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +020058 self.assertIn('After the exception.', output)
xdegayee0582a32017-11-12 16:50:48 +010059 # Exit code 120: Py_FinalizeEx() failed to flush stdout and stderr.
60 self.assertIn(p.returncode, (1, 120))
61
Pablo Galindo5ec91f72020-01-06 15:59:09 +000062 @cpython_only
63 def test_multiline_string_parsing(self):
64 # bpo-39209: Multiline string tokens need to be handled in the tokenizer
65 # in two places: the interactive path and the non-interactive path.
66 user_input = '''\
67 x = """<?xml version="1.0" encoding="iso-8859-1"?>
68 <test>
69 <Users>
70 <fun25>
71 <limits>
72 <total>0KiB</total>
73 <kbps>0</kbps>
74 <rps>1.3</rps>
75 <connections>0</connections>
76 </limits>
77 <usages>
78 <total>16738211KiB</total>
79 <kbps>237.15</kbps>
80 <rps>1.3</rps>
81 <connections>0</connections>
82 </usages>
83 <time_to_refresh>never</time_to_refresh>
84 <limit_exceeded_URL>none</limit_exceeded_URL>
85 </fun25>
86 </Users>
87 </test>"""
88 '''
89 user_input = dedent(user_input)
Pablo Galindo5ec91f72020-01-06 15:59:09 +000090 p = spawn_repl()
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +020091 p.stdin.write(user_input)
Pablo Galindo5ec91f72020-01-06 15:59:09 +000092 output = kill_python(p)
93 self.assertEqual(p.returncode, 0)
94
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +020095 def test_close_stdin(self):
96 user_input = dedent('''
97 import os
98 print("before close")
99 os.close(0)
100 ''')
Victor Stinnerf6e58ae2020-06-10 18:49:23 +0200101 prepare_repl = dedent('''
102 from test.support import suppress_msvcrt_asserts
103 suppress_msvcrt_asserts()
104 ''')
105 process = spawn_repl('-c', prepare_repl)
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +0200106 output = process.communicate(user_input)[0]
107 self.assertEqual(process.returncode, 0)
108 self.assertIn('before close', output)
109
Pablo Galindo5ec91f72020-01-06 15:59:09 +0000110
xdegayee0582a32017-11-12 16:50:48 +0100111if __name__ == "__main__":
112 unittest.main()