Skip to content

Commit

Permalink
failure case
Browse files Browse the repository at this point in the history
  • Loading branch information
mfarrugi committed Sep 18, 2021
1 parent 7b61b05 commit a483a7f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
28 changes: 26 additions & 2 deletions examples/ffi/python_calling_rust/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,41 @@
# limitations under the License.

load("@rules_python//python:defs.bzl", "py_test")
load("@rules_rust//rust:defs.bzl", "rust_library")
load("@rules_rust//python:py_rust_library.bzl", "py_rust_library")

rust_library(
name = "helper",
srcs = ["helper.rs"],
)

py_rust_library(
name = "hello_py",
srcs = ["hello_py.rs"],
edition = "2018",
deps = ["//ffi/python_calling_rust/raze:pyo3"],
deps = [
":helper",
"//ffi/python_calling_rust/raze:pyo3",
],
)

py_rust_library(
name = "hello_py_2",
srcs = ["hello_py_2.rs"],
edition = "2018",
deps = [
":helper",
"//ffi/python_calling_rust/raze:pyo3",
],
)



py_test(
name = "test",
srcs = ["test.py"],
deps = [":hello_py"],
deps = [
":hello_py",
":hello_py_2",
],
)
6 changes: 6 additions & 0 deletions examples/ffi/python_calling_rust/hello_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Ok((a + b).to_string())
}

#[pyfunction]
fn secret() -> i32 {
&helper::secret as *const u32 as usize as i32
}

#[pymodule]
fn hello_py(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
m.add_function(wrap_pyfunction!(secret, m)?)?;

Ok(())
}
20 changes: 20 additions & 0 deletions examples/ffi/python_calling_rust/hello_py_2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;

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

#[pyfunction]
fn secret() -> i32 {
&helper::secret as *const u32 as usize as i32
}

#[pymodule]
fn hello_py_2(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
m.add_function(wrap_pyfunction!(secret, m)?)?;

Ok(())
}
1 change: 1 addition & 0 deletions examples/ffi/python_calling_rust/helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub static secret: u32 = 42;
14 changes: 12 additions & 2 deletions examples/ffi/python_calling_rust/test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
from ffi.python_calling_rust.hello_py import sum_as_string
from ffi.python_calling_rust import hello_py
from ffi.python_calling_rust import hello_py_2


assert hello_py.sum_as_string(1, 1) == "2"
assert hello_py_2.sum_as_string(1, 1) == "2"


s1 = hello_py.secret()
s2 = hello_py_2.secret()

assert s1 == s2, (s1, s2)

assert sum_as_string(1, 1) == "2"
print("Ok!")

0 comments on commit a483a7f

Please sign in to comment.