[Python-checkins] bpo-29639: change test.support.HOST to "localhost"

Gregory P. Smith webhook-mailer at python.org
Sat Sep 9 03:30:18 EDT 2017


https://github.com/python/cpython/commit/efb1d0a3c001a6153211063ba439b9847aa03509
commit: efb1d0a3c001a6153211063ba439b9847aa03509
branch: master
author: Gregory P. Smith <greg at krypto.org>
committer: GitHub <noreply at github.com>
date: 2017-09-09T00:30:15-07:00
summary:

bpo-29639: change test.support.HOST to "localhost"

test.support.HOST should be "localhost" as it was in the past. See the bpo-29639.

Tests that need the IP address should use HOSTv4 (added) or the existing HOSTv6 constant.

This changes the definition and fixes tests that needed updating to deal with HOST being
the hostname rather than the hardcoded IP address.

This is only the first step in addressing https://bugs.python.org/issue29639.

files:
A Misc/NEWS.d/next/Tests/2017-09-08-15-59-07.bpo-29639.yIZecp.rst
M Lib/asyncore.py
M Lib/test/support/__init__.py
M Lib/test/test_smtpd.py
M Lib/test/test_smtplib.py
M Lib/test/test_socket.py

diff --git a/Lib/asyncore.py b/Lib/asyncore.py
index 03d16838b73..828f4d4fe78 100644
--- a/Lib/asyncore.py
+++ b/Lib/asyncore.py
@@ -287,7 +287,6 @@ def create_socket(self, family=socket.AF_INET, type=socket.SOCK_STREAM):
 
     def set_socket(self, sock, map=None):
         self.socket = sock
-##        self.__dict__['socket'] = sock
         self._fileno = sock.fileno()
         self.add_channel(map)
 
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index bfceba151a1..f57b2517792 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -601,9 +601,8 @@ def wrapper(*args, **kw):
     return decorator
 
 
-# Don't use "localhost", since resolving it uses the DNS under recent
-# Windows versions (see issue #18792).
-HOST = "127.0.0.1"
+HOST = "localhost"
+HOSTv4 = "127.0.0.1"
 HOSTv6 = "::1"
 
 
diff --git a/Lib/test/test_smtpd.py b/Lib/test/test_smtpd.py
index 3eebe948ad8..a9f7d5a3b8b 100644
--- a/Lib/test/test_smtpd.py
+++ b/Lib/test/test_smtpd.py
@@ -170,11 +170,11 @@ def tearDown(self):
 
     @unittest.skipUnless(support.IPV6_ENABLED, "IPv6 not enabled")
     def test_socket_uses_IPv6(self):
-        server = smtpd.SMTPServer((support.HOSTv6, 0), (support.HOST, 0))
+        server = smtpd.SMTPServer((support.HOSTv6, 0), (support.HOSTv4, 0))
         self.assertEqual(server.socket.family, socket.AF_INET6)
 
     def test_socket_uses_IPv4(self):
-        server = smtpd.SMTPServer((support.HOST, 0), (support.HOSTv6, 0))
+        server = smtpd.SMTPServer((support.HOSTv4, 0), (support.HOSTv6, 0))
         self.assertEqual(server.socket.family, socket.AF_INET)
 
 
diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py
index 42f4266b249..4c9b7d367c8 100644
--- a/Lib/test/test_smtplib.py
+++ b/Lib/test/test_smtplib.py
@@ -19,10 +19,9 @@
 
 import unittest
 from test import support, mock_socket
+from test.support import HOST, HOSTv4, HOSTv6
 
 
-HOST = support.HOST
-
 if sys.platform == 'darwin':
     # select.poll returns a select.POLLHUP at the end of the tests
     # on darwin, so just ignore it
@@ -208,8 +207,8 @@ def setUp(self):
         # Pick a random unused port by passing 0 for the port number
         self.serv = smtpd.DebuggingServer((HOST, 0), ('nowhere', -1),
                                           decode_data=True)
-        # Keep a note of what port was assigned
-        self.port = self.serv.socket.getsockname()[1]
+        # Keep a note of what server host and port were assigned
+        self.host, self.port = self.serv.socket.getsockname()[:2]
         serv_args = (self.serv, self.serv_evt, self.client_evt)
         self.thread = threading.Thread(target=debugging_server, args=serv_args)
         self.thread.start()
@@ -231,6 +230,11 @@ def tearDown(self):
         smtpd.DEBUGSTREAM.close()
         smtpd.DEBUGSTREAM = self.old_DEBUGSTREAM
 
+    def get_output_without_xpeer(self):
+        test_output = self.output.getvalue()
+        return re.sub(r'(.*?)^X-Peer:\s*\S+\n(.*)', r'\1\2',
+                      test_output, flags=re.MULTILINE|re.DOTALL)
+
     def testBasic(self):
         # connect
         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
@@ -238,16 +242,16 @@ def testBasic(self):
 
     def testSourceAddress(self):
         # connect
-        port = support.find_unused_port()
+        src_port = support.find_unused_port()
         try:
-            smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
-                    timeout=3, source_address=('127.0.0.1', port))
-            self.assertEqual(smtp.source_address, ('127.0.0.1', port))
+            smtp = smtplib.SMTP(self.host, self.port, local_hostname='localhost',
+                                timeout=3, source_address=(self.host, src_port))
+            self.assertEqual(smtp.source_address, (self.host, src_port))
             self.assertEqual(smtp.local_hostname, 'localhost')
             smtp.quit()
         except OSError as e:
             if e.errno == errno.EADDRINUSE:
-                self.skipTest("couldn't bind to port %d" % port)
+                self.skipTest("couldn't bind to source port %d" % src_port)
             raise
 
     def testNOOP(self):
@@ -374,10 +378,14 @@ def testSendMessage(self):
         self.client_evt.set()
         self.serv_evt.wait()
         self.output.flush()
-        # Add the X-Peer header that DebuggingServer adds
-        m['X-Peer'] = socket.gethostbyname('localhost')
+        # Remove the X-Peer header that DebuggingServer adds as figuring out
+        # exactly what IP address format is put there is not easy (and
+        # irrelevant to our test).  Typically 127.0.0.1 or ::1, but it is
+        # not always the same as socket.gethostbyname(HOST). :(
+        test_output = self.get_output_without_xpeer()
+        del m['X-Peer']
         mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
-        self.assertEqual(self.output.getvalue(), mexpect)
+        self.assertEqual(test_output, mexpect)
 
     def testSendMessageWithAddresses(self):
         m = email.mime.text.MIMEText('A test message')
@@ -397,12 +405,13 @@ def testSendMessageWithAddresses(self):
         self.client_evt.set()
         self.serv_evt.wait()
         self.output.flush()
-        # Add the X-Peer header that DebuggingServer adds
-        m['X-Peer'] = socket.gethostbyname('localhost')
+        # Remove the X-Peer header that DebuggingServer adds.
+        test_output = self.get_output_without_xpeer()
+        del m['X-Peer']
         # The Bcc header should not be transmitted.
         del m['Bcc']
         mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
-        self.assertEqual(self.output.getvalue(), mexpect)
+        self.assertEqual(test_output, mexpect)
         debugout = smtpd.DEBUGSTREAM.getvalue()
         sender = re.compile("^sender: foo at bar.com$", re.MULTILINE)
         self.assertRegex(debugout, sender)
@@ -426,10 +435,11 @@ def testSendMessageWithSomeAddresses(self):
         self.client_evt.set()
         self.serv_evt.wait()
         self.output.flush()
-        # Add the X-Peer header that DebuggingServer adds
-        m['X-Peer'] = socket.gethostbyname('localhost')
+        # Remove the X-Peer header that DebuggingServer adds.
+        test_output = self.get_output_without_xpeer()
+        del m['X-Peer']
         mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
-        self.assertEqual(self.output.getvalue(), mexpect)
+        self.assertEqual(test_output, mexpect)
         debugout = smtpd.DEBUGSTREAM.getvalue()
         sender = re.compile("^sender: foo at bar.com$", re.MULTILINE)
         self.assertRegex(debugout, sender)
@@ -452,10 +462,11 @@ def testSendMessageWithSpecifiedAddresses(self):
         self.client_evt.set()
         self.serv_evt.wait()
         self.output.flush()
-        # Add the X-Peer header that DebuggingServer adds
-        m['X-Peer'] = socket.gethostbyname('localhost')
+        # Remove the X-Peer header that DebuggingServer adds.
+        test_output = self.get_output_without_xpeer()
+        del m['X-Peer']
         mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
-        self.assertEqual(self.output.getvalue(), mexpect)
+        self.assertEqual(test_output, mexpect)
         debugout = smtpd.DEBUGSTREAM.getvalue()
         sender = re.compile("^sender: joe at example.com$", re.MULTILINE)
         self.assertRegex(debugout, sender)
@@ -481,10 +492,11 @@ def testSendMessageWithMultipleFrom(self):
         self.client_evt.set()
         self.serv_evt.wait()
         self.output.flush()
-        # Add the X-Peer header that DebuggingServer adds
-        m['X-Peer'] = socket.gethostbyname('localhost')
+        # Remove the X-Peer header that DebuggingServer adds.
+        test_output = self.get_output_without_xpeer()
+        del m['X-Peer']
         mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
-        self.assertEqual(self.output.getvalue(), mexpect)
+        self.assertEqual(test_output, mexpect)
         debugout = smtpd.DEBUGSTREAM.getvalue()
         sender = re.compile("^sender: the_rescuers at Rescue-Aid-Society.com$", re.MULTILINE)
         self.assertRegex(debugout, sender)
@@ -515,10 +527,11 @@ def testSendMessageResent(self):
         # The Resent-Bcc headers are deleted before serialization.
         del m['Bcc']
         del m['Resent-Bcc']
-        # Add the X-Peer header that DebuggingServer adds
-        m['X-Peer'] = socket.gethostbyname('localhost')
+        # Remove the X-Peer header that DebuggingServer adds.
+        test_output = self.get_output_without_xpeer()
+        del m['X-Peer']
         mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
-        self.assertEqual(self.output.getvalue(), mexpect)
+        self.assertEqual(test_output, mexpect)
         debugout = smtpd.DEBUGSTREAM.getvalue()
         sender = re.compile("^sender: holy at grail.net$", re.MULTILINE)
         self.assertRegex(debugout, sender)
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index 27d9d4944e1..05d8761241e 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -864,12 +864,12 @@ def testHostnameRes(self):
             self.fail("Error testing host resolution mechanisms. (fqdn: %s, all: %s)" % (fqhn, repr(all_host_names)))
 
     def test_host_resolution(self):
-        for addr in [support.HOST, '10.0.0.1', '255.255.255.255']:
+        for addr in [support.HOSTv4, '10.0.0.1', '255.255.255.255']:
             self.assertEqual(socket.gethostbyname(addr), addr)
 
         # we don't test support.HOSTv6 because there's a chance it doesn't have
         # a matching name entry (e.g. 'ip6-localhost')
-        for host in [support.HOST]:
+        for host in [support.HOSTv4]:
             self.assertIn(host, socket.gethostbyaddr(host)[2])
 
     def test_host_resolution_bad_address(self):
diff --git a/Misc/NEWS.d/next/Tests/2017-09-08-15-59-07.bpo-29639.yIZecp.rst b/Misc/NEWS.d/next/Tests/2017-09-08-15-59-07.bpo-29639.yIZecp.rst
new file mode 100644
index 00000000000..7f49eb4bab4
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2017-09-08-15-59-07.bpo-29639.yIZecp.rst
@@ -0,0 +1,2 @@
+test.support.HOST is now "localhost", a new HOSTv4 constant has been added
+for your ``127.0.0.1`` needs, similar to the existing HOSTv6 constant.



More information about the Python-checkins mailing list