def check_open_file_gc_behaviour(self, file, **kwargs): def mangle(file): with open_file(file, **kwargs) as result: self.assertIsInstance(result, io.TextIOBase) with open_file(file, encoding='binary') as f: mangle(f) self.assertFalse(f.closed)
def check_open_file_state_contents(self, file, contents, is_binary, **kwargs): with open_file(file, **kwargs) as f: if is_binary: self.assertIsInstance(f, (io.BufferedReader, io.BufferedRandom)) else: self.assertIsInstance(f, io.TextIOBase) self.assertTrue(f.readable()) self.assertEqual(f.read(), contents) self.assertEqual(f.closed, self.expected_close) f.close() self.assertTrue(f.closed) self.check_closed(file, True)
def check_open_file_buffer_close_behaviour(self, file, **kwargs): if hasattr(file, 'close'): with open_file(file, **kwargs) as wrapped: file.close() self.assertTrue(wrapped.closed)
def mangle(file): with open_file(file, **kwargs) as result: self.assertIsInstance(result, io.TextIOBase)