diff --git a/wakatime/packages/requests/models.py b/wakatime/packages/requests/models.py index 4b2648f..ce1eb40 100644 --- a/wakatime/packages/requests/models.py +++ b/wakatime/packages/requests/models.py @@ -688,6 +688,8 @@ class Response(object): """Iterates over the response data, one line at a time. When stream=True is set on the request, this avoids reading the content at once into memory for large responses. + + .. note:: This method is not reentrant safe. """ pending = None diff --git a/wakatime/packages/requests/packages/__init__.py b/wakatime/packages/requests/packages/__init__.py index ec6a9e0..4dcf870 100644 --- a/wakatime/packages/requests/packages/__init__.py +++ b/wakatime/packages/requests/packages/__init__.py @@ -27,9 +27,13 @@ import sys class VendorAlias(object): - def __init__(self): + def __init__(self, package_names): + self._package_names = package_names self._vendor_name = __name__ self._vendor_pkg = self._vendor_name + "." + self._vendor_pkgs = [ + self._vendor_pkg + name for name in self._package_names + ] def find_module(self, fullname, path=None): if fullname.startswith(self._vendor_pkg): @@ -44,6 +48,14 @@ class VendorAlias(object): ) ) + if not (name == self._vendor_name or + any(name.startswith(pkg) for pkg in self._vendor_pkgs)): + raise ImportError( + "Cannot import %s, must be one of %s." % ( + name, self._vendor_pkgs + ) + ) + # Check to see if we already have this item in sys.modules, if we do # then simply return that. if name in sys.modules: @@ -92,4 +104,4 @@ class VendorAlias(object): return module -sys.meta_path.append(VendorAlias()) +sys.meta_path.append(VendorAlias(["urllib3", "chardet"]))