A git_strarray wrapper Use this in order to get a git_strarray* to pass to libgit2 out of a list of strings. This has a context manager, which you should use, e.g. with StrArray(list_of_strings) as arr: C.git_function_that_takes_strarray(arr.ptr) To make a pre-exis
| 124 | |
| 125 | |
| 126 | class StrArray: |
| 127 | """A git_strarray wrapper |
| 128 | |
| 129 | Use this in order to get a git_strarray* to pass to libgit2 out of a |
| 130 | list of strings. This has a context manager, which you should use, e.g. |
| 131 | |
| 132 | with StrArray(list_of_strings) as arr: |
| 133 | C.git_function_that_takes_strarray(arr.ptr) |
| 134 | |
| 135 | To make a pre-existing git_strarray point to the provided list of strings, |
| 136 | use the context manager's assign_to() method: |
| 137 | |
| 138 | struct = ffi.new('git_strarray *', [ffi.NULL, 0]) |
| 139 | with StrArray(list_of_strings) as arr: |
| 140 | arr.assign_to(struct) |
| 141 | |
| 142 | The above construct is still subject to FFI scoping rules, i.e. the |
| 143 | contents of 'struct' only remain valid within the StrArray context. |
| 144 | """ |
| 145 | |
| 146 | __array: 'GitStrrayC | ffi.NULL_TYPE' |
| 147 | __strings: list['None | ArrayC[char]'] |
| 148 | __arr: 'ArrayC[char_pointer]' |
| 149 | |
| 150 | def __init__(self, lst: None | Sequence[str | os.PathLike[str]]): |
| 151 | # Allow passing in None as lg2 typically considers them the same as empty |
| 152 | if lst is None: |
| 153 | self.__array = ffi.NULL |
| 154 | return |
| 155 | |
| 156 | if not isinstance(lst, (list, tuple)): |
| 157 | raise TypeError('Value must be a list') |
| 158 | |
| 159 | strings: list[None | 'ArrayC[char]'] = [None] * len(lst) |
| 160 | for i in range(len(lst)): |
| 161 | li = lst[i] |
| 162 | if not isinstance(li, str) and not hasattr(li, '__fspath__'): |
| 163 | raise TypeError('Value must be a string or PathLike object') |
| 164 | |
| 165 | strings[i] = ffi.new('char []', to_bytes(li)) |
| 166 | |
| 167 | self.__arr = ffi.new('char *[]', strings) |
| 168 | self.__strings = strings |
| 169 | self.__array = ffi.new('git_strarray *', [self.__arr, len(strings)]) # type: ignore[call-overload] |
| 170 | |
| 171 | def __enter__(self) -> 'StrArray': |
| 172 | return self |
| 173 | |
| 174 | def __exit__( |
| 175 | self, |
| 176 | exc_type: Optional[type[BaseException]], |
| 177 | exc_value: Optional[BaseException], |
| 178 | traceback: Optional[TracebackType], |
| 179 | ) -> None: |
| 180 | pass |
| 181 | |
| 182 | @property |
| 183 | def ptr(self) -> 'GitStrrayC | ffi.NULL_TYPE': |
no outgoing calls
no test coverage detected
searching dependent graphs…