{"id":21358280,"url":"https://github.com/snacsnoc/cporter","last_synced_at":"2025-08-03T20:34:10.730Z","repository":{"id":146240634,"uuid":"615919825","full_name":"snacsnoc/cporter","owner":"snacsnoc","description":"Integrate C into your Python projects easily","archived":false,"fork":false,"pushed_at":"2023-03-20T07:46:00.000Z","size":49,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-22T18:34:05.288Z","etag":null,"topics":["ctypes-wrapper","ffi","python"],"latest_commit_sha":null,"homepage":"https://test.pypi.org/project/cporter/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/snacsnoc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-19T04:02:45.000Z","updated_at":"2023-08-19T16:50:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"fa8235cd-b648-4ffa-8993-6c866327c3fc","html_url":"https://github.com/snacsnoc/cporter","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/snacsnoc%2Fcporter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/snacsnoc%2Fcporter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/snacsnoc%2Fcporter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/snacsnoc%2Fcporter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/snacsnoc","download_url":"https://codeload.github.com/snacsnoc/cporter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243830955,"owners_count":20354856,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ctypes-wrapper","ffi","python"],"created_at":"2024-11-22T05:15:24.913Z","updated_at":"2025-03-16T06:16:50.976Z","avatar_url":"https://github.com/snacsnoc.png","language":"Python","readme":"# CPorter\n[![Mypy check](https://github.com/snacsnoc/cporter/actions/workflows/mypy.yml/badge.svg)](https://github.com/snacsnoc/cporter/actions/workflows/mypy.yml)\n\nCPorter is a Python library using ctypes that makes it easy to integrate C code into your Python projects. \nIt simplifies the process of compiling, loading, and calling C functions from Python by automatically handling C types and providing performance profiling utilities.\n\n\n## Features\n* Compile and load C libraries with a single function call\n* Automatic C function type detection and handling\n* Simplified function calling syntax in Python\n* Multithreading support\n* Performance profiling for both C and Python functions\n* Get C function documentation from source code(/* Text */)\n* Automatic memory management for C code allocating memory using malloc (don't rely on this)\n\n## Installation\nTo use CPorter in your project, install from pip or by cloning.\n```bash\npip install -i https://test.pypi.org/simple/ cporter\n```\nor\n\n```\ngit clone https://github.com/snacsnoc/cporter.git\n```\nand add the `src/` to your Python lib path\n## Usage\nHere's an example of using CPorter to load and call a C function:\n\n```python\nfrom cporter.cporter import CPorter\n\n# Create a CPorter instance and load the library\ncporter = CPorter()\ncporter.set_library_path(\"lib\")\ncporter.add_library(\"my_library\")\n\n# Call the C function\nc_function = cporter.get_function(\"my_library\", \"my_function\")\nresult = c_function(arg1, arg2)\n```\n\n## Profiling\nCPorter also supports performance profiling for both C and Python functions. Here's an example of profiling a C function:\n\n```python\nfrom cporter.cporter import CPorter\n\ncporter = CPorter()\ncporter.set_library_path(\"lib\")\ncporter.add_library(\"my_library\")\n\nresult, elapsed_time = cporter.profile_function(\"my_library\", \"my_function\", arg1, arg2)\nprint(f\"Elapsed time: {elapsed_time:.6f} seconds\")\n```\nFor profiling Python functions, you can use the `profile_python_function` method:\n\n```python\nfrom cporter.cporter import CPorter\n\ncporter = CPorter()\n\ndef my_python_function(arg1, arg2):\n    # Your Python function implementation here\n    pass\n\nresult, elapsed_time = cporter.profile_python_function(my_python_function, arg1, arg2)\nprint(f\"Elapsed time: {elapsed_time:.6f} seconds\")\n```\n## Multithreading\nCPorter supports multithreading to run C and Python functions concurrently. Here's an example of running a C function and a Python function concurrently using a thread pool:\n\n```python\nimport ctypes\nfrom concurrent.futures import ThreadPoolExecutor, as_completed\nfrom cporter.cporter import CPorter\n\ncporter = CPorter()\ncporter.set_library_path(\"lib\")\ncporter.add_library(\"my_library\")\n\n# Define your Python function here\ndef my_python_function(arg1, arg2):\n    pass\n\n# Define input arguments for the C and Python functions\narg1, arg2 = 1, 1\n\n# Execute the C and Python functions concurrently using a thread pool\nwith ThreadPoolExecutor() as executor:\n    c_futures = [executor.submit(cporter.execute_c_function, \"my_library\", \"my_function\", arg1, arg2) for _ in range(10)]\n    py_futures = [executor.submit(my_python_function, arg1, arg2) for _ in range(10)]\n\n    # Wait for the tasks to complete and retrieve the results\n    c_results = [future.result() for future in as_completed(c_futures)]\n    py_results = [future.result() for future in as_completed(py_futures)]\n\n# Process the results as needed\n```\n\n## Function Documentation\nCPorter can now extract the docstring for a C function from its source code. If the C function has a docstring in the form of a block comment immediately preceding its declaration, CPorter can parse it and attach the docstring to the corresponding Python-wrapped function. \n\nExample:\n```c\n/* Compute the sum of two integers. */\nint add(int a, int b) {\n    return a + b;\n}\n```\nIn Python, you can access the docstring like this:\n\n```python\ncporter = CPorter()\n# ... Load the library and get the function ...\nadd_function = cporter.get_function(\"my_library\", \"add\")\nprint(add_function.__doc__)\n```\nThis will output:\n\n```python\nCompute the sum of two integers.\n```\n\n## Automatic Type Checking with ctypes\nCPorter automatically handles type checking and conversion for function arguments and return values. \nWhen calling a C function wrapped by CPorter, you can pass Python values directly, and CPorter will attempt to convert them to the appropriate ctypes equivalents. \nIt also checks if the provided values are valid for the expected C types, and raises a helpful error message if there's a type mismatch or an invalid value.\n\nThis makes it easier to work with C functions from Python, as you don't need to manually convert Python values to ctypes objects.\n```python\ncporter = CPorter()\n# ... Load the library and get the function ...\nadd_function = cporter.get_function(\"my_library\", \"add\")\nresult = add_function(1, 2)  # You can pass Python values directly\nprint(result)  # The result is automatically converted back to a Python value\n\n```\n## Examples\nCall two different functions with arguments example: `python examples/run.py`\n\nMultithreading example: `python examples/multithreading.py`\n\n## Contributing\nPull requests are always welcome. For major changes, please open an issue first to discuss what you would like to change.\n\n## License\nGNU GPLv3\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsnacsnoc%2Fcporter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsnacsnoc%2Fcporter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsnacsnoc%2Fcporter/lists"}