GeezerVison is showing github.com — chr raises OverflowError · Issue #76763 · python/cpython
chr raises OverflowError #76763Labels 3.13only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error Description
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = None
closed_at = None
created_at = <Date 2018-01-17.14:32:06.372>
labels = ['interpreter-core', 'type-bug', '3.11']
title = 'chr raises OverflowError'
updated_at = <Date 2021-11-22.15:20:54.228>
user = 'https://bugs.python.org/ukl'
bugs.python.org fields:
activity = <Date 2021-11-22.15:20:54.228>
actor = 'mark.dickinson'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Interpreter Core']
creation = <Date 2018-01-17.14:32:06.372>
creator = 'ukl'
dependencies = []
files = ['47390']
hgrepos = []
issue_num = 32582
keywords = ['patch']
message_count = 5.0
messages = ['310178', '310180', '310288', '310330', '406721']
nosy_count = 6.0
nosy_names = ['terry.reedy', 'mark.dickinson', 'vstinner', 'serhiy.storchaka', 'ukl', 'iritkatriel']
pr_nums = ['5218']
priority = 'normal'
resolution = None
stage = 'patch review'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue32582'
versions = ['Python 3.11']
Linked PRs
Activityukl commented on Jan 17, 2018 MannequinAuthor More actions Hello,
the description for chr (from https://docs.python.org/3/library/functions.html#chr) reads as:
Return the string representing a character whose Unicode code
point is the integer i. [...] The valid range for the argument
is from 0 through 1,114,111 (0x10FFFF in base 16). ValueError
will be raised if i is outside that range.
If however a value > 0x7fffffff (or < -0x80000000) is provided, the function raises an Overflow error:
$ python3 -c 'print(chr(0x80000000))'
Traceback (most recent call last):
File "<string>", line 1, in <module>
OverflowError: signed integer is greater than maximum
This is either a documentation problem or (more like) an implementation issue. I attached a patch that fixes the issue for me. (I'm not sure however if I should call PyErr_Clear() before raising ValueError.) vstinner commented on Jan 17, 2018 It's more an implementation issue.
I proposed PR 5218 to also raise a ValueError on overflow.
I propose to only change Python 3.7, and only Python 2.7 and 3.6 unchanged. terryjreedy commented on Jan 19, 2018 The message I get on Windows is
OverflowError: Python int too large to convert to C long
Given that the exception type is documented, making this a clear bug, I would be tempted to fix in 3.6 also. But your decision. serhiy-storchaka commented on Jan 20, 2018 Perhaps this issue should be raised on Python-Dev, since it changes the old and well-known behavior of buildins. I think that ValueError is more appropriate, but in past we decided to keep OverflowError for compatibility.
If change chr(), other code needs to be changed. "%c", PyUnicode_FromOrdinal(), PyUnicode_Format(), and possible the same for bytes.
See also bpo-29833 and bpo-15988. iritkatriel commented on Nov 21, 2021 Still raising OVerflowError on 3.11:
>>> print(chr(0x80000000))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C int hauntsaninja commented on Feb 4, 2024 Given that this is a long standing behaviour and there's little reason to be passing large values here, should we just document that chr can raise OverflowError?
Also note "%c" % 1114113 raises OverflowError, should that raise ValueError instead? added a commit that references this issue on Feb 10, 2024gh-76763: Make chr() always raising ValueError for out-of-range values ( vstinner commented on Feb 12, 2024
Also note "%c" % 1114113 raises OverflowError, should that raise ValueError instead?
To clarify for myself, with change e2c4038, chr() no longer raises OverflowError, but always raises ValueError.
@serhiy-storchaka updated the old change on the main branch and merged it.
MetadataMetadata3.13only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error DevelopmentNo branches or pull requestsIssue actions
|