Library¶
-
class
ctyped.library.
Library
(name: Union[str, pathlib.Path], *, autoload: bool = True, prefix: Optional[str] = None, str_type: Type[ctyped.types.CastedTypeBase] = <class 'ctyped.types.CChars'>, int_bits: Optional[int] = None, int_sign: Optional[bool] = None)¶ Main entry point to describe C library interface.
Basic usage:
lib = Library('mylib') with lib.scope(prefix='mylib_'): @lib.function() def my_func(): ... lib.bind_types()
Parameters: - name – Shared library name or filepath.
- autoload – Load library just on Library object initialization.
- prefix –
Function name prefix to apply to functions in the library.
Useful when C functions have common prefixes.
- str_type –
Type to represent strings.
CChars
- strings as chars (ANSI) defaultCCharsW
- strings as wide chars (UTF)
Note
This setting is global to library. Can be changed on function definition level.
- int_bits –
int length to use by default.
Possible values: 8, 16, 32, 64
Note
This setting is global to library. Can be changed on function definition level.
- int_sign –
Flag. Whether to use signed (True) or unsigned (False) ints.
Note
This setting is global to library. Can be changed on function definition level.
-
bind_types
()¶ Deduces ctypes argument and result types from Python type hints, binding those types to ctypes functions.
-
cls
(*, prefix: Optional[str] = None, str_type: Optional[ctyped.types.CastedTypeBase] = None, int_bits: Optional[int] = None, int_sign: Optional[bool] = None)¶ Class decorator. Allows common parameters application for class methods.
@lib.cls(prefix='common_', str_type=CCharsW) class Wide: @staticmethod @lib.function() def get_utf(some: str) -> str: ...
Parameters: - prefix – Function name prefix to apply to functions under the manager.
- str_type – Type to represent strings.
- int_bits – int length to be used in function.
- int_sign – Flag. Whether to use signed (True) or unsigned (False) ints.
-
f
(name_c: Union[str, Callable, None] = None, *, wrap: bool = False, str_type: Optional[ctyped.types.CastedTypeBase] = None, int_bits: Optional[int] = None, int_sign: Optional[bool] = None) → Callable¶ Shortcut for
.function()
.
-
function
(name_c: Union[str, Callable, None] = None, *, wrap: bool = False, str_type: Optional[ctyped.types.CastedTypeBase] = None, int_bits: Optional[int] = None, int_sign: Optional[bool] = None) → Callable¶ Decorator to mark functions which exported from the library.
Parameters: - name_c – C function name with or without prefix (see
.scope(prefix=)
). If not set, Python function name is used. - wrap –
Do not replace decorated function with ctypes function, but with wrapper, allowing pre- or post-process ctypes function call.
Useful to organize functions to classes (to automatically pass
self
) to ctypes function to C function.class Thing(CObject): @lib.function(wrap=True) def one(self, some: int) -> int: # Implicitly pass Thing instance alongside # with explicitly passed `some` arg. ... @lib.function(wrap=True) def two(self, some:int, cfunc: Callable) -> int: # `cfunc` is a wrapper, calling an actual ctypes function. # If no arguments provided the wrapper will try detect them # automatically. result = cfunc() return result + 1
- str_type –
Type to represent strings.
Note
Overrides the same named param from library level (see
__init__
description). - int_bits –
int length to be used in function.
Note
Overrides the same named param from library level (see
__init__
description). - int_sign –
Flag. Whether to use signed (True) or unsigned (False) ints.
Note
Overrides the same named param from library level (see
__init__
description).
- name_c – C function name with or without prefix (see
-
load
()¶ Loads shared library.
-
m
(name_c: Optional[str] = None, **kwargs)¶ Shortcut for
.method()
.
-
method
(name_c: Optional[str] = None, **kwargs)¶ Decorator. The same as
.function()
withwrap=True
.
-
s
= None¶ Shortcut for
.scope()
.
-
sniff
() → ctyped.sniffer.SniffResult¶ Sniffs the library for symbols.
Sniffing result can be used as ‘ctyped’ code generator.
-
structure
(*, pack: Optional[int] = None, str_type: Optional[ctyped.types.CastedTypeBase] = None, int_bits: Optional[int] = None, int_sign: Optional[bool] = None)¶ Class decorator for C structures definition.
@lib.structure class MyStruct: first: int second: str third: 'MyStruct'
Parameters: - pack – Allows custom maximum alignment for the fields (as #pragma pack(n)).
- str_type – Type to represent strings.
- int_bits – int length to be used in function.
- int_sign – Flag. Whether to use signed (True) or unsigned (False) ints.