(self)
| 2228 | test_pickle_dump_load(self.assertIs, NEI) |
| 2229 | |
| 2230 | def test_subclasses_with_reduce_ex(self): |
| 2231 | class NamedInt(int): |
| 2232 | __qualname__ = 'NamedInt' # needed for pickle protocol 4 |
| 2233 | def __new__(cls, *args): |
| 2234 | _args = args |
| 2235 | name, *args = args |
| 2236 | if len(args) == 0: |
| 2237 | raise TypeError("name and value must be specified") |
| 2238 | self = int.__new__(cls, *args) |
| 2239 | self._intname = name |
| 2240 | self._args = _args |
| 2241 | return self |
| 2242 | def __reduce_ex__(self, proto): |
| 2243 | return self.__class__, self._args |
| 2244 | @bltns.property |
| 2245 | def __name__(self): |
| 2246 | return self._intname |
| 2247 | def __repr__(self): |
| 2248 | # repr() is updated to include the name and type info |
| 2249 | return "{}({!r}, {})".format( |
| 2250 | type(self).__name__, |
| 2251 | self.__name__, |
| 2252 | int.__repr__(self), |
| 2253 | ) |
| 2254 | def __str__(self): |
| 2255 | # str() is unchanged, even if it relies on the repr() fallback |
| 2256 | base = int |
| 2257 | base_str = base.__str__ |
| 2258 | if base_str.__objclass__ is object: |
| 2259 | return base.__repr__(self) |
| 2260 | return base_str(self) |
| 2261 | # for simplicity, we only define one operator that |
| 2262 | # propagates expressions |
| 2263 | def __add__(self, other): |
| 2264 | temp = int(self) + int( other) |
| 2265 | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
| 2266 | return NamedInt( |
| 2267 | '({0} + {1})'.format(self.__name__, other.__name__), |
| 2268 | temp, |
| 2269 | ) |
| 2270 | else: |
| 2271 | return temp |
| 2272 | |
| 2273 | class NEI(NamedInt, Enum): |
| 2274 | __qualname__ = 'NEI' # needed for pickle protocol 4 |
| 2275 | x = ('the-x', 1) |
| 2276 | y = ('the-y', 2) |
| 2277 | |
| 2278 | self.assertIs(NEI.__new__, Enum.__new__) |
| 2279 | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
| 2280 | globals()['NamedInt'] = NamedInt |
| 2281 | globals()['NEI'] = NEI |
| 2282 | NI5 = NamedInt('test', 5) |
| 2283 | self.assertEqual(NI5, 5) |
| 2284 | test_pickle_dump_load(self.assertEqual, NI5, 5) |
| 2285 | self.assertEqual(NEI.y.value, 2) |
| 2286 | test_pickle_dump_load(self.assertIs, NEI.y) |
| 2287 | test_pickle_dump_load(self.assertIs, NEI) |
nothing calls this directly
no test coverage detected