我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用fileinput.fileno()。
def test_fileno_when_ValueError_raised(self): class FilenoRaisesValueError(UnconditionallyRaise): def __init__(self): UnconditionallyRaise.__init__(self, ValueError) def fileno(self): self.__call__() unconditionally_raise_ValueError = FilenoRaisesValueError() t = writeTmp(1, ["\n"]) self.addCleanup(remove_tempfiles, t) with FileInput(files=[t]) as fi: file_backup = fi._file try: fi._file = unconditionally_raise_ValueError result = fi.fileno() finally: fi._file = file_backup # make sure the file gets cleaned up # sanity check to make sure that our test scenario was actually hit self.assertTrue(unconditionally_raise_ValueError.invoked, "_file.fileno() was not invoked") self.assertEqual(result, -1, "fileno() should return -1")
def test_fileno(self): t1 = t2 = None try: t1 = writeTmp(1, ["A\nB"]) t2 = writeTmp(2, ["C\nD"]) fi = FileInput(files=(t1, t2)) self.assertEqual(fi.fileno(), -1) line =next( fi) self.assertNotEqual(fi.fileno(), -1) fi.nextfile() self.assertEqual(fi.fileno(), -1) line = list(fi) self.assertEqual(fi.fileno(), -1) finally: remove_tempfiles(t1, t2)
def fileno(self): self.invocation_counts["fileno"] += 1 return self.return_values["fileno"]
def test_state_is_None(self): """Tests fileinput.fileno() when fileinput._state is None. Ensure that it raises RuntimeError with a meaningful error message and does not modify fileinput._state""" fileinput._state = None with self.assertRaises(RuntimeError) as cm: fileinput.fileno() self.assertEqual(("no active input()",), cm.exception.args) self.assertIsNone(fileinput._state)
def test_state_is_not_None(self): """Tests fileinput.fileno() when fileinput._state is not None. Ensure that it invokes fileinput._state.fileno() exactly once, returns whatever it returns, and does not modify fileinput._state to point to a different object.""" fileno_retval = object() instance = MockFileInput() instance.return_values["fileno"] = fileno_retval instance.fileno_retval = fileno_retval fileinput._state = instance retval = fileinput.fileno() self.assertExactlyOneInvocation(instance, "fileno") self.assertIs(retval, fileno_retval) self.assertIs(fileinput._state, instance)