mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-03 19:33:07 +03:00
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:
parent
9dd09fa4bf
commit
7e67b9c58f
|
@ -357,22 +357,14 @@ def _save(im, fp, filename, eps=1):
|
||||||
else:
|
else:
|
||||||
raise ValueError("image mode is not supported")
|
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
|
base_fp = fp
|
||||||
|
wrapped_fp = False
|
||||||
if fp != sys.stdout:
|
if fp != sys.stdout:
|
||||||
fp = NoCloseStream(fp)
|
|
||||||
if sys.version_info.major > 2:
|
if sys.version_info.major > 2:
|
||||||
fp = io.TextIOWrapper(fp, encoding='latin-1')
|
fp = io.TextIOWrapper(fp, encoding='latin-1')
|
||||||
|
wrapped_fp = True
|
||||||
|
|
||||||
|
try:
|
||||||
if eps:
|
if eps:
|
||||||
#
|
#
|
||||||
# write EPS header
|
# write EPS header
|
||||||
|
@ -405,6 +397,9 @@ def _save(im, fp, filename, eps=1):
|
||||||
fp.write("grestore end\n")
|
fp.write("grestore end\n")
|
||||||
if hasattr(fp, "flush"):
|
if hasattr(fp, "flush"):
|
||||||
fp.flush()
|
fp.flush()
|
||||||
|
finally:
|
||||||
|
if wrapped_fp:
|
||||||
|
fp.detach()
|
||||||
|
|
||||||
#
|
#
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue
Block a user