Skip to content

Avoid indiscriminate glob-remove in xpdf_distill. #22423

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
Feb 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 17 additions & 24 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import datetime
from enum import Enum
import functools
import glob
from io import StringIO
import logging
import math
Expand Down Expand Up @@ -1214,32 +1213,26 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
mpl._get_executable_info("gs") # Effectively checks for ps2pdf.
mpl._get_executable_info("pdftops")

pdffile = tmpfile + '.pdf'
psfile = tmpfile + '.ps'

# Pass options as `-foo#bar` instead of `-foo=bar` to keep Windows happy
# (https://www.ghostscript.com/doc/9.22/Use.htm#MS_Windows).
cbook._check_and_log_subprocess(
["ps2pdf",
"-dAutoFilterColorImages#false",
"-dAutoFilterGrayImages#false",
"-sAutoRotatePages#None",
"-sGrayImageFilter#FlateEncode",
"-sColorImageFilter#FlateEncode",
"-dEPSCrop" if eps else "-sPAPERSIZE#%s" % ptype,
tmpfile, pdffile], _log)
cbook._check_and_log_subprocess(
["pdftops", "-paper", "match", "-level2", pdffile, psfile], _log)

os.remove(tmpfile)
shutil.move(psfile, tmpfile)

with TemporaryDirectory() as tmpdir:
tmppdf = pathlib.Path(tmpdir, "tmp.pdf")
tmpps = pathlib.Path(tmpdir, "tmp.ps")
# Pass options as `-foo#bar` instead of `-foo=bar` to keep Windows
# happy (https://www.ghostscript.com/doc/9.22/Use.htm#MS_Windows).
cbook._check_and_log_subprocess(
["ps2pdf",
"-dAutoFilterColorImages#false",
"-dAutoFilterGrayImages#false",
"-sAutoRotatePages#None",
"-sGrayImageFilter#FlateEncode",
"-sColorImageFilter#FlateEncode",
"-dEPSCrop" if eps else "-sPAPERSIZE#%s" % ptype,
tmpfile, tmppdf], _log)
cbook._check_and_log_subprocess(
["pdftops", "-paper", "match", "-level2", tmppdf, tmpps], _log)
shutil.move(tmpps, tmpfile)
if eps:
pstoeps(tmpfile)

for fname in glob.glob(tmpfile+'.*'):
os.remove(fname)


def get_bbox_header(lbrt, rotated=False):
"""
Expand Down