本文整理汇总了Python中test.support.captured_output函数的典型用法代码示例。如果您正苦于以下问题:Python captured_output函数的具体用法?Python captured_output怎么用?Python captured_output使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了captured_output函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_help_output_redirect
def test_help_output_redirect(self):
# issue 940286, if output is set in Helper, then all output from
# Helper.help should be redirected
old_pattern = expected_text_pattern
getpager_old = pydoc.getpager
getpager_new = lambda: (lambda x: x)
self.maxDiff = None
buf = StringIO()
helper = pydoc.Helper(output=buf)
unused, doc_loc = get_pydoc_text(pydoc_mod)
module = "test.pydoc_mod"
help_header = """
Help on module test.pydoc_mod in test:
""".lstrip()
help_header = textwrap.dedent(help_header)
expected_help_pattern = help_header + expected_text_pattern
pydoc.getpager = getpager_new
try:
with captured_output('stdout') as output, \
captured_output('stderr') as err:
helper.help(module)
result = buf.getvalue().strip()
expected_text = expected_help_pattern % (
(doc_loc,) +
expected_text_data_docstrings +
(inspect.getabsfile(pydoc_mod),))
self.assertEqual('', output.getvalue())
self.assertEqual('', err.getvalue())
self.assertEqual(expected_text, result)
finally:
pydoc.getpager = getpager_old
开发者ID:524777134,项目名称:cpython,代码行数:34,代码来源:test_pydoc.py
示例2: test_unhashable
def test_unhashable(self):
from _testcapi import exception_print
class UnhashableException(Exception):
def __eq__(self, other):
return True
ex1 = UnhashableException('ex1')
ex2 = UnhashableException('ex2')
try:
raise ex2 from ex1
except UnhashableException:
try:
raise ex1
except UnhashableException:
exc_type, exc_val, exc_tb = sys.exc_info()
with captured_output("stderr") as stderr_f:
exception_print(exc_val)
tb = stderr_f.getvalue().strip().splitlines()
self.assertEqual(11, len(tb))
self.assertEqual(context_message.strip(), tb[5])
self.assertIn('UnhashableException: ex2', tb[3])
self.assertIn('UnhashableException: ex1', tb[10])
开发者ID:Eyepea,项目名称:cpython,代码行数:25,代码来源:test_traceback.py
示例3: get_report
def get_report(self, e):
e = self.get_exception(e)
s = "".join(traceback.format_exception(type(e), e, e.__traceback__))
with captured_output("stderr") as sio:
traceback.print_exception(type(e), e, e.__traceback__)
self.assertEqual(sio.getvalue(), s)
return s
开发者ID:collinanderson,项目名称:ensurepip,代码行数:7,代码来源:test_traceback.py
示例4: test_showwarning_not_callable
def test_showwarning_not_callable(self):
with original_warnings.catch_warnings(module=self.module):
self.module.filterwarnings("always", category=UserWarning)
self.module.showwarning = print
with support.captured_output('stdout'):
self.module.warn('Warning!')
self.module.showwarning = 23
self.assertRaises(TypeError, self.module.warn, "Warning!")
开发者ID:ARK4579,项目名称:cpython,代码行数:8,代码来源:__init__.py
示例5: test_slow
def test_slow(self):
# Verify the behavior of the timer's thread under load.
MT = self.timer(self.increment, 0.125)
with support.captured_output('stderr') as stderr:
MT.start()
time.sleep(0.5)
MT.stop()
time.sleep(0.25)
self.assert_(self.slow(), 'ValueError: Count')
self.assert_(self.test(stderr.getvalue()), 'IOError: Thread')
开发者ID:jacob-carrier,项目名称:code,代码行数:10,代码来源:recipe-576636.py
示例6: test_showwarnmsg_missing
def test_showwarnmsg_missing(self):
# Test that _showwarnmsg() missing is okay.
text = 'del _showwarnmsg test'
with original_warnings.catch_warnings(module=self.module):
self.module.filterwarnings("always", category=UserWarning)
del self.module._showwarnmsg
with support.captured_output('stderr') as stream:
self.module.warn(text)
result = stream.getvalue()
self.assertIn(text, result)
开发者ID:ARK4579,项目名称:cpython,代码行数:10,代码来源:__init__.py
示例7: check_traceback_format
def check_traceback_format(self, cleanup_func=None):
from _testcapi import traceback_print
try:
self.some_exception()
except KeyError:
type_, value, tb = sys.exc_info()
if cleanup_func is not None:
# Clear the inner frames, not this one
cleanup_func(tb.tb_next)
traceback_fmt = "Traceback (most recent call last):\n" + "".join(traceback.format_tb(tb))
file_ = StringIO()
traceback_print(tb, file_)
python_fmt = file_.getvalue()
# Call all _tb and _exc functions
with captured_output("stderr") as tbstderr:
traceback.print_tb(tb)
tbfile = StringIO()
traceback.print_tb(tb, file=tbfile)
with captured_output("stderr") as excstderr:
traceback.print_exc()
excfmt = traceback.format_exc()
excfile = StringIO()
traceback.print_exc(file=excfile)
else:
raise Error("unable to create test traceback string")
# Make sure that Python and the traceback module format the same thing
self.assertEqual(traceback_fmt, python_fmt)
# Now verify the _tb func output
self.assertEqual(tbstderr.getvalue(), tbfile.getvalue())
# Now verify the _exc func output
self.assertEqual(excstderr.getvalue(), excfile.getvalue())
self.assertEqual(excfmt, excfile.getvalue())
# Make sure that the traceback is properly indented.
tb_lines = python_fmt.splitlines()
self.assertEqual(len(tb_lines), 5)
banner = tb_lines[0]
location, source_line = tb_lines[-2:]
self.assertTrue(banner.startswith("Traceback"))
self.assertTrue(location.startswith(" File"))
self.assertTrue(source_line.startswith(" raise"))
开发者ID:collinanderson,项目名称:ensurepip,代码行数:43,代码来源:test_traceback.py
示例8: test_addpackage_import_bad_pth_file
def test_addpackage_import_bad_pth_file(self):
# Issue 5258
pth_dir, pth_fn = self.make_pth("abc\x00def\n")
with captured_output("stderr") as err_out:
site.addpackage(pth_dir, pth_fn, set())
self.assertRegex(err_out.getvalue(), "line 1")
self.assertRegex(err_out.getvalue(),
re.escape(os.path.join(pth_dir, pth_fn)))
# XXX: ditto previous XXX comment.
self.assertRegex(err_out.getvalue(), 'Traceback')
self.assertRegex(err_out.getvalue(), 'TypeError')
开发者ID:isaiah,项目名称:jython3,代码行数:11,代码来源:test_site.py
示例9: test_addpackage_import_bad_exec
def test_addpackage_import_bad_exec(self):
# Issue 10642
pth_dir, pth_fn = self.make_pth("randompath\nimport nosuchmodule\n")
with captured_output("stderr") as err_out:
site.addpackage(pth_dir, pth_fn, set())
self.assertRegex(err_out.getvalue(), "line 2")
self.assertRegex(err_out.getvalue(),
re.escape(os.path.join(pth_dir, pth_fn)))
# XXX: ditto previous XXX comment.
self.assertRegex(err_out.getvalue(), 'Traceback')
self.assertRegex(err_out.getvalue(), 'ImportError')
开发者ID:isaiah,项目名称:jython3,代码行数:11,代码来源:test_site.py
示例10: test_stack_format
def test_stack_format(self):
# Verify _stack functions. Note we have to use _getframe(1) to
# compare them without this frame appearing in the output
with captured_output("stderr") as ststderr:
traceback.print_stack(sys._getframe(1))
stfile = StringIO()
traceback.print_stack(sys._getframe(1), file=stfile)
self.assertEqual(ststderr.getvalue(), stfile.getvalue())
stfmt = traceback.format_stack(sys._getframe(1))
self.assertEqual(ststderr.getvalue(), "".join(stfmt))
开发者ID:collinanderson,项目名称:ensurepip,代码行数:12,代码来源:test_traceback.py
示例11: test_print_stack
def test_print_stack(self):
def prn():
traceback.print_stack()
with captured_output("stderr") as stderr:
prn()
lineno = prn.__code__.co_firstlineno
self.assertEqual(stderr.getvalue().splitlines()[-4:], [
' File "%s", line %d, in test_print_stack' % (__file__, lineno+3),
' prn()',
' File "%s", line %d, in prn' % (__file__, lineno+1),
' traceback.print_stack()',
])
开发者ID:invisiblek,项目名称:android_external_python,代码行数:12,代码来源:test_traceback.py
示例12: test_addpackage_import_bad_syntax
def test_addpackage_import_bad_syntax(self):
# Issue 10642
pth_dir, pth_fn = self.make_pth("import bad)syntax\n")
with captured_output("stderr") as err_out:
site.addpackage(pth_dir, pth_fn, set())
self.assertRegex(err_out.getvalue(), "line 1")
self.assertRegex(err_out.getvalue(),
re.escape(os.path.join(pth_dir, pth_fn)))
# XXX: the previous two should be independent checks so that the
# order doesn't matter. The next three could be a single check
# but my regex foo isn't good enough to write it.
self.assertRegex(err_out.getvalue(), 'Traceback')
self.assertRegex(err_out.getvalue(), r'import bad\)syntax')
self.assertRegex(err_out.getvalue(), 'SyntaxError')
开发者ID:isaiah,项目名称:jython3,代码行数:14,代码来源:test_site.py
示例13: test_save_exception_state_on_error
def test_save_exception_state_on_error(self):
# See issue #14474
def task():
started.release()
raise SyntaxError
def mywrite(self, *args):
try:
raise ValueError
except ValueError:
pass
real_write(self, *args)
started = thread.allocate_lock()
with support.captured_output("stderr") as stderr:
real_write = stderr.write
stderr.write = mywrite
started.acquire()
with support.wait_threads_exit():
thread.start_new_thread(task, ())
started.acquire()
self.assertIn("Traceback", stderr.getvalue())
开发者ID:1st1,项目名称:cpython,代码行数:20,代码来源:test_thread.py
示例14: test_show_warning_output
def test_show_warning_output(self):
# With showarning() missing, make sure that output is okay.
text = 'test show_warning'
with original_warnings.catch_warnings(module=self.module):
self.module.filterwarnings("always", category=UserWarning)
del self.module.showwarning
with support.captured_output('stderr') as stream:
warning_tests.inner(text)
result = stream.getvalue()
self.assertEqual(result.count('\n'), 2,
"Too many newlines in %r" % result)
first_line, second_line = result.split('\n', 1)
expected_file = warning_tests_py
first_line_parts = first_line.rsplit(':', 3)
path, line, warning_class, message = first_line_parts
line = int(line)
self.assertEqual(expected_file, path)
self.assertEqual(warning_class, ' ' + UserWarning.__name__)
self.assertEqual(message, ' ' + text)
expected_line = ' ' + linecache.getline(path, line).strip() + '\n'
assert expected_line
self.assertEqual(second_line, expected_line)
开发者ID:isaiah,项目名称:jython3,代码行数:22,代码来源:test_warnings.py
示例15: test_3611
def test_3611(self):
# A re-raised exception in a __del__ caused the __context__
# to be cleared
class C:
def __del__(self):
try:
1/0
except:
raise
def f():
x = C()
try:
try:
x.x
except AttributeError:
del x
raise TypeError
except Exception as e:
self.assertNotEqual(e.__context__, None)
self.assertIsInstance(e.__context__, AttributeError)
with support.captured_output("stderr"):
f()
开发者ID:Apoorvadabhere,项目名称:cpython,代码行数:24,代码来源:test_raise.py
示例16: test_after_start_fails
def test_after_start_fails(self):
if captured_output:
with captured_output('stderr'):
yield self._test_hooks(behavior=ERROR, status='stopped',
hook_name='after_start')
开发者ID:ChaoticMind,项目名称:circus,代码行数:5,代码来源:test_watcher.py
示例17: _check_recursive_traceback_display
def _check_recursive_traceback_display(self, render_exc):
# Always show full diffs when this test fails
# Note that rearranging things may require adjusting
# the relative line numbers in the expected tracebacks
self.maxDiff = None
# Check hitting the recursion limit
def f():
f()
with captured_output("stderr") as stderr_f:
try:
f()
except RecursionError as exc:
render_exc()
else:
self.fail("no recursion occurred")
lineno_f = f.__code__.co_firstlineno
result_f = (
'Traceback (most recent call last):\n'
f' File "{__file__}", line {lineno_f+5}, in _check_recursive_traceback_display\n'
' f()\n'
f' File "{__file__}", line {lineno_f+1}, in f\n'
' f()\n'
f' File "{__file__}", line {lineno_f+1}, in f\n'
' f()\n'
f' File "{__file__}", line {lineno_f+1}, in f\n'
' f()\n'
# XXX: The following line changes depending on whether the tests
# are run through the interactive interpreter or with -m
# It also varies depending on the platform (stack size)
# Fortunately, we don't care about exactness here, so we use regex
r' \[Previous line repeated (\d+) more times\]' '\n'
'RecursionError: maximum recursion depth exceeded\n'
)
expected = result_f.splitlines()
actual = stderr_f.getvalue().splitlines()
# Check the output text matches expectations
# 2nd last line contains the repetition count
self.assertEqual(actual[:-2], expected[:-2])
self.assertRegex(actual[-2], expected[-2])
self.assertEqual(actual[-1], expected[-1])
# Check the recursion count is roughly as expected
rec_limit = sys.getrecursionlimit()
self.assertIn(int(re.search(r"\d+", actual[-2]).group()), range(rec_limit-50, rec_limit))
# Check a known (limited) number of recursive invocations
def g(count=10):
if count:
return g(count-1)
raise ValueError
with captured_output("stderr") as stderr_g:
try:
g()
except ValueError as exc:
render_exc()
else:
self.fail("no value error was raised")
lineno_g = g.__code__.co_firstlineno
result_g = (
f' File "{__file__}", line {lineno_g+2}, in g\n'
' return g(count-1)\n'
f' File "{__file__}", line {lineno_g+2}, in g\n'
' return g(count-1)\n'
f' File "{__file__}", line {lineno_g+2}, in g\n'
' return g(count-1)\n'
' [Previous line repeated 6 more times]\n'
f' File "{__file__}", line {lineno_g+3}, in g\n'
' raise ValueError\n'
'ValueError\n'
)
tb_line = (
'Traceback (most recent call last):\n'
f' File "{__file__}", line {lineno_g+7}, in _check_recursive_traceback_display\n'
' g()\n'
)
expected = (tb_line + result_g).splitlines()
actual = stderr_g.getvalue().splitlines()
self.assertEqual(actual, expected)
# Check 2 different repetitive sections
def h(count=10):
if count:
return h(count-1)
g()
with captured_output("stderr") as stderr_h:
try:
h()
except ValueError as exc:
render_exc()
else:
self.fail("no value error was raised")
#.........这里部分代码省略.........
开发者ID:yoongkang,项目名称:cpython,代码行数:101,代码来源:test_traceback.py
示例18: test_after_spawn_failure
def test_after_spawn_failure(self):
if captured_output:
with captured_output('stdout'):
yield self._test_hooks(behavior=ERROR, status='stopped',
hook_name='after_spawn',
call=self._stop)
开发者ID:ChaoticMind,项目名称:circus,代码行数:6,代码来源:test_watcher.py
示例19: test_before_stop_fails
def test_before_stop_fails(self):
if captured_output:
with captured_output('stdout'):
yield self._test_hooks(behavior=ERROR, status='stopped',
hook_name='before_stop',
call=self._stop)
开发者ID:ChaoticMind,项目名称:circus,代码行数:6,代码来源:test_watcher.py
示例20: test_print_function
def test_print_function(self):
with support.captured_output("stderr") as s:
print("foo", file=sys.stderr)
self.assertEqual(s.getvalue(), "foo\n")
开发者ID:0jpq0,项目名称:kbengine,代码行数:4,代码来源:test_future5.py
注:本文中的test.support.captured_output函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论