Repository: matplotlib/matplotlib
File: lib/matplotlib/texmanager.py (Python)

Code before:
                '{prog} was not able to process the following string:\n'
                '{tex!r}\n\n'
                'Here is the full command invocation and its output:\n\n'
                '{format_command}\n\n'
                '{exc}\n\n'.format(
                    prog=command[0],
                    format_command=cbook._pformat_subprocess(command),
                    tex=tex.encode('unicode_escape'),
                    exc=exc.output.decode('utf-8', 'backslashreplace'))
                ) from None
        _log.debug(report)
        return report

    @classmethod
    def make_dvi(cls, tex, fontsize):
        """
        Generate a dvi file containing latex's layout of tex string.

        Return the file name.
        """
        basefile = cls.get_basefile(tex, fontsize)
        dvifile = '%s.dvi' % basefile
        if not os.path.exists(dvifile):
            # Also generate the tex source in the main cache directory, but
            # only for backcompat.
            cls.make_tex(tex, fontsize)
            # Generate the tex and dvi in a temporary directory to avoid race
            # conditions e.g. if multiple processes try to process the same tex
            # string at the same time.  Having tmpdir be a subdirectory of the
            # final output dir ensures that they are on the same filesystem,
            # and thus replace() works atomically.  It also allows referring to
            # the texfile with a relative path (for pathological MPLCONFIGDIRs,
            # the absolute path may contain characters (e.g. ~) that TeX does
            # not support; n.b. relative paths cannot traverse parents, or it
            # will be blocked when `openin_any = p` in texmf.cnf).
            cwd = Path(dvifile).parent
            with TemporaryDirectory(dir=cwd) as tmpdir:
                Path(tmpdir, "file.tex").write_text(
                    cls._get_tex_source(tex, fontsize), encoding='utf-8')
                cls._run_checked_subprocess(
                    ["latex", "-interaction=nonstopmode", "--halt-on-error",
                     "file.tex"], tex, cwd=tmpdir)
                Path(tmpdir, "file.dvi").replace(dvifile)
        return dvifile

    @classmethod
    def make_png(cls, tex, fontsize, dpi):
        """
        Generate a png file containing latex's rendering of tex string.

        Return the file name.

Review comment (performance):
Maybe inline `make_tex` for clarity

```suggestion
            tex_source = cls._get_tex_source(tex, fontsize), encoding='utf-8')
            # Also generate the .tex file in the main cache directory, but
            # only for backcompat.
            Path(cls.get_basefile(tex, fontsize) + ".tex").write_text(tex_source)
```

Code after:
                '{prog} was not able to process the following string:\n'
                '{tex!r}\n\n'
                'Here is the full command invocation and its output:\n\n'
                '{format_command}\n\n'
                '{exc}\n\n'.format(
                    prog=command[0],
                    format_command=cbook._pformat_subprocess(command),
                    tex=tex.encode('unicode_escape'),
                    exc=exc.output.decode('utf-8', 'backslashreplace'))
                ) from None
        _log.debug(report)
        return report

    @classmethod
    def make_dvi(cls, tex, fontsize):
        """
        Generate a dvi file containing latex's layout of tex string.

        Return the file name.
        """
        dvifile = Path(cls.get_basefile(tex, fontsize)).with_suffix(".dvi")
        if not dvifile.exists():
            # Generate the tex and dvi in a temporary directory to avoid race
            # conditions e.g. if multiple processes try to process the same tex
            # string at the same time.  Having tmpdir be a subdirectory of the
            # final output dir ensures that they are on the same filesystem,
            # and thus replace() works atomically.  It also allows referring to
            # the texfile with a relative path (for pathological MPLCONFIGDIRs,
            # the absolute path may contain characters (e.g. ~) that TeX does
            # not support; n.b. relative paths cannot traverse parents, or it
            # will be blocked when `openin_any = p` in texmf.cnf).
            with TemporaryDirectory(dir=dvifile.parent) as tmpdir:
                Path(tmpdir, "file.tex").write_text(
                    cls._get_tex_source(tex, fontsize), encoding='utf-8')
                cls._run_checked_subprocess(
                    ["latex", "-interaction=nonstopmode", "--halt-on-error",
                     "file.tex"], tex, cwd=tmpdir)
                Path(tmpdir, "file.dvi").replace(dvifile)
                # Also move the tex source to the main cache directory, but
                # only for backcompat.
                Path(tmpdir, "file.tex").replace(dvifile.with_suffix(".tex"))
        return str(dvifile)

    @classmethod
    def make_png(cls, tex, fontsize, dpi):
        """
        Generate a png file containing latex's rendering of tex string.

        Return the file name.
        """
        pngfile = Path(cls.get_basefile(tex, fontsize)).with_suffix(".png")