Changes between Version 1 and Version 86 of Ticket #13394

11/01/13 15:22:09 (5 years ago)

Good to go for me! Incidentally, we're running into something I can only explain as an illegal optimization in python's print command:

sage: from sage.misc.weak_dict import WeakValueDictionary
sage: D=WeakValueDictionary()
sage: D[1]=QQ
sage: print D
{1: <weakref at 0x34128f0; to 'RationalField_with_category' at 0x1ffb4d0>}
sage: repr(D)
'<WeakValueDictionary at 0x6225ae0>'
sage: str(D)
'<WeakValueDictionary at 0x6225ae0>'

and it's an odd optimization too. The only explanation I can see is that print doesn't think dict can be subclassed and thus, based on a type test, decides to use PyDict routines on it. I'm afraid that our solution won't necessarily work in all places where a UserDict would get recognized as a type that needs method resolution.

EDIT: I think the problem is that the dict type has its tp_print field set, so we inherit that routine. Ideally we should override it, but I don't think cython provides support for that. Discussion on cython-users has some details. We could hack our way around it by doing

cdef extern from "Python.h":
    ctypedef struct PyTypeObject:
        void * tp_print

cdef WeakValueDictionary(dict):
    def __init__(self):

#clear the tp_print field on the type after PyType_Ready has executed on it.
(<PyTypeObject *><void *>WeakValueDictionary).tp_print = NULL 

It feels like a horrible hack but it does have the desired effect.

UPDATE: Cython is planning to do this for us in their next release.


  • Ticket #13394

    • Property Status changed from new to positive_review
    • Property Reviewers changed from to Nils Bruin, Simon King
    • Property Authors changed from to Simon King, Nils Bruin
    • Property Cc SimonKing added
    • Property Milestone changed from sage-5.11 to sage-5.12
    • Property Branch changed from to u/SimonKing/ticket/13394
    • Property Commit changed from to 1a12ce6982bbe1f050e4ae627972023d5ddfa5ef
    • Property Report Upstream changed from N/A to None of the above - read trac for reasoning.
  • Ticket #13394 – Description

    v1 v86  
    11On ticket #12313 we found that the use of `WeakValueDictionaries` as caches can cause `removal` callbacks in rather harsh environments. Normal `WeakValueDictionaries` remove keys with dead values by looking up the key. This involves Python equality testing on the key, which can cause any kind of operation in Sage. We need a dictionary where we can delete entries without key comparisons. See below for possible strategies.
     3'''To the release manager:''' please fold this branch upon merging. There is quite a bit of back-and-forth in the history of this branch that is not very instructive to have in the global history of sage.