Creating a Python script 'interface' to c/c++ module

Fredrik Lundh effbot at telia.com
Tue Mar 7 10:58:49 EST 2000


Makhno <mak at imakhno.freeserve.co.uk> wrote:
> Unfortuanately, I can't seem to provide the Python wrapper, the way the
> modules work does not seem to let me. For example, I want to call a C
> function called my_func(). With Perl I would use the hierarchy:
> Users code->Perl wrapper->C wrapper (XS file)->C function
> with Python, I can't seem to include the second step, only
> Users code->C wrapper->C function
>
> So calling my_module.my_func() in Python, calls C code directly, there is
no
> chance of executing any Python inbetween.
>
> The question is how do I get some Python code in there? The only way I can
> think of is writing a Python module which acts as a wrapper for the C
> module, which doesn't seem like the best way.

well, that's how everyone else does this.  if you don't
want to expose the C module directly, call it _mymodule
and provide a mymodule.py that wraps it.

note that you can let mymodule.py import everything
from _mymodule, and just override stuff as necessary:

    # mymodule.py

    import _mymodule
    from _mymodule import *

    def myfunction(args):
        # override _mymodule.myfunction
        args = convert(args)
        result = _mymodule.myfunction(args)
        return process(result)

hope this helps!

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list