]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-100374: Fixed a bug in socket.getfqdn() (gh-100375)
authorDominic Socular <BBH@awsl.rip>
Wed, 21 Dec 2022 13:25:04 +0000 (21:25 +0800)
committerGitHub <noreply@github.com>
Wed, 21 Dec 2022 13:25:04 +0000 (22:25 +0900)
Lib/socket.py
Lib/test/test_socket.py
Misc/NEWS.d/next/Core and Builtins/2022-12-20-16-14-19.gh-issue-100374.YRrVHT.rst [new file with mode: 0644]

index 1c8cef6ce658102f2670a929e8eae78d51395ae1..3a4f94de9cc03a987eda641152e286b9d1546ea6 100644 (file)
@@ -785,11 +785,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)
index d808f3f62b96dca19e84645aa8c95135d55265f8..f1b4018c265e1816896bc72605332aeef386f1ba 100644 (file)
@@ -1769,6 +1769,10 @@ class GeneralModuleTests(unittest.TestCase):
         )
         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 (file)
index 0000000..e78352f
--- /dev/null
@@ -0,0 +1 @@
+Fix incorrect result and delay in :func:`socket.getfqdn`. Patch by Dominic Socular.