classification
Title: chr raises OverflowError
Type: behavior Stage: patch review
Components: Interpreter Core Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: iritkatriel, mark.dickinson, serhiy.storchaka, terry.reedy, ukl, vstinner
Priority: normal Keywords: patch

Created on 2018-01-17 14:32 by ukl, last changed 2021-11-22 15:20 by mark.dickinson.

Files
File name Uploaded Description Edit
chr-OverflowError.patch ukl, 2018-01-17 14:32
Pull Requests
URL Status Linked Edit
PR 5218 closed vstinner, 2018-01-17 14:43
Messages (5)
msg310178 - (view) Author: Uwe Kleine-K├Ânig (ukl) * Date: 2018-01-17 14:32
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.)
msg310180 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-01-17 14:44
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.
msg310288 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-01-19 21:10
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.
msg310330 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-01-20 08:04
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 issue29833 and issue15988.
msg406721 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-11-21 14:53
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
History
Date User Action Args
2021-11-22 15:20:54mark.dickinsonsetnosy: + mark.dickinson
2021-11-21 15:29:18AlexWaygoodsettype: behavior
2021-11-21 14:53:23iritkatrielsetnosy: + iritkatriel

messages: + msg406721
versions: + Python 3.11, - Python 3.7
2018-01-20 19:11:20serhiy.storchakalinkissue29833 dependencies
2018-01-20 08:04:04serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg310330
2018-01-19 21:10:11terry.reedysetnosy: + terry.reedy
messages: + msg310288
2018-01-17 14:44:42vstinnersetnosy: + vstinner

messages: + msg310180
versions: + Python 3.7, - Python 3.6
2018-01-17 14:43:14vstinnersetstage: patch review
pull_requests: + pull_request5071
2018-01-17 14:32:06uklcreate