Skip to content

Commit c11dcf9

Browse files
committed
fix error handling of TranslationUnit.reparse
1 parent 98e4413 commit c11dcf9

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

clang/bindings/python/clang/cindex.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,41 @@ def b(x: str | bytes) -> bytes:
152152
### Exception Classes ###
153153

154154

155+
class CXError(Exception):
156+
'''Represents C error of type enum CXErrorCode.'''
157+
158+
# A generic error code, no further details are available.
159+
#
160+
# Errors of this kind can get their own specific error codes in future
161+
# libclang versions.
162+
ERROR_FAILURE = 1
163+
164+
# libclang crashed while performing the requested operation.
165+
ERROR_CRASHED = 2
166+
167+
# The function detected that the arguments violate the function
168+
# contract.
169+
ERROR_INVALID_ARGUMENTS = 3
170+
171+
# An AST deserialization error has occurred.
172+
ERROR_AST_READ_ERROR = 4
173+
174+
error_code: int
175+
176+
def __init__(self, enumeration: int, message: str):
177+
assert isinstance(enumeration, int)
178+
179+
if enumeration < 1 or enumeration > 4:
180+
raise Exception(
181+
"Encountered undefined CXError "
182+
"constant: %d. Please file a bug to have this "
183+
"value supported." % enumeration
184+
)
185+
186+
self.error_code = enumeration
187+
Exception.__init__(self, "Error %d: %s" % (enumeration, message))
188+
189+
155190
class TranslationUnitLoadError(Exception):
156191
"""Represents an error that occurred when loading a TranslationUnit.
157192
@@ -3263,9 +3298,11 @@ def reparse(self, unsaved_files=None, options=0):
32633298
unsaved_files = []
32643299

32653300
unsaved_files_array = self.process_unsaved_files(unsaved_files)
3266-
ptr = conf.lib.clang_reparseTranslationUnit(
3301+
result = conf.lib.clang_reparseTranslationUnit(
32673302
self, len(unsaved_files), unsaved_files_array, options
32683303
)
3304+
if result != 0:
3305+
raise CXError(result, 'Error reparsing TranslationUnit.')
32693306

32703307
def save(self, filename):
32713308
"""Saves the TranslationUnit to a file.

0 commit comments

Comments
 (0)