(arg: Optional[Union[str, bytes, FfiByteBuffer]])
| 51 | |
| 52 | |
| 53 | def _encode_bytes(arg: Optional[Union[str, bytes, FfiByteBuffer]]) -> FfiByteBuffer: |
| 54 | if isinstance(arg, FfiByteBuffer): |
| 55 | return arg |
| 56 | buf = FfiByteBuffer() |
| 57 | if isinstance(arg, memoryview): |
| 58 | buf.length = arg.nbytes |
| 59 | if arg.contiguous and not arg.readonly: |
| 60 | buf.data = (c_ubyte * buf.length).from_buffer(arg.obj) |
| 61 | else: |
| 62 | buf.data = (c_ubyte * buf.length).from_buffer_copy(arg.obj) |
| 63 | elif isinstance(arg, bytearray): |
| 64 | buf.length = len(arg) |
| 65 | if buf.length > 0: |
| 66 | buf.data = (c_ubyte * buf.length).from_buffer(arg) |
| 67 | elif arg is not None: |
| 68 | if isinstance(arg, str): |
| 69 | arg = arg.encode("utf-8") |
| 70 | buf.length = len(arg) |
| 71 | if buf.length > 0: |
| 72 | buf.data = (c_ubyte * buf.length).from_buffer_copy(arg) |
| 73 | return buf |
| 74 | |
| 75 | |
| 76 | def _load_library(lib_name: str) -> CDLL: |
no test coverage detected