2020-09-23 12:50:40 +00:00
|
|
|
#![cfg(not(Py_LIMITED_API))]
|
2020-10-11 01:12:47 +00:00
|
|
|
//! Support for the Python `marshal` format. Not supported in limited API
|
|
|
|
//! builds.
|
2020-09-23 12:50:40 +00:00
|
|
|
|
2019-04-24 09:04:17 +00:00
|
|
|
use crate::ffi;
|
2019-04-25 13:05:59 +00:00
|
|
|
use crate::types::{PyAny, PyBytes};
|
|
|
|
use crate::{AsPyPointer, FromPyPointer, PyResult, Python};
|
|
|
|
use std::os::raw::{c_char, c_int};
|
2019-04-24 09:04:17 +00:00
|
|
|
|
|
|
|
/// The current version of the marshal binary format.
|
|
|
|
pub const VERSION: i32 = 4;
|
|
|
|
|
|
|
|
/// Serialize an object to bytes using the Python built-in marshal module.
|
|
|
|
///
|
2020-03-17 14:16:15 +00:00
|
|
|
/// The built-in marshalling only supports a limited range of objects.
|
2019-04-25 13:15:59 +00:00
|
|
|
/// The exact types supported depend on the version argument.
|
|
|
|
/// The [`VERSION`] constant holds the highest version currently supported.
|
|
|
|
///
|
2020-03-17 14:16:15 +00:00
|
|
|
/// See the [Python documentation](https://docs.python.org/3/library/marshal.html) for more details.
|
2019-04-25 13:15:59 +00:00
|
|
|
///
|
2021-03-20 07:45:56 +00:00
|
|
|
/// # Examples
|
2019-04-25 13:15:59 +00:00
|
|
|
/// ```
|
|
|
|
/// # use pyo3::{marshal, types::PyDict};
|
2021-03-20 07:44:28 +00:00
|
|
|
/// # pyo3::Python::with_gil(|py| {
|
2019-04-25 13:15:59 +00:00
|
|
|
/// let dict = PyDict::new(py);
|
|
|
|
/// dict.set_item("aap", "noot").unwrap();
|
|
|
|
/// dict.set_item("mies", "wim").unwrap();
|
|
|
|
/// dict.set_item("zus", "jet").unwrap();
|
|
|
|
///
|
|
|
|
/// let bytes = marshal::dumps(py, dict, marshal::VERSION);
|
2021-03-20 07:44:28 +00:00
|
|
|
/// # });
|
2019-04-25 13:15:59 +00:00
|
|
|
/// ```
|
2019-04-25 13:05:59 +00:00
|
|
|
pub fn dumps<'a>(py: Python<'a>, object: &impl AsPyPointer, version: i32) -> PyResult<&'a PyBytes> {
|
2019-04-24 09:04:17 +00:00
|
|
|
unsafe {
|
2019-04-25 13:05:59 +00:00
|
|
|
let bytes = ffi::PyMarshal_WriteObjectToString(object.as_ptr(), version as c_int);
|
|
|
|
FromPyPointer::from_owned_ptr_or_err(py, bytes)
|
2019-04-24 09:04:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deserialize an object from bytes using the Python built-in marshal module.
|
2019-04-25 13:05:59 +00:00
|
|
|
pub fn loads<'a, B>(py: Python<'a>, data: &B) -> PyResult<&'a PyAny>
|
|
|
|
where
|
|
|
|
B: AsRef<[u8]> + ?Sized,
|
|
|
|
{
|
2019-04-24 09:04:17 +00:00
|
|
|
let data = data.as_ref();
|
2019-04-25 13:05:59 +00:00
|
|
|
unsafe {
|
|
|
|
let c_str = data.as_ptr() as *const c_char;
|
|
|
|
let object = ffi::PyMarshal_ReadObjectFromString(c_str, data.len() as isize);
|
|
|
|
FromPyPointer::from_owned_ptr_or_err(py, object)
|
2019-04-24 09:04:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2021-07-26 17:19:53 +00:00
|
|
|
mod tests {
|
2019-04-24 09:04:17 +00:00
|
|
|
use super::*;
|
2019-04-25 13:05:59 +00:00
|
|
|
use crate::types::PyDict;
|
2019-04-24 09:04:17 +00:00
|
|
|
|
|
|
|
#[test]
|
2021-08-12 00:47:41 +00:00
|
|
|
fn marshal_roundtrip() {
|
|
|
|
Python::with_gil(|py| {
|
|
|
|
let dict = PyDict::new(py);
|
|
|
|
dict.set_item("aap", "noot").unwrap();
|
|
|
|
dict.set_item("mies", "wim").unwrap();
|
|
|
|
dict.set_item("zus", "jet").unwrap();
|
2019-04-24 09:04:17 +00:00
|
|
|
|
2021-08-12 00:47:41 +00:00
|
|
|
let bytes = dumps(py, dict, VERSION)
|
|
|
|
.expect("marshalling failed")
|
|
|
|
.as_bytes();
|
|
|
|
let deserialized = loads(py, bytes).expect("unmarshalling failed");
|
2019-04-24 09:04:17 +00:00
|
|
|
|
2021-08-12 00:47:41 +00:00
|
|
|
assert!(equal(py, dict, deserialized));
|
|
|
|
});
|
2019-04-24 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 13:05:59 +00:00
|
|
|
fn equal(_py: Python, a: &impl AsPyPointer, b: &impl AsPyPointer) -> bool {
|
2019-04-24 09:04:17 +00:00
|
|
|
unsafe { ffi::PyObject_RichCompareBool(a.as_ptr(), b.as_ptr(), ffi::Py_EQ) != 0 }
|
|
|
|
}
|
|
|
|
}
|