Files
Pierre-Marie de Rodat eab5ae7a8b Python API: fix charset handling for UnitProvider.auto
The low level function accepts a ctypes.c_char_p, which requires a bytes
instance, so users passing a str will get a TypeError exception. Use
the dedicated _unwrap_charset function to run the automatic conversion
in this case.

TN: VB02-014
2022-11-04 15:41:02 +00:00

36 lines
1.2 KiB
Plaintext

## vim: filetype=makopython
@classmethod
def for_project(cls, project_file, project=None, scenario_vars=None,
target=None, runtime=None):
${py_doc('libadalang.create_project_unit_provider', 8)}
prj = GPRProject(project_file, scenario_vars, target, runtime,
print_errors=False)
return prj.create_unit_provider(project)
@classmethod
def auto(cls, input_files, charset=None):
${py_doc('libadalang.create_auto_provider', 8)}
# Create a NULL-terminated array of strings
c_strings = [
ctypes.c_char_p(_coerce_bytes('input_files', f,
'a list of bytes strings'))
for f in input_files
]
c_array_type = ctypes.c_char_p * (len(input_files) + 1)
c_array = c_array_type()
for i, c_str in enumerate(c_strings):
c_array[i] = c_str
c_array[-1] = None
c_array_ptr = ctypes.pointer(c_array)
input_files_arg = ctypes.cast(c_array_ptr,
ctypes.POINTER(ctypes.c_char_p))
c_charset = _unwrap_charset(charset)
c_value = _create_auto_provider(input_files_arg, c_charset)
return cls(c_value)