py2many/py2many

Transpile to native extension

pombredanne opened this issue · 6 comments

When I transpile Python code to say C or Rust, I would like to be able to use the transpiled code from Python. It would be very nice if there was an option (at least for some language that are amenable to this) such that the transpiled code would be a valid Python native extension callable from Python.

@pombredanne - the most interesting candidate in this space is

https://github.com/PyO3/pyo3

I can imagine a mode where we add a new command line option:

py2many.py --rust=1 --extension ...

That results in:

  • Appropriate use statements and [#pyfunction] attribute are added
  • Types are munged. Generate PyResult<String> instead of <str>

@adsharma this would be awesome!

@pombredanne - could you please take a look? These are the basics. Could you give us feedback on what's missing?

Missing feature: handling strings and return values

If you look at the example here:

https://pyo3.rs/v0.13.2/#using-rust-from-python

fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
    Ok((a + b).to_string())
}

py2many produces:

pub fn sum_as_string(a: i32, b: i32) -> &str {
    return &(a + b).to_string();
}

Two things we need to teach the transpiler:

  • Return PyResult instead of &str when using extensions.
  • Use Ok() for Result types.

Some discussion of this topic here:

https://gitter.im/PyO3/Lobby#

#228 is doing classes and methods as per documentation