Skip to content
Closed
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions pypdf/_cmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Any, Dict, List, Tuple, Union, cast

from ._codecs import adobe_glyphs, charset_encoding
from ._utils import b_, logger_error, logger_warning
from ._utils import bytes_, logger_error, logger_warning
from .generic import (
DecodedStreamObject,
DictionaryObject,
Expand Down Expand Up @@ -258,7 +258,7 @@ def prepare_cm(ft: DictionaryObject) -> bytes:
tu = ft["/ToUnicode"]
cm: bytes
if isinstance(tu, StreamObject):
cm = b_(cast(DecodedStreamObject, ft["/ToUnicode"]).get_data())
cm = bytes_(cast(DecodedStreamObject, ft["/ToUnicode"]).get_data())
elif isinstance(tu, str) and tu.startswith("/Identity"):
# the full range 0000-FFFF will be processed
cm = b"beginbfrange\n<0000> <0001> <0000>\nendbfrange"
Expand Down
4 changes: 2 additions & 2 deletions pypdf/_doc_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from ._page import PageObject, _VirtualList
from ._page_labels import index2label as page_index2page_label
from ._utils import (
b_,
bytes_,
deprecate_with_replacement,
logger_warning,
parse_iso8824_date,
Expand Down Expand Up @@ -1258,7 +1258,7 @@ def xfa(self) -> Optional[Dict[str, Any]]:
if isinstance(f, IndirectObject):
field = cast(Optional[EncodedStreamObject], f.get_object())
if field:
es = zlib.decompress(b_(field._data))
es = zlib.decompress(bytes_(field._data))
retval[tag] = es
return retval

Expand Down
6 changes: 3 additions & 3 deletions pypdf/_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
rc4_encrypt,
)

from ._utils import b_, logger_warning
from ._utils import bytes_, logger_warning
from .generic import (
ArrayObject,
ByteStringObject,
Expand Down Expand Up @@ -78,7 +78,7 @@ def encrypt_object(self, obj: PdfObject) -> PdfObject:
elif isinstance(obj, StreamObject):
obj2 = StreamObject()
obj2.update(obj)
obj2.set_data(self.stm_crypt.encrypt(b_(obj._data)))
obj2.set_data(self.stm_crypt.encrypt(bytes_(obj._data)))
for key, value in obj.items(): # Dont forget the Stream dict.
obj2[key] = self.encrypt_object(value)
obj = obj2
Expand All @@ -96,7 +96,7 @@ def decrypt_object(self, obj: PdfObject) -> PdfObject:
data = self.str_crypt.decrypt(obj.original_bytes)
obj = create_string_object(data)
elif isinstance(obj, StreamObject):
obj._data = self.stm_crypt.decrypt(b_(obj._data))
obj._data = self.stm_crypt.decrypt(bytes_(obj._data))
for key, value in obj.items(): # Dont forget the Stream dict.
obj[key] = self.decrypt_object(value)
elif isinstance(obj, DictionaryObject):
Expand Down
6 changes: 3 additions & 3 deletions pypdf/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
from ._utils import (
StrByteType,
StreamType,
b_,
bytes_,
logger_warning,
read_non_whitespace,
read_previous_line,
Expand Down Expand Up @@ -328,7 +328,7 @@ def _get_object_from_stream(
assert cast(str, obj_stm["/Type"]) == "/ObjStm"
# /N is the number of indirect objects in the stream
assert idx < obj_stm["/N"]
stream_data = BytesIO(b_(obj_stm.get_data()))
stream_data = BytesIO(bytes_(obj_stm.get_data()))
for i in range(obj_stm["/N"]): # type: ignore
read_non_whitespace(stream_data)
stream_data.seek(-1, 1)
Expand Down Expand Up @@ -932,7 +932,7 @@ def _read_pdf15_xref_stream(
xrefstream = cast(ContentStream, read_object(stream, self))
assert cast(str, xrefstream["/Type"]) == "/XRef"
self.cache_indirect_object(generation, idnum, xrefstream)
stream_data = BytesIO(b_(xrefstream.get_data()))
stream_data = BytesIO(bytes_(xrefstream.get_data()))
# Index pairs specify the subsections in the dictionary. If
# none create one subsection that spans everything.
idx_pairs = xrefstream.get("/Index", [0, xrefstream.get("/Size")])
Expand Down
6 changes: 3 additions & 3 deletions pypdf/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,13 @@ def mark_location(stream: StreamType) -> None:
stream.seek(-radius, 1)


B_CACHE: Dict[str, bytes] = {}
BYTE_CACHE: Dict[str, bytes] = {}


def b_(s: Union[str, bytes]) -> bytes:
def bytes_(s: Union[str, bytes]) -> bytes:
if isinstance(s, bytes):
return s
bc = B_CACHE
bc = BYTE_CACHE
if s in bc:
return bc[s]
try:
Expand Down
4 changes: 2 additions & 2 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
StrByteType,
StreamType,
_get_max_pdf_version_header,
b_,
bytes_,
deprecate_with_replacement,
logger_warning,
)
Expand Down Expand Up @@ -680,7 +680,7 @@ def add_attachment(self, filename: str, data: Union[str, bytes]) -> None:
# endobj

file_entry = DecodedStreamObject()
file_entry.set_data(b_(data))
file_entry.set_data(bytes_(data))
file_entry.update({NameObject(PA.TYPE): NameObject("/EmbeddedFile")})

# The Filespec entry
Expand Down
4 changes: 2 additions & 2 deletions pypdf/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

from ._utils import (
WHITESPACES_AS_BYTES,
b_,
bytes_,
deprecate_with_replacement,
deprecation_no_replacement,
logger_warning,
Expand Down Expand Up @@ -678,7 +678,7 @@ def decode_stream_data(stream: Any) -> Union[bytes, str]: # utils.StreamObject
decodparms = stream.get(SA.DECODE_PARMS, ({},) * len(filters))
if not isinstance(decodparms, (list, tuple)):
decodparms = (decodparms,)
data: bytes = b_(stream._data)
data: bytes = bytes_(stream._data)
# If there is not data to decode we should not try to decode the data.
if data:
for filter_type, params in zip(filters, decodparms):
Expand Down
6 changes: 3 additions & 3 deletions pypdf/generic/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from .._protocols import PdfObjectProtocol, PdfWriterProtocol
from .._utils import (
StreamType,
b_,
bytes_,
deprecate_no_replacement,
logger_warning,
read_non_whitespace,
Expand Down Expand Up @@ -607,7 +607,7 @@ def write_to_stream(
# https://github.com/davidhalter/parso/issues/207
stream.write(("\\%03o" % c).encode())
else:
stream.write(b_(chr(c)))
stream.write(bytes_(chr(c)))
stream.write(b")")


Expand Down Expand Up @@ -713,7 +713,7 @@ def encode_pdfdocencoding(unicode_string: str) -> bytes:
retval = bytearray()
for c in unicode_string:
try:
retval += b_(chr(_pdfdoc_encoding_rev[c]))
retval += bytes_(chr(_pdfdoc_encoding_rev[c]))
except KeyError:
raise UnicodeEncodeError(
"pdfdocencoding", c, -1, -1, "does not exist in translation table"
Expand Down
22 changes: 11 additions & 11 deletions pypdf/generic/_data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
from .._utils import (
WHITESPACES,
StreamType,
b_,
bytes_,
deprecate_no_replacement,
deprecate_with_replacement,
logger_warning,
Expand Down Expand Up @@ -885,7 +885,7 @@ def set_data(self, data: bytes) -> None:

def hash_value_data(self) -> bytes:
data = super().hash_value_data()
data += b_(self._data)
data += bytes_(self._data)
return data

def write_to_stream(
Expand Down Expand Up @@ -955,7 +955,7 @@ def flate_encode(self, level: int = -1) -> "EncodedStreamObject":
retval[NameObject(SA.FILTER)] = f
if params is not None:
retval[NameObject(SA.DECODE_PARMS)] = params
retval._data = FlateDecode.encode(b_(self._data), level)
retval._data = FlateDecode.encode(bytes_(self._data), level)
return retval

def decode_as_image(self) -> Any:
Expand Down Expand Up @@ -1003,7 +1003,7 @@ def get_data(self) -> Union[bytes, str]:
# create decoded object
decoded = DecodedStreamObject()

decoded.set_data(b_(decode_stream_data(self)))
decoded.set_data(bytes_(decode_stream_data(self)))
for key, value in list(self.items()):
if key not in (SA.LENGTH, SA.FILTER, SA.DECODE_PARMS):
decoded[key] = value
Expand Down Expand Up @@ -1069,14 +1069,14 @@ def __init__(
if isinstance(stream, ArrayObject):
data = b""
for s in stream:
data += b_(s.get_object().get_data())
data += bytes_(s.get_object().get_data())
if len(data) == 0 or data[-1] != b"\n":
data += b"\n"
super().set_data(bytes(data))
else:
stream_data = stream.get_data()
assert stream_data is not None
super().set_data(b_(stream_data))
super().set_data(bytes_(stream_data))
self.forced_encoding = forced_encoding

def clone(
Expand Down Expand Up @@ -1132,7 +1132,7 @@ def _clone(
ignore_fields:
"""
src_cs = cast("ContentStream", src)
super().set_data(b_(src_cs._data))
super().set_data(bytes_(src_cs._data))
self.pdf = pdf_dest
self._operations = list(src_cs._operations)
self.forced_encoding = src_cs.forced_encoding
Expand Down Expand Up @@ -1249,10 +1249,10 @@ def get_data(self) -> bytes:
for op in operands:
op.write_to_stream(new_data)
new_data.write(b" ")
new_data.write(b_(operator))
new_data.write(bytes_(operator))
new_data.write(b"\n")
self._data = new_data.getvalue()
return b_(self._data)
return bytes_(self._data)

# This overrides the parent method:
def set_data(self, data: bytes) -> None:
Expand All @@ -1262,7 +1262,7 @@ def set_data(self, data: bytes) -> None:
@property
def operations(self) -> List[Tuple[Any, Any]]:
if not self._operations and self._data:
self._parse_content_stream(BytesIO(b_(self._data)))
self._parse_content_stream(BytesIO(bytes_(self._data)))
self._data = b""
return self._operations

Expand All @@ -1276,7 +1276,7 @@ def isolate_graphics_state(self) -> None:
self._operations.insert(0, ([], "q"))
self._operations.append(([], "Q"))
elif self._data:
self._data = b"q\n" + b_(self._data) + b"\nQ\n"
self._data = b"q\n" + bytes_(self._data) + b"\nQ\n"

# This overrides the parent method:
def write_to_stream(
Expand Down
6 changes: 3 additions & 3 deletions pypdf/generic/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Dict, List, Tuple, Union

from .._codecs import _pdfdoc_encoding
from .._utils import StreamType, b_, logger_warning, read_non_whitespace
from .._utils import StreamType, bytes_, logger_warning, read_non_whitespace
from ..errors import STREAM_TRUNCATED_PREMATURELY, PdfStreamError
from ._base import ByteStringObject, TextStringObject

Expand Down Expand Up @@ -32,7 +32,7 @@ def read_hex_string_from_stream(
x += b"0"
if len(x) == 2:
txt += chr(int(x, base=16))
return create_string_object(b_(txt), forced_encoding)
return create_string_object(bytes_(txt), forced_encoding)


def read_string_from_stream(
Expand Down Expand Up @@ -92,7 +92,7 @@ def read_string_from_stream(
else:
stream.seek(-1, 1) # ntok has to be analyzed
break
tok = b_(chr(int(tok, base=8)))
tok = bytes_(chr(int(tok, base=8)))
elif tok in b"\n\r":
# This case is hit when a backslash followed by a line
# break occurs. If it's a multi-char EOL, consume the
Expand Down
4 changes: 2 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ def test_mark_location():
("😀😃", "😀😃".encode()),
],
)
def test_b(input_str: str, expected: bytes):
assert pypdf._utils.b_(input_str) == expected
def test_bytes_(input_str: str, expected: bytes):
assert pypdf._utils.bytes_(input_str) == expected


def test_deprecate_no_replacement():
Expand Down