Skip to content

Commit 11b8195

Browse files
authored
MAINT: Change name of class attribute (#3287)
The SOLIDUS is a prefix, so use this as the variable name.
1 parent 8c542f3 commit 11b8195

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

pypdf/generic/_base.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
from .._protocols import PdfObjectProtocol, PdfWriterProtocol
4444
from .._utils import (
4545
StreamType,
46+
classproperty,
4647
deprecate_no_replacement,
48+
deprecate_with_replacement,
4749
logger_warning,
4850
read_non_whitespace,
4951
read_until_regex,
@@ -778,7 +780,7 @@ def write_to_stream(
778780

779781
class NameObject(str, PdfObject): # noqa: SLOT000
780782
delimiter_pattern = re.compile(rb"\s+|[\(\)<>\[\]{}/%]")
781-
surfix = b"/"
783+
prefix = b"/"
782784
renumber_table: ClassVar[Dict[str, bytes]] = {
783785
**{chr(i): f"#{i:02X}".encode() for i in b"#()<>[]{}/%"},
784786
**{chr(i): f"#{i:02X}".encode() for i in range(33)},
@@ -833,6 +835,11 @@ def renumber(self) -> bytes:
833835
out += c.encode("utf-8")
834836
return out
835837

838+
@classproperty
839+
def surfix(cls) -> bytes: # noqa: N805
840+
deprecate_with_replacement("surfix", "prefix", "6.0.0")
841+
return b"/"
842+
836843
@staticmethod
837844
def unnumber(sin: bytes) -> bytes:
838845
i = sin.find(b"#", 0)
@@ -851,7 +858,7 @@ def unnumber(sin: bytes) -> bytes:
851858
@staticmethod
852859
def read_from_stream(stream: StreamType, pdf: Any) -> "NameObject": # PdfReader
853860
name = stream.read(1)
854-
if name != NameObject.surfix:
861+
if name != NameObject.prefix:
855862
raise PdfReadError("Name read error")
856863
name += read_until_regex(stream, NameObject.delimiter_pattern)
857864
try:

tests/test_generic.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,25 @@ def test_name_object(caplog):
194194
with pytest.raises(PdfReadError) as exc:
195195
NameObject.read_from_stream(stream, None)
196196
assert exc.value.args[0] == "Name read error"
197+
198+
with pytest.warns(
199+
DeprecationWarning,
200+
match="surfix is deprecated and will be removed in pypdf 6.0.0. Use prefix instead.",
201+
):
202+
_ = NameObject.surfix
203+
197204
assert (
198205
NameObject.read_from_stream(
199206
BytesIO(b"/A;Name_With-Various***Characters?"), None
200207
)
201208
== "/A;Name_With-Various***Characters?"
202209
)
210+
203211
assert (
204212
NameObject.read_from_stream(BytesIO(b"/paired#28#29parentheses"), None)
205213
== "/paired()parentheses"
206214
)
215+
207216
assert NameObject.read_from_stream(BytesIO(b"/A#42"), None) == "/AB"
208217

209218
assert (
@@ -222,7 +231,7 @@ def test_name_object(caplog):
222231
)
223232
) == "/你好世界"
224233

225-
# to test PDFDocEncoding (latin-1)
234+
# test PDFDocEncoding (latin-1)
226235
assert (
227236
NameObject.read_from_stream(BytesIO(b"/DocuSign\xae"), None)
228237
) == "/DocuSign®"

0 commit comments

Comments
 (0)