Skip to content

Conversion to Py_hash_t requires an actual "int" on Python 3 #2752

Description

@jdemeyer

There is something special with the way how Cython treats conversion to Py_hash_t (as opposed to other C integer types). Usually, the appropriate __index__ (or __int__) method is called but this is not the case for Py_hash_t on Python 3: that requires an actual Python int. Also on Python 2, conversion to Py_hash_t works fine in all cases.

More precisely: this works on Python 2 and 3:

def to_int(x):
    return <Py_ssize_t>x

class X(object):
    def __index__(self): return 0

print(to_int(X()))

But this only works on Python 2:

def to_int(x):
    return <Py_hash_t>x

class X(object):
    def __index__(self): return 0

print(to_int(X()))

Activity

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

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions