Skip to content

chr raises OverflowError #76763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Tracked by #74019
ukl mannequin opened this issue Jan 17, 2018 · 7 comments
Closed
Tracked by #74019

chr raises OverflowError #76763

ukl mannequin opened this issue Jan 17, 2018 · 7 comments
Labels
3.13 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@ukl
Copy link
Mannequin

ukl mannequin commented Jan 17, 2018

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

    @ukl
    Copy link
    Mannequin Author

    ukl mannequin commented Jan 17, 2018

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

    @ukl ukl mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Jan 17, 2018
    @vstinner
    Copy link
    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.

    @vstinner vstinner added the 3.7 (EOL) end of life label Jan 17, 2018
    @terryjreedy
    Copy link
    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.

    @serhiy-storchaka
    Copy link
    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.

    @iritkatriel
    Copy link
    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

    @iritkatriel iritkatriel added 3.11 only security fixes and removed 3.7 (EOL) end of life labels Nov 21, 2021
    @AlexWaygood AlexWaygood added the type-bug An unexpected behavior, bug, or error label Nov 21, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    furkanonder added a commit to furkanonder/cpython that referenced this issue May 14, 2023
    netbsd-srcmastr referenced this issue in NetBSD/pkgsrc Jan 14, 2024
    2.1.2 (2023-08-03)
    ------------------
    
    - Fix test failures on Python 3.11.4+
    - Fix an incorrect type hint
    - Add project URLs to setup.py
    
    2.1.1 (2022-12-09)
    ------------------
    
    - :func:`~w3lib.url.safe_url_string`, :func:`~w3lib.url.safe_download_url`
      and :func:`~w3lib.url.canonicalize_url` now strip whitespace and control
      characters urls according to the URL living standard.
    
    
    2.1.0 (2022-11-28)
    ------------------
    
    -   Dropped Python 3.6 support, and made Python 3.11 support official.
    
    -   :func:`~w3lib.url.safe_url_string` now generates safer URLs.
    
        To make URLs safer for the `URL living standard`_:
    
        .. _URL living standard: https://url.spec.whatwg.org/
    
        -   ``;=`` are percent-encoded in the URL username.
    
        -   ``;:=`` are percent-encoded in the URL password.
    
        -   ``'`` is percent-encoded in the URL query if the URL scheme is `special
            <https://url.spec.whatwg.org/#special-scheme>`__.
    
        To make URLs safer for `RFC 2396`_ and `RFC 3986`_, ``|[]`` are
        percent-encoded in URL paths, queries, and fragments.
    
        .. _RFC 2396: https://www.ietf.org/rfc/rfc2396.txt
        .. _RFC 3986: https://www.ietf.org/rfc/rfc3986.txt
    
    
    
    -   :func:`~w3lib.encoding.html_to_unicode` now checks for the `byte order
        mark`_ before inspecting the ``Content-Type`` header when determining the
        content encoding, in line with the `URL living standard`_.
    
        .. _byte order mark: https://en.wikipedia.org/wiki/Byte_order_mark
    
    -   :func:`~w3lib.url.canonicalize_url` now strips spaces from the input URL,
        to be more in line with the `URL living standard`_.
    
    -   :func:`~w3lib.html.get_base_url` now ignores HTML comments.
    
    -   Fixed :func:`~w3lib.url.safe_url_string` re-encoding percent signs on
        the URL username and password even when they were being used as part of an
        escape sequence.
    
    -   Fixed :func:`~w3lib.http.basic_auth_header` using the wrong flavor of
        base64 encoding, which could prevent authentication in rare cases.
    
    -   Fixed :func:`~w3lib.html.replace_entities` raising :exc:`OverflowError` in
        some cases due to `a bug in CPython
        <https://github.com/python/cpython/issues/76763>`__.
    
    -   Improved typing and fixed typing issues.
    
    -   Made CI and test improvements.
    
    -   Adopted a Code of Conduct.
    
    
    2.0.1 (2022-08-11)
    ------------------
    Minor documentation fix (release date is set in the changelog).
    
    2.0.0 (2022-08-11)
    ------------------
    
    Backwards incompatible changes:
    
    - Python 2 is no longer supported; Python 3.6+ is required now
    - :func:`w3lib.url.safe_url_string` and :func:`w3lib.url.canonicalize_url`
      no longer convert "%23" to "#" when it appears in the URL path. This is a bug
      fix. It's listed as a backward-incomatible change because in some cases the
      output of :func:`w3lib.url.canonicalize_url` is going to change, and so, if
      this output is used to generate URL fingerprints, new fingerprints might be
      incompatible with those created with the previous w3lib versions
    
    
    Deprecation removals
    
    - The ``w3lib.form`` module is removed.
    - The ``w3lib.html.remove_entities`` function is removed.
    - The ``w3lib.url.urljoin_rfc`` function is removed.
    
    The following functions are deprecated, and will be removed in future releases:
    
    - ``w3lib.util.str_to_unicode``
    - ``w3lib.util.unicode_to_str``
    - ``w3lib.util.to_native_str``
    
    Other improvements and bug fixes:
    
    - Type annotations are added
    - Added support for Python 3.9 and 3.10
    - Fixed :func:`w3lib.html.get_meta_refresh` for ``<meta>`` tags where
      ``http-equiv`` is written after ``content``
    - Fixed :func:`w3lib.url.safe_url_string` for IDNA domains with ports
    - :func:`w3lib.url.url_query_cleaner` no longer adds an unneeded ``#`` when
      ``keep_fragments=True`` is passed, and the URL doesn't have a fragment
    
    - Removed a workaround for an ancient pathname2url bug
    - CI is migrated to GitHub Actions
    - The code is formatted using black
    
    1.22.0 (2020-05-13)
    -------------------
    
    - Python 3.4 is no longer supported
    - :func:`w3lib.url.safe_url_string` now supports an optional ``quote_path``
      parameter to disable the percent-encoding of the URL path
    - :func:`w3lib.url.add_or_replace_parameter` and
      :func:`w3lib.url.add_or_replace_parameters` no longer remove duplicate
      parameters from the original query string that are not being added or
      replaced
    - :func:`w3lib.html.remove_tags` now raises a :exc:`ValueError` exception
      instead of :exc:`AssertionError` when using both the ``which_ones`` and the
      ``keep`` parameters
    - Test improvements
    - Documentation improvements
    - Code cleanup
    serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Feb 1, 2024
    … values
    
    Previously it raised OverflowError for very large or very small values.
    @hauntsaninja
    Copy link
    Contributor

    hauntsaninja commented 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?

    serhiy-storchaka added a commit that referenced this issue Feb 10, 2024
    GH-114882)
    
    Previously it raised OverflowError for very large or very small values.
    @serhiy-storchaka serhiy-storchaka added 3.13 bugs and security fixes and removed 3.11 only security fixes labels Feb 10, 2024
    @vstinner
    Copy link
    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.

    fsc-eriker pushed a commit to fsc-eriker/cpython that referenced this issue Feb 14, 2024
    … values (pythonGH-114882)
    
    Previously it raised OverflowError for very large or very small values.
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.13 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    6 participants