Skip to content

Commit 1fcb40a

Browse files
committed
Remove to_native/to_unicode calls from message, signers, utils
These were Python 2 compatibility helpers. On Python 3: - to_native(s) where s is str → noop, removed - to_native(s) where s is bytes → replaced with s.decode() - to_unicode(s) where s is str → noop, removed - to_unicode(value) for non-str → replaced with explicit bytes.decode() / str() branching Functions kept in utils.py for now (still used by loader/).
1 parent 9b5b220 commit 1fcb40a

3 files changed

Lines changed: 7 additions & 11 deletions

File tree

emails/message.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from email.utils import getaddresses
77
from typing import Any, IO
88

9-
from .utils import (formataddr, to_unicode, to_native,
9+
from .utils import (formataddr,
1010
SafeMIMEText, SafeMIMEMultipart, sanitize_address,
1111
parse_name_and_email, load_email_charsets,
1212
encode_header as encode_header_,
@@ -231,7 +231,7 @@ def set_header(self, msg: SafeMIMEMultipart, key: str,
231231
return
232232

233233
if not isinstance(value, str):
234-
value = to_unicode(value)
234+
value = value.decode() if isinstance(value, bytes) else str(value)
235235

236236
# Prevent header injection
237237
if '\n' in value or '\r' in value:
@@ -345,7 +345,7 @@ def as_string(self, message_cls: type | None = None) -> str:
345345
Note: this method costs one less message-to-string conversions
346346
for dkim in compare to self.as_message().as_string()
347347
"""
348-
r = to_native(self.build_message(message_cls=message_cls).as_string())
348+
r = self.build_message(message_cls=message_cls).as_string()
349349
if self._signer:
350350
r = self.sign_string(r)
351351
return r

emails/signers.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .packages import dkim
1010
from .packages.dkim import DKIMException, UnparsableKeyError
1111
from .packages.dkim.crypto import parse_pem_private_key
12-
from .utils import to_bytes, to_native
12+
from .utils import to_bytes
1313

1414

1515
class DKIMSigner:
@@ -61,9 +61,7 @@ def get_sign_header(self, message: bytes) -> tuple[str, str] | None:
6161
# pydkim returns string, so we should split
6262
s = self.get_sign_string(message)
6363
if s:
64-
native = to_native(s)
65-
assert native is not None # s is bytes, to_native always returns str
66-
(header, value) = native.split(': ', 1)
64+
(header, value) = s.decode().split(': ', 1)
6765
if value.endswith("\r\n"):
6866
value = value[:-2]
6967
return header, value
@@ -98,9 +96,7 @@ def sign_message_string(self, message_string: str) -> str:
9896
assert msg_bytes is not None
9997
s = self.get_sign_string(msg_bytes)
10098
if s:
101-
header = to_native(s)
102-
assert header is not None
103-
return header + message_string
99+
return s.decode() + message_string
104100
return message_string
105101

106102
def sign_message_bytes(self, message_bytes: bytes) -> bytes:

emails/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def parse_name_and_email(obj: str | tuple[str | None, str] | list[str],
219219
else:
220220
raise ValueError("Can not parse_name_and_email from %s" % obj)
221221

222-
return to_unicode(name, encoding) or None, to_unicode(email, encoding) or None
222+
return name or None, email or None
223223

224224

225225
def sanitize_email(addr: str, encoding: str = 'ascii', parse: bool = False) -> str:

0 commit comments

Comments
 (0)