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

Skip to content

Commit 57c01ce

Browse files
committed
In backend_pgf, directly open subprocess in utf8 mode.
... rather than manually encoding and decoding the inputs and outputs.
1 parent 330a054 commit 57c01ce

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

doc/api/next_api_changes/behaviour.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ did nothing, when passed an unsupported value. It now raises a ``ValueError``.
3636
`.Axis.set_tick_params` (and the higher level `.axes.Axes.tick_params` and
3737
`.pyplot.tick_params`) used to accept any value for ``which`` and silently
3838
did nothing, when passed an unsupported value. It now raises a ``ValueError``.
39+
40+
``backend_pgf.LatexManager.latex``
41+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42+
``backend_pgf.LatexManager.latex`` is now created with ``encoding="utf-8"``, so
43+
its ``stdin``, ``stdout``, and ``stderr`` attributes are utf8-encoded.

doc/api/next_api_changes/deprecations.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ For all locator classes defined in :mod:`mpl_toolkits.axisartist.angle_helper`,
2929
the ``den`` parameter has been renamed to ``nbins``, and the ``den`` attribute
3030
deprecated in favor of its (preexisting) synonym ``nbins``, for consistency
3131
with locator classes defined in :mod:`matplotlib.ticker`.
32+
33+
``backend_pgf.LatexManager.latex_stdin_utf8``
34+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35+
``backend_pgf.LatexManager.latex`` is now created with ``encoding="utf-8"``, so
36+
its ``stdin`` attribute is already utf8-encoded; the ``latex_stdin_utf8``
37+
attribute is thus deprecated.

lib/matplotlib/backends/backend_pgf.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -254,21 +254,21 @@ def _cleanup_remaining_instances():
254254
latex_manager._cleanup()
255255

256256
def _stdin_writeln(self, s):
257-
self.latex_stdin_utf8.write(s)
258-
self.latex_stdin_utf8.write("\n")
259-
self.latex_stdin_utf8.flush()
257+
self.latex.stdin.write(s)
258+
self.latex.stdin.write("\n")
259+
self.latex.stdin.flush()
260260

261261
def _expect(self, s):
262-
exp = s.encode("utf8")
263-
buf = bytearray()
262+
s = list(s)
263+
chars = []
264264
while True:
265-
b = self.latex.stdout.read(1)
266-
buf += b
267-
if buf[-len(exp):] == exp:
265+
c = self.latex.stdout.read(1)
266+
chars.append(c)
267+
if chars[-len(s):] == s:
268268
break
269-
if not len(b):
270-
raise LatexError("LaTeX process halted", buf.decode("utf8"))
271-
return buf.decode("utf8")
269+
if not c:
270+
raise LatexError("LaTeX process halted", "".join(chars))
271+
return "".join(chars)
272272

273273
def _expect_prompt(self):
274274
return self._expect("\n*")
@@ -287,28 +287,27 @@ def __init__(self):
287287
self.latex_header = LatexManager._build_latex_header()
288288
latex_end = "\n\\makeatletter\n\\@@end\n"
289289
try:
290-
latex = subprocess.Popen([self.texcommand, "-halt-on-error"],
291-
stdin=subprocess.PIPE,
292-
stdout=subprocess.PIPE,
293-
cwd=self.tmpdir)
290+
latex = subprocess.Popen(
291+
[self.texcommand, "-halt-on-error"],
292+
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
293+
encoding="utf-8", cwd=self.tmpdir)
294294
except FileNotFoundError:
295295
raise RuntimeError(
296296
"Latex command not found. Install %r or change "
297297
"pgf.texsystem to the desired command." % self.texcommand)
298298
except OSError:
299299
raise RuntimeError("Error starting process %r" % self.texcommand)
300300
test_input = self.latex_header + latex_end
301-
stdout, stderr = latex.communicate(test_input.encode("utf-8"))
301+
stdout, stderr = latex.communicate(test_input)
302302
if latex.returncode != 0:
303303
raise LatexError("LaTeX returned an error, probably missing font "
304304
"or error in preamble:\n%s" % stdout)
305305

306306
# open LaTeX process for real work
307-
latex = subprocess.Popen([self.texcommand, "-halt-on-error"],
308-
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
309-
cwd=self.tmpdir)
310-
self.latex = latex
311-
self.latex_stdin_utf8 = codecs.getwriter("utf8")(self.latex.stdin)
307+
self.latex = subprocess.Popen(
308+
[self.texcommand, "-halt-on-error"],
309+
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
310+
encoding="utf-8", cwd=self.tmpdir)
312311
# write header with 'pgf_backend_query_start' token
313312
self._stdin_writeln(self._build_latex_header())
314313
# read all lines until our 'pgf_backend_query_start' token appears
@@ -318,13 +317,15 @@ def __init__(self):
318317
# cache for strings already processed
319318
self.str_cache = {}
320319

320+
@cbook.deprecated("3.3")
321+
def latex_stdin_utf8(self):
322+
return self.latex.stdin
323+
321324
def _cleanup(self):
322325
if not self._os_path.isdir(self.tmpdir):
323326
return
324327
try:
325328
self.latex.communicate()
326-
self.latex_stdin_utf8.close()
327-
self.latex.stdout.close()
328329
except Exception:
329330
pass
330331
try:

0 commit comments

Comments
 (0)