Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 13 Jul 2007 16:55:29 -0400
From:      Mike Meyer <mwm-keyword-freebsdhackers2.e313df@mired.org>
To:        "Mathieu Prevot" <mathieu.prevot@gmail.com>
Cc:        freebsd-hackers@freebsd.org, Joseph Koshy <jkoshy@freebsd.org>
Subject:   Re: python modules
Message-ID:  <20070713165529.7082898c@bhuda.mired.org>
In-Reply-To: <3e473cc60707131334u7cf75dbckd3a2b42b1815fbf8@mail.gmail.com>
References:  <3e473cc60707131205s1a2912c3p9895832de54e6b94@mail.gmail.com> <3e473cc60707131212u1e3b4552v2dfc5c10cfe8e533@mail.gmail.com> <20070713153502.110b1c99@bhuda.mired.org> <3e473cc60707131334u7cf75dbckd3a2b42b1815fbf8@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, 13 Jul 2007 22:34:20 +0200 "Mathieu Prevot" <mathieu.prevot@gmail.com> wrote:
> 2007/7/13, Mike Meyer <mwm@mired.org>:
> > On Fri, 13 Jul 2007 21:12:33 +0200 "Mathieu Prevot" <mathieu.prevot@gmail.com> wrote:
> > > 2007/7/13, Mathieu Prevot <mprevot@freebsd.org>:
> > > > I learn that modules loaded with import fall into 4 general categories:
> > > > - code written in Python (.py)
> > > > - C or C++ extensions that have been compiled into shared libraries (or DLLs)
> >
> > These are *Python extensions* written in C or C++ (among other
> > things), not arbitrary shared object libraries (or .so's).
> >
> > > > - Packages containing  collection of modules
> > > > - Built-in modles writen in C and linked into the Python interpreter
> > > > Why don't we use directly the libpmc library in C instead of rewritting
> > > > things in python  ?
> >
> > Are you writing in Python now and want to use libpmc? Doing that is
> > one approach.
> 
> I'm doing this. This approach _seems_ to be the easier way.
> 
> > > I copied libpmc.so and tryed  'import libpmc'. I have:
> > > ImportError: dynamic module does not define init function (initlibpmc)
> >
> > To be expected. The init function is part of what turns it into a
> > Python extension library.
> >
> > > Are we really far from having a libpmc module ?
> >
> > There are at least two other approaches to getting access to libpmc
> > from Python:
> >
> > 1) Write a wrapper library that is a Python extensions and translates
> >    calls.
> 
> This work is in progress in fact, but I wanted to have ASAP access to
> pmc(3) with a minimum of (keyboard) effort.  I don't care of
> docstrings.
> 
> > 2) Use the ctypes python module to access libpmc. ctypes has been
> >    bundled into 2.5, so that would be my preference. Just one less
> >    thing to install.
> >
> > This is probably more appropriate in c.l.python, but it's hard to say
> > without context.
> 
> I didn't tryed this module, rather (a bit) the ezpyinline module,
> coupled to `gcc -E /usr/src/lib/libpmc/libpmc.c` (no preprocessing
> with the module, only compilation).

ezpyline could do the job - if you can get the dlopen magic to happen
so libpmc is loaded and it's functions are available.

> The ezpyinline module work like this:
> 
> ----
>     #!/usr/bin/python
>     import ezpyinline
> 
>     #step 1
>     code = r"""
>         int helloworld() {
>             printf("hello ezpyinline!
>     ");
>         }
>     """
>     #step 2
>     ezc = ezpyinline.C(code)
> 
>     #step 3
>     ezc.helloworld()
> ----
> 
> I'll give ctypes a chance (when I have time :) )... thanks

I suspect ctypes is your best bet - it was designed for doing the kind
of thing:

>>> from ctypes import *
>>> pmc = cdll.LoadLibrary('libpmc.so')
>>> pmc.pmc_init()
-1

[Ok, it failed - but I'm not familiar with the library, so probably
screwed something in the environment up...]

Pyrex might also be of interest, but I suspect it's going to have the
same issues that ezpyinline does.

     <mike
-- 
Mike Meyer <mwm@mired.org>		http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20070713165529.7082898c>