Repository navigation
chr raises OverflowError #76763
Description
Activity
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.)
Reacted by Martin Thoma- addedinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
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.
The message I get on Windows is
OverflowError: Python int too large to convert to C longGiven that the exception type is documented, making this a clear bug, I would be tempted to fix in 3.6 also. But your decision.
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.
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
- added3.11only security fixesonly security fixesand removed3.7 (EOL)end of lifeend of life
on Nov 21, 2021 - addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Nov 21, 2021 - added a commit that references this issue
on May 14, 2023 - added a commit that references this issue
on Jan 14, 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
chrcan raise OverflowError?Also note
"%c" % 1114113raises OverflowError, should that raise ValueError instead?- added a commit that references this issue
on Feb 10, 2024 - added3.13only security fixesonly security fixesand removed3.11only security fixesonly security fixes
on Feb 10, 2024 Also note "%c" % 1114113 raises OverflowError, should that raise ValueError instead?
To clarify for myself, with change e2c4038,
chr()no longer raisesOverflowError, but always raisesValueError.@serhiy-storchaka updated the old change on the main branch and merged it.
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:
bugs.python.org fields:
Linked PRs