imaplib.IMAP4() ends with "Name or service not known" on Fedora 18 #62740
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
assignee = None closed_at = <Date 2018-08-07.02:38:52.606> created_at = <Date 2013-07-24.09:19:31.148> labels = ['3.7', '3.8', 'type-bug', 'library', 'expert-email'] title = 'imaplib.IMAP4() ends with "Name or service not known" on Fedora 18' updated_at = <Date 2018-08-07.02:38:52.546> user = 'https://bugs.python.org/sYnfo'
activity = <Date 2018-08-07.02:38:52.546> actor = 'berker.peksag' assignee = 'none' closed = True closed_date = <Date 2018-08-07.02:38:52.606> closer = 'berker.peksag' components = ['Library (Lib)', 'email'] creation = <Date 2013-07-24.09:19:31.148> creator = 'sYnfo' dependencies =  files = ['31027', '34271', '35903', '35904', '40347'] hgrepos =  issue_num = 18540 keywords = ['patch'] message_count = 15.0 messages = ['193632', '193655', '193663', '193665', '212603', '218704', '222576', '222577', '222578', '249708', '249750', '315559', '323231', '323232', '323233'] nosy_count = 7.0 nosy_names = ['barry', 'mcepl', 'r.david.murray', 'berker.peksag', 'sYnfo', 'dveeden', 'zvyn'] pr_nums = ['8634', '8697', '8698'] priority = 'normal' resolution = 'fixed' stage = 'resolved' status = 'closed' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue18540' versions = ['Python 2.7', 'Python 3.6', 'Python 3.7', 'Python 3.8']
The text was updated successfully, but these errors were encountered:
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:
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! :)
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:
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.
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.