Skip to content
Myr

Fix when packages are unvendored

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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

authored by Ian Cordasco · committed by Ian Cordasco · parent 9d8a57f · Browse files · 1 file+15 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+15 added−4 removed

  • other:+15 added−4 removed

1 file+15 added−4 removed

modified:requests/packages/__init__.py+15 added-4 removed
25 unmodified lines
262728class VendorAlias(object):2930 def __init__(self, package_name):31 self._package_name = package_name30 def __init__(self, package_names):31 self._package_names = package_names32 self._vendor_name = __name__33 self._vendor_pkg = self._vendor_name + "." + self._package_name33 self._vendor_pkg = self._vendor_name + "."34 self._vendor_pkgs = [35 self._vendor_pkg + name for name in self._package_names36 ]3738 def find_module(self, fullname, path=None):39 if fullname.startswith(self._vendor_pkg):40 return self
6 unmodified lines
47 name, self._vendor_name,48 )49 )5051 if not (name == self._vendor_name or52 any(name.startswith(pkg) for pkg in self._vendor_pkgs)):53 raise ImportError(54 "Cannot import %s, must be one of %s." % (55 name, self._vendor_pkgs56 )57 )5859 # Check to see if we already have this item in sys.modules, if we do60 # then simply return that.61 if name in sys.modules:62 return sys.modules[name]
40 unmodified lines
103 # Finally, return the loaded module104 return module10510696sys.meta_path.extend([VendorAlias("urllib3"), VendorAlias("chardet")])107sys.meta_path.append(VendorAlias(["urllib3", "chardet"]))