#!/usr/bin/env python3 import io class copen(io.IOBase): def __init__(self,name=None,mode='rt'): """Open a file to test an issue of an iterator expecting bytes when it gets, and should expect, str.""" if mode != 'rt': exit(f"in topen.py/copen.__init__: this test needs mode 'rt', not {mode!r} ... aborting.") self.f = open(name,mode) return def close(self): result = self.f.close() return result def __getattr__(self,*args): return getattr(self.f,*args) f = copen('topen.py','rt') l = [x for x in f] # the exception happens here f.close() for x in l: print(x) exit('DONE')