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

Skip to content

Simplify tmpdir handling in backend_pgf. #18654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2020

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Oct 4, 2020

backend_pgf uses a complicated way to handle temporary directories, in
particular because (on top of the normal calls to tex) it runs a
separate, long-standing tex instance to compute text bounding boxes.
I'm not sure this is actually needed, but at least, for the places
where we just run tex locally, we can certainly just use normal
TemporaryDirectories with automatic cleanup.

Do so in _print_pdf_to_fh (also with some pathlibification),
_print_png_to_fh, and PdfPages._run_latex (where all the temporary files
can be set up in _run_latex, rather than in __init__, which saves
the need for a bunch of attributes).

PR Summary

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and pydocstyle<4 and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

shutil.rmtree(tmpdir)
except:
TmpDirCleaner.add(tmpdir)
with TemporaryDirectory() as tmpdir:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We seriously don't have even a smoketest for this method?

Copy link
Contributor

@dopplershift dopplershift left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Would love to have a test exercise the entirely unexecuted method.

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree that an example would be good.

backend_pgf uses a complicated way to handle temporary directories, in
particular because (on top of the normal calls to tex) it runs a
separate, long-standing tex instance to compute text bounding boxes.
I'm not sure this is actually needed, but at least, for the places
where we just run tex locally, we can certainly just use normal
TemporaryDirectories with automatic cleanup.

Do so in _print_pdf_to_fh (also with some pathlibification),
_print_png_to_fh, and PdfPages._run_latex (where all the temporary files
can be set up in `_run_latex`, rather than in `__init__`, which saves
the need for a bunch of attributes).
@anntzer
Copy link
Contributor Author

anntzer commented Oct 5, 2020

Added a smoketest. I really don't want to add a new baseline image here because even the current pgf tests don't pass locally; my suspicion is that we have the same problem as with freetype versions, but worse because now it's the freetype versions used by the tex machinery, which we don't control...

@QuLogic
Copy link
Member

QuLogic commented Oct 5, 2020

The problem that TmpDirCleaner is trying to solve is that on Windows you can't delete a file that's open. I don't think TemporaryDirectory handles this, and may cause unexpected crashes on saving.

@anntzer
Copy link
Contributor Author

anntzer commented Oct 5, 2020

My understanding is that that's relevant for the long-running LatexManager (and even then my guess is that we could arrange for LatexManager to properly clean after itself). Here we're only looking at fully self-contained tex invocations, which should all be done by the end of the block.

@QuLogic
Copy link
Member

QuLogic commented Oct 5, 2020

It seems like all these code paths end up going through subprocess.run, which is supposed to wait for the process, so maybe these are all okay? Hard to say without a slow Windows to check.

@anntzer
Copy link
Contributor Author

anntzer commented Oct 6, 2020

That's my point. Compare with LatexManager, which keeps the subprocess around for the whole duration of the python process.

@QuLogic
Copy link
Member

QuLogic commented Oct 7, 2020

Well, let's see if anyone complains.

@QuLogic QuLogic merged commit e5404d8 into matplotlib:master Oct 7, 2020
@anntzer anntzer deleted the pgftmpdir branch October 7, 2020 09:08
@anntzer
Copy link
Contributor Author

anntzer commented Oct 7, 2020

Ah, it looks like some of this was also fixed on Python 3.4 per #1324 (comment). In any case I've confirmed locally that things work on Windows.

@QuLogic QuLogic added this to the v3.4.0 milestone Jun 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants