(self)
| 2258 | test_pickle_dump_load(self.assertIs, NEI) |
| 2259 | |
| 2260 | def test_subclasses_with_reduce_ex(self): |
| 2261 | class NamedInt(int): |
| 2262 | __qualname__ = 'NamedInt' # needed for pickle protocol 4 |
| 2263 | def __new__(cls, *args): |
| 2264 | _args = args |
| 2265 | name, *args = args |
| 2266 | if len(args) == 0: |
| 2267 | raise TypeError("name and value must be specified") |
| 2268 | self = int.__new__(cls, *args) |
| 2269 | self._intname = name |
| 2270 | self._args = _args |
| 2271 | return self |
| 2272 | def __reduce_ex__(self, proto): |
| 2273 | return self.__class__, self._args |
| 2274 | @bltns.property |
| 2275 | def __name__(self): |
| 2276 | return self._intname |
| 2277 | def __repr__(self): |
| 2278 | # repr() is updated to include the name and type info |
| 2279 | return "{}({!r}, {})".format( |
| 2280 | type(self).__name__, |
| 2281 | self.__name__, |
| 2282 | int.__repr__(self), |
| 2283 | ) |
| 2284 | def __str__(self): |
| 2285 | # str() is unchanged, even if it relies on the repr() fallback |
| 2286 | base = int |
| 2287 | base_str = base.__str__ |
| 2288 | if base_str.__objclass__ is object: |
| 2289 | return base.__repr__(self) |
| 2290 | return base_str(self) |
| 2291 | # for simplicity, we only define one operator that |
| 2292 | # propagates expressions |
| 2293 | def __add__(self, other): |
| 2294 | temp = int(self) + int( other) |
| 2295 | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
| 2296 | return NamedInt( |
| 2297 | '({0} + {1})'.format(self.__name__, other.__name__), |
| 2298 | temp, |
| 2299 | ) |
| 2300 | else: |
| 2301 | return temp |
| 2302 | |
| 2303 | class NEI(NamedInt, Enum): |
| 2304 | __qualname__ = 'NEI' # needed for pickle protocol 4 |
| 2305 | x = ('the-x', 1) |
| 2306 | y = ('the-y', 2) |
| 2307 | |
| 2308 | self.assertIs(NEI.__new__, Enum.__new__) |
| 2309 | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
| 2310 | globals()['NamedInt'] = NamedInt |
| 2311 | globals()['NEI'] = NEI |
| 2312 | NI5 = NamedInt('test', 5) |
| 2313 | self.assertEqual(NI5, 5) |
| 2314 | test_pickle_dump_load(self.assertEqual, NI5, 5) |
| 2315 | self.assertEqual(NEI.y.value, 2) |
| 2316 | test_pickle_dump_load(self.assertIs, NEI.y) |
| 2317 | test_pickle_dump_load(self.assertIs, NEI) |
nothing calls this directly
no test coverage detected