Skip to content

Commit faabbbc

Browse files
Add bool support to c-api (#7938)
1 parent ae8a7c1 commit faabbbc

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎crates/capi/src/boolobject.rs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use crate::object::define_py_check;
2+
use crate::{PyObject, pystate::with_vm};
3+
use core::ffi::{c_int, c_long};
4+
use rustpython_vm::AsObject;
5+
6+
define_py_check!(fn PyBool_Check, types.bool_type);
7+
8+
#[unsafe(no_mangle)]
9+
pub unsafe extern "C" fn Py_IsTrue(obj: *mut PyObject) -> c_int {
10+
with_vm(|vm| unsafe { obj.as_ref().is_some_and(|obj| obj.is(&vm.ctx.true_value)) })
11+
}
12+
13+
#[unsafe(no_mangle)]
14+
pub unsafe extern "C" fn Py_IsFalse(obj: *mut PyObject) -> c_int {
15+
with_vm(|vm| unsafe { obj.as_ref().is_some_and(|obj| obj.is(&vm.ctx.false_value)) })
16+
}
17+
18+
#[unsafe(no_mangle)]
19+
pub extern "C" fn PyBool_FromLong(value: c_long) -> *mut PyObject {
20+
with_vm(|vm| {
21+
if value == 0 {
22+
&vm.ctx.false_value
23+
} else {
24+
&vm.ctx.true_value
25+
}
26+
.to_owned()
27+
})
28+
}

‎crates/capi/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::sync::MutexGuard;
99
extern crate alloc;
1010

1111
pub mod abstract_;
12+
pub mod boolobject;
1213
pub mod bytesobject;
1314
pub mod ceval;
1415
pub mod dictobject;

0 commit comments

Comments
 (0)