{"id":6425,"library":"pyclibrary","title":"pyclibrary","description":"PyCLibrary is a pure-python C parser and ctypes automation library, designed to simplify the process of creating Python bindings for C libraries. It parses C header files to automatically generate ctypes structures, unions, enums, and function prototypes, enabling easier interaction with shared C libraries. The project is actively maintained, with recent updates focused on stability and improvements, and currently supports Python 3.10 and newer.","status":"active","version":"0.3.0","language":"en","source_language":"en","source_url":"https://github.com/MatthieuDartiailh/pyclibrary","tags":["C binding","ctypes","FFI","C parser","automation","foreign function interface"],"install":[{"cmd":"pip install pyclibrary","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"CParser","correct":"from pyclibrary import CParser"},{"symbol":"CLibrary","correct":"from pyclibrary import CLibrary"}],"quickstart":{"code":"import os\nfrom pyclibrary import CParser\n\n# Create a dummy header file for demonstration\nheader_content = '''\n#define MY_VERSION_MAJOR 1\n#define MY_VERSION_MINOR 0\n\ntypedef struct {\n    int width;\n    int height;\n} Size;\n\nenum Color {\n    RED = 0,\n    GREEN,\n    BLUE\n};\n\nint calculate_area(Size s);\nvoid print_message(const char* msg);\n'''\nheader_file_path = \"temp_example_header.h\"\nwith open(header_file_path, \"w\") as f:\n    f.write(header_content)\n\ntry:\n    # Initialize the CParser with the header file(s)\n    parser = CParser([header_file_path])\n\n    print(\"--- Parsed Macros ---\")\n    print(f\"MY_VERSION_MAJOR: {parser.macros['MY_VERSION_MAJOR']}\")\n    print(f\"MY_VERSION_MINOR: {parser.macros['MY_VERSION_MINOR']}\")\n\n    print(\"\\n--- Parsed Types ---\")\n    print(f\"Size struct definition: {parser.structs['Size']}\")\n    print(f\"Color enum definition: {parser.enums['Color']}\")\n\n    print(\"\\n--- Parsed Functions ---\")\n    print(f\"calculate_area signature: {parser.functions['calculate_area']}\")\n    print(f\"print_message signature: {parser.functions['print_message']}\")\n\n    # To bind to a C library, you would typically do:\n    # from pyclibrary import CLibrary\n    # # Assuming a compiled shared library like 'libmyexample.so' or 'myexample.dll'\n    # # For this quickstart, we only demonstrate parsing, not live binding.\n    # try:\n    #     lib = CLibrary(header_file_path, 'libmyexample.so') # Or 'myexample.dll' on Windows\n    #     # Example function call (requires a real shared library):\n    #     # area = lib.calculate_area(width=10, height=20).r # Access return value\n    #     # lib.print_message(msg='Hello from Python')\n    # except OSError as e:\n    #     print(f\"\\nCould not load C library for live binding: {e}\")\n\nexcept Exception as e:\n    print(f\"An error occurred during parsing: {e}\")\nfinally:\n    # Clean up the dummy header file\n    if os.path.exists(header_file_path):\n        os.remove(header_file_path)","lang":"python","description":"This quickstart demonstrates how to use `pyclibrary.CParser` to parse a C header file and access its defined macros, structs, enums, and function prototypes. It creates a temporary header file, parses it, and prints the extracted information. While `pyclibrary` can also bind to dynamic libraries using `CLibrary`, that functionality is commented out in this example as it requires a pre-compiled C shared library."},"warnings":[{"fix":"Be aware that current implementations are `ctypes`-based. If designing for future FFI backend compatibility, stick to simpler data types or abstract complex C object interactions.","message":"While PyCLibrary aims for an FFI-agnostic API, currently only the `ctypes` backend is implemented. Code that manipulates complex C objects directly may not be easily portable if other FFI backends (e.g., cffi) are introduced later.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always inspect the `CallResult` object for output parameters (including modified pointers) rather than expecting in-place modifications on Python objects passed as arguments.","message":"When calling C functions that expect uninitialized pointers, PyCLibrary can automatically create them. However, you must access the results of these pointers through the `CallResult` object returned by the function call, which encapsulates both the return value and modified arguments.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure that the `CParser` is provided with all necessary include paths and macro definitions (via `CParser(include_dirs=..., defines=...)`) to accurately simulate the C compilation environment.","message":"The C parser evaluates macros and preprocessor directives. Complex macros or those relying on external build-time definitions not provided to `CParser` might lead to incorrect parsing or unexpected values.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z"}