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

TST: Improve tests for convert_to_int #899

Merged
merged 4 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion PyPDF2/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@

def convert_to_int(d: bytes, size: int) -> Union[int, Tuple[Any, ...]]:
if size > 8:
raise PdfReadError("invalid size in convertToInt")
raise PdfReadError("invalid size in convert_to_int")
d = b_("\x00\x00\x00\x00\x00\x00\x00\x00") + b_(d)
d = d[-8:]
return struct.unpack(">q", d)[0]
Expand Down
10 changes: 0 additions & 10 deletions tests/test_basic_features.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import os

import pytest

from PyPDF2 import PdfReader, PdfWriter
from PyPDF2._reader import convertToInt
from PyPDF2.errors import PdfReadError

TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = os.path.dirname(TESTS_ROOT)
Expand Down Expand Up @@ -59,9 +55,3 @@ def test_basic_features():

# cleanup
os.remove(tmp_path)


def test_convertToInt():
with pytest.raises(PdfReadError) as exc:
convertToInt(b"256", 16)
assert exc.value.args[0] == "invalid size in convertToInt"
16 changes: 16 additions & 0 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest

from PyPDF2 import PdfReader
from PyPDF2._reader import convert_to_int, convertToInt
from PyPDF2.constants import ImageAttributes as IA
from PyPDF2.constants import PageAttributes as PG
from PyPDF2.constants import Ressources as RES
Expand Down Expand Up @@ -603,3 +604,18 @@ def test_VirtualList():

# Test if getting as slice throws an error
assert len(reader.pages[:]) == 1


def test_convert_to_int():
assert convert_to_int(b'\x01', 8) == 1


def test_convert_to_int_error():
with pytest.raises(PdfReadError) as exc:
convert_to_int(b"256", 16)
assert exc.value.args[0] == "invalid size in convert_to_int"


def test_convertToInt_deprecated():
with pytest.warns(PendingDeprecationWarning, match="convertToInt will be removed with PyPDF2 2.0.0. Use convert_to_int instead."):
assert convertToInt(b'\x01', 8) == 1