Create C bindings for python automatically with the help of libclang

Overview

Python C Import

Dynamic library + header + ctypes = Module like object!

Create C bindings for python automatically with the help of libclang.

Examples

from c_import import loader
import ctypes

libc = loader.load("libc.so.6", ["stdio.h", "stdint.h", "math.h", "stdlib.h", "string.h", "time.h"])

# Work with file streams
filestream = libc.fopen(b"/tmp/x", b"rw+")
text = ctypes.create_string_buffer(b"Hello world")
libc.fwrite(text, len(text), 1, fstream)
libc.fflush(fstream)
libc.fclose(fstream)

libc.fileno(libc.stdin)
# 1

libc.fputc(ord('A'), libc.stdout)
libc.fflush(libc.stdout)
# A
libc.fprintf(libc.stderr, b"%d\n", 100) # 100

# Use math functions
libc.abs(-1)
# 1

# Create instances of structs
cal_date = libc.tm()
dir(cal_date)
# All fields are available
# [...,
#  'tm_gmtoff',
#  'tm_hour',
#  'tm_isdst',
#  'tm_mday',
#  'tm_min',
#  'tm_mon',
#  'tm_sec',
#  'tm_wday',
#  'tm_yday',
#  'tm_year',
#  'tm_zone']
cal_date.tm_sec = 1
cal_date.tm_year = 1999

# View the fields of "private" structs
# Great when experimenting inside an interactive shell.
dir(libc.stdout.contents)
# (Fields differ between libc implementations)
# [...,
#  '_IO_backup_base',
#  '_IO_buf_base',
#  '_IO_buf_end',
#  '_IO_read_base',
#  '_IO_read_end',
#  '_IO_read_ptr',
#  '_IO_save_base',
#  '_IO_save_end',
#  '_IO_write_base',
#  '_IO_write_end',
#  '_IO_write_ptr',
#  '__pad5',
#  '_chain',
#  '_codecvt',
#  '_cur_column',
#  '_fields_',
#  '_fileno',
#  '_flags',
#  '_flags2',
#  '_freeres_buf',
#  '_freeres_list',
#  '_lock',
#  '_markers',
#  '_mode',
#  '_objects',
#  '_offset',
#  '_old_offset',
#  '_shortbuf',
#  '_unused2',
#  '_vtable_offset',
#  '_wide_data']
libc.stdout.contents._fileno # 1

# Functions that return a struct work too
x = libc.div(49, 8)
x.quot # 6
x.rem  # 1
x.__class__.__name__ # 'div_t'

# Abort the process
libc.abort()

How does that work

The loader calls the c pre-processor to resolve any “include”s and “define”s.

The resulting header is processed by libclang

The header parser module converts the parsed header into ctype types.

The loader wraps symbols with their python ctype.

Features

Working

Basic C types

Functions

Pointers

Arrays

Structs

Unions

Enums

Typedefs

Globals

Bitfields

Forward declarations

Anonymous structs

Anonymous unions

Anonymous enums

Typedefs to anonymous types.

Nested anonymous types.

“Packed” attribute

Variadic arguments

Not implemented yet

Non cdecl functions (Important for Win32API)

Pre-processor constant integer expressions

Their declarations are not preserved after the pre-processing.

Vector types

I thought it was python, why am I seeing lisp?

Hy is sort of a python lisp hybrid. Its written like lisp, but its translated during runtime into python. It has interoperability with python, the fact that its written in hy is abstracted to the end user of the library.

https://docs.hylang.org/en/stable/whyhy.html

LICENSE

This library is licensed under GNU Lesser General Public License version 3 or later. See COPYING and COPYING.LESSER for further details.

DiddiParser 2: The DiddiScript parser.

DiddiParser 2 The DiddiScript parser, written in Python. Installation DiddiParser2 can be installed via pip: pip install diddiparser2 Usage DiddiPars

Diego Ramirez 3 Dec 28, 2022
A random cats photos python module

A random cats photos python module

Fayas Noushad 6 Dec 01, 2021
Personal Toolbox Package

Jammy (Jam) A personal toolbox by Qsh.zh. Usage setup For core package, run pip install jammy To access functions in bin git clone https://gitlab.com/

5 Sep 16, 2022
Python program to do with percentages and chances, random generation.

Chances and Percentages Python program to do with percentages and chances, random generation. What is this? This small program will generate a list wi

n0 3 Jul 15, 2021
convert a dict-list object from / to a typed object(class instance with type annotation)

objtyping 带类型定义的对象转换器 由来 Python不是强类型语言,开发人员没有给数据定义类型的习惯。这样虽然灵活,但处理复杂业务逻辑的时候却不够方便——缺乏类型检查可能导致很难发现错误,在IDE里编码时也没

Song Hui 15 Dec 22, 2022
python script to generate color coded resistor images

Resistor image generator I got nerdsniped into making this. It's not finished at all, and the code is messy. The end goal it generate a whole E-series

MichD 1 Nov 12, 2021
A multipurpose python module

pysherlock pysherlock is a Python library for dealing with web scraping using images, it's a Python application of the rendertron headless browser API

Sachit 2 Nov 11, 2021
ecowater-softner is a Python library for collecting information from Ecowater water softeners.

Ecowater Softner ecowater-softner is a Python library for collecting information from Ecowater water softeners. Installation Use the package manager p

6 Dec 08, 2022
Spacegit is a .git exposed finder

Spacegit Spacegit is a basic .git exposed finder Usage: You need python3 installed to run spacegit use: python3 spacegit.py (url) Disclaimer: **This i

2 Nov 30, 2021
This project is a set of programs that I use to create a README.md file.

This project is a set of programs that I use to create a README.md file.

Tom Dörr 223 Dec 24, 2022
ColorController is a Pythonic interface for managing colors by english-language name and various color values.

ColorController.py Table of Contents Encode color data in various formats. 1.1: Create a ColorController object using a familiar, english-language col

Tal Zaken 2 Feb 12, 2022
A work in progress box containing various Python utilities

python-wipbox A set of modern Python libraries under development to simplify the execution of reusable routines by different projects. Table of Conten

Deepnox 2 Jan 20, 2022
Conveniently measures the time of your loops, contexts and functions.

Conveniently measures the time of your loops, contexts and functions.

Maciej J Mikulski 79 Nov 15, 2022
Simple RGB to HEX game made in python

Simple RGB to HEX game made in python

5 Aug 26, 2022
More routines for operating on iterables, beyond itertools

More Itertools Python's itertools library is a gem - you can compose elegant solutions for a variety of problems with the functions it provides. In mo

2.9k Jan 06, 2023
Run functions in parallel easily, with their results typed correctly!

typesafe_parmap pip install pip install typesafe-parmap Run functions in parallel safely with typesafe parmap! GitHub: https://github.com/thejaminato

James Chua 3 Nov 06, 2021
This utility synchronises spelling dictionaries from various tools with each other.

This utility synchronises spelling dictionaries from various tools with each other. This way the words that have been trained on MS Office are also correctly checked in vim or Firefox. And vice versa

Patrice Neff 2 Feb 11, 2022
[P]ython [w]rited [B]inary [C]onverter

pwbinaryc [P]ython [w]rited [Binary] [C]onverter You have rights to: Modify the code and use it private (friends are allowed too) Make a page and redi

0 Jun 21, 2022
Lark is a parsing toolkit for Python, built with a focus on ergonomics, performance and modularity.

Lark is a parsing toolkit for Python, built with a focus on ergonomics, performance and modularity.

Lark - Parsing Library & Toolkit 3.5k Jan 05, 2023
Definitely legit social credit generator with python

definitely-legit-social-credit-generator I made this simple GUI program for a meme, no cap. Video: https://youtu.be/RmjxKtoli04 How to run: Clone this

Joshua Malabanan 8 Nov 01, 2021