Friday, December 29, 2006

A wish: Compile functions with shedskin

I have a wish. I wish to be able to compile a function in a module with Shed Skin at run time, much like it is done in weave and f2py2e. When I first though about it, it seemed a simple task:
  1. write a decorator that would allow the function to read its arguments from stdin and write what ever it returns to stdout.
  2. save the decorated function to a file, and compile it with shed skin
  3. call the resulting executable with os.popen, passing the arguments and receiving the output of the function.
It "would" be easy if there was a way to get the code of the function into a file at runtime.... As far as I know there is no way to do this.

I any case I am still sticking to my wish, hoping for suggestions of how to make it happen.

According to Mark Dufour, Shed Skin's developer, there is somebody working on using boost.python to wrap SS generated C++ code. That would be something very nice, but not as much as having a @skinner decorator.

4 comments:

srepmub said...

hello flavio,

I'm not sure I understand what you mean (not familiar with decorators, for instance), but could a tool like decompyle help here? decompyle is a 'decompiler' that converts python bytecode into equivalent python source code. maybe you could pass your function through decompyle and then shed skin?

thanks,
mark.

srepmub said...

btw, using boost would also open up the possibility of using arbitrary python modules from SS compiled code (such as 're'..)


thanks,
mark.

Flávio said...

Hi Mark,

Decorators are functions that wrap other functions, adding functionality.

The problem with decompyle, besides its not suporting recent python code (>2.3), is that i expects a .pyc file as input. I was refering to something that could take a python object and return a string with the original code. But that does not exist, AFAICT.

Given this fact, I believe that putting code to be compiled into separate modules and compiling it as extension modules with BoostPython is the way to go.

srepmub said...

I'm guessing that if you have a python object, then with some basic standard library calls, you can create a .pyc file out of it? :-) but yeah, it seems more like an interesting trick.


mark.

ccp

Amazon