classification
Title: imaplib.IMAP4() ends with "Name or service not known" on Fedora 18
Type: behavior Stage: resolved
Components: email, Library (Lib) Versions: Python 3.8, Python 3.7, Python 3.6, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: barry, berker.peksag, dveeden, mcepl, r.david.murray, sYnfo, zvyn
Priority: normal Keywords: patch

Created on 2013-07-24 09:19 by sYnfo, last changed 2018-08-07 02:38 by berker.peksag. This issue is now closed.

Files
File name Uploaded Description Edit
imaplib.patch sYnfo, 2013-07-24 09:19 Substitutes '' for None as the default value for host in imaplib.py
imaplib-zvyn.patch zvyn, 2014-03-02 23:35 Substitutes '' for 'localhost' as the default value for host in imaplib.py review
imaplib_interpret_empty_string_as_None.patch zvyn, 2014-07-08 19:00 review
imaplib_interpret_empty_string_as_NoneV2.patch zvyn, 2014-07-08 19:36 review
imaplib_interpret_empty_string_as_None-final.patch zvyn, 2015-09-04 03:35 review
Pull Requests
URL Status Linked Edit
PR 8634 merged berker.peksag, 2018-08-02 18:37
PR 8697 merged miss-islington, 2018-08-07 02:12
PR 8698 merged miss-islington, 2018-08-07 02:12
Messages (15)
msg193632 - (view) Author: Matěj Stuchlík (sYnfo) Date: 2013-07-24 09:19
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! :)
msg193655 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-07-24 15:08
So this represents a change in behavior of the socket library on Fedora 18 vs earlier versions?  Do any of the impalib tests fail?
msg193663 - (view) Author: Matěj Stuchlík (sYnfo) Date: 2013-07-24 16:35
>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.
msg193665 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-07-24 18:00
OK, yeah, this seems to be long standing, untested behavior.  I will take a look at what should be done here.
msg212603 - (view) Author: Milan Oberkirch (zvyn) * Date: 2014-03-02 23:35
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
msg218704 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-05-17 15:04
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.
msg222576 - (view) Author: Milan Oberkirch (zvyn) * Date: 2014-07-08 19:00
I patched it as you suggested (9 lines added/changed in total).
msg222577 - (view) Author: Milan Oberkirch (zvyn) * Date: 2014-07-08 19:16
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 ;)
msg222578 - (view) Author: Milan Oberkirch (zvyn) * Date: 2014-07-08 19:36
I'm still wondering if the test could be done better. At least it fixes the bug.
msg249708 - (view) Author: Milan Oberkirch (zvyn) * Date: 2015-09-04 03:35
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.)
msg249750 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2015-09-04 10:23
Hi Milan, thanks for the updated patch. Did you see my review comments? http://bugs.python.org/review/18540/
msg315559 - (view) Author: Matej Cepl (mcepl) * Date: 2018-04-21 09:12
ping?
msg323231 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2018-08-07 02:12
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
msg323232 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2018-08-07 02:37
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
msg323233 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2018-08-07 02:38
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
History
Date User Action Args
2018-08-07 02:38:52berker.peksagsetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.7, Python 3.8, - Python 3.4, Python 3.5
2018-08-07 02:38:08berker.peksagsetmessages: + msg323233
2018-08-07 02:37:41berker.peksagsetmessages: + msg323232
2018-08-07 02:12:44miss-islingtonsetpull_requests: + pull_request8191
2018-08-07 02:12:36miss-islingtonsetpull_requests: + pull_request8190
2018-08-07 02:12:21berker.peksagsetmessages: + msg323231
2018-08-02 18:37:19berker.peksagsetpull_requests: + pull_request8139
2018-04-21 09:12:51mceplsetnosy: + mcepl
messages: + msg315559
2015-09-04 10:23:35berker.peksagsetversions: + Python 3.6
nosy: + berker.peksag

messages: + msg249750

stage: patch review
2015-09-04 03:35:42zvynsetfiles: + imaplib_interpret_empty_string_as_None-final.patch

messages: + msg249708
2014-10-17 18:42:57r.david.murraylinkissue22658 superseder
2014-07-08 19:36:37zvynsetfiles: + imaplib_interpret_empty_string_as_NoneV2.patch

messages: + msg222578
2014-07-08 19:16:02zvynsetmessages: + msg222577
2014-07-08 19:00:48zvynsetfiles: + imaplib_interpret_empty_string_as_None.patch

messages: + msg222576
2014-05-17 15:04:13r.david.murraysetmessages: + msg218704
2014-03-02 23:35:48zvynsetfiles: + imaplib-zvyn.patch
nosy: + zvyn
messages: + msg212603

2013-08-22 13:43:58dveedensetnosy: + dveeden
2013-07-24 18:00:02r.david.murraysetmessages: + msg193665
2013-07-24 16:35:15sYnfosetmessages: + msg193663
2013-07-24 15:08:37r.david.murraysetversions: + Python 3.4
nosy: + barry, r.david.murray

messages: + msg193655

components: + email
2013-07-24 09:19:31sYnfocreate