MCPcopy Index your code
hub / github.com/python/cpython / test_basic_inheritance

Method test_basic_inheritance

Lib/test/test_descr.py:2862–3192  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2860 super(Base, kw=1)
2861
2862 def test_basic_inheritance(self):
2863 # Testing inheritance from basic types...
2864
2865 class hexint(int):
2866 def __repr__(self):
2867 return hex(self)
2868 def __add__(self, other):
2869 return hexint(int.__add__(self, other))
2870 # (Note that overriding __radd__ doesn't work,
2871 # because the int type gets first dibs.)
2872 self.assertEqual(repr(hexint(7) + 9), "0x10")
2873 self.assertEqual(repr(hexint(1000) + 7), "0x3ef")
2874 a = hexint(12345)
2875 self.assertEqual(a, 12345)
2876 self.assertEqual(int(a), 12345)
2877 self.assertIs(int(a).__class__, int)
2878 self.assertEqual(hash(a), hash(12345))
2879 self.assertIs((+a).__class__, int)
2880 self.assertIs((a >> 0).__class__, int)
2881 self.assertIs((a << 0).__class__, int)
2882 self.assertIs((hexint(0) << 12).__class__, int)
2883 self.assertIs((hexint(0) >> 12).__class__, int)
2884
2885 class octlong(int):
2886 __slots__ = []
2887 def __str__(self):
2888 return oct(self)
2889 def __add__(self, other):
2890 return self.__class__(super(octlong, self).__add__(other))
2891 __radd__ = __add__
2892 self.assertEqual(str(octlong(3) + 5), "0o10")
2893 # (Note that overriding __radd__ here only seems to work
2894 # because the example uses a short int left argument.)
2895 self.assertEqual(str(5 + octlong(3000)), "0o5675")
2896 a = octlong(12345)
2897 self.assertEqual(a, 12345)
2898 self.assertEqual(int(a), 12345)
2899 self.assertEqual(hash(a), hash(12345))
2900 self.assertIs(int(a).__class__, int)
2901 self.assertIs((+a).__class__, int)
2902 self.assertIs((-a).__class__, int)
2903 self.assertIs((-octlong(0)).__class__, int)
2904 self.assertIs((a >> 0).__class__, int)
2905 self.assertIs((a << 0).__class__, int)
2906 self.assertIs((a - 0).__class__, int)
2907 self.assertIs((a * 1).__class__, int)
2908 self.assertIs((a ** 1).__class__, int)
2909 self.assertIs((a // 1).__class__, int)
2910 self.assertIs((1 * a).__class__, int)
2911 self.assertIs((a | 0).__class__, int)
2912 self.assertIs((a ^ 0).__class__, int)
2913 self.assertIs((a & -1).__class__, int)
2914 self.assertIs((octlong(0) << 12).__class__, int)
2915 self.assertIs((octlong(0) >> 12).__class__, int)
2916 self.assertIs(abs(octlong(0)).__class__, int)
2917
2918 # Because octlong overrides __add__, we can't check the absence of +0
2919 # optimizations using octlong.

Callers

nothing calls this directly

Calls 15

hexintClass · 0.85
strFunction · 0.85
octlongClass · 0.85
absFunction · 0.85
longcloneClass · 0.85
precfloatClass · 0.85
madcomplexClass · 0.85
madtupleClass · 0.85
madstringClass · 0.85
madunicodeClass · 0.85
sublistClass · 0.85
listClass · 0.85

Tested by

no test coverage detected