Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1d5ccdb

Browse files
committed
Close #14136 by cleaning up the PEP 409 command line test (patch by Ethan Furman)
1 parent 3267a30 commit 1d5ccdb

2 files changed

Lines changed: 20 additions & 52 deletions

File tree

Lib/test/test_cmd_line_script.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os.path
88
import py_compile
99

10+
import textwrap
1011
from test import support
1112
from test.script_helper import (
1213
make_pkg, make_script, make_zip_pkg, make_zip_script,
@@ -286,6 +287,24 @@ def test_issue8202_dash_m_file_ignored(self):
286287
self._check_output(script_name, rc, out,
287288
script_name, script_name, '', '')
288289

290+
def test_pep_409_verbiage(self):
291+
# Make sure PEP 409 syntax properly suppresses
292+
# the context of an exception
293+
script = textwrap.dedent("""\
294+
try:
295+
raise ValueError
296+
except:
297+
raise NameError from None
298+
""")
299+
with temp_dir() as script_dir:
300+
script_name = _make_test_script(script_dir, 'script', script)
301+
exitcode, stdout, stderr = assert_python_failure(script_name)
302+
text = stderr.decode('ascii').split('\n')
303+
self.assertEqual(len(text), 4)
304+
self.assertTrue(text[0].startswith('Traceback'))
305+
self.assertTrue(text[1].startswith(' File '))
306+
self.assertTrue(text[3].startswith('NameError'))
307+
289308
def test_main():
290309
support.run_unittest(CmdLineTest)
291310
support.reap_children()

Lib/test/test_raise.py

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,13 @@
33

44
"""Tests for the raise statement."""
55

6-
from test import support, script_helper
6+
from test import support
77
import re
88
import sys
99
import types
1010
import unittest
1111

1212

13-
try:
14-
from resource import setrlimit, RLIMIT_CORE, error as resource_error
15-
except ImportError:
16-
prepare_subprocess = None
17-
else:
18-
def prepare_subprocess():
19-
# don't create core file
20-
try:
21-
setrlimit(RLIMIT_CORE, (0, 0))
22-
except (ValueError, resource_error):
23-
pass
24-
25-
26-
2713
def get_tb():
2814
try:
2915
raise OSError()
@@ -224,43 +210,6 @@ def __init__(self):
224210

225211
class TestTraceback(unittest.TestCase):
226212

227-
def get_output(self, code, filename=None):
228-
"""
229-
Run the specified code in Python (in a new child process) and read the
230-
output from the standard error or from a file (if filename is set).
231-
Return the output lines as a list.
232-
"""
233-
options = {}
234-
if prepare_subprocess:
235-
options['preexec_fn'] = prepare_subprocess
236-
process = script_helper.spawn_python('-c', code, **options)
237-
stdout, stderr = process.communicate()
238-
exitcode = process.wait()
239-
output = support.strip_python_stderr(stdout)
240-
output = output.decode('ascii', 'backslashreplace')
241-
if filename:
242-
self.assertEqual(output, '')
243-
with open(filename, "rb") as fp:
244-
output = fp.read()
245-
output = output.decode('ascii', 'backslashreplace')
246-
output = re.sub('Current thread 0x[0-9a-f]+',
247-
'Current thread XXX',
248-
output)
249-
return output.splitlines(), exitcode
250-
251-
def test_traceback_verbiage(self):
252-
code = """
253-
try:
254-
raise ValueError
255-
except:
256-
raise NameError from None
257-
"""
258-
text, exitcode = self.get_output(code)
259-
self.assertEqual(len(text), 3)
260-
self.assertTrue(text[0].startswith('Traceback'))
261-
self.assertTrue(text[1].startswith(' File '))
262-
self.assertTrue(text[2].startswith('NameError'))
263-
264213
def test_sets_traceback(self):
265214
try:
266215
raise IndexError()

0 commit comments

Comments
 (0)