Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transpile to native extension #62

Open
pombredanne opened this issue Apr 15, 2021 · 6 comments
Open

Transpile to native extension #62

pombredanne opened this issue Apr 15, 2021 · 6 comments

Comments

@pombredanne
Copy link

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.

@adsharma
Copy link
Collaborator

@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>

@pombredanne
Copy link
Author

@adsharma this would be awesome!

adsharma added a commit that referenced this issue May 13, 2021
This was tested with:

```
def sum_as_string(a: int, b: int) -> int:
    return a + b
```

py2many --rust=1 --extension=1 /tmp/foo.py

and by copying over foo.rs as src/lib.rs in a pyO3 example template.

Related to: #62
@adsharma
Copy link
Collaborator

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

@adsharma
Copy link
Collaborator

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.

adsharma added a commit that referenced this issue May 13, 2021
This was tested with:

```
def sum_as_string(a: int, b: int) -> int:
    return a + b
```

py2many --rust=1 --extension=1 /tmp/foo.py

and by copying over foo.rs as src/lib.rs in a pyO3 example template.

Related to: #62
adsharma added a commit that referenced this issue May 13, 2021
This was tested with:

```
def sum_as_string(a: int, b: int) -> int:
    return a + b
```

py2many --rust=1 --extension=1 /tmp/foo.py

and by copying over foo.rs as src/lib.rs in a pyO3 example template.

Related to: #62
adsharma added a commit that referenced this issue May 13, 2021
This was tested with:

```
def sum_as_string(a: int, b: int) -> int:
    return a + b
```

py2many --rust=1 --extension=1 /tmp/foo.py

and by copying over foo.rs as src/lib.rs in a pyO3 example template.

Related to: #62
adsharma added a commit that referenced this issue May 14, 2021
This covers the mechanics of using PyResult from an extension.

Tested via the canonical pyO3 example:

```
def sum_as_string(a: int, b: int) -> str:
    return str(a + b)
```

Related to: #62
@adsharma
Copy link
Collaborator

Some discussion of this topic here:

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

@adsharma
Copy link
Collaborator

#228 is doing classes and methods as per documentation

MiguelMarcelino pushed a commit to MiguelMarcelino/py2many that referenced this issue May 23, 2022
This was tested with:

```
def sum_as_string(a: int, b: int) -> int:
    return a + b
```

py2many --rust=1 --extension=1 /tmp/foo.py

and by copying over foo.rs as src/lib.rs in a pyO3 example template.

Related to: py2many#62
MiguelMarcelino pushed a commit to MiguelMarcelino/py2many that referenced this issue May 23, 2022
This covers the mechanics of using PyResult from an extension.

Tested via the canonical pyO3 example:

```
def sum_as_string(a: int, b: int) -> str:
    return str(a + b)
```

Related to: py2many#62
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants