[Python-Dev] More feedback from script scape

Paul Prescod paulp@ActiveState.com
Wed, 22 Aug 2001 16:10:58 -0700


"Barry A. Warsaw" wrote:
> 
>...
> 
> How about a PEP, Paul?!
> 
> I've no clue what inline::C or inline::Python do, and no time to learn
> about it, but if you have a good grasp of them, try to think about how
> those ideas might translate to Python.  A PEP on simplifying extension
> writing would be welcome.

I have only a superficial understanding which I can convey in this
email. If you think my superficial understanding is worth PEP-ing, then
I can do that.

http://inline.perl.org/

Okay, remember in the old days when you did inline assembly in C code?
You did that because assembly was a speedup over C and there were a few
things you could do in assembly but not in C (e.g. talk directly to
devices and registers by memory address). Fast forward to the present.
Why do you use C extensions? 1. Speedup 2. Direct access to stuff that
has a C API. So why not inline C in Python (or Perl)?

use Inline C => <<'END_C';
    void greet() {
        printf("Hello, world\n");
    }
    END_C    

greet();

This does what you would expect. But what's going on under the hood?

 1. Inline pseudo-parses the C code and looks for function and parameter
declarations.

 2. Inline generates the wrapper code that Python programmers usually
either write by hand or get SWIG to do.

 3. Inline generates a dynamic library in a special "cache" directory
using MakeMaker, the system C compiler, etc.

 4. Inline loads the library it just created as a Perl extension module.

You (the user) invoke all of this stuff just by running the program.
Inline doesn't recompile over and over again because it uses MD5 to
recognize that the thing has already been compiled. Inline does have a
strategy for dealing with modules that is supposed to be distributed
instead of just used locally. It essentially compiles the code once and
then you ask an external program to package it as a source or binary
distribution.

I suggest you skim the examples in this article:

http://www.perl.com/pub/a/2001/02/inline.html

You'll see: 

 * calling win32 functions without using distutils or MakeMaker
explicitly

 * writing a C program that behaves as if it were a script (no build
step)

 * embedding Python in Perl

and other weird stuff that will make you feel like your "head has just
been wrapped around a brick". (quote from the article)

-- 
Take a recipe. Leave a recipe.  
Python Cookbook!  http://www.ActiveState.com/pythoncookbook