[Python-checkins] bpo-25430: improve performance of IPNetwork.__contains__ (GH-1785)

Inada Naoki webhook-mailer at python.org
Tue Apr 30 03:54:51 EDT 2019


https://github.com/python/cpython/commit/3bbcc92577f8e616bc94c679040043bacd00ebf1
commit: 3bbcc92577f8e616bc94c679040043bacd00ebf1
branch: master
author: gescheit <gescheit12 at gmail.com>
committer: Inada Naoki <songofacandy at gmail.com>
date: 2019-04-30T16:54:30+09:00
summary:

bpo-25430: improve performance of IPNetwork.__contains__ (GH-1785)

make a compare in bit-operation manner.

files:
A Misc/NEWS.d/next/Library/2019-04-15-12-22-09.bpo-25430.7_8kqc.rst
M Lib/ipaddress.py

diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
index 909a55de4f19..662d73738907 100644
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -697,8 +697,7 @@ def __contains__(self, other):
         # dealing with another address
         else:
             # address
-            return (int(self.network_address) <= int(other._ip) <=
-                    int(self.broadcast_address))
+            return other._ip & self.netmask._ip == self.network_address._ip
 
     def overlaps(self, other):
         """Tell if self is partly contained in other."""
diff --git a/Misc/NEWS.d/next/Library/2019-04-15-12-22-09.bpo-25430.7_8kqc.rst b/Misc/NEWS.d/next/Library/2019-04-15-12-22-09.bpo-25430.7_8kqc.rst
new file mode 100644
index 000000000000..922bdef56ec3
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-04-15-12-22-09.bpo-25430.7_8kqc.rst
@@ -0,0 +1 @@
+improve performance of ``IPNetwork.__contains__()``
\ No newline at end of file



More information about the Python-checkins mailing list