From 708dbce70bcaf6da9cdfa24123bead8a80489204 Mon Sep 17 00:00:00 2001 From: Kevin Turcios Date: Sat, 21 Nov 2026 04:39:37 -0500 Subject: [PATCH] chore(pdf): remove the pdfminer psparser patch that can no longer apply `patch_psparser()` replaced `PSBaseParser.seek`, `_parse_keyword` and `nexttoken` only when `pdfminer.__version__ <= "20240706"`. The project now requires `pdfminer.six>=20251230`, so the condition is always false: the function runs at import time and does nothing, and the three replacement methods are never installed. Remove `unstructured/patches/` and the import-time call in `pdf.py`. Behavior is unchanged for every supported pdfminer.six version. --- unstructured/partition/pdf.py | 7 --- unstructured/patches/__init__.py | 0 unstructured/patches/pdfminer.py | 76 -------------------------------- 3 files changed, 83 deletions(-) delete mode 100644 unstructured/patches/__init__.py delete mode 100644 unstructured/patches/pdfminer.py diff --git a/unstructured/partition/pdf.py b/unstructured/partition/pdf.py index f71523052d..df37782893 100644 --- a/unstructured/partition/pdf.py +++ b/unstructured/partition/pdf.py @@ -86,7 +86,6 @@ PartitionStrategy, ) from unstructured.partition.utils.sorting import coord_has_valid_points, sort_page_elements -from unstructured.patches.pdfminer import patch_psparser from unstructured.telemetry import ( mark_partition_ocr_used, partition_runtime_telemetry, @@ -100,12 +99,6 @@ from unstructured_inference.inference.layoutelement import LayoutElement -# Correct a bug that was introduced by a previous patch to -# pdfminer.six, causing needless and unsuccessful repairing of PDFs -# which were not actually broken. -patch_psparser() - - RE_MULTISPACE_INCLUDING_NEWLINES = re.compile(pattern=r"\s+", flags=re.DOTALL) # Regex patterns for counting graphics and text operators in PDF content streams. GRAPHICS_OPS_PATTERN = re.compile( diff --git a/unstructured/patches/__init__.py b/unstructured/patches/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/unstructured/patches/pdfminer.py b/unstructured/patches/pdfminer.py deleted file mode 100644 index cc0c7dab21..0000000000 --- a/unstructured/patches/pdfminer.py +++ /dev/null @@ -1,76 +0,0 @@ -import functools -from typing import Tuple, Union - -import pdfminer -from pdfminer.psparser import ( - END_KEYWORD, - KWD, - PSEOF, - PSBaseParser, - PSBaseParserToken, - PSKeyword, - log, -) - -factory_seek = PSBaseParser.seek - - -@functools.wraps(PSBaseParser.seek) -def seek(self: PSBaseParser, pos: int) -> None: - factory_seek(self, pos) - self.eof = False - - -@functools.wraps(PSBaseParser._parse_keyword) -def _parse_keyword(self, s: bytes, i: int) -> int: - m = END_KEYWORD.search(s, i) - if m: - j = m.start(0) - self._curtoken += s[i:j] - else: - self._curtoken += s[i:] - return len(s) - if self._curtoken == b"true": - token: Union[bool, PSKeyword] = True - elif self._curtoken == b"false": - token = False - else: - token = KWD(self._curtoken) - self._add_token(token) - self._parse1 = self._parse_main - return j - - -@functools.wraps(PSBaseParser.nexttoken) -def nexttoken(self) -> Tuple[int, PSBaseParserToken]: - if self.eof: - # It's not really unexpected, come on now... - raise PSEOF("Unexpected EOF") - while not self._tokens: - try: - self.fillbuf() - self.charpos = self._parse1(self.buf, self.charpos) - except PSEOF: - # If we hit EOF in the middle of a token, try to parse - # it by tacking on whitespace, and delay raising PSEOF - # until next time around - self.charpos = self._parse1(b"\n", 0) - self.eof = True - # Oh, so there wasn't actually a token there? OK. - if not self._tokens: - raise - token = self._tokens.pop(0) - log.debug("nexttoken: %r", token) - return token - - -def patch_psparser(): - """Monkey-patch certain versions of pdfminer.six to avoid dropping - tokens at EOF (before 20231228) and splitting tokens at buffer - boundaries (20231228 and 20240706). - """ - # Presuming the bug will be fixed in the next release - if pdfminer.__version__ <= "20240706": - PSBaseParser.seek = seek - PSBaseParser._parse_keyword = _parse_keyword - PSBaseParser.nexttoken = nexttoken