473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Jun 9 '06 #1
4 1207
On 10/06/2006 8:32 AM, Redefined Horizons wrote:
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?
No.

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.
No way. You need only *one* layer of glue.

Or can an extension module for Python wrap any C function?
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.
If this is
the case I think I can skip the intermediate C DLL.
I think so too.

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.
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 :-)
Could the same
extension module export both C functions and serve as a Python
module?)


Yes.

Cheers,
John
Jun 9 '06 #2
Redefined Horizons wrote:
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 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
Jun 9 '06 #3
Redefined Horizons wrote:
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 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

Jun 9 '06 #4
In article <44**************@websafe.com>,
Larry Bates <la*********@websafe.com> wrote:
Redefined Horizons wrote:

Jun 12 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been...
3
by: stefan | last post by:
Hi Folks, I currenty extended some of my C++ functionality to python and also embedded python to use python functionality in my C++ system (and use as well these extended functions). While...
3
by: ch424 | last post by:
Hi there, I'm using Python 2.4.1 on Ubuntu Linux, and I'm having problems extending python in C: The C code is below: #include <Python.h> #include "ni488.h"
1
by: Richard Townsend | last post by:
In the "Extending and Embedding" part of the Python documentation: section 5.4 "Extending Embedded Python" - it describes how to use a Python extension module from Python that is embedded in a C...
3
by: Marco Meoni | last post by:
Hi all! I've a problem with a C++ class that has to be included in a python application. One way to do it is Extending and Embedding the Python Interpreter Now i have 2 questions 1) Is there a...
1
by: punitha | last post by:
hai, Iam using python to tie C functions(premitives). Iam calling C functions from python through swig(Extending with C). This working well in Linux,but i dont know how to do it in...
1
by: jeremito | last post by:
I am trying to learn how to extend and/or embed Python. I have looked at the document "Extending and Embedding the Python Interpreter" and also "Python/C API Reference Manual. In the examples...
6
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
7
by: Maximus Decimus | last post by:
HI all, I am using python v2.5 and I am an amateur working on python. I am extending python for my research work and would like some help and guidance w.r.t this matter from you experienced...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.