Created on 2013-07-24 09:19 by sYnfo, last changed 2018-08-07 02:38 by berker.peksag. This issue is now closed.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/imaplib.py", line 163, in __init__
self.open(host, port)
File "/usr/lib64/python2.7/imaplib.py", line 229, in open
self.sock = socket.create_connection((host, port))
File "/usr/lib64/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
Steps to Reproduce:
1. run python interpreter
2. import imaplib
3. imaplib.IMAP4()
Expected behavior would be, as per documentation, for imaplib to try to connect to localhost.
The root cause is, I believe, this:
socket.py::create_connection states "An host of '' [...] tells the OS to use the default." and thus imaplib uses '' as the default value for host, however from getaddrinfo (to which function the host variable is passed) documentation and source it seems to me that it expect None, not '' as the default value.
Substituting '' for None as the default value for host in imaplib.py seems to resolve this issue, I've included a tentative patch that does just that.
In case this will need patching I'd be more than happy to work on a more complete patch! :)
So this represents a change in behavior of the socket library on Fedora 18 vs earlier versions? Do any of the impalib tests fail?
>So this represents a change in behavior of the socket library on Fedora 18 vs earlier versions? I don't believe so. This happens on my Fedora 18 system, my Debian box with Python 2.6.6, on Fedora 19 (https://bugzilla.redhat.com/show_bug.cgi?id=987340), another Debian system (http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=591471) and python compiled from the default branch does the same thing. The documentation for imaplib.IMAP4 says: "If host is not specified, '' (the local host) is used." My point is that imaplib.IMAP4 without any arguments should try to connect to localhost, however this is not what is currently happening, at least on the systems mentioned above. Why I think this is happening I've tried to explain in my previous comment. >Do any of the impalib tests fail? Nope.
OK, yeah, this seems to be long standing, untested behavior. I will take a look at what should be done here.
According to the documentation[1] the values for host should be set to 'localhost' by default. I attached a patch that does just that. Also note, that the minimal example at the end of [1] is broken by this bug. [1]: http://docs.python.org/2/library/imaplib.html?highlight=imap#imaplib.IMAP4
Milan: using 'localhost' is incorrect, since the string 'localhost' will not always resolve to the local host IP, while passing None to getaddrinfo will. It is significant that the example fails. We need a test for this case (the imap tests have been historically very poor, though we've improved that somewhat in the past few years). Although it is a low-probability thing, someone might be depending on self.host being '' by default, so I think the better (and simpler) fix is to convert '' to None in the _create_socket method.
I patched it as you suggested (9 lines added/changed in total).
Ignore what I just did (the test is obviously dump; it fails if you run it on an IMAP server). I'll make a new attempt after a coffee break ;)
I'm still wondering if the test could be done better. At least it fixes the bug.
I shouldn't criticize my own patches if I want to have them committed: Here comes the final and perfect patch for this issue ;) (I only rephrased the comment for the test to make the intuition clear.)
Hi Milan, thanks for the updated patch. Did you see my review comments? http://bugs.python.org/review/18540/
ping?
New changeset e4dcbbd7f4ac18d01c0ec85f64ae98b8281ed403 by Berker Peksag in branch 'master': bpo-18540: Fix EAI_NONAME in imaplib.IMAP4*() (GH-8634) https://github.com/python/cpython/commit/e4dcbbd7f4ac18d01c0ec85f64ae98b8281ed403
New changeset 5799e5a84c78eac672e5f5f4f3fd2d903ba51a9d by Berker Peksag (Miss Islington (bot)) in branch '3.7': bpo-18540: Fix EAI_NONAME in imaplib.IMAP4*() (GH-8634) https://github.com/python/cpython/commit/5799e5a84c78eac672e5f5f4f3fd2d903ba51a9d
New changeset 671a13a7b6ff1022a6fd868e5842687123ab9fd1 by Berker Peksag (Miss Islington (bot)) in branch '3.6': bpo-18540: Fix EAI_NONAME in imaplib.IMAP4*() (GH-8634) https://github.com/python/cpython/commit/671a13a7b6ff1022a6fd868e5842687123ab9fd1
messages: + msg249750
stage: patch review
messages: + msg249708
messages: + msg222578
messages: + msg222576
messages: + msg193655
components: + email