Only alias the imports for vendored dependencies
authorIan Cordasco <graffatcolmingov@gmail.com>
Sun, 1 Mar 2015 02:13:45 +0000 (20:13 -0600)
committerIan Cordasco <graffatcolmingov@gmail.com>
Sun, 1 Mar 2015 02:13:46 +0000 (20:13 -0600)
While discussion the issue, Donald Stufft (@dstufft) and I realized the
simplest solution is to simply add an alias per vendored dependency. The
resulting changes are simple and effective. It prevents the issue in
2.5.2 and 2.5.3 where the following would work:

    from requests.packages import webbrowser

This now appropriately raises an ImportError.

Closes #2465

requests/packages/__init__.py

index ec6a9e0646d6c1122bbd98858889ce6523f1f9f0..7b2a09138262b90423659d3ef849dce8aa4a3248 100644 (file)
@@ -27,9 +27,10 @@ import sys
 
 class VendorAlias(object):
 
-    def __init__(self):
+    def __init__(self, package_name):
+        self._package_name = package_name
         self._vendor_name = __name__
-        self._vendor_pkg = self._vendor_name + "."
+        self._vendor_pkg = self._vendor_name + "." + self._package_name
 
     def find_module(self, fullname, path=None):
         if fullname.startswith(self._vendor_pkg):
@@ -92,4 +93,4 @@ class VendorAlias(object):
         return module
 
 
-sys.meta_path.append(VendorAlias())
+sys.meta_path.extend([VendorAlias("urllib3"), VendorAlias("chardet")])