Skip to content

Fix 32-bit random bit counts and hash compatibility - #9437

Merged
youknowone merged 1 commit into
RustPython:mainfrom
youknowone:windows-x86-random-bits
Oct 29, 2026
Merged

youknowone merged 1 commit into
RustPython:mainfrom
youknowone:windows-x86-random-bits

Conversation

@youknowone

@youknowone youknowone commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Summary

Fix three reproducible Windows x86 compatibility failures without modifying Lib/ or existing test assertions:

  • Accept u64 bit counts in random.getrandbits(), matching CPython 3.15. Previously 2**31 was rejected on x86. Check the word allocation size and reserve fallibly before advancing the RNG.
  • Use native-word arithmetic for frozenset hashing so equal sets agree with collections.abc.Set._hash(), which masks using sys.maxsize. Existing 64-bit results are unchanged.
  • Use a fixed 64-bit slice-length prefix for byte/string hashing. This preserves existing x64 digests and fixes the existing fixed-key digest regression on x86; bytes, str, and memoryview remain hash-compatible.

Add Rust regression coverage for allocation boundaries and set hashing. Only crates/common/src/hash.rs and crates/stdlib/src/random.rs change. C API, ctypes, PyO3, and CI are untouched.

Validation

  • Actual Windows x86 and x64 release runs: all 12 relevant modules pass on each architecture (2,143 tests run, 136 skipped): random, secrets, uuid, statistics, collections, set, dict, tuple, hash, dictviews, bytes, memoryview.
  • x86 common hash unit tests: all 3 pass, including the previously failing fixed-key digest assertion.
  • Full native workspace tests and all-targets Clippy with -D warnings pass, excluding wasm, venvlauncher, and C API.
  • Separate C API Clippy passes. Separate C API tests were attempted but remain blocked by missing pythonXY.lib; no C API workaround is included.
  • Formatting, pre-commit hooks, focused automated review, and CPython comparisons pass. Oversized/negative random requests raise the expected exception without changing RNG state.

Investigation, implementation, validation, and focused automated review were assisted by Codex. The automated review is not human review.

Summary by CodeRabbit

  • Bug Fixes
    • Improved hash consistency for byte sequences and set values.
    • Improved handling of large getrandbits requests: unrepresentable or excessively large requests now return a memory error instead of proceeding with an invalid allocation. Allocation failures are also handled safely.

Accept u64 bit counts and reserve random word storage fallibly before changing RNG state. Match frozenset hashing to the native-width Set._hash mixin and use a fixed-width length prefix for byte/string hashes, preserving existing 64-bit results. Add allocation-boundary and set-hash regression coverage without changing existing test assertions.

Implementation, investigation, validation, and focused automated review assisted by Codex.

Assisted-by: Codex:GPT-5
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-28T15:07:19.299875Z
We couldn’t complete this review. Comment @codex review to try again.
b78f2d3 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between dd32b70 and b78f2d3.

Files selected for processing: 2
  • crates/common/src/hash.rs
  • crates/stdlib/src/random.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

Walkthrough

The changes update byte and frozen-set hashing, and change getrandbits to check word counts and use fallible allocation for larger requests.

Changes: Hash behavior

Layer / File(s) Summary
Byte and frozen-set hashing
crates/common/src/hash.rs
hash_bytes prefixes nonempty inputs with their length. FrozenSetHash uses usize arithmetic and converts its result through isize. A test compares results with the pointer-width set-mixin calculation.

getrandbits allocation

Layer / File(s) Summary
Checked word allocation
crates/stdlib/src/random.rs
getrandbits accepts counts representable as u64. For requests above 32 bits, it checks the word count and uses fallible reservation, returning MemoryError for invalid counts or reservation failures. Tests cover representative counts and platform limits.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Possibly related PRs

Suggested reviewers: shaharnaveh

Merge Risk: Minimal · up to b78f2

No actionable merge-blocking issue is established. The reported compatibility fixes and successful platform tests support merging after normal checks, though those results have not been independently verified.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely identifies the two main changes: fixing 32-bit random bit-count handling and restoring hash compatibility.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 6 functions across 2 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@youknowone
youknowone merged commit 723d542 into RustPython:main Oct 29, 2026
28 checks passed
@youknowone
youknowone deleted the windows-x86-random-bits branch October 29, 2026 00:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant