Skip to content

Add example for calling between rust and python #4276

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

Merged
merged 4 commits into from
Dec 2, 2022

Conversation

Gelox
Copy link

@Gelox Gelox commented Nov 14, 2022

Add an example which illustrates how to call between rust and python. Most importantly there no examples which illustrate how to call rust from python and it is not obvious how to do this.

I never figured out if there was a way to nicely call a constructor of a rust struct since all methods in python require the self parameter I assumed that this was not possible and that construction should be done via a standalone function.
If it is encouraged or idiomatic to use a python constructor from rust for some reason then perhaps that should be added to the example.

Previous opened issue: #4124

@Gelox
Copy link
Author

Gelox commented Nov 15, 2022

Is there a way that you can have a String with python code instead of having a separate file in the OS? How would I import this instead of the file?

@youknowone
Copy link
Member

You might set git email wrong (not same as your github id email)

@youknowone
Copy link
Member

to use import, this is the best way. consider vm.compile and vm.run_code_obj to avoid external file, but this is not 100% compliant with import.

@Gelox
Copy link
Author

Gelox commented Nov 16, 2022

I fixed the formatting.

You might set git email wrong (not same as your github id email)

Yeah, I'm intentionally setting the email to a random one because I don't like having the other one associated with my account. However git refuses to not use the global config which causes me to forget and have to amend the email with a force push.

I hope it's not important that the email is set the same as my github email?

to use import, this is the best way. consider vm.compile and vm.run_code_obj to avoid external file, but this is not 100% compliant with import.

Alright, then I'll leave the import business be as it is.

@youknowone
Copy link
Member

It will be ok if the email is real, but I think this is not ideal

commit f0faccfe46852552027bd1e43e3c95b2e763aa85 (HEAD -> Gelox/main)
Author: q <q@q.q>
Date:   Wed Nov 16 12:06:23 2022 +0100

    Format

You can set local name and email which doesn't affect global config using git config user.email <emailaddress>

@Gelox
Copy link
Author

Gelox commented Nov 20, 2022

Alright, I will update the email address to a real one.

I would like to put one more thing into the example which I realize is currently missing, which is to be able to use rust struct fields from inside of python.

Something like:
rust:

#[pyattr]
#[pyclass(module = "rust_py_module", name = "RustStruct")]
struct RustStruct {
    pub number: f32
}

python:

from rust_py_module import RustStruct

def python_func():
    rust_struct = rust_function()
    print(rust_struct.number)

Currently this is not working and I'm not sure how to accomplish it or if it is even possible, but I don't see any obvious reason for why it wouldn't be possible.

@youknowone
Copy link
Member

That's not enough. Adding getset descriptor is necessary.

impl RustStruct {
    #[pygetset]
    fn number(&self) -> f32 {
        self.number
    }
}

we'd like to support member descriptor like below one day:

#[pyattr]
#[pyclass(module = "rust_py_module", name = "RustStruct")]
struct RustStruct {
    #[pymember]
    pub number: f32
}

but not yet.

cc @moreal

Add an example which illustrates how to call between rust and python.
Most importantly there no examples which illustrate how to call rust
from python and it is not obvious how to do this.
In the example demonstrating how to call between rust and python add
direct member access.
@Gelox
Copy link
Author

Gelox commented Nov 21, 2022

The direct member access is included and I've corrected the email to an existing one that I own. Let me know if there are any further changes you want (I had to leave that Vec::clone() in there, couldn't figure out how to get around it).

@youknowone
Copy link
Member

awesome! thank you!

@DimitrisJim DimitrisJim merged commit 18af44b into RustPython:main Dec 2, 2022
@DimitrisJim
Copy link
Member

Thanks @Gelox!

@m172021
Copy link

m172021 commented Jul 18, 2024

I am running into similar issues. I am looking at:

https://github.com/RustPython/RustPython/blob/main/examples/call_between_rust_and_python.rs#L65-L83

Here is my question: on the Rust side, I have a RustStruct. Question: how do I bind this in the vm so python code can access this RustStruct ?

Do I add it via VM, goobal scope? .... ?

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

Successfully merging this pull request may close these issues.

6 participants