What I need to accomplish:
Given a binary file, decode it in a couple different ways providing a TextIOBase
API. Ideally these subseque
Just detach your TextIOWrapper()
object before letting it be garbage collected:
def mangle(x):
wrapper = io.TextIOWrapper(x)
wrapper.detach()
The TextIOWrapper()
object only closes streams it is attached to. If you can't alter the code where the object goes out of scope, then simply keep a reference to the TextIOWrapper()
object locally and detach at that point.
If you must subclass TextIOWrapper()
, then just call detach()
in the __del__
hook:
class DetachingTextIOWrapper(io.TextIOWrapper):
def __del__(self):
self.detach()