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.

🚧Useful shortcuts for simple task on windows

Windows Manager A tool containg useful utilities for performing simple shortcut tasks on Windows 10 OS. Features Lit Up - Turns up screen brightness t

Olawale Oyeyipo 0 Mar 24, 2022
Convert any-bit number to decimal number and vise versa.

2deci Convert any-bit number to decimal number and vise versa. --bit n to set bit to n --exp xxx to set expression to xxx --r to run reversely (from d

3 Sep 15, 2021
Every 2 minutes, check for visa slots at VFS website

vfs-visa-slot-germany Every 2 minutes, check for visa slots at VFS website. If there are any, send a call and a message of the format: Sent from your

12 Dec 15, 2022
osqueryIR is an artifact collection tool for Linux systems.

osqueryIR osqueryIR is an artifact collection tool for Linux systems. It provides the following capabilities: Execute osquery SQL queries Collect file

AbdulRhman Alfaifi 7 Nov 02, 2022
A Python script that parses and checks public proxies. Multithreading is supported.

A Python script that parses and checks public proxies. Multithreading is supported.

LevPrav 7 Nov 25, 2022
A Python class for checking the status of an enabled Minecraft server

mcstatus provides an easy way to query Minecraft servers for any information they can expose. It provides three modes of access (query, status and ping), the differences of which are listed below in

Nathan Adams 1.1k Jan 06, 2023
Script to autocompound 3commas BO:SO based on user provided risk factor

3commas_compounder Script to autocompound 3commas BO:SO based on user provided risk factor Setup Step 1 git clone this repo into your working director

0 Feb 24, 2022
A python module to manipulate XCode projects

This module can read, modify, and write a .pbxproj file from an Xcode 4+ projects. The file is usually called project.pbxproj and can be found inside the .xcodeproj bundle. Because some task cannot b

Ignacio Calderon 1.1k Jan 02, 2023
Extract XML from the OS X dictionaries.

Extract XML from the OS X dictionaries.

Joshua Olson 13 Dec 11, 2022
Aggregating gridded data (xarray) to polygons

A package to aggregate gridded data in xarray to polygons in geopandas using area-weighting from the relative area overlaps between pixels and polygons.

Kevin Schwarzwald 42 Nov 09, 2022
Simple integer-valued time series bit packing

Smahat allows to encode a sequence of integer values using a fixed (for all values) number of bits but minimal with regards to the data range. For example: for a series of boolean values only one bit

Ghiles Meddour 7 Aug 27, 2021
glip is a module for retrieve ip address like local-ip, global-ip, external-ip as string.

gle_ip_info glip is a module for retrieve ip address like local-ip, global-ip, external-ip as string.

Fatin Shadab 3 Nov 21, 2021
Python Classes Without Boilerplate

attrs is the Python package that will bring back the joy of writing classes by relieving you from the drudgery of implementing object protocols (aka d

The attrs Cabal 4.6k Jan 06, 2023
Dependency Injector is a dependency injection framework for Python.

What is Dependency Injector? Dependency Injector is a dependency injection framework for Python. It helps implementing the dependency injection princi

ETS Labs 2.6k Jan 04, 2023
WindowsDebloat - Windows Debloat with python

Windows Debloat 🗑️ Quickly and easily configure Windows 10. Disclaimer I am NOT

1 Mar 26, 2022
Standard implementations of FedLab and its provided benchmarks.

FedLab-benchmarks This repo contains standard implementations of FedLab and its provided benchmarks. Currently, following algorithms or benchrmarks ar

SMILELab-FL 104 Dec 05, 2022
Script for generating Hearthstone card spoilers & checklists

This is a script for generating text spoilers and set checklists for Hearthstone. Installation & Running Python 3.6 or higher is required. Copy/clone

John T. Wodder II 1 Oct 11, 2022
Just some scripts to export vector tiles to geojson.

Vector tiles to GeoJSON Nowadays modern web maps are usually based on vector tiles. The great thing about vector tiles is, that they are not just imag

Lilith Wittmann 77 Jul 26, 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
A simple tool to extract python code from a Jupyter notebook, and then run pylint on it for static analysis.

Jupyter Pylinter A simple tool to extract python code from a Jupyter notebook, and then run pylint on it for static analysis. If you find this tool us

Edmund Goodman 10 Oct 13, 2022