473,327 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

[Dream] A meta-wrapper module to interface any C dynamic library

Hi all,

I was wondering if it would be possible to make a module which allows to
do something like that:

import c_interface
math_lib = c_interface.load('math.so', 'math.h' )
sin_f = math_lib.get_function('sin')
print "sin(3.14) = %f" % sin_f (3.14)

The idea would be to make a module that allows to use any C dynamic
library ( I used an hypothetical math.so only as example ) without
the need of a specific wrapper module.

I've given some thought to this idea, but I was blocked by the theoric (?)
impossibility to correctly call a C function without knowing its prototype
at compile time.

Did anybody ever attempted to do something like this?

Ciao
-----
FB

Jul 18 '05 #1
6 1794
"Francesco Bochicchio" <bo*****@virgilio.it> writes:
Hi all,

I was wondering if it would be possible to make a module which allows to
do something like that:

import c_interface
math_lib = c_interface.load('math.so', 'math.h' )
sin_f = math_lib.get_function('sin')
print "sin(3.14) = %f" % sin_f (3.14)

The idea would be to make a module that allows to use any C dynamic
library ( I used an hypothetical math.so only as example ) without
the need of a specific wrapper module.

I've given some thought to this idea, but I was blocked by the theoric (?)
impossibility to correctly call a C function without knowing its prototype
at compile time.

Did anybody ever attempted to do something like this?


Yes, it already exists (to some degree).

http://starship.python.net/crew/theller/ctypes/

Thomas
Jul 18 '05 #2
On Fri, 04 Jul 2003 10:59:39 +0000, Francesco Bochicchio wrote:

Did anybody ever attempted to do something like this?

Ciao
-----
FB


yeah,

http://arrowtheory.com/software/python/index.html

it's hard :)

Simon.

Jul 18 '05 #3
Use ctypes.

http://starship.python.net/crew/theller/ctypes/

To see is to believe. The following is an actual IDLE session on WinXP.
However, ctypes is cross-platform -- it rusn on Windows, Linux, MacOS X.
import ctypes
loader = ctypes.cdll
dll = loader.msvcrt
sin = dll.sin
sin.argtypes = [ctypes.c_double]
sin.restype = ctypes.c_double
sin(3.14) 0.0015926529164868282

Jul 18 '05 #4
On Fri, 04 Jul 2003 13:33:27 +0200, Thomas Heller wrote:

Yes, it already exists (to some degree).

http://starship.python.net/crew/theller/ctypes/

Thomas


Thanks for the pointer. Is more or less what I had in mind, and lead me to
find out about libffi, which is also quite interesting.

This proves a theory of mine: whatever piece of software you can think of,
there is always some developer in the world that already did it :-)

Ciao
-----
FB
Jul 18 '05 #5
"Simon Burton" <si****@webone.com.au> writes:
On Fri, 04 Jul 2003 10:59:39 +0000, Francesco Bochicchio wrote:

Did anybody ever attempted to do something like this?

Ciao
-----
FB


yeah,

http://arrowtheory.com/software/python/index.html

it's hard :)

I have tried cdecl.py to parse windows header files, and it does a
pretty good job.

I say this after also having tried PLY
http://systems.cs.uchicago.edu/ply/ with a partial C grammer, and
finally gave up because I'm not sure it is possible to parse ANSI C with
a parser like this (at least its not possible for me).

I found one problem in cdecl.py (it doesn't parse hex constants
correctly), and I had to extend it somewhat for MS specific keywords
like __stdcall or so. I also ran the header files through the MSVC
preprocessor, and hand-edit them somewhat before throwing them at it.

I'm not really sure if it's a good idea to automatically create ctypes
wrappers from windows.h...

But I finally gave up when I encountered some really strange errors...

Thomas
Jul 18 '05 #6
hw***@hotmail.com (Will Stuyvesant) writes:
[Seo Sanghyeon]
To see is to believe.
>>> import ctypes
>>> loader = ctypes.cdll
>>> dll = loader.msvcrt
>>> sin = dll.sin
>>> sin.argtypes = [ctypes.c_double]
>>> sin.restype = ctypes.c_double
>>> sin(3.14)

0.0015926529164868282


I love examples like this! And ctypes is very good, I downloaded the
.EXE installer and ran it and this example works "out of the box".

How do you know what functions are available in "dll"? Any general
strategy?


On windows, you use dependency walker, although it would also be
possible to write something similar in ctypes ;-)

http://www.dependencywalker.com/

On unix like systems, I don't know. Use nm(1) ?

Thomas
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Cezary | last post by:
Hello. I was read PHP manual, but i'm not sure yet. Here is my meta tags in html: <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-2"> <META HTTP-EQUIV="Expires"...
44
by: Will Stuyvesant | last post by:
Suppose you have the time and the money to start a new project in Python. What would you like to do? I can think of: - A civilization like game in Python, with multiplayer support via...
4
by: Brian | last post by:
Hi, I'm trying to use standard meta tags in an xsl doc and using cocoon as my processor. The problem is that cocoon changes for example: <meta name="keywords" content="test, test, test" /> ...
1
by: Darren Blackley | last post by:
Hi there I have documents that I want to automatically add additional meta tags to. The documents already have some meta tags and I want to keep them all together, so I want to add my new meta tags...
19
by: Christian Hvid | last post by:
Hello groups. I have a series of applet computer games on my homepage: http://vredungmand.dk/games/erik-spillet/index.html http://vredungmand.dk/games/nohats/index.html...
3
by: J1C | last post by:
How can I programatically add meta tags with javascript?
5
by: RodneyDunes | last post by:
My site did validate and now it doesn't. The error I get is the following: document type does not allow element "META" here ....nt-type" content="text/html;charset=iso-8859-1"> Can someone...
1
by: Maziar Aflatoun | last post by:
Hi everyone, My goal is to modify the contents of my meta tag (html refresh). However, my code adds a new instance of the meta tag at the bottom of the page. Is there a way to modify it instead...
16
by: Edward | last post by:
Hi All, I am having huge problems with a very simple dotnet framework web page (www.gbab.net/ztest3.aspx) , it does NOT render correctly under Apple's Safari. The DIV's do not align amd float as...
29
by: castironpi | last post by:
What is dream hardware for the Python interpreter?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.