Please use GitHub pull requests for new patches. Avoid migrating existing patches. Phabricator shutdown timeline
Changeset View
Standalone View
lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
Show First 20 Lines • Show All 766 Lines • ▼ Show 20 Lines | def test_stdout(self): | ||||
f = io.StringIO() | f = io.StringIO() | ||||
status = self.debugger.SetOutputFile(f) | status = self.debugger.SetOutputFile(f) | ||||
self.assertTrue(status.Success()) | self.assertTrue(status.Success()) | ||||
self.handleCmd(r"script sys.stdout.write('foobar\n')") | self.handleCmd(r"script sys.stdout.write('foobar\n')") | ||||
self.assertEqual(f.getvalue().strip().split(), ["foobar", "7"]) | self.assertEqual(f.getvalue().strip().split(), ["foobar", "7"]) | ||||
@add_test_categories(['pyapi']) | @add_test_categories(['pyapi']) | ||||
@expectedFailureAll() # FIXME implement SBFile::GetFile | def test_stdout_file(self): | ||||
labath: So, if I understand correctly, these tests check that you can feed a file opened by python into… | |||||
Yea, foo is bar in python is essentially a pointer comparison between the PyObject* pointers, so this is testing that you get the same identical file object back in the situations where you should. There's no test going the other way, because going the other way isn't implemented. I didn't write anything that could stash an arbitrary lldb_private::File in a python object . If you convert a non-python File, you will get a new python file based on the descriptor, if it's available, or the conversion will fail if one is not. We do test that here, on line 801, we're testing that a NativeFile is converted to a working python file and the file descriptors match. We could, in a follow-on patch make the other direction work with identity too, but right now I can't think of what it would be useful for. lawrence_danna: Yea, `foo is bar` in python is essentially a pointer comparison between the `PyObject*`… | |||||
Right, sorry, that came out a bit wrong. While I think it would be cool to have the other direction be an "identity" too, I don't think that is really necessary. Nevertheless, taking a File out of lldb and then back in will do _something_ right now, and that "something" could probably use a test. I suppose you could construct an SBFile from a path, convert it to a python file and back, and then ensure that reading/writing on those two SBFiles does something reasonable. labath: Right, sorry, that came out a bit wrong. While I think it would be cool to have the other… | |||||
I was gonna say that this test already covers it, but then I thought, "oh whatever I'll just write another test". And the test I wrote uncovered a bug 😐. lawrence_danna: I was gonna say that this test already covers it, but then I thought, "oh whatever I'll just… | |||||
:) labath: :) | |||||
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 | |||||
I find this comment confusing. Does that refer to the write call above? If so, I don't see how that is relevant here.. labath: I find this comment confusing. Does that refer to the write call above? If so, I don't see how… | |||||
When we check the output on the next line we have to strip out the 7, which is the number of bytes written. lawrence_danna: When we check the output on the next line we have to strip out the 7, which is the number of… | |||||
Not Done ReplyInline ActionsAh, right. Thanks for clearing that up. labath: Ah, right. Thanks for clearing that up. | |||||
# 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,)]) | @skipIf(py_version=['<', (3,)]) | ||||
def test_identity(self): | def test_identity(self): | ||||
f = io.StringIO() | f = io.StringIO() | ||||
sbf = lldb.SBFile(f) | sbf = lldb.SBFile(f) | ||||
self.assertTrue(f is sbf.GetFile()) | self.assertTrue(f is sbf.GetFile()) | ||||
sbf.Close() | sbf.Close() | ||||
self.assertTrue(f.closed) | self.assertTrue(f.closed) | ||||
Show All 37 Lines | def test_identity(self): | ||||
self.assertTrue(f is sbf.GetFile()) | self.assertTrue(f is sbf.GetFile()) | ||||
sbf.Write(b"foobar\n") | sbf.Write(b"foobar\n") | ||||
self.assertEqual(f.fileno(), sbf.GetFile().fileno()) | self.assertEqual(f.fileno(), sbf.GetFile().fileno()) | ||||
sbf.Close() | sbf.Close() | ||||
self.assertTrue(f.closed) | self.assertTrue(f.closed) | ||||
with open(self.out_filename, 'r') as f: | with open(self.out_filename, 'r') as f: | ||||
self.assertEqual("foobar", f.read().strip()) | 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()))) | |||||
So, if I understand correctly, these tests check that you can feed a file opened by python into lldb, and then pump it back out to python. Are there any tests which do the opposite (have lldb open a file, move it to python and then reinject it into lldb)?