This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: random.choice integer overflow v3.5.2
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: dev00790, mark.dickinson, steven.daprano
Priority: normal Keywords:

Created on 2020-04-25 08:56 by dev00790, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg367266 - (view) Author: Nicholas Prowse (dev00790) Date: 2020-04-25 08:56
Filename: random.py
Location of file: /use/lib/python3.5/random.py
Function: random.choice()
Input: 40 digit integer
Expected output: no crash
Actual output: Crash
Line number: 253
Error message: "Python int too large to convert to C ssize_t"

Traceback:
a = random.choice(sequence)
i= self._randbelow(len(seq))

Error occurs on last line 253.
msg367268 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-04-25 09:26
What you are describing is not what we mean by a crash (a core dump or segfault); it sounds like a regular Python exception.

Python 3.5 is obsolete and there are no more bug fixes for it except for security fixes.

You have not given us enough information to reproduce the problem. The description you give:

    Function: random.choice()
    Input: 40 digit integer
    Expected output: no crash
    Actual output: Crash


works for me:

    py> import random
    py> random.choice(1234567890123456789012345678901234567890) # 40 digits
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python3.5/random.py", line 253, in choice
        i = self._randbelow(len(seq))
    TypeError: object of type 'int' has no len()

is exactly the exception I would expect from using an int (whether 1 digit or 400 digits) as argument to random.choice.

Can you show us the actual code you run, and the actual traceback? Please copy and paste it as text, don't take a screen shot.
msg367271 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-04-25 10:17
I suspect that the OP is doing something like `random.choice(range(10**40))`:

    Python 3.8.2 (default, Feb 27 2020, 19:56:42) 
    [Clang 10.0.1 (clang-1001.0.46.4)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import random
    >>> random.choice(range(10**40))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/random.py", line 288, in choice
        i = self._randbelow(len(seq))
    OverflowError: Python int too large to convert to C ssize_t

The advice in this case would be to use something like random.randrange(10**40) instead.

The actual bug here, if there is a bug, has nothing to do with random, but instead has to do with "virtual" sequences whose length exceeds 2**63 - 1. There are other issues in the tracker for this, for example issue 12159.

I suggest closing as "not a bug".
msg367285 - (view) Author: Nicholas Prowse (dev00790) Date: 2020-04-25 22:26
Code as requested is similar to the below:

N = <40 digit int>
sequence=range(1,N)
a = random.choice(sequence)

Since you class the error previously mentioned as an exception, what function should be used instead for choosing a random integer between 1 and including N-1?
msg367287 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-04-25 22:57
Take your pick between either of these:

random.randrange(1, N)
random.randint(1, N-1)

https://docs.python.org/3/library/random.html#functions-for-integers
History
Date User Action Args
2022-04-11 14:59:29adminsetgithub: 84568
2020-04-27 00:01:29rhettingersetstatus: open -> closed
stage: resolved
2020-04-25 22:57:44steven.dapranosetmessages: + msg367287
2020-04-25 22:26:29dev00790setstatus: pending -> open

messages: + msg367285
2020-04-25 16:54:49mark.dickinsonsetstatus: open -> pending
resolution: not a bug
2020-04-25 10:17:10mark.dickinsonsetnosy: + mark.dickinson
messages: + msg367271
2020-04-25 09:26:50steven.dapranosettype: crash -> behavior

messages: + msg367268
nosy: + steven.daprano
2020-04-25 08:56:27dev00790create