Repository navigation
Conversation
mahmoud
added a commit
that referenced
this pull request
Oct 25, 2026
Owner
|
Folded into #609 (see the PR description for credit). Thanks for the report and the repro. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The change
Reject negative explicit lengths in
Bits.__init__before calculating the value bound or storing the object's state. Currently,Bits(0, -1)succeeds withlen == -1;len(bits)then raisesValueError: __len__() should return >= 0, andas_bin()fails on its format specification. Zero-valued binary/hex strings and lists can produce the same invalid object.The constructor now raises
ValueError("Bits length must be non-negative"). Zero-length empty bit strings and valid positive lengths keep their behavior. Add a short docstring clarification and regression tests across the accepted input representations andfrom_int.Related open PRs #495 and #533 address oversized right shifts by clamping their resulting width. This patch validates direct constructor input; those shift changes would remain useful alongside it.
Validation on Windows, CPython 3.12.13:
python -m pytest tests/test_mathutils.py -q— 36 passed.python -m pytest --doctest-modules boltons tests -q— 700 passed, 12 subtests passed.git diff --check— passed.Other Python versions and operating systems were not tested locally.
The backstory
This came from reviewing boundary handling in the Bits constructor. An explicitly negative length could create an invalid object that only failed later during ordinary operations such as
len()andas_bin(). The change rejects that invalid input at construction time and adds regression coverage for the supported input representations.