From d9b553ec9961e95740535d3aeff62817f867767f Mon Sep 17 00:00:00 2001 From: Lawrence D'Anna Date: Tue, 15 Oct 2019 16:46:27 +0000 Subject: SBFile::GetFile: convert SBFile back into python native files. Summary: This makes SBFile::GetFile public and adds a SWIG typemap to convert the result back into a python native file. If the underlying File itself came from a python file, it is returned identically. Otherwise a new python file object is created using the file descriptor. Reviewers: JDevlieghere, jasonmolenda, labath Reviewed By: labath Subscribers: lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D68737 llvm-svn: 374911 --- .../test/python_api/file_handle/TestFileHandle.py | 35 +++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'lldb/packages/Python/lldbsuite/test/python_api/file_handle') 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 index 577c6b3e88f..1bd002e6bb7 100644 --- a/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py +++ b/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py @@ -772,7 +772,21 @@ class FileHandleTestCase(lldbtest.TestBase): @add_test_categories(['pyapi']) - @expectedFailureAll() # FIXME implement SBFile::GetFile + def test_stdout_file(self): + with open(self.out_filename, 'w') as f: + status = self.debugger.SetOutputFile(f) + self.assertTrue(status.Success()) + self.handleCmd(r"script sys.stdout.write('foobar\n')") + with open(self.out_filename, 'r') as f: + # In python2 sys.stdout.write() returns None, which + # the REPL will ignore, but in python3 it will + # return the number of bytes written, which the REPL + # will print out. + lines = [x for x in f.read().strip().split() if x != "7"] + self.assertEqual(lines, ["foobar"]) + + + @add_test_categories(['pyapi']) @skipIf(py_version=['<', (3,)]) def test_identity(self): @@ -826,3 +840,22 @@ class FileHandleTestCase(lldbtest.TestBase): with open(self.out_filename, 'r') as f: self.assertEqual("foobar", f.read().strip()) + + + @add_test_categories(['pyapi']) + def test_back_and_forth(self): + with open(self.out_filename, 'w') as f: + # at each step here we're borrowing the file, so we have to keep + # them all alive until the end. + sbf = lldb.SBFile.Create(f, borrow=True) + def i(sbf): + for i in range(10): + f = sbf.GetFile() + yield f + sbf = lldb.SBFile.Create(f, borrow=True) + yield sbf + sbf.Write(str(i).encode('ascii') + b"\n") + files = list(i(sbf)) + with open(self.out_filename, 'r') as f: + self.assertEqual(list(range(10)), list(map(int, f.read().strip().split()))) + -- cgit v1.2.3