{"record":{"id":"062f85a3b5c5564a","repo":"mono/mono","slug":"cannot-convert-to","errorCode":null,"errorMessage":"Cannot convert '{}' to '{}'","messagePattern":"Cannot convert '(.+?)' to '(.+?)'","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"mono/tools/offsets-tool/clang/cindex.py","lineNumber":100,"sourceCode":"        def __str__(self):\n            return self.value\n\n        @property\n        def value(self):\n            if super(c_char_p, self).value is None:\n                return None\n            return super(c_char_p, self).value.decode(\"utf8\")\n\n        @classmethod\n        def from_param(cls, param):\n            if isinstance(param, str):\n                return cls(param)\n            if isinstance(param, bytes):\n                return cls(param)\n            if param is None:\n                # Support passing null to C functions expecting char arrays\n                return None\n            raise TypeError(\"Cannot convert '{}' to '{}'\".format(type(param).__name__, cls.__name__))\n\n        @staticmethod\n        def to_python_string(x, *args):\n            return x.value\n\n    def b(x):\n        if isinstance(x, bytes):\n            return x\n        return x.encode('utf8')\n\nelif sys.version_info[0] == 2:\n    # Python 2 strings are utf8 byte strings, no translation is needed for\n    # C-interop.\n    c_interop_string = c_char_p\n\n    def _to_python_string(x, *args):\n        return x\n","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/mono/mono/blob/0f53e9e151d92944cacab3e24ac359410c606df6/mono/tools/offsets-tool/clang/cindex.py#L82-L118","documentation":"Raised by the from_param classmethod of the custom c_char_p subclass (the Python3 interop string type) used by the libclang ctypes bindings. ctypes calls from_param to coerce each argument to the declared C type; here only str, bytes, and None are accepted. Any other type falls through all isinstance checks and raises TypeError naming both the offending type and the target class.","triggerScenarios":"A libclang function declared with an argument of this c_interop_string type is called with a value that is not str, bytes, or None — e.g. an int (a file descriptor), a pathlib.Path passed without fspath conversion, a list, or a ctypes object. from_param is invoked by ctypes at call time, so the error surfaces at the clang_* call site.","commonSituations":"Passing a Path object directly to a function that expects a filename string (most bindings wrap these with fspath(), but a direct ctypes call or a custom binding does not). Passing an integer or None-like sentinel that is not literally None. Mixing str and bytes incorrectly across Python 2/3 code paths.","solutions":["Convert the argument to str or bytes before the call: wrap Path values with str(...) or os.fspath(...).","If you intend to pass a null pointer, pass Python None explicitly rather than 0 or an empty container.","Inspect the binding's argtypes for the failing function and match the declared c_interop_string / c_char_p expectation.","Use the higher-level cindex.py wrappers (which already call fspath/b) instead of calling conf.lib.* clang functions directly."],"exampleFix":"# before\nconf.lib.clang_parseTranslationUnit(idx, some_pathlib_path, args, len(args), None, 0, 0)\n# TypeError: Cannot convert 'PosixPath' to 'c_char_p'\n\n# after\nfrom ctypes import c_char_p\nconf.lib.clang_parseTranslationUnit(idx, str(some_pathlib_path), args, len(args), None, 0, 0)","handlingStrategy":"type-guard","validationCode":"def _as_str_or_none(v):\n    if v is None or isinstance(v, (str, bytes)):\n        return v\n    raise TypeError(f'expected str/bytes/None, got {type(v).__name__}')","typeGuard":"def isInteropString(v): return v is None or isinstance(v, (str, bytes))","tryCatchPattern":null,"preventionTips":["Always convert pathlib.Path via os.fspath/str before ctypes calls.","Prefer the high-level cindex wrappers (which call fspath/b) over direct conf.lib.* calls.","Pass None explicitly for null pointers, never 0."],"tags":["libclang","ctypes","type-conversion","python3","cindex"],"backgroundTag":null,"analyzedSha":"0f53e9e151d92944cacab3e24ac359410c606df6","analyzedAt":"2026-08-13T18:54:37.190Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}