diff options
author | Jason Molenda <jmolenda@apple.com> | 2017-11-02 02:43:27 +0000 |
---|---|---|
committer | Jason Molenda <jmolenda@apple.com> | 2017-11-02 02:43:27 +0000 |
commit | edc2def4a65764991ffb50e9c9af1c740ced534c (patch) | |
tree | 1956183f5d71079fc4d2e3503a5e892c8d27dbda /lldb/packages/Python/lldbsuite/test/python_api/file_handle | |
parent | 9e27b70a07c35880da49619bd85ce47cb327fb02 (diff) | |
download | bcm5719-llvm-edc2def4a65764991ffb50e9c9af1c740ced534c.tar.gz bcm5719-llvm-edc2def4a65764991ffb50e9c9af1c740ced534c.zip |
Commit Lawrence D'Anna's patch to change
SetOututFileHandle to work with IOBase.
I did make one change after checking with Larry --
I renamed SBDebugger::Flush to FlushDebuggerOutputHandles
and added a short docstring to the .i file to make it
a little clearer under which context programs may need
to use this API.
Differential Revision: https://reviews.llvm.org/D39128
<rdar://problem/34870417>
llvm-svn: 317182
Diffstat (limited to 'lldb/packages/Python/lldbsuite/test/python_api/file_handle')
-rw-r--r-- | lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py | 228 |
1 files changed, 228 insertions, 0 deletions
diff --git a/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py b/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py new file mode 100644 index 00000000000..5ddd6334738 --- /dev/null +++ b/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py @@ -0,0 +1,228 @@ +""" +Test lldb Python API for setting output and error file handles +""" + +from __future__ import print_function + + +import contextlib +import os +import io +import re +import platform +import unittest + +import lldb +from lldbsuite.test.decorators import * +from lldbsuite.test.lldbtest import * +from lldbsuite.test import lldbutil + + +class StringIO(io.TextIOBase): + + def __init__(self, buf=''): + self.buf = buf + + def writable(self): + return True + + def write(self, s): + self.buf += s + return len(s) + + +class BadIO(io.TextIOBase): + + def writable(self): + return True + + def write(self, s): + raise Exception('OH NOE') + + +@contextlib.contextmanager +def replace_stdout(new): + old = sys.stdout + sys.stdout = new + try: + yield + finally: + sys.stdout = old + + +def handle_command(debugger, cmd, raise_on_fail=True, collect_result=True): + + ret = lldb.SBCommandReturnObject() + + if collect_result: + interpreter = debugger.GetCommandInterpreter() + interpreter.HandleCommand(cmd, ret) + else: + debugger.HandleCommand(cmd) + + if hasattr(debugger, 'FlushDebuggerOutputHandles'): + debugger.FlushDebuggerOutputHandles() + + if collect_result and raise_on_fail and not ret.Succeeded(): + raise Exception + + return ret.GetOutput() + + + +class FileHandleTestCase(TestBase): + + mydir = TestBase.compute_mydir(__file__) + + def comment(self, *args): + if self.session is not None: + print(*args, file=self.session) + + def skip_windows(self): + if platform.system() == 'Windows': + self.skipTest('windows') + + + @add_test_categories(['pyapi']) + @no_debug_info_test + def test_file_out(self): + + debugger = lldb.SBDebugger.Create() + try: + with open('output', 'w') as f: + debugger.SetOutputFileHandle(f, False) + handle_command(debugger, 'script print("foobar")') + + with open('output', 'r') as f: + self.assertEqual(f.read().strip(), "foobar") + + finally: + self.RemoveTempFile('output') + lldb.SBDebugger.Destroy(debugger) + + + @add_test_categories(['pyapi']) + @no_debug_info_test + def test_file_error(self): + + debugger = lldb.SBDebugger.Create() + try: + with open('output', 'w') as f: + debugger.SetErrorFileHandle(f, False) + handle_command(debugger, 'lolwut', raise_on_fail=False, collect_result=False) + + with open('output', 'r') as f: + errors = f.read() + self.assertTrue(re.search(r'error:.*lolwut', errors)) + + finally: + self.RemoveTempFile('output') + lldb.SBDebugger.Destroy(debugger) + + + @add_test_categories(['pyapi']) + @no_debug_info_test + def test_string_out(self): + + self.skip_windows() + + io = StringIO() + debugger = lldb.SBDebugger.Create() + try: + debugger.SetOutputFileHandle(io, False) + handle_command(debugger, 'script print("foobar")') + + self.assertEqual(io.buf.strip(), "foobar") + + finally: + lldb.SBDebugger.Destroy(debugger) + + + @add_test_categories(['pyapi']) + @no_debug_info_test + def test_string_error(self): + + self.skip_windows() + + io = StringIO() + debugger = lldb.SBDebugger.Create() + try: + debugger.SetErrorFileHandle(io, False) + handle_command(debugger, 'lolwut', raise_on_fail=False, collect_result=False) + + errors = io.buf + self.assertTrue(re.search(r'error:.*lolwut', errors)) + + finally: + lldb.SBDebugger.Destroy(debugger) + + @add_test_categories(['pyapi']) + @no_debug_info_test + def test_replace_stdout(self): + + self.skip_windows() + + io = StringIO() + debugger = lldb.SBDebugger.Create() + try: + + with replace_stdout(io): + handle_command(debugger, 'script print("lol, crash")', collect_result=False) + + finally: + lldb.SBDebugger.Destroy(debugger) + + + @add_test_categories(['pyapi']) + @no_debug_info_test + def test_replace_stdout_with_nonfile(self): + + self.skip_windows() + + io = StringIO() + + with replace_stdout(io): + + class Nothing(): + pass + + debugger = lldb.SBDebugger.Create() + try: + with replace_stdout(Nothing): + self.assertEqual(sys.stdout, Nothing) + handle_command(debugger, 'script print("lol, crash")', collect_result=False) + self.assertEqual(sys.stdout, Nothing) + finally: + lldb.SBDebugger.Destroy(debugger) + + sys.stdout.write("FOO") + + self.assertEqual(io.buf, "FOO") + + + @add_test_categories(['pyapi']) + @no_debug_info_test + def test_stream_error(self): + + self.skip_windows() + + messages = list() + + io = BadIO() + debugger = lldb.SBDebugger.Create() + try: + debugger.SetOutputFileHandle(io, False) + debugger.SetLoggingCallback(messages.append) + handle_command(debugger, 'log enable lldb script') + handle_command(debugger, 'script print "foobar"') + + finally: + lldb.SBDebugger.Destroy(debugger) + + for message in messages: + self.comment("GOT: " + message.strip()) + + self.assertTrue(any('OH NOE' in msg for msg in messages)) + self.assertTrue(any('BadIO' in msg for msg in messages)) + + |