Monday, April 18, 2011

RPython modules for CPython




It is now possible to use RPythonic to write CPython extension modules in RPython, RPythonic will work as the front-end that uses the PyPy translation toolchain to generate C code, compile it with GCC, package it in a cache directory with generated ctypes wrappers, and finally replace the decorated functions in-place in CPython.

RPythonic 0.2.8



RPython CPython Module API



import os, sys
sys.path.append('..')
import rpythonic
rpythonic.set_cache( '../cache' )
rpythonic.set_pypy_root( '../../pypy' )
################################
rpy = rpythonic.RPython()
@rpy.bind() # declare arg types is optional if,
def add( a=1, b=1000 ): # keyword defaults are given
return a+b
@rpy.bind(a=float, b=float)
def sub( a, b ):
return a-b
rpy.cache('test1') # only compiles if cache is dirty
########### now functions are using compiled version ###########
print add( 99, 88 )

Module Compiling and Caching


3 comments:

  1. Pretty interesting, would be good to see some performance comparisons with Cython (this would probably be useful feedback for both projects, whichever turned out quicker).

    ReplyDelete
  2. I've been playing with cython, it would be cool if you could support an API similar to pyximport (rpyimport?).

    ReplyDelete
  3. Hi Stuaxo,
    I haven't done any tests yet to see if Cython or Rpython is faster. My guess is that Rpython can be faster if properly optimized because it is not calling into the PythonVM to interpret precompiled byte-codes; however, as i understand it, Cython will not have to do this much if you cdef your code well.

    Thanks for the comment, i have added "rpyimport" as an alias for "cache"

    ReplyDelete