bpo-32820: __format__ method for ipaddress (#5627)
* bits method and test_bits
* Cleaned up assert string
* blurb
* added docstring
* Faster method, per Eric Smith
* redoing as __format__
* added ipv6 method
* test cases and cleanup
* updated news
* cleanup and NEWS.d
* cleaned up old NEWS
* removed cut and paste leftover
* one more cleanup
* moved to regexp, moved away from v4- and v6-specific versions of __format__
* More cleanup, added ipv6 test cases
* more cleanup
* more cleanup
* cleanup
* cleanup
* cleanup per review, part 1
* addressed review comments around help string and regexp matching
* wrapped v6 test strings. contiguous integers: break at 72char. with underscores: break so that it looks clean.
* 's' and '' tests for pv4 and ipv6
* whitespace cleanup
* Remove trailing whitespace
* Remove more trailing whitespace
* Remove an excess blank line
diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py
index de77111..3a59a61 100644
--- a/Lib/test/test_ipaddress.py
+++ b/Lib/test/test_ipaddress.py
@@ -174,6 +174,31 @@
class AddressTestCase_v4(BaseTestCase, CommonTestMixin_v4):
factory = ipaddress.IPv4Address
+ def test_format(self):
+ v4 = ipaddress.IPv4Address("1.2.3.42")
+ v4_pairs = [
+ ("b" ,"00000001000000100000001100101010"),
+ ("n" ,"00000001000000100000001100101010"),
+ ("x" ,"0102032a"),
+ ("X" ,"0102032A"),
+ ("_b" ,"0000_0001_0000_0010_0000_0011_0010_1010"),
+ ("_n" ,"0000_0001_0000_0010_0000_0011_0010_1010"),
+ ("_x" ,"0102_032a"),
+ ("_X" ,"0102_032A"),
+ ("#b" ,"0b00000001000000100000001100101010"),
+ ("#n" ,"0b00000001000000100000001100101010"),
+ ("#x" ,"0x0102032a"),
+ ("#X" ,"0X0102032A"),
+ ("#_b" ,"0b0000_0001_0000_0010_0000_0011_0010_1010"),
+ ("#_n" ,"0b0000_0001_0000_0010_0000_0011_0010_1010"),
+ ("#_x" ,"0x0102_032a"),
+ ("#_X" ,"0X0102_032A"),
+ ("s" ,"1.2.3.42"),
+ ("" ,"1.2.3.42"),
+ ]
+ for (fmt, txt) in v4_pairs:
+ self.assertEqual(txt, format(v4, fmt))
+
def test_network_passed_as_address(self):
addr = "127.0.0.1/24"
with self.assertAddressError("Unexpected '/' in %r", addr):
@@ -261,6 +286,47 @@
class AddressTestCase_v6(BaseTestCase, CommonTestMixin_v6):
factory = ipaddress.IPv6Address
+ def test_format(self):
+
+ v6 = ipaddress.IPv6Address("::1.2.3.42")
+ v6_pairs = [
+ ("b",
+ "000000000000000000000000000000000000000000000000000000"
+ "000000000000000000000000000000000000000000000000010000"
+ "00100000001100101010"),
+ ("n", "0000000000000000000000000102032a"),
+ ("x", "0000000000000000000000000102032a"),
+ ("X", "0000000000000000000000000102032A"),
+ ("_b",
+ "0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000"
+ "_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000"
+ "_0000_0000_0000_0000_0001_0000_0010_0000_0011_0010"
+ "_1010"),
+ ("_n", "0000_0000_0000_0000_0000_0000_0102_032a"),
+ ("_x", "0000_0000_0000_0000_0000_0000_0102_032a"),
+ ("_X", "0000_0000_0000_0000_0000_0000_0102_032A"),
+ ("#b",
+ "0b0000000000000000000000000000000000000000000000000000"
+ "000000000000000000000000000000000000000000000000000100"
+ "0000100000001100101010"),
+ ("#n", "0x0000000000000000000000000102032a"),
+ ("#x", "0x0000000000000000000000000102032a"),
+ ("#X", "0X0000000000000000000000000102032A"),
+ ("#_b",
+ "0b0000_0000_0000_0000_0000_0000_0000_0000_0000_0000"
+ "_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000"
+ "_0000_0000_0000_0000_0000_0001_0000_0010_0000_0011"
+ "_0010_1010"),
+ ("#_n", "0x0000_0000_0000_0000_0000_0000_0102_032a"),
+ ("#_x", "0x0000_0000_0000_0000_0000_0000_0102_032a"),
+ ("#_X", "0X0000_0000_0000_0000_0000_0000_0102_032A"),
+ ("s", "::102:32a"),
+ ("", "::102:32a"),
+ ]
+
+ for (fmt, txt) in v6_pairs:
+ self.assertEqual(txt, format(v6, fmt))
+
def test_network_passed_as_address(self):
addr = "::1/24"
with self.assertAddressError("Unexpected '/' in %r", addr):