Use TextIOWrapper.detach() instead of NoCloseStream

Usage and this pattern is discussed in Python bug: https://bugs.python.org/issue21363
This commit is contained in:
Jon Dufresne 2016-11-11 11:18:27 -08:00
parent 9dd09fa4bf
commit 7e67b9c58f

View File

@ -357,22 +357,14 @@ def _save(im, fp, filename, eps=1):
else:
raise ValueError("image mode is not supported")
class NoCloseStream(object):
def __init__(self, fp):
self.fp = fp
def __getattr__(self, name):
return getattr(self.fp, name)
def close(self):
pass
base_fp = fp
wrapped_fp = False
if fp != sys.stdout:
fp = NoCloseStream(fp)
if sys.version_info.major > 2:
fp = io.TextIOWrapper(fp, encoding='latin-1')
wrapped_fp = True
try:
if eps:
#
# write EPS header
@ -405,6 +397,9 @@ def _save(im, fp, filename, eps=1):
fp.write("grestore end\n")
if hasattr(fp, "flush"):
fp.flush()
finally:
if wrapped_fp:
fp.detach()
#
# --------------------------------------------------------------------