NEWBIE: Extending Python with C

Grant Edwards grante at visi.com
Fri Nov 8 23:53:13 EST 2002


In article <3dcc7e2c.3568851 at news.earthlink.net>, 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>

What you're talking about isn't "extending Python".  It sounds like you want
to run an external executable program (it doesn't matter what language it's
in).

Take a look at os.system() or the popen family of funtions.

> 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.

Your C compiler cares not a whit whether there's a main() or not. I suspect
that your linker combined with your default C run time libraries do, though.

> 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.
> 
> The second problem is the concept of translating PyObjects to C and vice
> versa. As I say, I just don't get it yet.

If you've looked at existing C extension code and "don't get it", then I'm
not sure where you should start.  Are you familiar with C programming?  Do
you know how to develope a dynamic library module?

> 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?

Maybe.  Maybe 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.

Sorry, I don't know anything about NT.  In Linux you create a .so shared
library that follows certain API conventions.  Then it gets dynamically
loaded by the Python interpreter when you "import" the module.  Python calls
functions in your C code (and your C code probably calls functions in the
Python interpreter).  

> 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.

Maybe you do want to extend Python.  Do you want to add new data types that
are implimented in C?  Do you want to use C code to perform operations on
Python data types?

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

-- 
Grant Edwards                   grante             Yow!  What I want to find
                                  at               out is -- do parrots know
                               visi.com            much about Astro-Turf?



More information about the Python-list mailing list