From 10c703e11915dcc992f374c5e7bb4780c319aee7 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Thu, 2 Jun 2022 01:56:41 -0700 Subject: [PATCH] unpaper: use TemporaryDirectory(ignore_cleanup_errors=True) where available Fixes #974 when used in conjunction with Python 3.10. Reviewed other uses of TemporaryDirectory in ocrmypdf and decided it was not worth fixing them since neither are exactly production code. --- src/ocrmypdf/_exec/unpaper.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/ocrmypdf/_exec/unpaper.py b/src/ocrmypdf/_exec/unpaper.py index 69eb58ee..116ad64c 100644 --- a/src/ocrmypdf/_exec/unpaper.py +++ b/src/ocrmypdf/_exec/unpaper.py @@ -13,18 +13,36 @@ import logging import os import shlex +import sys from contextlib import contextmanager from decimal import Decimal from pathlib import Path from subprocess import PIPE, STDOUT -from tempfile import TemporaryDirectory from typing import Iterator, List, Optional, Tuple, Union from PIL import Image from ocrmypdf.exceptions import MissingDependencyError, SubprocessOutputError +from ocrmypdf.helpers import TemporaryDirectory from ocrmypdf.subprocess import get_version, run +if sys.version_info >= (3, 10): + from tempfile import TemporaryDirectory +else: + from tempfile import TemporaryDirectory as _TemporaryDirectory + + # Consume the ignore_cleanup_errors kwarg in Python 3.9 and older, without acting + # on this keyword. Users who need this issue full resolved should upgrade to Python + # 3.10. + # See: https://github.com/python/cpython/pull/24793 + + class TemporaryDirectory(_TemporaryDirectory): + def __init__(self, ignore_cleanup_errors=False, **kwargs): + super.__init__(**kwargs) + + del _TemporaryDirectory + + UNPAPER_IMAGE_PIXEL_LIMIT = 256 * 1024 * 1024 DecFloat = Union[Decimal, float] @@ -82,7 +100,7 @@ def _setup_unpaper_io(input_file: Path) -> Iterator[Tuple[Path, Path, Path]]: raise UnpaperImageTooLargeError(w=im.width, h=im.height) im, im_modified, suffix = _convert_image(im) - with TemporaryDirectory() as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: tmppath = Path(tmpdir) if im_modified or input_file.suffix != '.pnm': input_pnm = tmppath / 'input.pnm'