classification
Title: smtplib SMTP instances missing a default sock attribute
Type: Stage: resolved
Components: Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Romuald, amirjn, giampaolo.rodola, matrixise
Priority: normal Keywords: patch

Created on 2018-01-26 15:26 by Romuald, last changed 2018-10-09 14:35 by giampaolo.rodola. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 5345 merged Romuald, 2018-01-26 15:31
Messages (7)
msg310764 - (view) Author: Romuald Brunet (Romuald) * Date: 2018-01-26 15:26
SMTP instances from the smtplib module are not creating their sock attribute consistently after __init__

When host is sent as parameter a sock object is created (and hopefully, connected)

When the host is not sent, the sock attribute doesn't exist at all
msg310766 - (view) Author: Romuald Brunet (Romuald) * Date: 2018-01-26 15:34
My use case:

try:
  s = SMTP('myhost')
  s.do_some_sending()
finaly:
  if s is not None and s.sock is not None:
    s.quit()

But I realize just now that in that case, if s was initialized correctly, its sock was inevitably set
msg310774 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2018-01-26 16:10
You are right, but...

if there is an exception in the connect method before the init of
self.sock, the sock attribute will not exist and in this case, and only
in this case, there will be an exception in the close method.

your code will work fine if you use a try/finally but not in the case
where we use the with statement, for example.

with SMTP('myhost') as smtp:
   smtp.do_something()

If there is an exception in the __init__ of SMTP and self.sock is not
initialized -> traceback.
msg311553 - (view) Author: (amirjn) Date: 2018-02-03 13:55
same problem at apple.com
msg311554 - (view) Author: (amirjn) Date: 2018-02-03 13:57
<a href="apple.com">apple.com</a>
msg327409 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2018-10-09 14:32
New changeset 7b313971805ca9b53f181f7b97e5376d0b89dc06 by Giampaolo Rodola (Romuald Brunet) in branch 'master':
bpo-32680 add default "sock" on SMTP objects (#5345)
https://github.com/python/cpython/commit/7b313971805ca9b53f181f7b97e5376d0b89dc06
msg327410 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2018-10-09 14:35
PR merged for 3.8 branch. I don't think such a simple change deserves a backport for previous Python versions. Closing this out as resolved.
History
Date User Action Args
2018-10-09 14:35:41giampaolo.rodolasetstatus: open -> closed
versions: + Python 3.8
messages: + msg327410

resolution: fixed
stage: patch review -> resolved
2018-10-09 14:32:01giampaolo.rodolasetnosy: + giampaolo.rodola
messages: + msg327409
2018-02-03 13:57:00amirjnsetmessages: + msg311554
2018-02-03 13:55:55amirjnsetnosy: + amirjn
messages: + msg311553
2018-01-26 16:10:58matrixisesetnosy: + matrixise
messages: + msg310774
2018-01-26 15:34:10Romualdsetmessages: + msg310766
2018-01-26 15:31:26Romualdsetkeywords: + patch
stage: patch review
pull_requests: + pull_request5192
2018-01-26 15:26:15Romualdcreate