cython
1
2cimport cython
3
4
5class IntLike(object):
6def __init__(self, value):
7self.value = value
8def __index__(self):
9return self.value
10
11
12def assign_py_hash_t(x):
13"""
14>>> assign_py_hash_t(12)
1512
16>>> assign_py_hash_t(-12)
17-12
18
19>>> assign_py_hash_t(IntLike(-3))
20-3
21>>> assign_py_hash_t(IntLike(1 << 100)) # doctest: +ELLIPSIS
22Traceback (most recent call last):
23...
24OverflowError: ...
25>>> assign_py_hash_t(IntLike(1.5)) # doctest: +ELLIPSIS
26Traceback (most recent call last):
27...
28TypeError: __index__ ... (type ...float...)
29"""
30cdef Py_hash_t h = x
31return h
32
33
34def infer_hash_type(x):
35"""
36>>> infer_hash_type(123)
37'Py_hash_t'
38"""
39h = hash(x)
40return cython.typeof(h)
41
42
43def assign_to_name(x):
44"""
45>>> assign_to_name(321)
46321
47"""
48Py_hash_t = x
49return Py_hash_t
50