Skip to content

Commit 7ea97d1

Browse files
Add support for None, True & False
1 parent e4c8bd7 commit 7ea97d1

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

crates/capi/src/object.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use crate::pystate::with_vm;
33
use core::ffi::c_ulong;
44
use rustpython_vm::builtins::PyType;
55
use rustpython_vm::convert::IntoObject;
6-
use rustpython_vm::{Context, Py};
6+
use rustpython_vm::{AsObject, Context, Py};
7+
use std::ffi::c_uint;
78
use std::mem::MaybeUninit;
89

910
const PY_TPFLAGS_LONG_SUBCLASS: c_ulong = 1 << 24;
@@ -109,7 +110,16 @@ pub extern "C" fn PyObject_Str(_obj: *mut PyObject) -> *mut PyObject {
109110
}
110111

111112
#[unsafe(no_mangle)]
112-
pub extern "C" fn Py_GetConstantBorrowed(_constant_id: core::ffi::c_uint) -> *mut PyObject {
113-
crate::log_stub("Py_GetConstantBorrowed");
114-
std::ptr::null_mut()
113+
pub extern "C" fn Py_GetConstantBorrowed(constant_id: c_uint) -> *mut PyObject {
114+
with_vm(|vm| {
115+
let ctx = &vm.ctx;
116+
match constant_id {
117+
0 => ctx.none.as_object(),
118+
1 => ctx.true_value.as_object(),
119+
2 => ctx.true_value.as_object(),
120+
_ => panic!("Invalid constant_id passed to Py_GetConstantBorrowed"),
121+
}
122+
.as_raw()
123+
.cast_mut()
124+
})
115125
}

example_projects/pyo3_embed/src/main.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
use pyo3::prelude::*;
2-
use pyo3::types::{PyInt, PyString};
2+
use pyo3::types::{PyInt, PyNone, PyString};
3+
4+
5+
#[pyfunction]
6+
fn python_function(py: Python<'_>) -> Borrowed<'_, '_, PyNone> {
7+
PyNone::get(py)
8+
}
39

410
fn main() {
511
Python::initialize();
@@ -25,6 +31,8 @@ fn main() {
2531
.join()
2632
.unwrap();
2733

34+
assert!(python_function(py).is_none());
35+
2836
PyResult::Ok(())
2937
})
3038
.unwrap();

0 commit comments

Comments
 (0)