================ @@ -611,51 +612,25 @@ def register(value, name): ### Cursor Kinds ### -class BaseEnumeration: +class BaseEnumeration(Enum): """ Common base class for named enumerations held in sync with Index.h values. - - Subclasses must define their own _kinds and _name_map members, as: - _kinds = [] - _name_map = None - These values hold the per-subclass instances and value-to-name mappings, - respectively. - """ - def __init__(self, value): - if value >= len(self.__class__._kinds): - self.__class__._kinds += [None] * (value - len(self.__class__._kinds) + 1) - if self.__class__._kinds[value] is not None: - raise ValueError( - "{0} value {1} already loaded".format(str(self.__class__), value) - ) - self.value = value - self.__class__._kinds[value] = self - self.__class__._name_map = None def from_param(self): return self.value - @property - def name(self): - """Get the enumeration name of this cursor kind.""" - if self._name_map is None: - self._name_map = {} - for key, value in self.__class__.__dict__.items(): - if isinstance(value, self.__class__): - self._name_map[value] = key - return self._name_map[self] - @classmethod def from_id(cls, id): - if id < 0 or id >= len(cls._kinds) or cls._kinds[id] is None: - raise ValueError("Unknown template argument kind %d" % id) - return cls._kinds[id] + try: + return cls(id) + except ValueError: + raise ValueError("Unknown %s %d" % (cls.__name__, id)) from None ---------------- DeinAlptraum wrote:
Thank you for the feedback! I've removed the `try`-`except` block now so we're just using the `Enum` error message Irrelevant now, but regarding `from None`: This effectively removes the previous exception context. Since we are re-raising an exception here, we would get both the exception we raised ourselves _and_ the exception that we caught here. E.g. currently we get ``` >>> cindex.TokenKind.from_id(5) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/data/shared/programming/repos/llvm-project/clang/bindings/python/clang/cindex.py", line 589, in from_id raise ValueError("Unknown %s %d" % (cls.__name__, id)) from None ValueError: Unknown TokenKind 5 ``` and without the `from None` we would get ``` >>> cindex.TokenKind.from_id(5) Traceback (most recent call last): File "/data/shared/programming/repos/llvm-project/clang/bindings/python/clang/cindex.py", line 587, in from_id return cls(id) ^^^^^^^ File "/usr/lib/python3.12/enum.py", line 757, in __call__ return cls.__new__(cls, value) ^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.12/enum.py", line 1171, in __new__ raise ve_exc ValueError: 5 is not a valid TokenKind During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/data/shared/programming/repos/llvm-project/clang/bindings/python/clang/cindex.py", line 589, in from_id raise ValueError("Unknown %s %d" % (cls.__name__, id)) ValueError: Unknown TokenKind 5 ``` https://github.com/llvm/llvm-project/pull/95608 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits