Wrapper round x86 Assembler

Andrew MacIntyre andymac at bullseye.apana.org.au
Sat Apr 10 20:27:18 EDT 2004


On Sat, 10 Apr 2004, Fuzzyman wrote:

> *However* it's not quite what I had in mind. What I'd like to be able
> to do is to access machine code subroutines from within normal python.
>
> Not that exploring pyrex is a bad idea - I probably should.
> I don't think my 'idea' would be hard to achieve, since effectively
> all that would be needed is a wrapper to an existing freeware
> assembler *and* an interface to the assembled 'objects'. Ho hum - a
> bit beyond me at the moment and until I have a real burning need for
> it will have to remain an idea......

At the moment you have several alternatives available that I can see:

- build your assembly routines into a DLL/.so and use Thomas Heller's
  excellent ctypes module.

- build your assembly routines into a library or DLL/.so and use Pyrex
  to call them.

- find a C compiler that understands how to build asm files to objects,
  then tweak the Distutils support to call the compiler when it an
  assembly source file is included in a Distutils'ified package.  On
  Windows MinGW should get the job done; gcc can do so for posix
  platforms.

- do it the old fashioned way: create a makefile that builds the
  components and links to the Python core.

If you want to avoid dealing with the necessary Python C API boilerplate
required to allow Python to call your routine, you'll find the ctypes
solution by far the easiest as your code only has to have a calling
interface compatible with a C compiler.  The Pyrex solution could work as
well, but introduces an extra language layer into the situation.

The other 2 approaches require that your code deals with the Python C API,
and you would have to create the necessary include files to replicate
"Python.h" appropriately.

The only downside to the ctypes approach may be performance, as each call
may have a high overhead cost compared to the other approaches.

--
Andrew I MacIntyre                     "These thoughts are mine alone..."
E-mail: andymac at bullseye.apana.org.au  (pref) | Snail: PO Box 370
        andymac at pcug.org.au             (alt) |        Belconnen  ACT  2616
Web:    http://www.andymac.org/               |        Australia




More information about the Python-list mailing list