(self)
| 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: |
nothing calls this directly
no test coverage detected