Skip to content

Hint-friendly pyglet.gl.* funcs #1237

@iperov

Description

@iperov

Describe the solution you'd like

pyglet.gl.gl* funcs have no popup hint of arguments

here is my code example to use annotations which will work as IDE hint and ctypes configurator

dlls_by_name = {}

def dll_import(dll_name):
    """
    decorator for import DLL func.
    always annotate return type even if it is None !
    """
    dll_name = str(dll_name)

    def decorator(func):
        dll_func = None
        def wrapper(*args):
            nonlocal dll_func

            if dll_func is None:
                dll = dlls_by_name.get(dll_name, None)
                if dll is None:
                    try:
                        dll = ctypes.cdll.LoadLibrary(find_library(dll_name))
                    except:
                        pass

                    if dll is None:
                        raise RuntimeError(f'Unable to load {dll_name} library.')

                    dlls_by_name[dll_name] = dll

                dll_func = getattr(dll, func.__name__)
                anno = list(get_type_hints(func).values())
                anno = [ None if x is type(None) else x for x in anno ]

                dll_func.argtypes = anno[:-1]
                dll_func.restype = anno[-1]

            return dll_func(*args)

        return wrapper
    return decorator

@dll_import('OpenCL')
def clGetPlatformIDs (num_entries : cl_uint, platforms : POINTER(cl_platform_id), num_platforms : POINTER(cl_uint) ) -> cl_result: ...

Code_2024-11-27_12-36-11

Could probably be improved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions