Skip to content

Commit

Permalink
add 2-arg hash function (#1054)
Browse files Browse the repository at this point in the history
* add 2-arg hash function

* rm hashsalt

* Update src/PyCall.jl

---------

Co-authored-by: Steven G. Johnson <stevenj@mit.edu>
  • Loading branch information
jverzani and stevengj committed Jan 8, 2024
1 parent e54c4ee commit 1c074e2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/PyCall.jl
Expand Up @@ -271,22 +271,22 @@ end
# computing hashes of PyObjects

const pysalt = hash("PyCall.PyObject") # "salt" to mix in to PyObject hashes
hashsalt(x) = hash(x, pysalt)

function hash(o::PyObject)
function hash(o::PyObject, salt::UInt)
salt ⊻= pysalt
if ispynull(o)
hashsalt(C_NULL)
hash(C_NULL, salt)
elseif is_pyjlwrap(o)
# call native Julia hash directly on wrapped Julia objects,
# since on 64-bit Windows the Python 2.x hash is only 32 bits
hashsalt(unsafe_pyjlwrap_to_objref(o))
hash(unsafe_pyjlwrap_to_objref(o), salt)
else
h = ccall((@pysym :PyObject_Hash), Py_hash_t, (PyPtr,), o)
if h == -1 # error
pyerr_clear()
return hashsalt(PyPtr(o))
return hash(PyPtr(o), salt)
end
hashsalt(h)
hash(h, salt)
end
end

Expand Down
3 changes: 1 addition & 2 deletions src/precompile.jl
Expand Up @@ -372,7 +372,6 @@ precompile(Tuple{typeof(getproperty), PyDict{Int64, String, true}, Symbol})
precompile(Tuple{typeof(getproperty), PyError, Symbol})
precompile(Tuple{typeof(getproperty), PyObject, String})
precompile(Tuple{typeof(hash), PyObject})
precompile(Tuple{typeof(hashsalt), Function})
precompile(Tuple{typeof(hasproperty), PyObject, String})
precompile(Tuple{typeof(hasproperty), PyObject, Symbol})
precompile(Tuple{typeof(hassym), Ptr{Nothing}, Symbol})
Expand Down Expand Up @@ -528,4 +527,4 @@ precompile(Tuple{typeof(unsafe_load), Ptr{Ptr{PyObject_struct}}})
precompile(Tuple{typeof(weakref_callback), Ptr{PyObject_struct}, Ptr{PyObject_struct}})
precompile(Tuple{typeof(xor), Int64, PyObject})
precompile(Tuple{typeof(xor), PyObject, Int64})
precompile(Tuple{typeof(xor), PyObject, PyObject})
precompile(Tuple{typeof(xor), PyObject, PyObject})

0 comments on commit 1c074e2

Please sign in to comment.