Commit 5e77573c authored by Matthew Johnson's avatar Matthew Johnson

Add __hash__() and __eq__() to MemoryViewSliceType to avoid distinct

object id hashes resulting in duplicate utility code in specialization

The CppClassType.specialize() method uses a hash table to avoid
re-generating specializations and thus, for example, re-generating utility code:

    # values is a dict where values are instances of classes in
    # PyrexTypes.py
    key = tuple(values.items())
    if key in self.specializations:
        return self.specializations[key]
    # instantiate new CppClassType, eventually resulting in utility code

However, because MemoryViewSliceType used the default (object-id) hash,
distinct MemoryViewSliceType instances would ultimately give rise to
distinct keys. As a result, code like

     def test_memviews_same(a,b):
         cdef vector[double[:]] aa = a
         cdef vector[double[:]] bb = b

would successfully pass through the Cython compiler but then result in a
C++ compiler error due to redefinition of utility code:

    error: redefinition of 'std::vector<__Pyx_memviewslice> __pyx_convert_vector_from_py___Pyx_memviewslice(PyObject*)'

To allow the hash table in CppClassType.specialize() to identify 'equal'
specializations of CppClassType, we can add __hash__() and __eq__()
methods based on the existing
MemoryViewSliceType.same_as_resolved_type() method. Other classes in
PyrexTypes.py also have __hash__() methods defined and the choices made
here appear roughly consistent.

This addition allows the above example to compile successfully.
parent 6460e490
......@@ -552,6 +552,12 @@ class MemoryViewSliceType(PyrexType):
if not self.dtype.is_fused:
self.dtype_name = MemoryView.mangle_dtype_name(self.dtype)
def __hash__(self):
return hash(self.__class__) ^ hash(self.dtype) ^ hash(tuple(self.axes))
def __eq__(self,other):
return self.same_as_resolved_type(other)
def same_as_resolved_type(self, other_type):
return ((other_type.is_memoryviewslice and
self.dtype.same_as(other_type.dtype) and
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment