By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

Python Binding

P: n/a
Hello everybody.

There's a C library which i'd like to have python bindings for. I havn't
known anything before about how to write python bindings for a C library.

I succeeded now by using distutils to write the first bindings for functions
and similar.

Now, it seems as something is blocking my brain. For the library, i
need "custom" types, so types defined in this library (structures),
including pointers and similar.

I've been thinking about what i will need to represent this lists in python.
I thought about creating an external python object, providing "information"
i get from the list in C structures which can be converted.

Basically, it are list of packages, which have several attributes (next,
prev, etc). But i don't know how to supply a proper list from the binding /
object written in C.

Any suggestions or hints about this?

Thank you,
Georg
May 5 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Georg Grabler wrote:
There's a C library which i'd like to have python bindings for. I havn't
known anything before about how to write python bindings for a C library.

I succeeded now by using distutils to write the first bindings for functions
and similar.

Now, it seems as something is blocking my brain. For the library, i
need "custom" types, so types defined in this library (structures),
including pointers and similar.

I've been thinking about what i will need to represent this lists in python.
I thought about creating an external python object, providing "information"
i get from the list in C structures which can be converted.

Basically, it are list of packages, which have several attributes (next,
prev, etc). But i don't know how to supply a proper list from the binding /
object written in C.

Any suggestions or hints about this?
Use Pyrex. It's a Python-like language that was designed to write extension
modules in C and it's great for writing wrappers.

http://www.cosc.canterbury.ac.nz/gre.../python/Pyrex/

An example wrapper is described here:

http://ldots.org/pyrex-guide/

Hope it helps,
Stefan
May 6 '07 #2

P: n/a
Stefan Behnel schrieb:
Georg Grabler wrote:
>There's a C library which i'd like to have python bindings for. I havn't
known anything before about how to write python bindings for a C library.

I succeeded now by using distutils to write the first bindings for functions
and similar.

Now, it seems as something is blocking my brain. For the library, i
need "custom" types, so types defined in this library (structures),
including pointers and similar.

I've been thinking about what i will need to represent this lists in python.
I thought about creating an external python object, providing "information"
i get from the list in C structures which can be converted.

Basically, it are list of packages, which have several attributes (next,
prev, etc). But i don't know how to supply a proper list from the binding /
object written in C.

Any suggestions or hints about this?

Use Pyrex. It's a Python-like language that was designed to write extension
modules in C and it's great for writing wrappers.

http://www.cosc.canterbury.ac.nz/gre.../python/Pyrex/

An example wrapper is described here:

http://ldots.org/pyrex-guide/

Hope it helps,
Stefan
Hi,
use SWIG.
It is a no-brainer for simple libs. wxPython relies heavily on it.
I worked with some times and it is really straightforward.
See this: http://www.swig.org/Doc1.1/HTML/Python.html
Cheers,
Stefan
May 6 '07 #3

P: n/a
Stefan Behnel schrieb:
Georg Grabler wrote:
>There's a C library which i'd like to have python bindings for. I havn't
known anything before about how to write python bindings for a C library.

I succeeded now by using distutils to write the first bindings for functions
and similar.

Now, it seems as something is blocking my brain. For the library, i
need "custom" types, so types defined in this library (structures),
including pointers and similar.

I've been thinking about what i will need to represent this lists in python.
I thought about creating an external python object, providing "information"
i get from the list in C structures which can be converted.

Basically, it are list of packages, which have several attributes (next,
prev, etc). But i don't know how to supply a proper list from the binding /
object written in C.

Any suggestions or hints about this?

Use Pyrex. It's a Python-like language that was designed to write extension
modules in C and it's great for writing wrappers.

http://www.cosc.canterbury.ac.nz/gre.../python/Pyrex/

An example wrapper is described here:

http://ldots.org/pyrex-guide/

Hope it helps,
Stefan
Hi,
use SWIG.
It is a no-brainer for simple libs. wxPython relies heavily on it.
I worked with some times and it is really straightforward.
See this: http://www.swig.org/Doc1.1/HTML/Python.html
Cheers,
Stefan
May 6 '07 #4

P: n/a
Georg Grabler <gg******@gmail.comwrote:
There's a C library which i'd like to have python bindings for. I havn't
known anything before about how to write python bindings for a C library.

I succeeded now by using distutils to write the first bindings for functions
and similar.

Now, it seems as something is blocking my brain. For the library, i
need "custom" types, so types defined in this library (structures),
including pointers and similar.

I've been thinking about what i will need to represent this lists in python.
I thought about creating an external python object, providing "information"
i get from the list in C structures which can be converted.

Basically, it are list of packages, which have several attributes (next,
prev, etc). But i don't know how to supply a proper list from the binding /
object written in C.

Any suggestions or hints about this?
Sounds like a job for ctypes which is bundled with py 2.5.

http://docs.python.org/lib/module-ctypes.html

It is great for access C libraries (assuming you have a shared library
(.so or .dll).

You'll end up writing python code rather than C code which you'll
enjoy!

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
May 6 '07 #5

P: n/a
Hello,

I've basically had plans on just extending python for the wrapper.
That basically works fine, i just don't get along by the types
provided (http://docs.python.org/ext/ext.html).

Anyway, i want to extend python for types. The problem i currently
experience is the following.

Guess what - i have a base class, doing some wrapping. Currently,
called pybtest (just testing around though).
Now, i'd need a configuration object, accessable in python. By using
the library, it should be possible to access it using
pybtest.config.insertValue('xyz') as an example, but i havn't found
any way to archive this yet.

If i write config as an own (external) type, i can't get it into the
pybtest object for some reason. It's compiled as an extra "config.so",
and with the test script i need to do import config. Anyway, it's not
in the pybtest object., i've to build up a config and pass it by to
the pybtest binding, so it can set it's internal configuration
structures.

If i try to compile it within the pybtest, i can't use ext_modules,
and as expected, i can't do another pybtestinit function, so it's
quite useless, at least with the knowledge i could get out of the
documentation.

By just making a include file of the type, including the type
definition adding up some functions to pybtest, i actually can access
it, without the wanted functionality of pybtest.config.

Why do you all suggest other things than the way suggested by python?
I havn't got a real problem writing the code in C, actually, it looked
as if it would give me several possibilities i wouldn't have with
pyrex (like binding more library functions to one provided python
function and so on). I havn't had a closer look to SWIG yet.

It looks as if i could just provide single objects or modules, and no
additional object into a module.

Does anyone have closer knowledge on this?
Btw: Sorry for the long delay time, i had to describe the things
closer, and had to take a deeper look into the bindings than i had
before.

Kind regards,
Georg

May 8 '07 #6

P: n/a
STiAT wrote:
Why do you all suggest other things than the way suggested by python?
Because going to Paris is not the only way to get french bread?

Why would you want to write all that ugly glue code by hand that Pyrex
generates for free? Module descriptors? Class descriptors? Method descriptors?
Reference counting? That's what Pyrex saves you from. Honestly.

From what I read in your mail, that's exactly the kind of thing you're having
trouble with. Wouldn't you prefer concentrating on your real code instead?

I havn't got a real problem writing the code in C, actually, it looked
as if it would give me several possibilities i wouldn't have with
pyrex (like binding more library functions to one provided python
function and so on).
No idea what you mean in your parentheses, but I don't think there are many
"possibilities" you "wouldn't have with Pyrex".

We used Pyrex to write lxml, a wrapper around the huge API of libxml2 and
libxslt. It's some 11000 lines of Pyrex code by now, but the generated C code
is some 67000 lines in total. Even if it's somewhat verbose and generic in
places, I wouldn't have wanted to write that by hand.

Stefan
May 8 '07 #7

P: n/a
You are completely right wihtin this. It's some time i didn't reply, but
i've taken a look on pyrex and swig now which did cost me some time, and
they really make it easier. SWIG provides more possibilities and bindings
for other languages too, though - i must say i've been faster with pyrex,
for some reason it fits me better.

I'd like to use swig, but for some reason i've troubles defining a
completely new type, so a type which is not a wrapper type, but a type
provided to python.

Kind regards,
Georg

Stefan Behnel wrote:
STiAT wrote:
>Why do you all suggest other things than the way suggested by python?

Because going to Paris is not the only way to get french bread?

Why would you want to write all that ugly glue code by hand that Pyrex
generates for free? Module descriptors? Class descriptors? Method
descriptors? Reference counting? That's what Pyrex saves you from.
Honestly.

From what I read in your mail, that's exactly the kind of thing you're
having trouble with. Wouldn't you prefer concentrating on your real code
instead?

>I havn't got a real problem writing the code in C, actually, it looked
as if it would give me several possibilities i wouldn't have with
pyrex (like binding more library functions to one provided python
function and so on).

No idea what you mean in your parentheses, but I don't think there are
many "possibilities" you "wouldn't have with Pyrex".

We used Pyrex to write lxml, a wrapper around the huge API of libxml2 and
libxslt. It's some 11000 lines of Pyrex code by now, but the generated C
code is some 67000 lines in total. Even if it's somewhat verbose and
generic in places, I wouldn't have wanted to write that by hand.

Stefan
May 12 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.