Skip to content
Myr

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

1 file+93 added−1 removed

modified:requests/packages/__init__.py+93 added-1 removed
1"""2Copyright (c) Donald Stufft, pip, and individual contributors34Permission is hereby granted, free of charge, to any person obtaining5a copy of this software and associated documentation files (the6"Software"), to deal in the Software without restriction, including7without limitation the rights to use, copy, modify, merge, publish,8distribute, sublicense, and/or sell copies of the Software, and to9permit persons to whom the Software is furnished to do so, subject to10the following conditions:1112The above copyright notice and this permission notice shall be13included in all copies or substantial portions of the Software.1415THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,16EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF17MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND18NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE19LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION20OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION21WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.22"""23from __future__ import absolute_import243from . import urllib325import sys262728class VendorAlias(object):2930 def __init__(self):31 self._vendor_name = __name__32 self._vendor_pkg = self._vendor_name + "."3334 def find_module(self, fullname, path=None):35 if fullname.startswith(self._vendor_pkg):36 return self3738 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 )4647 # 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]5152 # 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)7273 # 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,))8485 # 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] = module9091 # Finally, return the loaded module92 return module939495sys.meta_path.append(VendorAlias())