(data: StringLike)
| 395 | |
| 396 | @handle |
| 397 | def make_string(data: StringLike) -> Union[bytes, ctypes.c_char_p]: |
| 398 | if (type(data) not in {VoidPointer, str, bytes, TypedCPointer}) and data: |
| 399 | raise InvalidBindingParameter( |
| 400 | f"expected a string-like object, got {repr(data)}" # noqa |
| 401 | ) |
| 402 | |
| 403 | if isinstance(data, bytes): |
| 404 | return data |
| 405 | |
| 406 | is_typed_ptr: bool = isinstance(data, TypedCPointer) |
| 407 | |
| 408 | if isinstance(data, VoidPointer) or isinstance(data, TypedCPointer): |
| 409 | # mypy is forcing me to call this twice |
| 410 | if is_typed_ptr and (data.type is not bytes): # type: ignore |
| 411 | raise InvalidBindingParameter( |
| 412 | f"{data} does not point to bytes", |
| 413 | ) |
| 414 | |
| 415 | if is_typed_ptr and (not data.alt): # type: ignore |
| 416 | return ctypes.c_char_p.from_address(data.ensure()) |
| 417 | return ctypes.c_char_p(data.ensure()) |
| 418 | |
| 419 | if isinstance(data, str): |
| 420 | return data.encode() |
| 421 | |
| 422 | if not data: |
| 423 | data = ctypes.c_char_p(None) # type: ignore |
| 424 | |
| 425 | assert isinstance(data, ctypes.c_char_p), f"{data} is not a char*" |
| 426 | return data |
| 427 | |
| 428 | |
| 429 | def make_format(*args: Format) -> Iterator[Format]: |
no test coverage detected