[Python-Dev] cpython: Fix closes Issue11281 - smtplib.STMP gets source_address parameter, which adds

Antoine Pitrou solipsis at pitrou.net
Sat Jul 30 08:46:00 CEST 2011


Hi Senthil,

> +        if source_address: self.source_address = source_address

Could you try to follow PEP 8?
(I know PEP 8 is not always followed in old code, but there's no reason
not to follow it in code that we add to the stdlib)

> +        SMTP.__init__(self, host, port, local_hostname = local_hostname,
> +                source_address = source_address)

Ditto here (and other similar occurrences).

> +    def testSourceAddress(self):
> +        # connect
> +        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3,
> +                            source_address=('127.0.0.1', 19876))
> +        self.assertEqual(smtp.source_address, ('127.0.0.1', 19876))
> +        self.assertEqual(smtp.local_hostname, 'localhost')

Unless this test is also using some kind of mock socket (it doesn't
seem to), this can break as soon as port 19876 is already in use.
There are utilities in test.support to help with this, such as
bind_port() or (less reliable) find_unused_port().

Actually, this can be triggered quite easily by running the test a
couple of times in parallel:

$ ./python -m test -m testSourceAddress -Fv -j3 test_smtplib

[...]

[ 14/4] test_smtplib
testSourceAddress (test.test_smtplib.GeneralTests) ... ok
testSourceAddress (test.test_smtplib.DebuggingServerTests) ... ERROR

======================================================================
ERROR: testSourceAddress (test.test_smtplib.DebuggingServerTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/antoine/cpython/default/Lib/test/test_smtplib.py", line
220, in testSourceAddress source_address=('127.0.0.1', 19876))
  File "/home/antoine/cpython/default/Lib/smtplib.py", line 238, in
__init__ (code, msg) = self.connect(host, port)
  File "/home/antoine/cpython/default/Lib/smtplib.py", line 313, in
connect self.sock = self._get_socket(host, port, self.timeout)
  File "/home/antoine/cpython/default/Lib/smtplib.py", line 287, in
_get_socket self.source_address)
  File "/home/antoine/cpython/default/Lib/socket.py", line 407, in
create_connection raise err
  File "/home/antoine/cpython/default/Lib/socket.py", line 397, in
create_connection sock.bind(source_address)
socket.error: [Errno 98] Address already in use

----------------------------------------------------------------------


> +        print(dir(smtp))

Usually, we avoid printing anything in tests, except when
support.verbose is True.

Regards

Antoine.




More information about the Python-Dev mailing list