xdegaye | e0582a3 | 2017-11-12 16:50:48 +0100 | [diff] [blame] | 1 | """Test the interactive interpreter.""" |
| 2 | |
| 3 | import sys |
| 4 | import os |
| 5 | import unittest |
| 6 | import subprocess |
| 7 | from textwrap import dedent |
| 8 | from test.support import cpython_only, SuppressCrashReport |
| 9 | from test.support.script_helper import kill_python |
| 10 | |
| 11 | def 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' |
| 32 | return subprocess.Popen(cmd_line, executable=sys.executable, |
| 33 | stdin=subprocess.PIPE, |
| 34 | stdout=stdout, stderr=stderr, |
| 35 | **kw) |
| 36 | |
| 37 | class TestInteractiveInterpreter(unittest.TestCase): |
| 38 | |
| 39 | @cpython_only |
| 40 | def test_no_memory(self): |
| 41 | # Issue #30696: Fix the interactive interpreter looping endlessly when |
| 42 | # no memory. Check also that the fix does not break the interactive |
| 43 | # loop when an exception is raised. |
| 44 | user_input = """ |
| 45 | import sys, _testcapi |
| 46 | 1/0 |
| 47 | print('After the exception.') |
| 48 | _testcapi.set_nomemory(0) |
| 49 | sys.exit(0) |
| 50 | """ |
| 51 | user_input = dedent(user_input) |
| 52 | user_input = user_input.encode() |
| 53 | p = spawn_repl() |
| 54 | with SuppressCrashReport(): |
| 55 | p.stdin.write(user_input) |
| 56 | output = kill_python(p) |
| 57 | self.assertIn(b'After the exception.', output) |
| 58 | # Exit code 120: Py_FinalizeEx() failed to flush stdout and stderr. |
| 59 | self.assertIn(p.returncode, (1, 120)) |
| 60 | |
| 61 | if __name__ == "__main__": |
| 62 | unittest.main() |