This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: smtplib.py initialisation defect
Type: behavior Stage: commit review
Components: Library (Lib) Versions: Python 2.5.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ocean-city Nosy List: loewis, ocean-city, piers
Priority: release blocker Keywords: patch

Created on 2008-11-12 08:17 by piers, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
smtplib-diff-c piers, 2008-11-12 08:17 diff -c of patch
smtplib_minor_fix.patch ocean-city, 2008-11-13 02:16
Messages (5)
msg75777 - (view) Author: Piers Lauder (piers) Date: 2008-11-12 08:17
smtplib does not initialise the valriable 'sock' in the case where
'host' is NULL on instantiation of smtplib.SMTP.

Eg:

% python
Python 2.5.2 (r252:60911, Oct  5 2008, 19:29:17) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import smtplib
>>> client=smtplib.SMTP('')
>>> client.sendmail('from@home', 'to@home', 'test')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/smtplib.py", line 676, in sendmail
    if not (200 <= self.ehlo()[0] <= 299):
  File "/usr/lib/python2.5/smtplib.py", line 397, in ehlo
    self.putcmd("ehlo", name or self.local_hostname)
  File "/usr/lib/python2.5/smtplib.py", line 333, in putcmd
    self.send(str)
  File "/usr/lib/python2.5/smtplib.py", line 318, in send
    if self.sock:
AttributeError: SMTP instance has no attribute 'sock'
>>>

The fix is to add "self.sock = None" in __init__ if host is not set,
and then the behaviour is much more helpful:

% python
Python 2.5.2 (r252:60911, Oct  5 2008, 19:29:17) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import smtplib
>>> client=smtplib.SMTP('')
>>> client.sendmail('from@home', 'to@home', 'test')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/smtplib.py", line 678, in sendmail
    if not (200 <= self.ehlo()[0] <= 299):
  File "/usr/lib/python2.5/smtplib.py", line 399, in ehlo
    self.putcmd("ehlo", name or self.local_hostname)
  File "/usr/lib/python2.5/smtplib.py", line 335, in putcmd
    self.send(str)
  File "/usr/lib/python2.5/smtplib.py", line 327, in send
    raise SMTPServerDisconnected('please run connect() first')
smtplib.SMTPServerDisconnected: please run connect() first
>>>
msg75778 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2008-11-12 08:53
This issue can be fixed by backporting r60975.
msg77503 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2008-12-10 08:42
Backporting is fine with me: any volunteers?
msg77527 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2008-12-10 09:47
I will backport this.
msg77528 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2008-12-10 09:56
Done. Fixed in r67686 (release25-maint).
History
Date User Action Args
2022-04-11 14:56:41adminsetgithub: 48552
2008-12-10 09:56:18ocean-citysetstatus: open -> closed
resolution: fixed
messages: + msg77528
2008-12-10 09:47:24ocean-citysetassignee: ocean-city
messages: + msg77527
2008-12-10 08:42:03loewissetpriority: release blocker
messages: + msg77503
2008-11-13 02:33:22benjamin.petersonsetnosy: + loewis
2008-11-13 02:16:47ocean-citysetfiles: + smtplib_minor_fix.patch
keywords: + patch
stage: commit review
2008-11-12 08:53:42ocean-citysetnosy: + ocean-city
messages: + msg75778
2008-11-12 08:17:19pierscreate