﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
6835	django.core.mail.SMTPConnection.open() method performs an unnecessary call to socket.getfqdn()	George Murdocca <gmurdocca@…>	Philippe Raoult	"'''Summary'''

This is an optimization fix as opposed to a bug. The django.core.mail.SMTPConnection.open() method performs an extraneous call to socket.getfqdn() which affects performance. The mail.py module has a class defined called !CachedDnsName() whos get_fqdn() method addresses this issue by calling socket.getfqdn(), and caches the result, thus removing the need to call it again later. The fix is extremely trivial to implement. Here are the details:

'''Detailed Description'''

On line 122 of the mail.py module, an smtplib.SMTP() object is instantiated:

{{{self.connection = smtplib.SMTP(self.host, self.port)}}}


The constructor of smtplib.SMTP() accepts three arguments, namely {{{host='', port=0, local_hostname=None}}}. However, {{{local_hostname}}} is omitted in mail.py. When omitted, socket.getfqdn() is called, and its value is assigned to the missing argument. This call is extraneous and slow, because:
* socket.getfqdn() is slow to evaluate (as mentioned in the comment at line 28 of mail.py)
* The result of socket.getfqdn() already done once and cached by django.core.mail.!CachedDnsName.get_fqdn(), and shoul dbe used instead.

'''The Fix'''

Change line 122 of mail.py to:

{{{ self.connection = smtplib.SMTP(self.host, self.port, local_hostname=DNS_NAME.get_fqdn())}}}


''' The Diff '''

{{{
--- mail.py     2008-03-02 12:45:20.000000000 +1100
+++ mailjrj.py  2008-03-20 14:04:07.000000000 +1100
@@ -119,7 +119,7 @@
             # Nothing to do if the connection is already open.
             return False
         try:
-            self.connection = smtplib.SMTP(self.host, self.port)
+            self.connection = smtplib.SMTP(self.host, self.port, local_hostname=DNS_NAME.get_fqdn())
             if self.use_tls:
                 self.connection.ehlo()
                 self.connection.starttls()
}}}

Cheers,
George Murdocca & Phil Wright."		closed	Core (Mail)	dev		fixed	local_hostname, smtplib, CachedDnsName, SMTPConnection	gmurdocca@…	Ready for checkin	1	0	0	0	0	0
