Merge pull request #2375 from sigmavirus24/fix-imports-when-unbundled
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Copy pip's import machinery wholesale
authored by Kenneth Reitz · committed by Kenneth Reitz · parents b176ccd, da82fb4 · Browse files · 1 file·+93 added−1 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+93 added−1 removed
- other:+93 added−1 removed
Provenance
- Commit
a57eacf- Tree
377ef72- Author
Kenneth Reitz <me@kennethreitz.org>- Committer
Kenneth Reitz <me@kennethreitz.org>- Parent 1
b176ccd- Parent 2
da82fb4
1 file changed+93 added−1 removed
modified:requests/packages/__init__.py+93 added-1 removed
1+"""2+Copyright (c) Donald Stufft, pip, and individual contributors3+4+Permission is hereby granted, free of charge, to any person obtaining5+a copy of this software and associated documentation files (the6+"Software"), to deal in the Software without restriction, including7+without limitation the rights to use, copy, modify, merge, publish,8+distribute, sublicense, and/or sell copies of the Software, and to9+permit persons to whom the Software is furnished to do so, subject to10+the following conditions:11+12+The above copyright notice and this permission notice shall be13+included in all copies or substantial portions of the Software.14+15+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,16+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF17+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND18+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE19+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION20+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION21+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.22+"""23 from __future__ import absolute_import24 3-from . import urllib325+import sys26+27+28+class VendorAlias(object):29+30+ def __init__(self):31+ self._vendor_name = __name__32+ self._vendor_pkg = self._vendor_name + "."33+34+ def find_module(self, fullname, path=None):35+ if fullname.startswith(self._vendor_pkg):36+ return self37+38+ def load_module(self, name):39+ # Ensure that this only works for the vendored name40+ if not name.startswith(self._vendor_pkg):41+ raise ImportError(42+ "Cannot import %s, must be a subpackage of '%s'." % (43+ name, self._vendor_name,44+ )45+ )46+47+ # Check to see if we already have this item in sys.modules, if we do48+ # then simply return that.49+ if name in sys.modules:50+ return sys.modules[name]51+52+ # Check to see if we can import the vendor name53+ try:54+ # We do this dance here because we want to try and import this55+ # module without hitting a recursion error because of a bunch of56+ # VendorAlias instances on sys.meta_path57+ real_meta_path = sys.meta_path[:]58+ try:59+ sys.meta_path = [60+ m for m in sys.meta_path61+ if not isinstance(m, VendorAlias)62+ ]63+ __import__(name)64+ module = sys.modules[name]65+ finally:66+ # Re-add any additions to sys.meta_path that were made while67+ # during the import we just did, otherwise things like68+ # requests.packages.urllib3.poolmanager will fail.69+ for m in sys.meta_path:70+ if m not in real_meta_path:71+ real_meta_path.append(m)72+73+ # Restore sys.meta_path with any new items.74+ sys.meta_path = real_meta_path75+ except ImportError:76+ # We can't import the vendor name, so we'll try to import the77+ # "real" name.78+ real_name = name[len(self._vendor_pkg):]79+ try:80+ __import__(real_name)81+ module = sys.modules[real_name]82+ except ImportError:83+ raise ImportError("No module named '%s'" % (name,))84+85+ # If we've gotten here we've found the module we're looking for, either86+ # as part of our vendored package, or as the real name, so we'll add87+ # it to sys.modules as the vendored name so that we don't have to do88+ # the lookup again.89+ sys.modules[name] = module90+91+ # Finally, return the loaded module92+ return module93+94+95+sys.meta_path.append(VendorAlias())