Skip to content

gh-87027: Add prev_network function to ipaddress library (follow-up to GH-24180) - #154041

Open
777GE90 wants to merge 3 commits into
python:mainfrom
777GE90:enhancement-bpo-42861-part2
Open

gh-87027: Add prev_network function to ipaddress library (follow-up to GH-24180)#154041
777GE90 wants to merge 3 commits into
python:mainfrom
777GE90:enhancement-bpo-42861-part2

Conversation

@777GE90

@777GE90 777GE90 commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Following the merge of #24180, it was previously raised that we should add a prev_network method for symmetry, I have now done this as part of this PR so hopefully this completes the entire feature.

FYI for:
@akulakov @merwok @Fidget-Spinner @orsenthil @AA-Turner

@777GE90
777GE90 requested a review from AA-Turner as a code owner July 19, 2026 01:54
@777GE90
777GE90 force-pushed the enhancement-bpo-42861-part2 branch from 069d12d to 250a20d Compare July 19, 2026 01:57
@python-cla-bot

python-cla-bot Bot commented Jul 19, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@read-the-docs-community

read-the-docs-community Bot commented Jul 19, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33650720 | 📁 Comparing 7b79473 against main (c08c89a)

  🔍 Preview build  

3 files changed
± library/ipaddress.html
± whatsnew/3.16.html
± whatsnew/changelog.html

@777GE90
777GE90 force-pushed the enhancement-bpo-42861-part2 branch from 250a20d to 7b79473 Compare July 19, 2026 02:02
@picnixz picnixz changed the title bpo-42861 (part 2): Add prev_network function to ipaddress library, missing from previous PR gh-87027: Add prev_network function to ipaddress library, missing from previous PR Jul 19, 2026
@picnixz picnixz changed the title gh-87027: Add prev_network function to ipaddress library, missing from previous PR gh-87027: Add prev_network function to ipaddress library (follow-up to GH-24180) Jul 19, 2026
@fedonman

Copy link
Copy Markdown
Contributor

_network_by_offset() bounds new_ip from above only in the prefix > self.prefixlen branch. The other branch falls through to except OverflowError, which only the IPv4 path raises, so on IPv6 the ValueError from _BaseV6._string_from_ip_int() escapes instead of error.

Against 7b79473:

IPv4Network('255.255.255.0/24').next_network()
    -> ValueError: out of address space, cannot make another /24 network
IPv6Network('ffff:ffff:ffff:ffff:ffff:ffff:ffff:0/112').next_network()
    -> ValueError: IPv6 address is too large
IPv6Network('ffff::/16').next_network()
    -> ValueError: IPv6 address is too large
IPv6Network('ffff:ffff:ffff:ffff:ffff:ffff:ffff:0/112').next_network(next_prefix=120)
    -> ValueError: out of address space, cannot make another /120 network

Checking new_ip > self._ALL_ONES next to the existing new_ip < 0 after the branch covers both cases and makes the try/except unnecessary.

Same defect as gh-156680; GH-156681 fixes it in next_network() on main.

@777GE90

777GE90 commented Aug 30, 2026

Copy link
Copy Markdown
Contributor Author

_network_by_offset() bounds new_ip from above only in the prefix > self.prefixlen branch. The other branch falls through to except OverflowError, which only the IPv4 path raises, so on IPv6 the ValueError from _BaseV6._string_from_ip_int() escapes instead of error.

Against 7b79473:

IPv4Network('255.255.255.0/24').next_network()
    -> ValueError: out of address space, cannot make another /24 network
IPv6Network('ffff:ffff:ffff:ffff:ffff:ffff:ffff:0/112').next_network()
    -> ValueError: IPv6 address is too large
IPv6Network('ffff::/16').next_network()
    -> ValueError: IPv6 address is too large
IPv6Network('ffff:ffff:ffff:ffff:ffff:ffff:ffff:0/112').next_network(next_prefix=120)
    -> ValueError: out of address space, cannot make another /120 network

Checking new_ip > self._ALL_ONES next to the existing new_ip < 0 after the branch covers both cases and makes the try/except unnecessary.

Same defect as gh-156680; GH-156681 fixes it in next_network() on main.

Thank you for raising this, I have taken a look and it seems whilst I did write tests for this I was not explicitly checking the error message so didn't notice this happy coincidence.

I have now updated the tests to explicitly check the reason/message and refactored the logic as suggested to handle this. It's nice to clean up that try/catch logic too.

See commit c6b1d1a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants