Skip to content

Commit 16f8074

Browse files
Use pyo3 as test harness
1 parent 55b1e14 commit 16f8074

File tree

16 files changed

+168
-75
lines changed

16 files changed

+168
-75
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
[env]
2-
PYO3_CONFIG_FILE = { value = "pyo3-rustpython.config", relative = true }
1+
[profile.test.env]
2+
PYO3_CONFIG_FILE = { value = "pyo3-rustpython.config", relative = true }

crates/capi/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ crate-type = ["staticlib"]
1414
[dependencies]
1515
rustpython-vm = { workspace = true, features = ["threading"]}
1616

17+
[dev-dependencies]
18+
pyo3 = { version = "0.28.3", features = ["auto-initialize", "abi3-py314"] }
19+
1720
[lints]
1821
workspace = true

example_projects/pyo3_embed/pyo3-rustpython.config renamed to crates/capi/pyo3-rustpython.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
implementation=CPython
22
version=3.14
3-
shared=false
3+
shared=true
44
abi3=true
55
build_flags=
66
suppress_build_script_link_lines=true

crates/capi/src/abstract_.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,17 @@ pub extern "C" fn PyObject_VectorcallMethod(
7575
)
7676
})
7777
}
78+
79+
#[cfg(test)]
80+
mod tests {
81+
use pyo3::prelude::*;
82+
use pyo3::types::PyString;
83+
84+
#[test]
85+
fn test_call_method1() {
86+
Python::attach(|py| {
87+
let string = PyString::new(py, "Hello, World!");
88+
assert!(string.call_method1("endswith", ("!",)).unwrap().is_truthy().unwrap());
89+
})
90+
}
91+
}

crates/capi/src/bytesobject.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,17 @@ pub extern "C" fn PyBytes_AsString(bytes: *mut PyObject) -> *mut c_char {
4040
bytes.as_bytes().as_ptr() as _
4141
})
4242
}
43+
44+
#[cfg(test)]
45+
mod tests {
46+
use pyo3::prelude::*;
47+
use pyo3::types::PyBytes;
48+
49+
#[test]
50+
fn test_bytes() {
51+
Python::attach(|py| {
52+
let bytes = PyBytes::new(py, b"Hello, World!");
53+
assert_eq!(bytes.as_bytes(), b"Hello, World!");
54+
})
55+
}
56+
}

crates/capi/src/import.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,15 @@ pub extern "C" fn PyImport_Import(name: *mut PyObject) -> *mut PyObject {
1515
)
1616
})
1717
}
18+
19+
#[cfg(test)]
20+
mod tests {
21+
use pyo3::prelude::*;
22+
23+
#[test]
24+
fn test_import() {
25+
Python::attach(|py| {
26+
let _module = py.import("sys").unwrap();
27+
})
28+
}
29+
}

crates/capi/src/longobject.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,18 @@ pub extern "C" fn PyLong_AsLong(obj: *mut PyObject) -> c_long {
5858
})
5959
})
6060
}
61+
62+
#[cfg(test)]
63+
mod tests {
64+
use pyo3::prelude::*;
65+
use pyo3::types::PyInt;
66+
67+
#[test]
68+
fn test_py_int() {
69+
Python::attach(|py| {
70+
let number = PyInt::new(py, 123);
71+
assert!(number.is_instance_of::<PyInt>());
72+
assert_eq!(number.extract::<i32>().unwrap(), 123);
73+
})
74+
}
75+
}

crates/capi/src/object.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,31 @@ pub extern "C" fn PyObject_IsTrue(obj: *mut PyObject) -> c_int {
133133
)
134134
})
135135
}
136+
137+
#[cfg(test)]
138+
mod tests {
139+
use pyo3::prelude::*;
140+
use pyo3::types::PyString;
141+
142+
#[test]
143+
fn test_is_truthy() {
144+
Python::attach(|py| {
145+
assert!(!py.None().is_truthy(py).unwrap());
146+
})
147+
}
148+
149+
#[test]
150+
fn test_is_none() {
151+
Python::attach(|py| {
152+
assert!(py.None().is_none(py));
153+
})
154+
}
155+
156+
#[test]
157+
fn test_type_name() {
158+
Python::attach(|py| {
159+
let string = PyString::new(py, "Hello, World!");
160+
assert_eq!(string.get_type().name().unwrap().to_str().unwrap(), "str");
161+
})
162+
}
163+
}

crates/capi/src/pyerrors.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,17 @@ pub extern "C" fn PyException_GetTraceback(_exc: *mut PyObject) -> *mut PyObject
135135
crate::log_stub("PyException_GetTraceback");
136136
std::ptr::null_mut()
137137
}
138+
139+
#[cfg(test)]
140+
mod tests {
141+
use pyo3::exceptions::PyTypeError;
142+
use pyo3::prelude::*;
143+
144+
#[test]
145+
fn test_raised_exception() {
146+
Python::attach(|py| {
147+
PyTypeError::new_err("This is a type error").restore(py);
148+
assert!(PyErr::take(py).is_some());
149+
})
150+
}
151+
}

0 commit comments

Comments
 (0)