First question on extending Python...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Redefined Horizons

    #1

    First question on extending Python...

    I still new to Python, and I've only dabbled in C, but I've got my
    first project I need to tackle that involves both languages. I was
    hoping to get some advice on how to proceed.

    There is a third-party application that I need to work with. It is
    closed-source, but it exposes a C API. I want to wrap this C API so
    that it is available from Python. I have no ability to modify the C
    API of the third part application.

    Do I the C functions that I wrap in an extensions module for Python
    need to be in a certian format? If so, I will have to write an
    intermediate DLL in C that wraps the third-party application and
    exports the functions in a form that Python can use.

    Or can an extension module for Python wrap any C function? If this is
    the case I think I can skip the intermediate C DLL.

    Are there any advantages to using the intermediate DLL written in C in
    this particular case where I will not have ability to manipulate the C
    API of the third party application directly? (For example, if the
    third-party application developers are willing to call a "call-back"
    function that must be written in C, but not Python. Could the same
    extension module export both C functions and serve as a Python
    module?)

    Thanks,

    Scott Huey
  • John Machin

    #2
    Re: First question on extending Python...

    On 10/06/2006 8:32 AM, Redefined Horizons wrote:[color=blue]
    > I still new to Python, and I've only dabbled in C, but I've got my
    > first project I need to tackle that involves both languages. I was
    > hoping to get some advice on how to proceed.
    >
    > There is a third-party application that I need to work with. It is
    > closed-source, but it exposes a C API. I want to wrap this C API so
    > that it is available from Python. I have no ability to modify the C
    > API of the third part application.
    >
    > Do I the C functions that I wrap in an extensions module for Python
    > need to be in a certian format?[/color]

    No.

    [color=blue]
    > If so, I will have to write an
    > intermediate DLL in C that wraps the third-party application and
    > exports the functions in a form that Python can use.[/color]

    No way. You need only *one* layer of glue.
    [color=blue]
    >
    > Or can an extension module for Python wrap any C function?[/color]

    I've never heard of a C function that couldn't be wrapped. If there were
    such a function, having more C code in a separate DLL wouldn't make it
    wrappable.
    [color=blue]
    > If this is
    > the case I think I can skip the intermediate C DLL.[/color]

    I think so too.
    [color=blue]
    >
    > Are there any advantages to using the intermediate DLL written in C in
    > this particular case where I will not have ability to manipulate the C
    > API of the third party application directly? (For example, if the
    > third-party application developers are willing to call a "call-back"
    > function that must be written in C, but not Python.[/color]

    No advantages that I can see. Your C call-back function could then call
    a Python function if you really wanted to -- the 3rd-party devs don't
    need to know :-)
    [color=blue]
    > Could the same
    > extension module export both C functions and serve as a Python
    > module?)[/color]

    Yes.

    Cheers,
    John

    Comment

    • Larry Bates

      #3
      Re: First question on extending Python...

      Redefined Horizons wrote:[color=blue]
      > I still new to Python, and I've only dabbled in C, but I've got my
      > first project I need to tackle that involves both languages. I was
      > hoping to get some advice on how to proceed.
      >
      > There is a third-party application that I need to work with. It is
      > closed-source, but it exposes a C API. I want to wrap this C API so
      > that it is available from Python. I have no ability to modify the C
      > API of the third part application.
      >
      > Do I the C functions that I wrap in an extensions module for Python
      > need to be in a certian format? If so, I will have to write an
      > intermediate DLL in C that wraps the third-party application and
      > exports the functions in a form that Python can use.
      >
      > Or can an extension module for Python wrap any C function? If this is
      > the case I think I can skip the intermediate C DLL.
      >
      > Are there any advantages to using the intermediate DLL written in C in
      > this particular case where I will not have ability to manipulate the C
      > API of the third party application directly? (For example, if the
      > third-party application developers are willing to call a "call-back"
      > function that must be written in C, but not Python. Could the same
      > extension module export both C functions and serve as a Python
      > module?)
      >
      > Thanks,
      >
      > Scott Huey[/color]

      John Machin has answered most of your questions in a separate post. All you
      need is to take a look at ctypes module for Python. You can call virtually
      any C based API that is stored in a .DLL using ctypes. I've called .DLL
      APIs for Castelle's Faxpress, Expervision's OCR toolkit and Softrak's ADS.DLL
      with no problems. The trick is creating some functions/classes that help
      with the C structures that need to be passed back and forth. For that you
      will probably also need to take a look at the Python struct module, but ctypes
      has some built-in helper functions also.

      ctypes can be located here: http://starship.python.net/crew/theller/ctypes/

      -Larry Bates

      Comment

      • Larry Bates

        #4
        Re: First question on extending Python...

        Redefined Horizons wrote:[color=blue]
        > I still new to Python, and I've only dabbled in C, but I've got my
        > first project I need to tackle that involves both languages. I was
        > hoping to get some advice on how to proceed.
        >
        > There is a third-party application that I need to work with. It is
        > closed-source, but it exposes a C API. I want to wrap this C API so
        > that it is available from Python. I have no ability to modify the C
        > API of the third part application.
        >
        > Do I the C functions that I wrap in an extensions module for Python
        > need to be in a certian format? If so, I will have to write an
        > intermediate DLL in C that wraps the third-party application and
        > exports the functions in a form that Python can use.
        >
        > Or can an extension module for Python wrap any C function? If this is
        > the case I think I can skip the intermediate C DLL.
        >
        > Are there any advantages to using the intermediate DLL written in C in
        > this particular case where I will not have ability to manipulate the C
        > API of the third party application directly? (For example, if the
        > third-party application developers are willing to call a "call-back"
        > function that must be written in C, but not Python. Could the same
        > extension module export both C functions and serve as a Python
        > module?)
        >
        > Thanks,
        >
        > Scott Huey[/color]

        John Machin has answered most of your questions in a separate post. All you
        need is to take a look at ctypes module for Python. You can call virtually
        any C based API that is stored in a .DLL using ctypes. I've called .DLL
        APIs for Castelle's Faxpress, Expervision's OCR toolkit and Softrak's ADS.DLL
        with no problems. The trick is creating some functions/classes that help
        with the C structures that need to be passed back and forth. For that you
        will probably also need to take a look at the Python struct module, but ctypes
        has some built-in helper functions also.

        ctypes can be located here: http://starship.python.net/crew/theller/ctypes/

        -Larry Bates

        Comment

        • Cameron Laird

          #5
          Re: First question on extending Python...

          In article <448A048B.70000 07@websafe.com> ,
          Larry Bates <larry.bates@we bsafe.com> wrote:[color=blue]
          >Redefined Horizons wrote:[/color]

          Comment

          Working...