def get_output(self, code, filename=None, **kwargs): """ Run the specified code in Python (in a new child process) and read the output from the standard error or from a file (if filename is set). Return the output lines as a list. Strip the reference count from the standard error for Python debug build, and replace "Current thread 0x00007f8d8fbd9700" by "Current thread XXX". """ code = dedent(code).strip() with SuppressCrashReport(): process = spawn_python('-c', code, **kwargs) stdout, stderr = process.communicate() exitcode = process.wait() output = re.sub(br"\[\d+ refs\]\r?\n?", b"", stdout).strip() output = output.decode('ascii', 'backslashreplace') if filename: self.assertEqual(output, '') with open(filename, "rb") as fp: output = fp.read() output = output.decode('ascii', 'backslashreplace') output = re.sub('Current thread 0x[0-9a-f]+', 'Current thread XXX', output) return output.splitlines(), exitcode
def test_multiline_string_parsing(self): # bpo-39209: Multiline string tokens need to be handled in the tokenizer # in two places: the interactive path and the non-interactive path. user_input = '''\ x = """<?xml version="1.0" encoding="iso-8859-1"?> <test> <Users> <fun25> <limits> <total>0KiB</total> <kbps>0</kbps> <rps>1.3</rps> <connections>0</connections> </limits> <usages> <total>16738211KiB</total> <kbps>237.15</kbps> <rps>1.3</rps> <connections>0</connections> </usages> <time_to_refresh>never</time_to_refresh> <limit_exceeded_URL>none</limit_exceeded_URL> </fun25> </Users> </test>""" ''' user_input = dedent(user_input) user_input = user_input.encode() p = spawn_repl() with SuppressCrashReport(): p.stdin.write(user_input) output = kill_python(p) self.assertEqual(p.returncode, 0)
def test_no_memory(self): # Issue #30696: Fix the interactive interpreter looping endlessly when # no memory. Check also that the fix does not break the interactive # loop when an exception is raised. user_input = """ import sys, _testcapi 1/0 print('After the exception.') _testcapi.set_nomemory(0) sys.exit(0) """ user_input = dedent(user_input) p = spawn_repl() with SuppressCrashReport(): p.stdin.write(user_input) output = kill_python(p) self.assertIn('After the exception.', output) # Exit code 120: Py_FinalizeEx() failed to flush stdout and stderr. self.assertIn(p.returncode, (1, 120))
def test_recursion_normalizing_with_no_memory(self): # Issue #30697. Test that in the abort that occurs when there is no # memory left and the size of the Python frames stack is greater than # the size of the list of preallocated MemoryError instances, the # Fatal Python error message mentions MemoryError. code = """if 1: import _testcapi class C(): pass def recurse(cnt): cnt -= 1 if cnt: recurse(cnt) else: _testcapi.set_nomemory(0) C() recurse(16) """ with SuppressCrashReport(): rc, out, err = script_helper.assert_python_failure("-c", code) self.assertIn(b'Fatal Python error: Cannot recover from ' b'MemoryErrors while normalizing exceptions.', err)
# Helper script for test_tempfile.py. argv[2] is the number of a file # descriptor which should _not_ be open. Check this by attempting to # write to it -- if we succeed, something is wrong. import sys import os from test.support import SuppressCrashReport with SuppressCrashReport(): verbose = (sys.argv[1] == 'v') try: fd = int(sys.argv[2]) try: os.write(fd, b"blat") except OSError: # Success -- could not write to fd. sys.exit(0) else: if verbose: sys.stderr.write("fd %d is open in child" % fd) sys.exit(1) except Exception: if verbose: raise sys.exit(1)