Python Binding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Georg Grabler

    #1

    Python Binding

    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 "informatio n"
    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
  • Stefan Behnel

    #2
    Re: Python Binding

    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 "informatio n"
    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.



    An example wrapper is described here:



    Hope it helps,
    Stefan

    Comment

    • Stefan Sonnenberg-Carstens

      #3
      Re: Python Binding

      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 "informatio n"
      >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.
      >

      >
      An example wrapper is described here:
      >

      >
      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

      Comment

      • Stefan Sonnenberg-Carstens

        #4
        Re: Python Binding

        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 "informatio n"
        >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.
        >

        >
        An example wrapper is described here:
        >

        >
        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

        Comment

        • Nick Craig-Wood

          #5
          Re: Python Binding

          Georg Grabler <ggrabler@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 "informatio n"
          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.

          Source code: Lib/ctypes ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these ...


          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 <nick@craig-wood.com-- http://www.craig-wood.com/nick

          Comment

          • STiAT

            #6
            Re: Python Binding

            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('xy z') 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

            Comment

            • Stefan Behnel

              #7
              Re: Python Binding

              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
              "possibilit ies" 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

              Comment

              • Georg Grabler

                #8
                Re: Python Binding

                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 "possibilit ies" 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

                Comment

                Working...