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

STY: Rename b_ to bytes_ #113

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
10 changes: 5 additions & 5 deletions .github/workflows/github-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13-dev"]
use-crypto-lib: ["cryptography"]
include:
- python-version: "3.7"
Expand Down Expand Up @@ -90,7 +90,7 @@ jobs:
cache-dependency-path: '**/requirements/ci.txt'
- name: Setup Python (3.11+)
uses: actions/setup-python@v5
if: matrix.python-version == '3.11' || matrix.python-version == '3.12'
if: matrix.python-version == '3.11' || matrix.python-version == '3.12' || matrix.python-version == '3.13-dev'
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true
Expand All @@ -106,7 +106,7 @@ jobs:
- name: Install requirements (Python 3.11+)
run: |
pip install -r requirements/ci-3.11.txt
if: matrix.python-version == '3.11' || matrix.python-version == '3.12'
if: matrix.python-version == '3.11' || matrix.python-version == '3.12' || matrix.python-version == '3.13-dev'
- name: Remove pycryptodome and cryptography
run: |
pip uninstall pycryptodome cryptography -y
Expand Down Expand Up @@ -215,8 +215,8 @@ jobs:
- name: Check Number of Downloaded Files
run: |
downloaded_files_count=$(find \.coverage* -type f | wc -l)
if [ $downloaded_files_count -eq 8 ]; then
echo "The expected number of files (8) were downloaded."
if [ $downloaded_files_count -eq 9 ]; then
echo "The expected number of files (9) were downloaded."
else
echo "ERROR: Expected 8 files, but found $downloaded_files_count files."
exit 1
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ on:
permissions:
contents: write

env:
HEAD_COMMIT_MESSAGE: ${{ github.event.head_commit.message }}

jobs:
build_and_publish:
name: Publish a new version
Expand All @@ -24,15 +27,15 @@ jobs:
- name: Extract version from commit message
id: extract_version
run: |
VERSION=$(echo "${{ github.event.head_commit.message }}" | grep -oP '(?<=REL: )\d+\.\d+\.\d+')
VERSION=$(echo "$HEAD_COMMIT_MESSAGE" | grep -oP '(?<=REL: )\d+\.\d+\.\d+')
echo "version=$VERSION" >> $GITHUB_OUTPUT

