Skip to content
Myr

Remove VendorAlias meta_path hook

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

authored by Ian Cordasco · committed by Ian Cordasco · parent 3d339ed · Browse files · 1 file+1 added−105 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+1 added−105 removed

  • other:+1 added−105 removed

1 file+1 added−105 removed

modified:requests/packages/__init__.py+1 added-105 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"""1from __future__ import absolute_import225import sys262728class VendorAlias(object):2930 def __init__(self, package_names):31 self._package_names = package_names32 self._vendor_name = __name__33 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 self4142 def load_module(self, name):43 # Ensure that this only works for the vendored name44 if not name.startswith(self._vendor_pkg):45 raise ImportError(46 "Cannot import %s, must be a subpackage of '%s'." % (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]6364 # Check to see if we can import the vendor name65 try:66 # We do this dance here because we want to try and import this67 # module without hitting a recursion error because of a bunch of68 # VendorAlias instances on sys.meta_path69 real_meta_path = sys.meta_path[:]70 try:71 sys.meta_path = [72 m for m in sys.meta_path73 if not isinstance(m, VendorAlias)74 ]75 __import__(name)76 module = sys.modules[name]77 finally:78 # Re-add any additions to sys.meta_path that were made while79 # during the import we just did, otherwise things like80 # requests.packages.urllib3.poolmanager will fail.81 for m in sys.meta_path:82 if m not in real_meta_path:83 real_meta_path.append(m)8485 # Restore sys.meta_path with any new items.86 sys.meta_path = real_meta_path87 except ImportError:88 # We can't import the vendor name, so we'll try to import the89 # "real" name.90 real_name = name[len(self._vendor_pkg):]91 try:92 __import__(real_name)93 module = sys.modules[real_name]94 except ImportError:95 raise ImportError("No module named '%s'" % (name,))9697 # If we've gotten here we've found the module we're looking for, either98 # as part of our vendored package, or as the real name, so we'll add99 # it to sys.modules as the vendored name so that we don't have to do100 # the lookup again.101 sys.modules[name] = module102103 # Finally, return the loaded module104 return module105106107sys.meta_path.insert(0, VendorAlias(["urllib3", "chardet"]))3from . import urllib3