Skip to content

Commit 7137186

Browse files
committed
Remove emails.compat module entirely
Move to_unicode, to_native, to_bytes, formataddr to emails.utils. Replace all compat aliases with direct stdlib imports: - string_types -> str - text_type -> str - is_callable -> callable() - urlparse -> urllib.parse - NativeStringIO/StringIO/BytesIO -> io - OrderedDict -> collections Delete emails/compat package and remove from setup.py. All 74 tests pass.
1 parent 998da0e commit 7137186

16 files changed

Lines changed: 89 additions & 106 deletions

File tree

emails/compat/__init__.py

Lines changed: 0 additions & 59 deletions
This file was deleted.

emails/compat/_urlparse.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

emails/loader/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
import os.path
33
from email.utils import formataddr
44

5-
from ..compat import to_unicode, to_native
6-
from ..compat import urlparse
5+
import urllib.parse as urlparse
6+
7+
from ..utils import to_unicode, to_native
78
from ..message import Message
89
from ..utils import fetch_url
910
from .local_store import (FileSystemLoader, ZipLoader, MsgLoader, FileNotFound)

emails/loader/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
except ImportError:
1414
import chardet
1515

16-
from ..compat import to_native, to_unicode
16+
from ..utils import to_native, to_unicode
1717

1818
# HTML page charset stuff
1919

emails/loader/local_store.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
from zipfile import ZipFile
1010
import email
1111

12-
from ..compat import to_unicode, string_types, to_native, formataddr as compat_formataddr
13-
12+
from ..utils import to_unicode, to_native, formataddr, decode_header
1413
from ..loader.helpers import decode_text
1514
from ..message import Message
16-
from ..utils import decode_header
1715

1816
class FileNotFound(Exception):
1917
pass
@@ -127,7 +125,7 @@ class FileSystemLoader(BaseLoader):
127125
"""
128126

129127
def __init__(self, searchpath, encoding='utf-8', base_path=None):
130-
if isinstance(searchpath, string_types):
128+
if isinstance(searchpath, str):
131129
searchpath = [searchpath]
132130
self.searchpath = list(searchpath)
133131
self.encoding = encoding
@@ -229,7 +227,7 @@ class MsgLoader(BaseLoader):
229227
common_charsets = ['ascii', 'utf-8', 'utf-16', 'windows-1252', 'cp850', 'windows-1251']
230228

231229
def __init__(self, msg, base_path=None):
232-
if isinstance(msg, string_types):
230+
if isinstance(msg, str):
233231
self.msg = email.message_from_string(msg)
234232
elif isinstance(msg, bytes):
235233
self.msg = email.message_from_string(to_native(msg))
@@ -366,7 +364,7 @@ def decode_address_header_value(self, value, skip_invalid=False):
366364
if not skip_invalid:
367365
r.append(decode_header(email))
368366
else:
369-
r.append(compat_formataddr([decode_header(name), email]))
367+
r.append(formataddr([decode_header(name), email]))
370368

371369
return r
372370

emails/message.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from email.utils import getaddresses
44

5-
from .compat import (string_types, is_callable, formataddr as compat_formataddr, to_unicode, to_native)
6-
from .utils import (SafeMIMEText, SafeMIMEMultipart, sanitize_address,
5+
from .utils import (formataddr, to_unicode, to_native,
6+
SafeMIMEText, SafeMIMEMultipart, sanitize_address,
77
parse_name_and_email, load_email_charsets,
88
encode_header as encode_header_,
99
renderable, format_date_header, parse_name_and_email_list,
@@ -156,9 +156,9 @@ def get_date(self):
156156
v = self._date
157157
if v is False:
158158
return None
159-
if is_callable(v):
159+
if callable(v):
160160
v = v()
161-
if not isinstance(v, string_types):
161+
if not isinstance(v, str):
162162
v = format_date_header(v)
163163
return v
164164

@@ -170,7 +170,7 @@ def message_id(self):
170170
mid = self._message_id
171171
if mid is False:
172172
return None
173-
return is_callable(mid) and mid() or mid
173+
return callable(mid) and mid() or mid
174174

175175
@message_id.setter
176176
def message_id(self, value):
@@ -210,7 +210,7 @@ def encode_address_header(self, pair):
210210
if not pair:
211211
return None
212212
name, email = pair
213-
return compat_formataddr((name or '', email))
213+
return formataddr((name or '', email))
214214

215215
encode_name_header = encode_address_header # legacy name
216216

@@ -220,7 +220,7 @@ def set_header(self, msg, key, value, encode=True):
220220
# TODO: may be remove header here ?
221221
return
222222

223-
if not isinstance(value, string_types):
223+
if not isinstance(value, str):
224224
value = to_unicode(value)
225225

226226
# Prevent header injection

emails/signers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .packages import dkim
1111
from .packages.dkim import DKIMException, UnparsableKeyError
1212
from .packages.dkim.crypto import parse_pem_private_key
13-
from .compat import to_bytes, to_native
13+
from .utils import to_bytes, to_native
1414

1515

1616
class DKIMSigner:

emails/store/file.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
from email.encoders import encode_base64
77
from os.path import basename
88

9-
from ..compat import urlparse, string_types, to_bytes
10-
from ..utils import fetch_url, encode_header
9+
import urllib.parse as urlparse
10+
11+
from ..utils import fetch_url, encode_header, to_bytes
1112

1213

1314
MIMETYPE_UNKNOWN = 'application/unknown'
@@ -50,7 +51,7 @@ def as_dict(self, fields=None):
5051

5152
def get_data(self):
5253
_data = getattr(self, '_data', None)
53-
if isinstance(_data, string_types):
54+
if isinstance(_data, str):
5455
return _data
5556
elif hasattr(_data, 'read'):
5657
return _data.read()

emails/store/store.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# encoding: utf-8
22
from os.path import splitext
33

4-
from ..compat import OrderedDict, string_types
4+
from collections import OrderedDict
5+
56
from .file import BaseFile
67

78

@@ -22,7 +23,7 @@ def __init__(self, file_cls=None):
2223
def __contains__(self, k):
2324
if isinstance(k, self.file_cls):
2425
return k.uri in self._files
25-
elif isinstance(k, string_types):
26+
elif isinstance(k, str):
2627
return k in self._files
2728
else:
2829
return False
@@ -41,7 +42,7 @@ def remove(self, uri):
4142
if isinstance(uri, self.file_cls):
4243
uri = uri.uri
4344

44-
assert isinstance(uri, string_types)
45+
assert isinstance(uri, str)
4546

4647
v = self[uri]
4748
if v:

emails/testsuite/loader/test_rfc822_loader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import email
44
import datetime
55
import os.path
6-
from emails.compat import to_native
6+
from emails.utils import to_native
77
import emails.loader
88
from emails.loader.local_store import MsgLoader
99

0 commit comments

Comments
 (0)