Skip to content

gh-161452: Fix python -m random with a non-positive integer input - #161453

Open
andresdelfino wants to merge 1 commit into
python:mainfrom
andresdelfino:random-cli-nonpositive-integer
Open

andresdelfino wants to merge 1 commit into
python:mainfrom
andresdelfino:random-cli-nonpositive-integer

Conversation

@andresdelfino

@andresdelfino andresdelfino commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

When python -m random gets a single integer input, main() called randint() inside the try block that converts the input with int(). For an integer N <= 0, the ValueError that randint() raises for an empty range was taken to mean that the input is not an integer, so python -m random 0 printed 0.0 and a negative N printed a random negative float. This keeps only the conversion inside the try block, so an integer input now behaves like --integer N, as documented.

The new test checks that 0 and -5 raise the same ValueError with and without --integer; it fails without the fix.

🤖 Generated with Claude Code

When given a single integer input, main() called randint() inside the
same try block that converts the input with int(). The ValueError that
randint() raises for an empty range was then taken to mean that the
input is not an integer, so "python -m random 0" printed 0.0 and a
negative N printed a random negative float, instead of failing like
"--integer N" does. Only convert the input inside the try block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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