ffledgling
ffledgling

Reputation: 12140

Porting a C library to Python via ctypes

I've read in a lot of places that C-libraries can be ported to or written in python using the ctypes module from the standard library.

I've gone through the help('ctypes') page and from what I could gather I can create some of the C structures in Python, but my question is how do I use these to access the underlying system calls? For eg. when trying to port something like 'sys/if.h' to Python?

Can someone point me to good resources/documentation regarding the same?

Upvotes: 2

Views: 450

Answers (1)

jrd1
jrd1

Reputation: 10716

If you want access to the system calls you could do something like this:

>>> from ctypes import CDLL
>>> libc = CDLL('libc.so.6')
>>> print libc.strlen('abcde')
5

Reference: http://blog.bstpierre.org/using-pythons-ctypes-to-make-system-calls

Or (This is the tricky part)

Wrap a system call as outlined here into your C code:

How to reimplement (or wrap) a syscall function in linux?

And, then write a compliant source code file which will be used by CTypes, as per here:

http://www.scipy.org/Cookbook/Ctypes

I hope this helps.

Upvotes: 3

Related Questions