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 on empty sequence should hide previous exception [patch]
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: josh.r, mark.dickinson, python-dev, rhettinger, then0rTh
Priority: normal Keywords: patch

Created on 2016-12-23 15:56 by then0rTh, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
random_choice_errmsg.patch then0rTh, 2016-12-23 15:56 patch for random.py review
Messages (4)
msg283884 - (view) Author: (then0rTh) Date: 2016-12-23 15:56
Passing empty sequence to random.choice function leads to:


Traceback (most recent call last):
  ...
ValueError: number of bits must be greater than zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  ...
IndexError: Cannot choose from an empty sequence


* the ValueError doesn't add any useful information, only bloats stderr
* the "During handling" line indicates that something went wrong inside random.py


This patch uses `raise x from None` to hide the ValueError, resulting in much cleaner output.

-Tested on Python 3.7.0a0
msg283886 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2016-12-23 16:47
Seems reasonable to me.
msg284074 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-12-27 09:07
New changeset 0001ae913759 by Raymond Hettinger in branch '3.6':
Issue #29055: Suppress upstream exception for random.choice()
https://hg.python.org/cpython/rev/0001ae913759
msg284075 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-12-27 09:09
Thanks for the patch.

Please submit a contributor agreement for future work.  We would like to have it for all patches (though this one was so tiny it isn't strictly required).
History
Date User Action Args
2022-04-11 14:58:41adminsetgithub: 73241
2016-12-27 09:09:33rhettingersetstatus: open -> closed
resolution: fixed
messages: + msg284075

versions: + Python 3.6
2016-12-27 09:07:23python-devsetnosy: + python-dev
messages: + msg284074
2016-12-27 08:55:50rhettingersetassignee: rhettinger
2016-12-23 16:47:03josh.rsetnosy: + josh.r
messages: + msg283886
2016-12-23 15:56:57then0rThcreate