summaryrefslogtreecommitdiff
path: root/pkg_resources
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2015-12-31 16:47:55 -0500
committerJason R. Coombs <jaraco@jaraco.com>2015-12-31 16:47:55 -0500
commitd8bf8413cd5b07690fd515498bc074592fae38a5 (patch)
tree20c2bfb84badebc4978e166a003c0b08c027ed74 /pkg_resources
parentcac10881a6f2c2a9d3e76bcca3918b8065d9b3eb (diff)
downloadpython-setuptools-bitbucket-d8bf8413cd5b07690fd515498bc074592fae38a5.tar.gz
Use the same technique in pkg_resources, relying on an 'extern' module to resolve the conditional import.
Diffstat (limited to 'pkg_resources')
-rw-r--r--pkg_resources/__init__.py13
-rw-r--r--pkg_resources/extern/__init__.py0
-rw-r--r--pkg_resources/extern/packaging.py45
-rw-r--r--pkg_resources/tests/test_resources.py3
4 files changed, 50 insertions, 11 deletions
diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py
index b55e4127..82382962 100644
--- a/pkg_resources/__init__.py
+++ b/pkg_resources/__init__.py
@@ -87,15 +87,10 @@ try:
except ImportError:
pass
-try:
- import pkg_resources._vendor.packaging.version
- import pkg_resources._vendor.packaging.specifiers
- packaging = pkg_resources._vendor.packaging
-except ImportError:
- # fallback to naturally-installed version; allows system packagers to
- # omit vendored packages.
- import packaging.version
- import packaging.specifiers
+
+from pkg_resources.extern import packaging
+__import__('pkg_resources.extern.packaging.version')
+__import__('pkg_resources.extern.packaging.specifiers')
if (3, 0) < sys.version_info < (3, 3):
diff --git a/pkg_resources/extern/__init__.py b/pkg_resources/extern/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/pkg_resources/extern/__init__.py
diff --git a/pkg_resources/extern/packaging.py b/pkg_resources/extern/packaging.py
new file mode 100644
index 00000000..47f58eab
--- /dev/null
+++ b/pkg_resources/extern/packaging.py
@@ -0,0 +1,45 @@
+"""
+Handle loading a package from system or from the bundled copy
+"""
+
+import imp
+
+
+_SEARCH_PATH = ['pkg_resources._vendor.packaging', 'packaging']
+
+
+def _find_module(name, path=None):
+ """
+ Alternative to `imp.find_module` that can also search in subpackages.
+ """
+
+ parts = name.split('.')
+
+ for part in parts:
+ if path is not None:
+ path = [path]
+
+ fh, path, descr = imp.find_module(part, path)
+
+ return fh, path, descr
+
+
+def _import_in_place(search_path=_SEARCH_PATH):
+ for mod_name in search_path:
+ try:
+ mod_info = _find_module(mod_name)
+ except ImportError:
+ continue
+
+ imp.load_module(__name__, *mod_info)
+ break
+
+ else:
+ raise ImportError(
+ "The '{name}' package is required; "
+ "normally this is bundled with this package so if you get "
+ "this warning, consult the packager of your "
+ "distribution.".format(name=_SEARCH_PATH[-1]))
+
+
+_import_in_place()
diff --git a/pkg_resources/tests/test_resources.py b/pkg_resources/tests/test_resources.py
index 9fda892f..141d63ad 100644
--- a/pkg_resources/tests/test_resources.py
+++ b/pkg_resources/tests/test_resources.py
@@ -5,14 +5,13 @@ import shutil
import string
import pytest
+from pkg_resources.extern import packaging
import pkg_resources
from pkg_resources import (parse_requirements, VersionConflict, parse_version,
Distribution, EntryPoint, Requirement, safe_version, safe_name,
WorkingSet)
-packaging = pkg_resources.packaging
-
def safe_repr(obj, short=False):
""" copied from Python2.7"""