MCPcopy Index your code
hub / github.com/ZeroIntensity/pointers.py / make_string

Function make_string

src/pointers/bindings.py:397–426  ·  view source on GitHub ↗
(data: StringLike)

Source from the content-addressed store, hash-verified

395
396@handle
397def 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
429def make_format(*args: Format) -> Iterator[Format]:

Callers 15

wrapperFunction · 0.85
va_parseMethod · 0.85
size_from_formatMethod · 0.85
from_string_and_sizeMethod · 0.85
from_format_vMethod · 0.85
from_stringMethod · 0.85
from_string_and_sizeMethod · 0.85
get_pointerMethod · 0.85
import_Method · 0.85
is_validMethod · 0.85
newMethod · 0.85

Calls 3

encodeMethod · 0.80
ensureMethod · 0.45

Tested by

no test coverage detected