From a1d5de1f321f614acb1a8f17b7a86abd0451b3cf Mon Sep 17 00:00:00 2001 From: Tommy Forte <5710966+thomas-forte@users.noreply.github.com> Date: Fri, 29 Nov 2024 14:41:46 -0500 Subject: [PATCH] BUG: Fixed reference of value with str.__new__ in TextStringObject (#2952) Closes #2949. --------- Co-authored-by: Stefan <96178532+stefan6419846@users.noreply.github.com> --- pypdf/_writer.py | 2 +- pypdf/generic/_base.py | 2 +- tests/test_generic.py | 5 +++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pypdf/_writer.py b/pypdf/_writer.py index 96611ff14..4ca5bec3e 100644 --- a/pypdf/_writer.py +++ b/pypdf/_writer.py @@ -1049,7 +1049,7 @@ def _update_field_annotation( def update_page_form_field_values( self, page: Union[PageObject, List[PageObject], None], - fields: Dict[str, Any], + fields: Dict[str, Union[str, List[str], Tuple[str, str, float]]], flags: FA.FfBits = FFBITS_NUL, auto_regenerate: Optional[bool] = True, ) -> None: diff --git a/pypdf/generic/_base.py b/pypdf/generic/_base.py index 9743e42e3..0982d5891 100644 --- a/pypdf/generic/_base.py +++ b/pypdf/generic/_base.py @@ -653,7 +653,7 @@ def __new__(cls, value: Any) -> "TextStringObject": o.autodetect_utf16 = False o.autodetect_pdfdocencoding = False o.utf16_bom = b"" - if value.startswith(("\xfe\xff", "\xff\xfe")): + if o.startswith(("\xfe\xff", "\xff\xfe")): assert org is not None # for mypy try: o = str.__new__(cls, org.decode("utf-16")) diff --git a/tests/test_generic.py b/tests/test_generic.py index fd0511679..33eea9c7e 100644 --- a/tests/test_generic.py +++ b/tests/test_generic.py @@ -507,6 +507,11 @@ def test_textstringobject_autodetect_utf16(): assert tso.get_encoded_bytes() == b"\xff\xfef\x00o\x00o\x00" +def test_textstringobject__numbers_as_input(): + _ = TextStringObject(42) + _ = TextStringObject(13.37) + + def test_remove_child_not_in_tree(): tree = TreeObject() with pytest.raises(ValueError) as exc: