[Python-checkins] gh-100374: Fixed a bug in socket.getfqdn() (gh-100375)

miss-islington webhook-mailer at python.org
Wed Dec 21 09:06:23 EST 2022


https://github.com/python/cpython/commit/b2076b00710c4366dcfe6cd236e480d68a3c38b7
commit: b2076b00710c4366dcfe6cd236e480d68a3c38b7
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-12-21T06:06:10-08:00
summary:

gh-100374: Fixed a bug in socket.getfqdn() (gh-100375)

(cherry picked from commit 12be23cf3c1301be2c6b8fd4cb2cd35a567d2ea2)

Co-authored-by: Dominic Socular <BBH at awsl.rip>

files:
A Misc/NEWS.d/next/Core and Builtins/2022-12-20-16-14-19.gh-issue-100374.YRrVHT.rst
M Lib/socket.py
M Lib/test/test_socket.py

diff --git a/Lib/socket.py b/Lib/socket.py
index 0717c696b12e..5a896bee7c49 100644
--- a/Lib/socket.py
+++ b/Lib/socket.py
@@ -784,11 +784,11 @@ def getfqdn(name=''):
 
     First the hostname returned by gethostbyaddr() is checked, then
     possibly existing aliases. In case no FQDN is available and `name`
-    was given, it is returned unchanged. If `name` was empty or '0.0.0.0',
+    was given, it is returned unchanged. If `name` was empty, '0.0.0.0' or '::',
     hostname from gethostname() is returned.
     """
     name = name.strip()
-    if not name or name == '0.0.0.0':
+    if not name or name in ('0.0.0.0', '::'):
         name = gethostname()
     try:
         hostname, aliases, ipaddrs = gethostbyaddr(name)
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index b07954989fdd..13cb2a7945d1 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -1760,6 +1760,10 @@ def test_getaddrinfo_ipv6_basic(self):
         )
         self.assertEqual(sockaddr, ('ff02::1de:c0:face:8d', 1234, 0, 0))
 
+    def test_getfqdn_filter_localhost(self):
+        self.assertEqual(socket.getfqdn(), socket.getfqdn("0.0.0.0"))
+        self.assertEqual(socket.getfqdn(), socket.getfqdn("::"))
+
     @unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.')
     @unittest.skipIf(sys.platform == 'win32', 'does not work on Windows')
     @unittest.skipIf(AIX, 'Symbolic scope id does not work')
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-12-20-16-14-19.gh-issue-100374.YRrVHT.rst b/Misc/NEWS.d/next/Core and Builtins/2022-12-20-16-14-19.gh-issue-100374.YRrVHT.rst
new file mode 100644
index 000000000000..e78352fb188e
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-12-20-16-14-19.gh-issue-100374.YRrVHT.rst	
@@ -0,0 +1 @@
+Fix incorrect result and delay in :func:`socket.getfqdn`. Patch by Dominic Socular.



More information about the Python-checkins mailing list