-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
context management support in imaplib, smtplib, ftplib #49222
Comments
In a program, I naturally wrote: >>> from ftplib import FTP
>>> with FTP('ftp.somewhere.com') as ftp:
... ftp.login('someone', 'pass')
... ... Until I realized this class is not equipped with __enter__ and __exit__, I think it could be a simple and pleasant enhancement to add it. |
positive feedbacks on python-ideas, so I'll start to write the patches. targets :
first patch : smtplib (will do ftplib and imaplib as well, then propose this enhancement to |
What is the rationale for swallowing all socket exceptions except Note that SMTP.quit() calls SMTP.close(), so in the normal termination The double call to close() also makes error path harder to analyze. It |
I am catching just the error that raises if the connection is closed
Right
Right i'll fix that Thanks for teh feedback |
On Mon, Jan 19, 2009 at 2:01 PM, Tarek Ziadé <report@bugs.python.org> wrote:
I see. I misread the double negative "except errno NOT equals 54", but if self.sock:
try:
self.sock.sendall(str)
except socket.error:
self.close()
raise SMTPServerDisconnected('Server not connected')
else:
raise SMTPServerDisconnected('please run connect() first') so you will never see a socket error from quit(). Furthermore, I don't think you should ignore return code from quit(): |
It did happen but on the close() call inside the quit() call. I have changed the patch and took car of the return code. I have also commited a first version of the imaplib one. This one but imaplib is undertested, this fake server might be a good start for |
Please remove the old smtplib.patch: it is confusing to have two A nit-pick: 221 is a success code (in smtp 2xx codes are successes and Your new code may leave socket open in the case when self.docmd("quit") BTW, I think your code will be simpler if you don't reuse quit() and |
I've simplified the code about the name of the vars. while I agree with you, I have use those names about the return code , i am wondering if it would be good to add in |
Feedback from my blog : other places where a context manager could be good to have :
|
Tarek: gzip and bz2 are already done |
Ah great, thanks for telling Antoine, I missed it on gzip. And the test |
As for ftplib patch you should make sure that close() gets called in any + def __exit__(self, *args): |
zipfile also would make a good target for a contextmanager (as noted here - |
There's a patch for zipfile in bpo-5511. |
Is this still desirable? |
It would be good for consistency, yes. |
A patch including tests and doc changes is in attachment. |
Magic methods usually don’t have docstrings, since they implement one operation that is described in one place (think __len__ vs. len). You could remove the doc of __enter__ and turn the doc of __exit__ into a comment or move it to the reST file when you say that FTP supports the context management protocol. What did you add a cmd_noop method that does the same thing as cmd_user for? Will the change from handler to handler_instance not break third-party code? |
Agreed, I only left it since it was there in the first place.
Basically all DummyFTPHandler commands are no-ops except pasv, port, retr, stor and some others. There's no real reason why I added NOOP except for consistency with the FTP procol.
Not sure what you mean by "third party code" but it shouldn't break the existing tests if that's what you're worried about. |
Re-reading the patch, I notice it’s in the test module, not on the FTP I meant code outside the standard library. I thought this was in ftplib, In summary, the diff for ftplib seems good to me, with a minor remark |
Whoops! No that should have been "200 noop ok". New patch in attachment (I changed the doc a little bit including the "with" example usage in ftplib.rst other than just in what's new). |
Ah, I’m glad I had one interesting comment out of three in my first This change, although nice, seems a bit trivial for warranting taking so |
This is now committed as r81041. The other two patches should be adapted for 3.2. |
The work on smtplib context manager continues in bpo-11289. I'm putting it as a dependency here. |
ftplib.FTP and smtplib.SMTP support the "with" statement since 3.2 and 3.3 respectively. |
Imaplib tests was changed a lot since 3.2. Here is an updated patch. |
The patch lacks documentation updates. |
Here is an updated patch. Updated documentation, added checks that logout executed after a with statement, now logout() can be called inside a with statement. |
Updated Serhiy's patch to 3.5. I also added a whatsnew entry. |
Thanks Berker. |
I think that's all with this issue. |
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
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: