forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarshal.rs
More file actions
37 lines (32 loc) · 1.22 KB
/
marshal.rs
File metadata and controls
37 lines (32 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
pub(crate) use decl::make_module;
#[pymodule(name = "marshal")]
mod decl {
use crate::bytecode;
use crate::byteslike::PyBytesLike;
use crate::common::borrow::BorrowValue;
use crate::obj::objbytes::PyBytes;
use crate::obj::objcode::{PyCode, PyCodeRef};
use crate::pyobject::{IntoPyObject, PyObjectRef, PyResult, TryFromObject};
use crate::vm::VirtualMachine;
#[pyfunction]
fn dumps(co: PyCodeRef) -> PyBytes {
PyBytes::from(co.code.to_bytes())
}
#[pyfunction]
fn dump(co: PyCodeRef, f: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
vm.call_method(&f, "write", vec![dumps(co).into_pyobject(vm)])?;
Ok(())
}
#[pyfunction]
fn loads(code_bytes: PyBytesLike, vm: &VirtualMachine) -> PyResult<PyCode> {
let code = bytecode::CodeObject::from_bytes(&*code_bytes.borrow_value())
.map_err(|_| vm.new_value_error("Couldn't deserialize python bytecode".to_owned()))?;
Ok(PyCode { code })
}
#[pyfunction]
fn load(f: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyCode> {
let read_res = vm.call_method(&f, "read", vec![])?;
let bytes = PyBytesLike::try_from_object(vm, read_res)?;
loads(bytes, vm)
}
}