Fix when packages are unvendored

When working these changes back upstream to pip, we realized that the
previous fix wasn't ideal since unvendoring the packages broke the
imports. For example, if urllib3 were unvendored, then the following
would fail:

    from requests.packages import urllib3
This commit is contained in:
Ian Cordasco
2015-02-28 23:04:22 -06:00
parent 9d8a57f2a2
commit 2669f0c0b1
+15 -4
View File
@@ -27,10 +27,13 @@ import sys
class VendorAlias(object):
def __init__(self, package_name):
self._package_name = package_name
def __init__(self, package_names):
self._package_names = package_names
self._vendor_name = __name__
self._vendor_pkg = self._vendor_name + "." + self._package_name
self._vendor_pkg = self._vendor_name + "."
self._vendor_pkgs = [
self._vendor_pkg + name for name in self._package_names
]
def find_module(self, fullname, path=None):
if fullname.startswith(self._vendor_pkg):
@@ -45,6 +48,14 @@ class VendorAlias(object):
)
)
if not (name == self._vendor_name or
any(name.startswith(pkg) for pkg in self._vendor_pkgs)):
raise ImportError(
"Cannot import %s, must be one of %s." % (
name, self._vendor_pkgs
)
)
# Check to see if we already have this item in sys.modules, if we do
# then simply return that.
if name in sys.modules:
@@ -93,4 +104,4 @@ class VendorAlias(object):
return module
sys.meta_path.extend([VendorAlias("urllib3"), VendorAlias("chardet")])
sys.meta_path.append(VendorAlias(["urllib3", "chardet"]))