Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d0ad9b0

Browse files
authored
Merge pull request #20348 from anntzer/tiow
codecs.getwriter has simpler lifetime semantics than TextIOWrapper.
2 parents 342bfb1 + 46eb7be commit d0ad9b0

File tree

3 files changed

+15
-38
lines changed

3 files changed

+15
-38
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
A PostScript backend, which can produce both PostScript .ps and .eps.
33
"""
44

5+
import codecs
56
import datetime
67
from enum import Enum
78
import glob
8-
from io import StringIO, TextIOWrapper
9+
from io import StringIO
910
import logging
1011
import math
1112
import os
@@ -865,12 +866,8 @@ def _print_figure(
865866
generated from the *metadata* parameter to `.print_figure`.
866867
"""
867868
is_eps = format == 'eps'
868-
if isinstance(outfile, (str, os.PathLike)):
869-
outfile = os.fspath(outfile)
870-
passed_in_file_object = False
871-
elif is_writable_file_like(outfile):
872-
passed_in_file_object = True
873-
else:
869+
if not (isinstance(outfile, (str, os.PathLike))
870+
or is_writable_file_like(outfile)):
874871
raise ValueError("outfile must be a path or a file-like object")
875872

876873
# find the appropriate papertype
@@ -997,23 +994,11 @@ def print_figure_impl(fh):
997994
tmpfile, is_eps, ptype=papertype, bbox=bbox)
998995
_move_path_to_path_or_stream(tmpfile, outfile)
999996

1000-
else:
1001-
# Write directly to outfile.
1002-
if passed_in_file_object:
1003-
requires_unicode = file_requires_unicode(outfile)
1004-
1005-
if not requires_unicode:
1006-
fh = TextIOWrapper(outfile, encoding="latin-1")
1007-
# Prevent the TextIOWrapper from closing the underlying
1008-
# file.
1009-
fh.close = lambda: None
1010-
else:
1011-
fh = outfile
1012-
1013-
print_figure_impl(fh)
1014-
else:
1015-
with open(outfile, 'w', encoding='latin-1') as fh:
1016-
print_figure_impl(fh)
997+
else: # Write directly to outfile.
998+
with cbook.open_file_cm(outfile, "w", encoding="latin-1") as file:
999+
if not file_requires_unicode(file):
1000+
file = codecs.getwriter("latin-1")(file)
1001+
print_figure_impl(file)
10171002

10181003
@_check_savefig_extra_args
10191004
def _print_figure_tex(

lib/matplotlib/backends/backend_svg.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import base64
2+
import codecs
23
import datetime
34
import gzip
45
import hashlib
5-
from io import BytesIO, StringIO, TextIOWrapper
6+
from io import BytesIO, StringIO
67
import itertools
78
import logging
89
import os
@@ -1310,24 +1311,13 @@ def print_svg(self, filename, *args, **kwargs):
13101311
__ DC_
13111312
"""
13121313
with cbook.open_file_cm(filename, "w", encoding="utf-8") as fh:
1313-
13141314
filename = getattr(fh, 'name', '')
13151315
if not isinstance(filename, str):
13161316
filename = ''
1317-
1318-
if cbook.file_requires_unicode(fh):
1319-
detach = False
1320-
else:
1321-
fh = TextIOWrapper(fh, 'utf-8')
1322-
detach = True
1323-
1317+
if not cbook.file_requires_unicode(fh):
1318+
fh = codecs.getwriter('utf-8')(fh)
13241319
self._print_svg(filename, fh, **kwargs)
13251320

1326-
# Detach underlying stream from wrapper so that it remains open in
1327-
# the caller.
1328-
if detach:
1329-
fh.detach()
1330-
13311321
@_api.delete_parameter("3.5", "args")
13321322
def print_svgz(self, filename, *args, **kwargs):
13331323
with cbook.open_file_cm(filename, "wb") as fh, \

lib/matplotlib/tests/test_backend_ps.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def test_savefig_to_stringio(format, use_log, rcParams, orientation):
6868
except tuple(allowable_exceptions) as exc:
6969
pytest.skip(str(exc))
7070

71+
assert not s_buf.closed
72+
assert not b_buf.closed
7173
s_val = s_buf.getvalue().encode('ascii')
7274
b_val = b_buf.getvalue()
7375

0 commit comments

Comments
 (0)