Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion crates/capi/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ use rustpython_vm::import::import_code_obj;
pub unsafe extern "C" fn PyImport_Import(name: *mut PyObject) -> *mut PyObject {
with_vm(|vm| {
let name = unsafe { (&*name).try_downcast_ref::<PyStr>(vm)? };
vm.import(name, 0)
let _ = vm.import(name, 0)?;

vm.sys_module
.get_attr(rustpython_vm::identifier!(vm, modules), vm)?
.get_item(name, vm)
})
}

Expand Down Expand Up @@ -74,4 +78,12 @@ mod tests {
let _module = py.import("types").unwrap();
})
}

#[test]
fn import_sub_module() {
Python::attach(|py| {
let module = py.import("collections.abc").unwrap();
module.getattr("Sequence").unwrap();
})
}
}
14 changes: 14 additions & 0 deletions crates/capi/src/methodobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub(crate) fn build_method_def(

let flags = PyMethodFlags::from_bits(ml.ml_flags as u32)
.ok_or_else(|| vm.new_system_error("PyMethodDef contains unknown flags"))?;
let has_self = has_self && !flags.contains(PyMethodFlags::STATIC);

let method = ml.ml_meth;

Expand Down Expand Up @@ -359,4 +360,17 @@ mod tests {
);
})
}

#[test]
fn wrap_static_no_args_function() {
#[pyfunction()]
fn f() {}

Python::attach(|py| {
let module = PyModule::new(py, "test_wrap_pyfunction_forms").unwrap();

let func = wrap_pyfunction!(f, &module).unwrap();
func.call0().unwrap();
});
}
}
16 changes: 14 additions & 2 deletions crates/capi/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub unsafe extern "C" fn PyObject_HasAttrWithError(
with_vm(|vm| {
let obj = unsafe { &*obj };
let name = unsafe { &*attr_name }.try_downcast_ref::<PyStr>(vm)?;
obj.has_attr(name, vm)
Ok(vm.get_attribute_opt(obj.to_owned(), name)?.is_some())
})
}

Expand Down Expand Up @@ -272,7 +272,7 @@ pub unsafe extern "C" fn PyObject_HasAttrStringWithError(
with_vm(|vm| {
let obj = unsafe { &*obj };
let name = unsafe { attr_name.try_as_str(vm) }?;
obj.has_attr(name, vm)
Ok(vm.get_attribute_opt(obj.to_owned(), name)?.is_some())
})
}

Expand Down Expand Up @@ -591,4 +591,16 @@ mod tests {
assert!(dict.get_item("foo").is_ok());
})
}

#[test]
fn hasattr() {
Python::attach(|py| {
let x = 5i32.into_pyobject(py).unwrap();
assert!(x.is_instance_of::<PyInt>());

// spell-checker:ignore bbbbbbytes
assert!(x.hasattr("to_bytes").unwrap());
assert!(!x.hasattr("bbbbbbytes").unwrap());
})
}
}
23 changes: 23 additions & 0 deletions crates/capi/src/pycapsule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ pub unsafe extern "C" fn PyCapsule_GetContext(capsule: *mut PyObject) -> *mut c_
let capsule = unsafe { &*capsule }
.downcast_ref_if_exact::<PyCapsule>(vm)
.ok_or_else(|| vm.new_value_error("Invalid capsule"))?;

if capsule.pointer().is_null() {
return Err(vm.new_value_error("Capsule has null pointer"));
}

Ok(capsule.context())
})
}
Expand Down Expand Up @@ -143,6 +148,7 @@ fn checked_capsule<'a>(

#[cfg(test)]
mod tests {
use pyo3::ffi;
use pyo3::prelude::*;
use pyo3::types::PyCapsule;

Expand All @@ -156,4 +162,21 @@ mod tests {
assert_eq!(unsafe { ptr.cast::<String>().as_ref() }, "Some data");
})
}

#[test]
fn capsule_context_on_invalid_capsule() {
Python::attach(|py| {
let cap = PyCapsule::new_with_value(py, 123u32, c"name").unwrap();

// Invalidate the capsule
// SAFETY: intentionally breaking the capsule for testing
unsafe {
ffi::PyCapsule_SetPointer(cap.as_ptr(), core::ptr::null_mut());
}

// context() on invalid capsule should fail
let result = cap.context();
assert!(result.is_err());
});
}
}
3 changes: 3 additions & 0 deletions crates/vm/src/builtins/capsule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ impl Representable for PyCapsule {

impl Destructor for PyCapsule {
fn del(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<()> {
if zelf.pointer().is_null() {
return Ok(());
}
if let Some(destructor) = zelf.destructor() {
unsafe { destructor(zelf.as_object().as_raw().cast_mut()) };
}
Expand Down
Loading