Skip to content

Commit b2076b0

Browse files
gh-100374: Fixed a bug in socket.getfqdn() (gh-100375)
(cherry picked from commit 12be23c) Co-authored-by: Dominic Socular <BBH@awsl.rip>
1 parent ae8520c commit b2076b0

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

Lib/socket.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -784,11 +784,11 @@ def getfqdn(name=''):
784784
785785
First the hostname returned by gethostbyaddr() is checked, then
786786
possibly existing aliases. In case no FQDN is available and `name`
787-
was given, it is returned unchanged. If `name` was empty or '0.0.0.0',
787+
was given, it is returned unchanged. If `name` was empty, '0.0.0.0' or '::',
788788
hostname from gethostname() is returned.
789789
"""
790790
name = name.strip()
791-
if not name or name == '0.0.0.0':
791+
if not name or name in ('0.0.0.0', '::'):
792792
name = gethostname()
793793
try:
794794
hostname, aliases, ipaddrs = gethostbyaddr(name)

Lib/test/test_socket.py

+4
Original file line numberDiff line numberDiff line change
@@ -1760,6 +1760,10 @@ def test_getaddrinfo_ipv6_basic(self):
17601760
)
17611761
self.assertEqual(sockaddr, ('ff02::1de:c0:face:8d', 1234, 0, 0))
17621762

1763+
def test_getfqdn_filter_localhost(self):
1764+
self.assertEqual(socket.getfqdn(), socket.getfqdn("0.0.0.0"))
1765+
self.assertEqual(socket.getfqdn(), socket.getfqdn("::"))
1766+
17631767
@unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.')
17641768
@unittest.skipIf(sys.platform == 'win32', 'does not work on Windows')
17651769
@unittest.skipIf(AIX, 'Symbolic scope id does not work')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix incorrect result and delay in :func:`socket.getfqdn`. Patch by Dominic Socular.

0 commit comments

Comments
 (0)