-
Notifications
You must be signed in to change notification settings - Fork 321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issues when importing requests with import_patched after importing normally #7
Comments
Had the same issue. Temporarily fixed it by converting the relative "from . import utils" in requests/init.py to "import utils". |
@pjwerneck Beware that this fix would break importing a "real" package named |
Indded, that's why I'm not sending a pull request for it, but I can't wait for a proper solution from eventlet now. |
Same issue here. |
Yup, there's no doubt the bug exists. It's just entirely unclear what's the cause and, consequently, how to fix it. |
Not having checked this but it might be worth looking at replacing the Note that there is more wrong with the patcher, it would still be buggy if the above fixes this issue. Problem is that the patcher does not save/restore enough state when importing packages rather then modules and you very easily end up with mixed packages, e.g. after import_patched you'd both the patched and unpatched |
What about using import hooks?
|
I've investigated this issue further, and reached the following conclusion after reproducing the issue in a patcher unit test. It appears that the reason patcher.import_patched explodes on requests is because it cannot handle module hoisting as requests does in # file: requests/__init__.py
from . import utils
from .models import Request, Response, PreparedRequest
from .api import request, get, head, post, patch, put, delete, options
from .sessions import session, Session
from .status_codes import codes
from .exceptions import (
RequestException, Timeout, URLRequired,
TooManyRedirects, HTTPError, ConnectionError
) One work around that may work from the application side (I've not extensively tested it) is to do: import eventlet
requests = eventlet.import_patched('requests.__init__') However, that's still a workaround. The solution to the problem in general may be to have For the interested, here's a reproducing test case. I'm posting it because I may not be able to look into the issue further for some time: # file: eventlet/tests/patcher_test.py
class ImportPatched(ProcessBase):
...
def test_import_requests_patched(self):
child_mod1 = """
from __future__ import print_function
import requests
print("requests", requests, requests.utils)
"""
child_mod2 = """
from __future__ import print_function
from eventlet import patcher
requests = patcher.import_patched('requests.__init__')
print("requests", requests, requests.utils)
"""
parent_mod = """
import child1
import child2
print("requests", child2.requests, child2.requests.utils)
"""
self.write_to_tempfile("child1", child_mod1)
self.write_to_tempfile("child2", child_mod2)
self.write_to_tempfile("parent", parent_mod)
output, lines = self.launch_subprocess('parent.py')
print output, lines
for line in lines:
self.assert_(not 'ImportError' in line, repr(line)) Thanks! |
Thank you very much @cabrera ! Test case worth a thousand words. |
The line Tested by modifying the example Producer Consumer Web Crawler to use Correction: Actually the line works only when I |
Removing the relative import seems to solve the eventlet issue at eventlet/eventlet#7
Avoids bug importing requests patched - see eventlet/eventlet#7
I got another error which is probably related to this one - here is a code sample: ## __init__.py
import eventlet
eventlet.monkey_patch()
## some module that gets imported first
requests_in_other_module = eventlet.import_patched('requests')
## Library code (simplified from https://github.com/influxdata/influxdb-python/blob/master/influxdb/client.py#L270)
import requests
import requests.exceptions
try:
requests.get('http://localhost:1') # raises ConnectionError
except requests.exceptions.ConnectionError as e:
print('Got ConnectionError:', e.args) Somehow the
|
I also met this problem, have you solved it? |
Not sure if this is requests fault or eventlets
The text was updated successfully, but these errors were encountered: