From 663d6756b16aedef9273ffbf077a4d9a614002e9 Mon Sep 17 00:00:00 2001 From: AN Long Date: Sat, 20 Jun 2026 21:38:30 +0900 Subject: [PATCH 1/3] gh-151785: Fix IPv4Interface.is_loopback to consider the network --- Lib/ipaddress.py | 4 ++++ Lib/test/test_ipaddress.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index f1062a8cd052a55..0a9f897463b2f86 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -1480,6 +1480,10 @@ def with_hostmask(self): def is_unspecified(self): return self._ip == 0 and self.network.is_unspecified + @property + def is_loopback(self): + return super().is_loopback and self.network.is_loopback + class IPv4Network(_BaseV4, _BaseNetwork): diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py index 3f017b97dc28a38..3f84592a31d467d 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -2307,6 +2307,10 @@ def testReservedIpv4(self): self.assertFalse(ipaddress.ip_interface('0.0.0.0/31').is_unspecified) self.assertFalse(ipaddress.ip_interface('1.2.3.4/32').is_unspecified) + self.assertTrue(ipaddress.ip_interface('127.0.0.1/8').is_loopback) + self.assertFalse(ipaddress.ip_interface('127.0.0.1/4').is_loopback) + self.assertFalse(ipaddress.ip_interface('1.2.3.4/32').is_loopback) + self.assertEqual(True, ipaddress.ip_interface( '192.168.1.1/17').is_private) self.assertEqual(False, ipaddress.ip_network('192.169.0.0').is_private) From e1976f0bd974a045df61a8d5fc831c3f26b2f897 Mon Sep 17 00:00:00 2001 From: AN Long Date: Sat, 20 Jun 2026 21:40:06 +0900 Subject: [PATCH 2/3] blurb it --- .../next/Library/2026-06-20-21-40-02.gh-issue-151785.YkmPlX.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-06-20-21-40-02.gh-issue-151785.YkmPlX.rst diff --git a/Misc/NEWS.d/next/Library/2026-06-20-21-40-02.gh-issue-151785.YkmPlX.rst b/Misc/NEWS.d/next/Library/2026-06-20-21-40-02.gh-issue-151785.YkmPlX.rst new file mode 100644 index 000000000000000..3b219e34ec4569c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-20-21-40-02.gh-issue-151785.YkmPlX.rst @@ -0,0 +1,2 @@ +Fix :attr:`ipaddress.IPv4Interface.is_loopback` to also consider the +interface's network, matching :class:`ipaddress.IPv6Interface`. From 0823bf6608e56c9ad552e6512756de49308e3f97 Mon Sep 17 00:00:00 2001 From: AN Long Date: Sat, 20 Jun 2026 21:55:44 +0900 Subject: [PATCH 3/3] Fix news entry --- .../next/Library/2026-06-20-21-40-02.gh-issue-151785.YkmPlX.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2026-06-20-21-40-02.gh-issue-151785.YkmPlX.rst b/Misc/NEWS.d/next/Library/2026-06-20-21-40-02.gh-issue-151785.YkmPlX.rst index 3b219e34ec4569c..b04754bb99ef364 100644 --- a/Misc/NEWS.d/next/Library/2026-06-20-21-40-02.gh-issue-151785.YkmPlX.rst +++ b/Misc/NEWS.d/next/Library/2026-06-20-21-40-02.gh-issue-151785.YkmPlX.rst @@ -1,2 +1,2 @@ -Fix :attr:`ipaddress.IPv4Interface.is_loopback` to also consider the +Fix :attr:`!ipaddress.IPv4Interface.is_loopback` to also consider the interface's network, matching :class:`ipaddress.IPv6Interface`.