| 110 | } |
| 111 | |
| 112 | def __new__(cls, id, /, _whence=None, _ownsref=None): |
| 113 | # There is only one instance for any given ID. |
| 114 | if not isinstance(id, int): |
| 115 | raise TypeError(f'id must be an int, got {id!r}') |
| 116 | id = int(id) |
| 117 | if _whence is None: |
| 118 | if _ownsref: |
| 119 | _whence = _interpreters.WHENCE_STDLIB |
| 120 | else: |
| 121 | _whence = _interpreters.whence(id) |
| 122 | assert _whence in cls._WHENCE_TO_STR, repr(_whence) |
| 123 | if _ownsref is None: |
| 124 | _ownsref = (_whence == _interpreters.WHENCE_STDLIB) |
| 125 | try: |
| 126 | self = _known[id] |
| 127 | assert hasattr(self, '_ownsref') |
| 128 | except KeyError: |
| 129 | self = super().__new__(cls) |
| 130 | _known[id] = self |
| 131 | self._id = id |
| 132 | self._whence = _whence |
| 133 | self._ownsref = _ownsref |
| 134 | if _ownsref: |
| 135 | # This may raise InterpreterNotFoundError: |
| 136 | _interpreters.incref(id) |
| 137 | return self |
| 138 | |
| 139 | def __repr__(self): |
| 140 | return f'{type(self).__name__}({self.id})' |