MCPcopy Index your code
hub / github.com/python/cpython / test_file_opening_hook

Method test_file_opening_hook

Lib/test/test_fileinput.py:255–280  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

253 sys.stdin = orig_stdin
254
255 def test_file_opening_hook(self):
256 try:
257 # cannot use openhook and inplace mode
258 fi = FileInput(inplace=True, openhook=lambda f, m: None)
259 self.fail("FileInput should raise if both inplace "
260 "and openhook arguments are given")
261 except ValueError:
262 pass
263 try:
264 fi = FileInput(openhook=1)
265 self.fail("FileInput should check openhook for being callable")
266 except ValueError:
267 pass
268
269 class CustomOpenHook:
270 def __init__(self):
271 self.invoked = False
272 def __call__(self, *args, **kargs):
273 self.invoked = True
274 return open(*args, encoding="utf-8")
275
276 t = self.writeTmp("\n")
277 custom_open_hook = CustomOpenHook()
278 with FileInput([t], openhook=custom_open_hook) as fi:
279 fi.readline()
280 self.assertTrue(custom_open_hook.invoked, "openhook not invoked")
281
282 def test_readline(self):
283 with open(TESTFN, 'wb') as f:

Callers

nothing calls this directly

Calls 6

readlineMethod · 0.95
FileInputClass · 0.90
CustomOpenHookClass · 0.85
writeTmpMethod · 0.80
assertTrueMethod · 0.80
failMethod · 0.45

Tested by

no test coverage detected