Skip to content

Commit 2893117

Browse files
committed
impl TryFromObject for CString
1 parent 255b5a6 commit 2893117

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

vm/src/obj/objstr.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::function::{single_or_tuple_any, OptionalArg, PyFuncArgs};
3131
use crate::pyhash;
3232
use crate::pyobject::{
3333
Either, IdProtocol, IntoPyObject, ItemProtocol, PyClassImpl, PyContext, PyIterable,
34-
PyObjectRef, PyRef, PyResult, PyValue, TryIntoRef, TypeProtocol,
34+
PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TryIntoRef, TypeProtocol,
3535
};
3636
use crate::vm::VirtualMachine;
3737

@@ -1351,6 +1351,14 @@ impl IntoPyObject for &String {
13511351
}
13521352
}
13531353

1354+
impl TryFromObject for std::ffi::CString {
1355+
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
1356+
let s = PyStringRef::try_from_object(vm, obj)?;
1357+
Self::new(s.as_str().to_owned())
1358+
.map_err(|_| vm.new_value_error("embedded null character".to_owned()))
1359+
}
1360+
}
1361+
13541362
#[derive(FromArgs)]
13551363
struct SplitArgs {
13561364
#[pyarg(positional_or_keyword, default = "None")]

0 commit comments

Comments
 (0)