Skip to content

Commit

Permalink
MAINT: Deprecate the encryption_key parameter of write_to_stream (#1936)
Browse files Browse the repository at this point in the history
* MAINT: Deprecate the encryption_key parameter of write_to_stream

Partially taken from #1816

Co-authored-by: exiledkingcc <exiledkingcc@gmail.com>
  • Loading branch information
MartinThoma and exiledkingcc authored Jul 2, 2023
1 parent c3279f7 commit 2b47d9a
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 32 deletions.
1 change: 0 additions & 1 deletion pypdf/_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,6 @@ def __init__(
self.R = R
self.Length = Length # key_size
self.P = (P + 0x100000000) % 0x100000000 # maybe P < 0
self.entry = entry
self.EncryptMetadata = EncryptMetadata
self.id1_entry = first_id_entry
self.StmF = StmF
Expand Down
3 changes: 1 addition & 2 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,7 @@ def encrypt(

if owner_password is None:
owner_password = user_password

if algorithm is not None:
try:
alg = getattr(EncryptAlgorithm, algorithm.replace("-", "_"))
Expand Down Expand Up @@ -1296,8 +1297,6 @@ def _write_pdf_structure(self, stream: StreamType) -> List[int]:
stream.write(b"%\xE2\xE3\xCF\xD3\n")

for i, obj in enumerate(self._objects):
obj = self._objects[i]
# If the obj is None we can't write anything
if obj is not None:
idnum = i + 1
object_positions.append(stream.tell())
Expand Down
53 changes: 44 additions & 9 deletions pypdf/generic/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from .._utils import (
StreamType,
b_,
deprecate_no_replacement,
deprecation_with_replacement,
logger_warning,
read_non_whitespace,
Expand Down Expand Up @@ -93,7 +94,9 @@ def clone(
Returns:
The cloned PdfObject
"""
raise Exception("clone PdfObject")
raise NotImplementedError(
f"{self.__class__.__name__} does not implement .clone so far"
)

def _reference_clone(
self, clone: Any, pdf_dest: PdfWriterProtocol, force_duplicate: bool = False
Expand Down Expand Up @@ -168,6 +171,10 @@ def clone(
def write_to_stream(
self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
) -> None:
if encryption_key is not None: # deprecated
deprecate_no_replacement(
"the encryption_key parameter of write_to_stream", "5.0.0"
)
stream.write(b"null")

@staticmethod
Expand All @@ -181,7 +188,7 @@ def writeToStream(
self, stream: StreamType, encryption_key: Union[None, str, bytes]
) -> None: # deprecated
deprecation_with_replacement("writeToStream", "write_to_stream", "3.0.0")
self.write_to_stream(stream, encryption_key)
self.write_to_stream(stream)

def __repr__(self) -> str:
return "NullObject"
Expand Down Expand Up @@ -222,6 +229,10 @@ def __repr__(self) -> str:
def write_to_stream(
self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
) -> None:
if encryption_key is not None: # deprecated
deprecate_no_replacement(
"the encryption_key parameter of write_to_stream", "5.0.0"
)
if self.value:
stream.write(b"true")
else:
Expand All @@ -231,7 +242,7 @@ def writeToStream(
self, stream: StreamType, encryption_key: Union[None, str, bytes]
) -> None: # deprecated
deprecation_with_replacement("writeToStream", "write_to_stream", "3.0.0")
self.write_to_stream(stream, encryption_key)
self.write_to_stream(stream)

@staticmethod
def read_from_stream(stream: StreamType) -> "BooleanObject":
Expand Down Expand Up @@ -315,13 +326,17 @@ def __ne__(self, other: Any) -> bool:
def write_to_stream(
self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
) -> None:
if encryption_key is not None: # deprecated
deprecate_no_replacement(
"the encryption_key parameter of write_to_stream", "5.0.0"
)
stream.write(b_(f"{self.idnum} {self.generation} R"))

def writeToStream(
self, stream: StreamType, encryption_key: Union[None, str, bytes]
) -> None: # deprecated
deprecation_with_replacement("writeToStream", "write_to_stream", "3.0.0")
self.write_to_stream(stream, encryption_key)
self.write_to_stream(stream)

@staticmethod
def read_from_stream(stream: StreamType, pdf: Any) -> "IndirectObject": # PdfReader
Expand Down Expand Up @@ -401,13 +416,17 @@ def as_numeric(self) -> float:
def write_to_stream(
self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
) -> None:
if encryption_key is not None: # deprecated
deprecate_no_replacement(
"the encryption_key parameter of write_to_stream", "5.0.0"
)
stream.write(self.myrepr().encode("utf8"))

def writeToStream(
self, stream: StreamType, encryption_key: Union[None, str, bytes]
) -> None: # deprecated
deprecation_with_replacement("writeToStream", "write_to_stream", "3.0.0")
self.write_to_stream(stream, encryption_key)
self.write_to_stream(stream)


class NumberObject(int, PdfObject):
Expand Down Expand Up @@ -438,13 +457,17 @@ def as_numeric(self) -> int:
def write_to_stream(
self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
) -> None:
if encryption_key is not None: # deprecated
deprecate_no_replacement(
"the encryption_key parameter of write_to_stream", "5.0.0"
)
stream.write(repr(self).encode("utf8"))

def writeToStream(
self, stream: StreamType, encryption_key: Union[None, str, bytes]
) -> None: # deprecated
deprecation_with_replacement("writeToStream", "write_to_stream", "3.0.0")
self.write_to_stream(stream, encryption_key)
self.write_to_stream(stream)

@staticmethod
def read_from_stream(stream: StreamType) -> Union["NumberObject", "FloatObject"]:
Expand Down Expand Up @@ -492,6 +515,10 @@ def original_bytes(self) -> bytes:
def write_to_stream(
self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
) -> None:
if encryption_key is not None: # deprecated
deprecate_no_replacement(
"the encryption_key parameter of write_to_stream", "5.0.0"
)
stream.write(b"<")
stream.write(binascii.hexlify(self))
stream.write(b">")
Expand All @@ -500,7 +527,7 @@ def writeToStream(
self, stream: StreamType, encryption_key: Union[None, str, bytes]
) -> None: # deprecated
deprecation_with_replacement("writeToStream", "write_to_stream", "3.0.0")
self.write_to_stream(stream, encryption_key)
self.write_to_stream(stream)


class TextStringObject(str, PdfObject): # noqa: SLOT000
Expand Down Expand Up @@ -565,6 +592,10 @@ def get_encoded_bytes(self) -> bytes:
def write_to_stream(
self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
) -> None:
if encryption_key is not None: # deprecated
deprecate_no_replacement(
"the encryption_key parameter of write_to_stream", "5.0.0"
)
bytearr = self.get_encoded_bytes()
stream.write(b"(")
for c in bytearr:
Expand All @@ -582,7 +613,7 @@ def writeToStream(
self, stream: StreamType, encryption_key: Union[None, str, bytes]
) -> None: # deprecated
deprecation_with_replacement("writeToStream", "write_to_stream", "3.0.0")
self.write_to_stream(stream, encryption_key)
self.write_to_stream(stream)


class NameObject(str, PdfObject): # noqa: SLOT000
Expand Down Expand Up @@ -611,13 +642,17 @@ def clone(
def write_to_stream(
self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
) -> None:
if encryption_key is not None: # deprecated
deprecate_no_replacement(
"the encryption_key parameter of write_to_stream", "5.0.0"
)
stream.write(self.renumber()) # b_(renumber(self)))

def writeToStream(
self, stream: StreamType, encryption_key: Union[None, str, bytes]
) -> None: # deprecated
deprecation_with_replacement("writeToStream", "write_to_stream", "3.0.0")
self.write_to_stream(stream, encryption_key)
self.write_to_stream(stream)

def renumber(self) -> bytes:
out = self[0].encode("utf-8")
Expand Down
37 changes: 27 additions & 10 deletions pypdf/generic/_data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
WHITESPACES,
StreamType,
b_,
deprecate_no_replacement,
deprecate_with_replacement,
deprecation_with_replacement,
logger_warning,
Expand Down Expand Up @@ -112,17 +113,21 @@ def items(self) -> Iterable[Any]:
def write_to_stream(
self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
) -> None:
if encryption_key is not None: # deprecated
deprecate_no_replacement(
"the encryption_key parameter of write_to_stream", "5.0.0"
)
stream.write(b"[")
for data in self:
stream.write(b" ")
data.write_to_stream(stream, encryption_key)
data.write_to_stream(stream)
stream.write(b" ]")

def writeToStream(
self, stream: StreamType, encryption_key: Union[None, str, bytes]
) -> None: # deprecated
deprecation_with_replacement("writeToStream", "write_to_stream", "3.0.0")
self.write_to_stream(stream, encryption_key)
self.write_to_stream(stream)

@staticmethod
def read_from_stream(
Expand Down Expand Up @@ -353,19 +358,23 @@ def xmpMetadata(self) -> Optional[PdfObject]: # deprecated
def write_to_stream(
self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
) -> None:
if encryption_key is not None: # deprecated
deprecate_no_replacement(
"the encryption_key parameter of write_to_stream", "5.0.0"
)
stream.write(b"<<\n")
for key, value in list(self.items()):
key.write_to_stream(stream, encryption_key)
key.write_to_stream(stream)
stream.write(b" ")
value.write_to_stream(stream, encryption_key)
value.write_to_stream(stream)
stream.write(b"\n")
stream.write(b">>")

def writeToStream(
self, stream: StreamType, encryption_key: Union[None, str, bytes]
) -> None: # deprecated
deprecation_with_replacement("writeToStream", "write_to_stream", "3.0.0")
self.write_to_stream(stream, encryption_key)
self.write_to_stream(stream)

@staticmethod
def read_from_stream(
Expand Down Expand Up @@ -798,8 +807,12 @@ def _data(self, value: Any) -> None:
def write_to_stream(
self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
) -> None:
if encryption_key is not None: # deprecated
deprecate_no_replacement(
"the encryption_key parameter of write_to_stream", "5.0.0"
)
self[NameObject(SA.LENGTH)] = NumberObject(len(self._data))
DictionaryObject.write_to_stream(self, stream, encryption_key)
DictionaryObject.write_to_stream(self, stream)
del self[SA.LENGTH]
stream.write(b"\nstream\n")
stream.write(self._data)
Expand Down Expand Up @@ -1449,18 +1462,22 @@ def getDestArray(self) -> "ArrayObject": # deprecated
def write_to_stream(
self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
) -> None:
if encryption_key is not None: # deprecated
deprecate_no_replacement(
"the encryption_key parameter of write_to_stream", "5.0.0"
)
stream.write(b"<<\n")
key = NameObject("/D")
key.write_to_stream(stream, encryption_key)
key.write_to_stream(stream)
stream.write(b" ")
value = self.dest_array
value.write_to_stream(stream, encryption_key)
value.write_to_stream(stream)

key = NameObject("/S")
key.write_to_stream(stream, encryption_key)
key.write_to_stream(stream)
stream.write(b" ")
value_s = NameObject("/GoTo")
value_s.write_to_stream(stream, encryption_key)
value_s.write_to_stream(stream)

stream.write(b"\n")
stream.write(b">>")
Expand Down
14 changes: 9 additions & 5 deletions pypdf/generic/_outline.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any, Union

from .._utils import StreamType, deprecation_with_replacement
from .._utils import StreamType, deprecate_no_replacement, deprecation_with_replacement
from ._base import NameObject
from ._data_structures import Destination

Expand All @@ -9,22 +9,26 @@ class OutlineItem(Destination):
def write_to_stream(
self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
) -> None:
if encryption_key is not None: # deprecated
deprecate_no_replacement(
"the encryption_key parameter of write_to_stream", "5.0.0"
)
stream.write(b"<<\n")
for key in [
NameObject(x)
for x in ["/Title", "/Parent", "/First", "/Last", "/Next", "/Prev"]
if x in self
]:
key.write_to_stream(stream, encryption_key)
key.write_to_stream(stream)
stream.write(b" ")
value = self.raw_get(key)
value.write_to_stream(stream, encryption_key)
value.write_to_stream(stream)
stream.write(b"\n")
key = NameObject("/Dest")
key.write_to_stream(stream, encryption_key)
key.write_to_stream(stream)
stream.write(b" ")
value = self.dest_array
value.write_to_stream(stream, encryption_key)
value.write_to_stream(stream)
stream.write(b"\n")
stream.write(b">>")

Expand Down
9 changes: 7 additions & 2 deletions pypdf/xmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from ._utils import (
StreamType,
deprecate_no_replacement,
deprecate_with_replacement,
deprecation_with_replacement,
)
Expand Down Expand Up @@ -234,7 +235,11 @@ def rdfRoot(self) -> XmlElement: # deprecated
def write_to_stream(
self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
) -> None:
self.stream.write_to_stream(stream, encryption_key)
if encryption_key is not None: # deprecated
deprecate_no_replacement(
"the encryption_key parameter of write_to_stream", "5.0.0"
)
self.stream.write_to_stream(stream)

def writeToStream(
self, stream: StreamType, encryption_key: Union[None, str, bytes]
Expand All @@ -245,7 +250,7 @@ def writeToStream(
.. deprecated:: 1.28.0
"""
deprecation_with_replacement("writeToStream", "write_to_stream", "3.0.0")
self.write_to_stream(stream, encryption_key)
self.write_to_stream(stream)

def get_element(self, about_uri: str, namespace: str, name: str) -> Iterator[Any]:
for desc in self.rdf_root.getElementsByTagNameNS(RDF_NAMESPACE, "Description"):
Expand Down
Loading

0 comments on commit 2b47d9a

Please sign in to comment.