diff --git a/PyPDF2/_utils.py b/PyPDF2/_utils.py index f67e29713..c4c370b65 100644 --- a/PyPDF2/_utils.py +++ b/PyPDF2/_utils.py @@ -31,7 +31,6 @@ __author__ = "Mathieu Fenniak" __author_email__ = "biziqe@mathieu.fenniak.net" -import os import warnings from codecs import getencoder from io import ( @@ -41,6 +40,7 @@ BytesIO, FileIO, ) +from os import SEEK_CUR from typing import Dict, Optional, Pattern, Tuple, Union, overload try: @@ -138,9 +138,6 @@ def read_until_regex( return name -CRLF = b"\r\n" - - def read_block_backwards(stream: StreamType, to_read: int) -> bytes: """Given a stream at position X, read a block of size to_read ending at position X. @@ -149,10 +146,10 @@ def read_block_backwards(stream: StreamType, to_read: int) -> bytes: if stream.tell() < to_read: raise PdfStreamError("Could not read malformed PDF file") # Seek to the start of the block we want to read. - stream.seek(-to_read, os.SEEK_CUR) + stream.seek(-to_read, SEEK_CUR) read = stream.read(to_read) # Seek to the start of the block we read after reading it. - stream.seek(-to_read, os.SEEK_CUR) + stream.seek(-to_read, SEEK_CUR) if len(read) != to_read: raise PdfStreamError(f"EOF: read {len(read)}, expected {to_read}?") return read @@ -181,7 +178,7 @@ def read_previous_line(stream: StreamType) -> bytes: if not found_crlf: # We haven't found our first CR/LF yet. # Read off characters until we hit one. - while idx >= 0 and block[idx] not in CRLF: + while idx >= 0 and block[idx] not in b"\r\n": idx -= 1 if idx >= 0: found_crlf = True @@ -192,7 +189,7 @@ def read_previous_line(stream: StreamType) -> bytes: # plus any previously read blocks. line_content.append(block[idx + 1 :]) # Continue to read off any more CRLF characters. - while idx >= 0 and block[idx] in CRLF: + while idx >= 0 and block[idx] in b"\r\n": idx -= 1 else: # Didn't find CR/LF yet - add this block to our @@ -201,7 +198,7 @@ def read_previous_line(stream: StreamType) -> bytes: if idx >= 0: # We found the next non-CRLF character. # Set the stream position correctly, then break - stream.seek(idx + 1, os.SEEK_CUR) + stream.seek(idx + 1, SEEK_CUR) break # Join all the blocks in the line (which are in reverse order) return b"".join(line_content[::-1]) @@ -300,7 +297,7 @@ def hex_str(num: int) -> str: return hex(num).replace("L", "") -WHITESPACES = [b" ", b"\n", b"\r", b"\t", b"\x00"] +WHITESPACES = (b" ", b"\n", b"\r", b"\t", b"\x00") def paeth_predictor(left: int, up: int, up_left: int) -> int: