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.