Skip to content

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

Merged
orsenthil merged 3 commits into
python:mainfrom
777GE90:enhancement-bpo-42861-part2
Nov 15, 2026
Merged

orsenthil merged 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

@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

@ambv
ambv force-pushed the enhancement-bpo-42861-part2 branch from c6b1d1a to fe2741b Compare October 20, 2026 21:12
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Oct 20, 2026

@orsenthil orsenthil left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@orsenthil
orsenthil merged commit 8750d38 into python:main Nov 15, 2026
57 of 59 checks passed
@bedevere-bot

Copy link
Copy Markdown

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot ARM64 MacOS M1 Refleaks NoGIL 3.x (tier-1) has failed when building commit 8750d38.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1368/builds/6078) and take a look at the build logs.
  4. Check if the failure is related to this commit (8750d38) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/1368/builds/6078

Failed tests:

  • test_free_threading.test_str

Summary of the results of the build (if available):

==

Click to see traceback logs
Auto packing the repository in background for optimum performance.
See "git help gc" for manual housekeeping.
Auto packing the repository in background for optimum performance.
See "git help gc" for manual housekeeping.
Note: switching to '8750d3827ac038d3f597bc3bb3bd36b3bb9b9fe4'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 8750d3827ac gh-87027: Add prev_network function to ipaddress library (follow-up to GH-24180) (#154041)
Switched to and reset branch 'main'

configure: WARNING: pkg-config is missing. Some dependencies may not be detected correctly.

make: *** [buildbottest] Error 2

Expanding reachable commits in commit graph: 161017
Expanding reachable commits in commit graph: 169564, done.

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

Labels

type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants