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

py_fn fails if a trailing comma is present #219

Closed
cpterry opened this issue Apr 22, 2020 · 2 comments
Closed

py_fn fails if a trailing comma is present #219

cpterry opened this issue Apr 22, 2020 · 2 comments

Comments

@cpterry
Copy link

cpterry commented Apr 22, 2020

The py_fn macro fails if a trailing comma is present after the last argument of the function. This comma is added by rustfmt if those arguments are split across multiple lines so it's troublesome to work around.

An example of the code that will not compile is below. The only change from the README.md example is adding the comma in b: i64,.

use cpython::{py_fn, py_module_initializer, PyResult, Python};

// add bindings to the generated python module
// N.B: names: "rust2py" must be the name of the `.so` or `.pyd` file
py_module_initializer!(rust2py, |py, m| {
    m.add(py, "__doc__", "This module is implemented in Rust.")?;
    m.add(
        py,
        "sum_as_string",
        py_fn!(py, sum_as_string_py(a: i64, b: i64,)),
    )?;
    Ok(())
});

// logic implemented as a normal rust function
fn sum_as_string(a: i64, b: i64) -> String {
    format!("{}", a + b).to_string()
}

// rust-cpython aware function. All of our python interface could be
// declared in a separate module.
// Note that the py_fn!() macro automatically converts the arguments from
// Python objects to Rust values; and the Rust return value back into a Python object.
fn sum_as_string_py(_: Python, a: i64, b: i64) -> PyResult<String> {
    let out = sum_as_string(a, b);
    Ok(out)
}
lausek added a commit to lausek/rust-cpython that referenced this issue Apr 23, 2020
@lausek
Copy link
Contributor

lausek commented Apr 23, 2020

I've extended the macro responsible for parsing the parameter list to ignore automatically appended commas. Could you please check if that fixes your error?

@cpterry
Copy link
Author

cpterry commented Apr 23, 2020

That fixed it! Perfect! Thanks.

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