GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [mypyc] Fix OverflowError when a native `__hash__` returns a large int by rheard · Pull Request #22439 · python/mypy

Conversation

@rheard

@rheard rheard commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Fixes mypyc/mypyc#1253

The tp_hash slot of a native class converted the value returned by __hash__ with CPyTagged_AsSsize_t(), so an int that doesn't fit in Py_hash_t made hash() raise OverflowError. CPython's slot_tp_hash, which a class defined in Python uses, accepts any int and hashes one that doesn't fit again with int.__hash__.

generate_hash_wrapper() now does the same:

  • A short int is still handled inline, with -1 changed to -2 as before.
  • A long int goes through the new CPy_HashFromResult() in generic_ops.c, which does what slot_tp_hash
    does with the result: an int that fits in Py_hash_t is used as is, a larger one is hashed again, and
    anything that isn't an int raises TypeError: __hash__ method should return an integer.
  • Other return types are boxed if needed and passed to CPy_HashFromResult(). With -> Any, a result that
    isn't an int used to raise TypeError: an integer is required. A __hash__ declared to return a native int
    type such as i64 segfaulted (or raised SystemError if it returned 0), because the slot passed the C value
    to PyLong_AsSsize_t() as an object pointer.

The new run test testDundersHashResult covers -> int and -> Any with values that fit in Py_hash_t, -1, values that don't fit, a long int that fits only on 64-bit platforms (2**62), True and results that aren't ints, plus an -> i64 method that returns -1. The expected results are the same on 32-bit platforms.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@rheard
[mypyc] Fix OverflowError when a native __hash__ returns a large int
96ded83
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

hash() raises OverflowError when a native __hash__ returns a large int

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@rheard
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.