Ticket #12215: trac12215_safe_callback.patch

File trac12215_safe_callback.patch, 1.3 KB (added by SimonKing, 7 years ago)

Safer callback in TripleDictEraser

  • sage/structure/coerce_dict.pyx

    # HG changeset patch
    # User Simon King <simon.king@uni-jena.de>
    # Date 1358858384 -3600
    # Node ID c0678c7fee0eb9d3534af5ad41654d8064b4423e
    # Parent  91575a5dce3c93b97283ec3cc7ceadb012f9e172
    #12215: Test for the existence of certain attributes before accessing them in a callback
    
    diff --git a/sage/structure/coerce_dict.pyx b/sage/structure/coerce_dict.pyx
    a b  
    112112            sage: len(T)    # indirect doctest
    113113            0
    114114        """
     115        cdef TripleDict D = self.D
     116        cdef list buckets = self.D.buckets
     117        if buckets is None:
     118            return
    115119        # r is a (weak) reference (typically to a parent), and it knows the
    116120        # stored key of the unique triple r() had been part of.
    117121        # We remove that unique triple from self.D
    118122        cdef size_t k1,k2,k3
    119123        k1,k2,k3 = r.key
    120124        cdef size_t h = (k1 + 13*k2 ^ 503*k3)
    121         cdef list bucket = <object>PyList_GET_ITEM(self.D.buckets, h % PyList_GET_SIZE(self.D.buckets))
     125        cdef list bucket = <object>PyList_GET_ITEM(buckets, h % PyList_GET_SIZE(buckets))
    122126        cdef int i
    123127        for i from 0 <= i < PyList_GET_SIZE(bucket) by 4:
    124128            if <size_t><object>PyList_GET_ITEM(bucket, i)==k1 and \