| 2079 | |
| 2080 | def test_subclasses_with_getnewargs(self): |
| 2081 | class NamedInt(int): |
| 2082 | __qualname__ = 'NamedInt' # needed for pickle protocol 4 |
| 2083 | def __new__(cls, *args): |
| 2084 | _args = args |
| 2085 | name, *args = args |
| 2086 | if len(args) == 0: |
| 2087 | raise TypeError("name and value must be specified") |
| 2088 | self = int.__new__(cls, *args) |
| 2089 | self._intname = name |
| 2090 | self._args = _args |
| 2091 | return self |
| 2092 | def __getnewargs__(self): |
| 2093 | return self._args |
| 2094 | @bltns.property |
| 2095 | def __name__(self): |
| 2096 | return self._intname |
| 2097 | def __repr__(self): |
| 2098 | # repr() is updated to include the name and type info |
| 2099 | return "{}({!r}, {})".format( |
| 2100 | type(self).__name__, |
| 2101 | self.__name__, |
| 2102 | int.__repr__(self), |
| 2103 | ) |
| 2104 | def __str__(self): |
| 2105 | # str() is unchanged, even if it relies on the repr() fallback |
| 2106 | base = int |
| 2107 | base_str = base.__str__ |
| 2108 | if base_str.__objclass__ is object: |
| 2109 | return base.__repr__(self) |
| 2110 | return base_str(self) |
| 2111 | # for simplicity, we only define one operator that |
| 2112 | # propagates expressions |
| 2113 | def __add__(self, other): |
| 2114 | temp = int(self) + int( other) |
| 2115 | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
| 2116 | return NamedInt( |
| 2117 | '({0} + {1})'.format(self.__name__, other.__name__), |
| 2118 | temp, |
| 2119 | ) |
| 2120 | else: |
| 2121 | return temp |
| 2122 | |
| 2123 | class NEI(NamedInt, Enum): |
| 2124 | __qualname__ = 'NEI' # needed for pickle protocol 4 |
no outgoing calls
searching dependent graphs…