(self)
| 2198 | test_pickle_dump_load(self.assertIs, NEI) |
| 2199 | |
| 2200 | def test_subclasses_with_reduce(self): |
| 2201 | class NamedInt(int): |
| 2202 | __qualname__ = 'NamedInt' # needed for pickle protocol 4 |
| 2203 | def __new__(cls, *args): |
| 2204 | _args = args |
| 2205 | name, *args = args |
| 2206 | if len(args) == 0: |
| 2207 | raise TypeError("name and value must be specified") |
| 2208 | self = int.__new__(cls, *args) |
| 2209 | self._intname = name |
| 2210 | self._args = _args |
| 2211 | return self |
| 2212 | def __reduce__(self): |
| 2213 | return self.__class__, self._args |
| 2214 | @bltns.property |
| 2215 | def __name__(self): |
| 2216 | return self._intname |
| 2217 | def __repr__(self): |
| 2218 | # repr() is updated to include the name and type info |
| 2219 | return "{}({!r}, {})".format( |
| 2220 | type(self).__name__, |
| 2221 | self.__name__, |
| 2222 | int.__repr__(self), |
| 2223 | ) |
| 2224 | def __str__(self): |
| 2225 | # str() is unchanged, even if it relies on the repr() fallback |
| 2226 | base = int |
| 2227 | base_str = base.__str__ |
| 2228 | if base_str.__objclass__ is object: |
| 2229 | return base.__repr__(self) |
| 2230 | return base_str(self) |
| 2231 | # for simplicity, we only define one operator that |
| 2232 | # propagates expressions |
| 2233 | def __add__(self, other): |
| 2234 | temp = int(self) + int( other) |
| 2235 | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
| 2236 | return NamedInt( |
| 2237 | '({0} + {1})'.format(self.__name__, other.__name__), |
| 2238 | temp, |
| 2239 | ) |
| 2240 | else: |
| 2241 | return temp |
| 2242 | |
| 2243 | class NEI(NamedInt, Enum): |
| 2244 | __qualname__ = 'NEI' # needed for pickle protocol 4 |
| 2245 | x = ('the-x', 1) |
| 2246 | y = ('the-y', 2) |
| 2247 | |
| 2248 | |
| 2249 | self.assertIs(NEI.__new__, Enum.__new__) |
| 2250 | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
| 2251 | globals()['NamedInt'] = NamedInt |
| 2252 | globals()['NEI'] = NEI |
| 2253 | NI5 = NamedInt('test', 5) |
| 2254 | self.assertEqual(NI5, 5) |
| 2255 | test_pickle_dump_load(self.assertEqual, NI5, 5) |
| 2256 | self.assertEqual(NEI.y.value, 2) |
| 2257 | test_pickle_dump_load(self.assertIs, NEI.y) |
nothing calls this directly
no test coverage detected