- name: Extract tag message from commit message
id: extract_message
run: |
VERSION="${{ steps.extract_version.outputs.version }}"
delimiter="$(openssl rand -hex 8)"
MESSAGE=$(echo "${{ github.event.head_commit.message }}" | sed "0,/REL: $VERSION/s///" )
MESSAGE=$(echo "$HEAD_COMMIT_MESSAGE" | sed "0,/REL: $VERSION/s///" )
echo "message<<${delimiter}" >> $GITHUB_OUTPUT
echo "$MESSAGE" >> $GITHUB_OUTPUT
echo "${delimiter}" >> $GITHUB_OUTPUT
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ history and [GitHub's 'Contributors' feature](https://github.com/py-pdf/pypdf/gr
* [ediamondscience](https://github.com/ediamondscience)
* [Ermeson, Felipe](https://github.com/FelipeErmeson)
* [Freitag, François](https://github.com/francoisfreitag)
* [Gagnon, William G.](https://github.com/williamgagnon)
* [Górny, Michał](https://github.com/mgorny)
* [Grillo, Miguel](https://github.com/Ineffable22)
* [Gutteridge, David H.](https://github.com/dhgutteridge)
Expand Down
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
26 changes: 17 additions & 9 deletions pypdf/_text_extraction/_layout_mode/_font.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Font constants and classes for "layout" mode text operations"""

from dataclasses import dataclass, field
from typing import Any, Dict, Sequence, Union
from typing import Any, Dict, Sequence, Union, cast

from ...errors import ParseError
from ...generic import IndirectObject
from ._font_widths import STANDARD_WIDTHS

Expand Down Expand Up @@ -58,6 +59,7 @@ def __post_init__(self) -> None:
skip_count = 0
_w = d_font.get("/W", [])
for idx, w_entry in enumerate(_w):
w_entry = w_entry.get_object()
if skip_count:
skip_count -= 1
continue
Expand All @@ -66,32 +68,38 @@ def __post_init__(self) -> None:
# warning and or use reader's "strict" to force an ex???
continue
# check for format (1): `int [int int int int ...]`
if isinstance(_w[idx + 1], Sequence):
start_idx, width_list = _w[idx : idx + 2]
w_next_entry = _w[idx + 1].get_object()
if isinstance(w_next_entry, Sequence):
start_idx, width_list = w_entry, w_next_entry
self.width_map.update(
{
ord_map[_cidx]: _width
for _cidx, _width in zip(
range(start_idx, start_idx + len(width_list), 1),
range(cast(int, start_idx), cast(int, start_idx) + len(width_list), 1),
width_list,
)
if _cidx in ord_map
}
)
skip_count = 1
# check for format (2): `int int int`
if not isinstance(_w[idx + 1], Sequence) and not isinstance(
_w[idx + 2], Sequence
):
start_idx, stop_idx, const_width = _w[idx : idx + 3]
elif isinstance(w_next_entry, (int, float)) and isinstance(_w[idx + 2].get_object(), (int, float)):
start_idx, stop_idx, const_width = w_entry, w_next_entry, _w[idx + 2].get_object()
self.width_map.update(
{
ord_map[_cidx]: const_width
for _cidx in range(start_idx, stop_idx + 1, 1)
for _cidx in range(cast(int, start_idx), cast(int, stop_idx + 1), 1)
if _cidx in ord_map
}
)
skip_count = 2
else:
# Note: this doesn't handle the case of out of bounds (reaching the end of the width definitions
# while expecting more elements). This raises an IndexError which is sufficient.
raise ParseError(
f"Invalid font width definition. Next elements: {w_entry}, {w_next_entry}, {_w[idx + 2]}"
) # pragma: no cover

if not self.width_map and "/BaseFont" in self.font_dictionary:
for key in STANDARD_WIDTHS:
if self.font_dictionary["/BaseFont"].startswith(f"/{key}"):
Expand Down
31 changes: 7 additions & 24 deletions pypdf/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,25 +336,22 @@ 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:
r = s.encode("latin-1")
if len(s) < 2:
bc[s] = r
return r
except Exception:
except UnicodeEncodeError:
r = s.encode("utf-8")
if len(s) < 2:
bc[s] = r
return r
if len(s) < 2:
bc[s] = r
return r


def str_(b: Any) -> str:
Expand Down Expand Up @@ -390,20 +387,6 @@ def ord_(b: Union[int, str, bytes]) -> Union[int, bytes]:
WHITESPACES_AS_REGEXP = b"[" + WHITESPACES_AS_BYTES + b"]"


def paeth_predictor(left: int, up: int, up_left: int) -> int:
p = left + up - up_left
dist_left = abs(p - left)
dist_up = abs(p - up)
dist_up_left = abs(p - up_left)

if dist_left <= dist_up and dist_left <= dist_up_left:
return left
elif dist_up <= dist_up_left:
return up
else:
return up_left


def deprecate(msg: str, stacklevel: int = 3) -> None:
warnings.warn(msg, DeprecationWarning, stacklevel=stacklevel)

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
9 changes: 6 additions & 3 deletions pypdf/_xobj_image_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from ._utils import check_if_whitespace_only, logger_warning
from .constants import ColorSpaces
from .errors import PdfReadError
from .errors import EmptyImageDataError, PdfReadError
from .generic import (
ArrayObject,
DecodedStreamObject,
Expand Down Expand Up @@ -148,9 +148,12 @@ def _extended_image_frombytes(
img = Image.frombytes(mode, size, data)
except ValueError as exc:
nb_pix = size[0] * size[1]
if len(data) % nb_pix != 0:
data_length = len(data)
if data_length == 0:
raise EmptyImageDataError("Data is 0 bytes, cannot process an image from empty data.") from exc
if data_length % nb_pix != 0:
raise exc
k = nb_pix * len(mode) / len(data)
k = nb_pix * len(mode) / data_length
data = b"".join([bytes((x,) * int(k)) for x in data])
img = Image.frombytes(mode, size, data)
return img
Expand Down
4 changes: 2 additions & 2 deletions pypdf/annotations/_markup_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ def __init__(
self[NameObject("/Rect")] = RectangleObject(rect)

font_str = "font: "
if bold is True:
if bold:
font_str = f"{font_str}bold "
if italic is True:
if italic:
font_str = f"{font_str}italic "
font_str = f"{font_str}{font} {font_size}"
font_str = f"{font_str};text-align:left;color:#{font_color}"
Expand Down
3 changes: 0 additions & 3 deletions pypdf/annotations/_non_markup_annotations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import TYPE_CHECKING, Any, Optional, Tuple, Union

from ..constants import AnnotationFlag
from ..generic._base import (
BooleanObject,
NameObject,
Expand All @@ -12,8 +11,6 @@
from ..generic._rectangle import RectangleObject
from ._base import AnnotationDictionary

DEFAULT_ANNOTATION_FLAG = AnnotationFlag(0)


class Link(AnnotationDictionary):
def __init__(
Expand Down
4 changes: 4 additions & 0 deletions pypdf/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ class EmptyFileError(PdfReadError):
"""Raised when a PDF file is empty or has no content."""


class EmptyImageDataError(PyPdfError):
"""Raised when trying to process an image that has no data."""


STREAM_TRUNCATED_PREMATURELY = "Stream has ended unexpectedly"
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
Loading