Skip to content

chr raises OverflowError #76763

Description

@ukl
mannequin
BPO 32582
Nosy @terryjreedy, @mdickinson, @vstinner, @serhiy-storchaka, @iritkatriel
PRs
  • bpo-32582: chr() doesn't raise OverflowError anymore #5218
  • Files
  • chr-OverflowError.patch
  • 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

    Activity

    1. ukl commented on Jan 17, 2018

      uklmannequin
      MannequinAuthor

      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.)

    2. vstinner commented on Jan 17, 2018

      @vstinner
      Member

      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.

    3. terryjreedy commented on Jan 19, 2018

      @terryjreedy
      Member

      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.

    4. serhiy-storchaka commented on Jan 20, 2018

      @serhiy-storchaka
      Member

      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.

    5. iritkatriel commented on Nov 21, 2021

      @iritkatriel
      Member

      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
    6. added
      3.11only security fixes
      and removed on Nov 21, 2021
    7. transferred this issue fromon Apr 10, 2022
    8. added a commit that references this issue on Jan 14, 2024
    9. added a commit that references this issue on Feb 1, 2024
    10. hauntsaninja commented on Feb 4, 2024

      @hauntsaninja
      Contributor

      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?

    11. added a commit that references this issue on Feb 10, 2024
    12. added
      3.13only security fixes
      and removed
      3.11only security fixes
      on Feb 10, 2024
    13. vstinner commented on Feb 12, 2024

      @vstinner
      Member

      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.

    14. added a commit that references this issue on Feb 14, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.13only security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions