Skip to content
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

MAINT: Reduce usage of b_ #2100

Merged
merged 6 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 2 additions & 3 deletions pypdf/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
from ._utils import (
StrByteType,
StreamType,
b_,
deprecate_no_replacement,
deprecation_no_replacement,
deprecation_with_replacement,
Expand Down Expand Up @@ -1257,7 +1256,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())) # type: ignore
stream_data = BytesIO(obj_stm.get_data()) # type: ignore
for i in range(obj_stm["/N"]): # type: ignore
read_non_whitespace(stream_data)
stream_data.seek(-1, 1)
Expand Down Expand Up @@ -1868,7 +1867,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(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
2 changes: 1 addition & 1 deletion pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ def add_attachment(self, filename: str, data: Union[str, bytes]) -> None:
# endobj

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

# The Filespec entry
Expand Down
2 changes: 0 additions & 2 deletions pypdf/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
from typing import Any, Dict, List, Optional, Tuple, Union, cast

from ._utils import (
b_,
deprecate_with_replacement,
logger_warning,
ord_,
Expand Down Expand Up @@ -1035,7 +1034,6 @@ def _handle_jpx(
else:
extension = ".png" # mime_type = "image/png"
image_format = "PNG"
data = b_(data)
img = Image.open(BytesIO(data), formats=("TIFF", "PNG"))
elif lfilters == FT.DCT_DECODE:
img, image_format, extension = Image.open(BytesIO(data)), "JPEG", ".jpg"
Expand Down
2 changes: 1 addition & 1 deletion pypdf/generic/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ def write_to_stream(
deprecate_no_replacement(
"the encryption_key parameter of write_to_stream", "5.0.0"
)
stream.write(self.renumber()) # b_(renumber(self)))
stream.write(self.renumber())

def writeToStream(
self, stream: StreamType, encryption_key: Union[None, str, bytes]
Expand Down
29 changes: 15 additions & 14 deletions pypdf/generic/_data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ def _clone(

def hash_value_data(self) -> bytes:
data = super().hash_value_data()
data += b_(self._data)
data += self._data
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't look quite right for a hash function.

return data

@property
Expand Down Expand Up @@ -906,10 +906,10 @@ def flate_encode(self, level: int = -1) -> "EncodedStreamObject":


class DecodedStreamObject(StreamObject):
def get_data(self) -> Any:
def get_data(self) -> bytes:
return self._data

def set_data(self, data: Any) -> Any:
def set_data(self, data: bytes) -> None:
self._data = data

def getData(self) -> Any: # deprecated
Expand All @@ -935,7 +935,7 @@ def decodedSelf(self, value: DecodedStreamObject) -> None: # deprecated
deprecation_with_replacement("decodedSelf", "decoded_self", "3.0.0")
self.decoded_self = value

def get_data(self) -> Union[None, str, bytes]:
def get_data(self) -> bytes:
from ..filters import decode_stream_data

if self.decoded_self is not None:
Expand All @@ -956,7 +956,7 @@ def getData(self) -> Union[None, str, bytes]: # deprecated
deprecation_with_replacement("getData", "get_data", "3.0.0")
return self.get_data()

def set_data(self, data: Any) -> None: # deprecated
def set_data(self, data: bytes) -> None: # deprecated
from ..filters import FlateDecode

if self.get(SA.FILTER, "") == FT.FLATE_DECODE:
Expand Down Expand Up @@ -996,14 +996,13 @@ def __init__(
if isinstance(stream, ArrayObject):
data = b""
for s in stream:
data += b_(s.get_object().get_data())
data += s.get_object().get_data()
if len(data) == 0 or data[-1] != b"\n":
data += b"\n"
stream_bytes = BytesIO(data)
else:
stream_data = stream.get_data()
assert stream_data is not None
stream_data_bytes = b_(stream_data)
stream_data_bytes = stream.get_data()
assert stream_data_bytes is not None
stream_bytes = BytesIO(stream_data_bytes)
self.forced_encoding = forced_encoding
self.__parse_content_stream(stream_bytes)
Expand Down Expand Up @@ -1194,8 +1193,8 @@ def _data(self) -> bytes: # type: ignore
return new_data.getvalue()

@_data.setter
def _data(self, value: Union[str, bytes]) -> None:
self.__parse_content_stream(BytesIO(b_(value)))
def _data(self, value: bytes) -> None:
self.__parse_content_stream(BytesIO(value))


def read_object(
Expand Down Expand Up @@ -1276,10 +1275,12 @@ def __init__(self, data: DictionaryObject) -> None:
if isinstance(self.get("/V"), EncodedStreamObject):
d = cast(EncodedStreamObject, self[NameObject("/V")]).get_data()
if isinstance(d, bytes):
d = d.decode()
d_str = d.decode()
elif d is None:
d = ""
self[NameObject("/V")] = TextStringObject(d)
d_str = ""
else:
raise Exception("Should never happen")
self[NameObject("/V")] = TextStringObject(d_str)

# TABLE 8.69 Entries common to all field dictionaries
@property
Expand Down
Loading