Message3181

Author leosoto
Recipients fwierzbicki, leosoto
Date 2008-05-03.02:15:14
SpamBayes Score 0.014143226
Marked as misclassified No
Message-id <1209780917.83.0.900264221361.issue1031@psf.upfronthosting.co.za>
In-reply-to
Content
OK, here is a test case, meant for including it on the test_dict_jy.py:

class DictCmpTest(unittest.TestCase):
    def testDictCmp(self):
        # 'Implicit' comparision of dicts against other types instances
        # shouldn't raise exception:
        self.assertNotEqual({}, '')
        # The same, but explicitly calling __cmp__ should raise TypeError:
        self.assertRaises(TypeError, {}.__cmp__, '')
    def testDictDerivedCmp(self):
        # With derived classes that doesn't override __cmp__, the behaviour
        # should be the same that with dicts:
        class derived_dict(dict): pass
        self.assertNotEqual(derived_dict(), '')
        self.assertRaises(TypeError, derived_dict().__cmp__, '')
        # But, if they *override* __cmp__ and raise TypeError from there, we
        # have exception raised when checking for equality...
        class non_comparable_dict(dict):
            def __cmp__(self, other):
                raise TypeError, "I always raise TypeError"
        self.assertRaises(TypeError, lambda: non_comparable_dict() == '')
        self.assertRaises(TypeError, non_comparable_dict().__cmp__, '')

        # The same happens even if the overridden __cmp__ doesn't
nothing apart
        # from calling super:
        class dummy_dict_with_cmp(dict):
            def __cmp__(self, other):
                return super(dummy_dict_with_cmp, self).__cmp__(other)

        self.assertEqual(dummy_dict_with_cmp(), {})
        # But TypeError is raised when comparing against other types
        self.assertRaises(TypeError, lambda: dummy_dict_with_cmp() == '')
        self.assertRaises(TypeError, dummy_dict_with_cmp().__cmp__, '')
        # Finally, the Python implementation shouldn't be tricked by not
        # implementing __cmp__ on the actual type of the dict-derived
instance,
        # but implementing it on a superclass.
        class derived_dict_with_custom_cmp(dict):
            def __cmp__(self, other):
                return 0
        class yet_another_dict(derived_dict_with_custom_cmp): pass
        self.assertEqual(derived_dict_with_custom_cmp(), '')
        self.assertEqual(yet_another_dict(), '')
History
Date User Action Args
2008-05-03 02:15:17leosotosetspambayes_score: 0.0141432 -> 0.014143226
messageid: <1209780917.83.0.900264221361.issue1031@psf.upfronthosting.co.za>
2008-05-03 02:15:17leosotosetspambayes_score: 0.0141432 -> 0.0141432
recipients: + leosoto, fwierzbicki
2008-05-03 02:15:17leosotolinkissue1031 messages
2008-05-03 02:15:15leosotocreate