NEWBIE: Extending Python with C

Bengt Richter bokr at oz.net
Sat Nov 9 04:54:01 EST 2002


On Sat, 09 Nov 2002 03:48:35 GMT, engsol at teleport.com wrote:

>I've read the books, tried some code, and I still just don't get it.
>
>If some kind soul can explain the (high level) process, I can figure out the details, I
>think.
>
>What I want to do is call (or invoke) a C exe  from Python, and return the results to
>Python. Or pass values of some sort to the C exe, and again have Python trap the returned
>results. I'm a long way from the concept of shared variables. <grin>
If it's a separate exe, you could go this route:

[ 1:45] C:\pywk\junk>type hello.c
/* hello.c */
#include <stdio.h>
void main(){
    printf("Hello World!\n");
}


[ 1:45] C:\pywk\junk>cl -Ox -nologo hello.c
hello.c

[ 1:46] C:\pywk\junk>dir hello.*
 Volume in drive C is System
 Volume Serial Number is 14CF-C4B9

 Directory of C:\pywk\junk

02-11-09  01:45                     85 hello.c
02-11-09  01:46                 28,672 hello.exe
02-11-09  01:46                    550 hello.obj
               3 File(s)         29,307 bytes
                            159,109,632 bytes free

[ 1:46] C:\pywk\junk>python
Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.popen('hello.exe').read()
'Hello World!\n'



>
>The first stumbling block is that the books offer examples of C code which don't seem to
>have a main(). My compilers don't care for that too much. I took a quick look at SWIG, but
>to be honest, I didn't understand how to use it. Again, I need the high level concept.
>
Well, it depends on what kind of coupling you need between your python stuff and the c stuff.
Some variant of the above may do it, or not ;-) Need more definite requirement statement.

>The second problem is the concept of translating PyObjects to C and vice versa. As I say,
>I just don't get it yet.
>
>The third problem is that the books spend one paragraph on the Python/C code, usually for
>Linux, then three pages on reference counts. Do I really have to worry about them, or not?
>
>I'm using Python 222 on NT. If I understood how to do  "Hello world" (in C, called from
>Python), I might be able to go from there.
>
the above was Python 2.2.2 on NT4 ;-)

>In case you're interested, the reason I need to learn this is that my company is in dire
>need of a new software test tool. I'm convinced that Python is the right scripting
>language for the testers, and will be best in terms of maintainabilty and enhancement, no
>matter if the platform is NT/2000/Linux. We have a huge library of legacy C code which we
>can re-use as is, assuming I can learn to marry Python and C.
Is it legacy .exe's that you want to run under scripted control, or is it C source that
you want to use in some python extension modules?

>
> BTW, I'm a diehard Python convert! (But my family doesn't know yet, so keep it under your
>hat please)
>
Ok.

Regards,
Bengt Richter



More information about the Python-list mailing list