This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eric.frederich
Recipients eric.frederich, eric.snow, rhettinger
Date 2015-07-23.17:31:27
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1437672688.43.0.87043463352.issue24685@psf.upfronthosting.co.za>
In-reply-to
Content
Raymond,

Thanks for the explanation of your reasoning.
Could you please provide an example of how to create a cooperative subclass of OrderedDict?

I have attempted to make one.
I succeeded to make it work where the previous example failed but in doing made the example that used to work now fail.
I have attached my attempt at making a cooperative OrderedDict in inj2.py

class coop_OrderedDict(OrderedDict):
    """
    A cooperative version of OrderedDict
    """
    def __setitem__(self, k, v, **kwargs):
        # OrderedDict calls dict.__setitem__ directly skipping over LoggingDict
        # fortunately we can control this with dict_setitem keyword argument

        # calculate OrderedDict's real parent instead of skipping right to dict.__setitem__
        # though depending on the hierarchy it may actually be dict.__setitem__
        m = super(OrderedDict, self).__setitem__
        # dict_setitem wants an unbound method
        unbound_m = m.im_func
        return super(coop_OrderedDict, self).__setitem__(k, v, dict_setitem=unbound_m)

In Python2 it fails with:

Traceback (most recent call last):
  File "/tmp/inj2.py", line 51, in <module>
    old1['hooray'] = 'it worked'
  File "/tmp/inj2.py", line 30, in __setitem__
    return super(LoggingDict, self).__setitem__(k, v)
  File "/tmp/inj2.py", line 18, in __setitem__
    unbound_m = m.im_func
AttributeError: 'method-wrapper' object has no attribute 'im_func'

In Python3 both cases fail.
History
Date User Action Args
2015-07-23 17:31:28eric.frederichsetrecipients: + eric.frederich, rhettinger, eric.snow
2015-07-23 17:31:28eric.frederichsetmessageid: <1437672688.43.0.87043463352.issue24685@psf.upfronthosting.co.za>
2015-07-23 17:31:28eric.frederichlinkissue24685 messages
2015-07-23 17:31:28eric.frederichcreate