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.