[clang] [libclang/python] Fix some type errors, add type annotations (PR #98745)
Jannick Kremer via cfe-commits
cfe-commits at lists.llvm.org
Sat Jul 13 06:35:33 PDT 2024
================
@@ -200,13 +236,16 @@ class _CXString(Structure):
_fields_ = [("spelling", c_char_p), ("free", c_int)]
- def __del__(self):
+ def __del__(self) -> None:
conf.lib.clang_disposeString(self)
@staticmethod
- def from_result(res, fn=None, args=None):
+ def from_result(res: _CXString, fn: Any = None, args: Any = None) -> str:
assert isinstance(res, _CXString)
- return conf.lib.clang_getCString(res)
+ pystr: str | None = conf.lib.clang_getCString(res)
+ if pystr is None:
+ return ""
+ return pystr
----------------
DeinAlptraum wrote:
`conf.lib.clang_getCString` may sometimes (though seemingly rarely) return `None` instead of a Python `str`. `_CXString.from_result()` is used in a lot of places throughout this file, and while parts of it seem to be aware of this (e.g. doc string saying that this may return `None`) other places are not (e.g. calling `len` on the return value). Many parts of the interface also directly return the result of this function, and having to check for `None` in all of these places seems potentially impractical, so I went for returning empty strings instead of `None` instead.
But due to the inconsistent usage throughout this file, I'm not sure how far this corresponds more or less to the original intention.
https://github.com/llvm/llvm-project/pull/98745
More information about the cfe-commits
mailing list