Skip to content

Commit 73651fd

Browse files
committed
Move most of the compatibility functionality to _compat
1 parent 647cf3b commit 73651fd

File tree

2 files changed

+71
-40
lines changed

2 files changed

+71
-40
lines changed

importlib_metadata/__init__.py

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,27 @@
66
import csv
77
import sys
88
import zipp
9-
import email
109
import operator
1110
import functools
1211
import itertools
1312
import collections
1413

15-
from ._compat import install, NullFinder
14+
from ._compat import (
15+
install,
16+
NullFinder,
17+
ConfigParser,
18+
suppress,
19+
map,
20+
FileNotFoundError,
21+
NotADirectoryError,
22+
pathlib,
23+
ModuleNotFoundError,
24+
MetaPathFinder,
25+
email_message_from_string,
26+
)
1627
from importlib import import_module
1728
from itertools import starmap
1829

19-
if sys.version_info > (3,): # pragma: nocover
20-
from configparser import ConfigParser
21-
from contextlib import suppress
22-
else: # pragma: nocover
23-
from backports.configparser import ConfigParser
24-
from itertools import imap as map # type: ignore
25-
from contextlib2 import suppress # noqa
26-
FileNotFoundError = IOError, OSError
27-
NotADirectoryError = IOError, OSError
28-
29-
if sys.version_info > (3, 5): # pragma: nocover
30-
import pathlib
31-
else: # pragma: nocover
32-
import pathlib2 as pathlib
33-
34-
try:
35-
BaseClass = ModuleNotFoundError
36-
except NameError: # pragma: nocover
37-
BaseClass = ImportError # type: ignore
38-
39-
40-
if sys.version_info >= (3,): # pragma: nocover
41-
from importlib.abc import MetaPathFinder
42-
else: # pragma: nocover
43-
class MetaPathFinder(object):
44-
__metaclass__ = abc.ABCMeta
45-
4630

4731
__metaclass__ = type
4832

@@ -60,7 +44,7 @@ class MetaPathFinder(object):
6044
]
6145

6246

63-
class PackageNotFoundError(BaseClass):
47+
class PackageNotFoundError(ModuleNotFoundError):
6448
"""The package was not found."""
6549

6650

@@ -232,7 +216,7 @@ def metadata(self):
232216
# (which points to the egg-info file) attribute unchanged.
233217
or self.read_text('')
234218
)
235-
return _email_message_from_string(text)
219+
return email_message_from_string(text)
236220

237221
@property
238222
def version(self):
@@ -411,15 +395,6 @@ def locate_file(self, path):
411395
return self._path.parent / path
412396

413397

414-
def _email_message_from_string(text):
415-
# Work around https://bugs.python.org/issue25545 where
416-
# email.message_from_string cannot handle Unicode on Python 2.
417-
if sys.version_info < (3,): # nocoverpy3
418-
io_buffer = io.StringIO(text)
419-
return email.message_from_file(io_buffer)
420-
return email.message_from_string(text) # nocoverpy2
421-
422-
423398
def distribution(package):
424399
"""Get the ``Distribution`` instance for the given package.
425400

importlib_metadata/_compat.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,49 @@
1+
from __future__ import absolute_import
2+
3+
import io
4+
import abc
15
import sys
6+
import email
7+
8+
9+
if sys.version_info > (3,): # pragma: nocover
10+
import builtins
11+
from configparser import ConfigParser
12+
from contextlib import suppress
13+
FileNotFoundError = builtins.FileNotFoundError
14+
NotADirectoryError = builtins.NotADirectoryError
15+
map = builtins.map
16+
else: # pragma: nocover
17+
from backports.configparser import ConfigParser
18+
from itertools import imap as map # type: ignore
19+
from contextlib2 import suppress # noqa
20+
FileNotFoundError = IOError, OSError
21+
NotADirectoryError = IOError, OSError
22+
23+
if sys.version_info > (3, 5): # pragma: nocover
24+
import pathlib
25+
else: # pragma: nocover
26+
import pathlib2 as pathlib
27+
28+
try:
29+
ModuleNotFoundError = builtins.FileNotFoundError
30+
except (NameError, AttributeError): # pragma: nocover
31+
ModuleNotFoundError = ImportError # type: ignore
32+
33+
34+
if sys.version_info >= (3,): # pragma: nocover
35+
from importlib.abc import MetaPathFinder
36+
else: # pragma: nocover
37+
class MetaPathFinder(object):
38+
__metaclass__ = abc.ABCMeta
239

340

441
__metaclass__ = type
42+
__all__ = [
43+
'install', 'NullFinder', 'MetaPathFinder', 'ModuleNotFoundError',
44+
'pathlib', 'ConfigParser', 'map', 'suppress', 'FileNotFoundError',
45+
'NotADirectoryError', 'email_message_from_string',
46+
]
547

648

749
def install(cls):
@@ -26,3 +68,17 @@ def find_spec(*args, **kwargs):
2668
# on sys.meta_path but having no other import
2769
# system functionality), the two methods are identical.
2870
find_module = find_spec
71+
72+
73+
def py2_message_from_string(text):
74+
# Work around https://bugs.python.org/issue25545 where
75+
# email.message_from_string cannot handle Unicode on Python 2.
76+
io_buffer = io.StringIO(text)
77+
return email.message_from_file(io_buffer)
78+
79+
80+
email_message_from_string = (
81+
py2_message_from_string
82+
if sys.version_info < (3,) else
83+
email.message_from_string
84+
)

0 commit comments

Comments
 (0)