Why does weakproxy not always preserve equivalence in python?
Question: MySQLDb uses weak proxy to prevent circular dependencies between cursors and
connections.

But you would expect from the documentation on weakref that you could still
tests for equivalence. Yet:

    
    
    In [36]: interactive.cursor.connection.thread_id()
    Out[36]: 4267758
    
    In [37]: interactive.web_logic.conns.primary.thread_id()
    Out[37]: 4267758
    
    In [38]: interactive.cursor.connection == interactive.web_logic.conns.primary
    Out[38]: False
    
    In [39]: interactive.cursor.connection
    Out[39]: <weakproxy at 0x3881c60 to Connection at 0x94c010>
    
    In [40]: interactive.web_logic.conns.primary
    Out[40]: <_mysql.connection open to 'xendb01' at 94c010>
    

How do I tell if the connections are the same ?


Answer: I've long found `weakref.proxy`'s design and implementation to be somewhat
shaky. Witness...:

    
    
    >>> import weakref
    >>> ob=set(range(23))
    >>> rob=weakref.proxy(ob)
    >>> rob==ob
    False
    >>> rob.__eq__(ob)
    True
    

...DEFINITELY peculiar! In practice what I use from `weakref` are weak-key or
sometimes weak-value dictionaries; but `weakref.ref` is sounder than the proxy
wrapper on top of it:

    
    
    >>> wr=weakref.ref(ob)
    >>> wr()==ob
    True
    

The need to "call" the ref to get the object (or None if the object has since
disappeared) unfortunately makes it non-transparent (so a DB API module
couldn't do it while staying compliant to the API). I don't understand why
MySqlDb wants weak cursor->connection referencing at all, but if they do I see
why they felt they had to use proxies rather than refs. However, one pays a
very high price for that transparency!

Btw, the "explicit `__eq__`" trick (or an equivalent one with `__cmp__`,
depending on the type of the underlying object) may help you, even though it's
definitely inelegant!

