Merge pull request #460 from dronesforwork-forks/marshal

Add PyMarshal_* functions to ffi interface.
This commit is contained in:
konstin 2019-04-28 10:46:51 +02:00 committed by GitHub
commit b5bf17f47a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 91 additions and 0 deletions

View file

@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* PyPy support by omerbenamram in [#393](https://github.com/PyO3/pyo3/pull/393)
* Have `PyModule` generate an index of its members (`__all__` list).
* Allow `slf: PyRef<T>` for pyclass(#419)
* Add `marshal` module. [#460](https://github.com/PyO3/pyo3/pull/460)
### Changed

11
src/ffi/marshal.rs Normal file
View file

@ -0,0 +1,11 @@
use super::PyObject;
use std::os::raw::{c_char, c_int};
#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
#[cfg_attr(PyPy, link_name = "PyMarshal_WriteObjectToString")]
pub fn PyMarshal_WriteObjectToString(object: *mut PyObject, version: c_int) -> *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyMarshal_ReadObjectFromString")]
pub fn PyMarshal_ReadObjectFromString(data: *const c_char, len: isize) -> *mut PyObject;
}

View file

@ -3,5 +3,7 @@
pub use crate::ffi3::*;
pub use self::datetime::*;
pub use self::marshal::*;
pub(crate) mod datetime;
pub(crate) mod marshal;

View file

@ -176,6 +176,7 @@ pub mod exceptions;
pub mod freelist;
mod gil;
mod instance;
pub mod marshal;
mod object;
mod objectprotocol;
pub mod prelude;

76
src/marshal.rs Normal file
View file

@ -0,0 +1,76 @@
use crate::ffi;
use crate::types::{PyAny, PyBytes};
use crate::{AsPyPointer, FromPyPointer, PyResult, Python};
use std::os::raw::{c_char, c_int};
/// 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.
///
/// The built-in marshalling only supports a limited range of object.
/// The exact types supported depend on the version argument.
/// The [`VERSION`] constant holds the highest version currently supported.
///
/// See the [python documentation](https://docs.python.org/3/library/marshal.html) for more details.
///
/// # Example:
/// ```
/// # use pyo3::{marshal, types::PyDict};
/// # let gil = pyo3::Python::acquire_gil();
/// # let py = gil.python();
/// #
/// 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);
/// ```
pub fn dumps<'a>(py: Python<'a>, object: &impl AsPyPointer, version: i32) -> PyResult<&'a PyBytes> {
unsafe {
let bytes = ffi::PyMarshal_WriteObjectToString(object.as_ptr(), version as c_int);
FromPyPointer::from_owned_ptr_or_err(py, bytes)
}
}
/// Deserialize an object from bytes using the Python built-in marshal module.
pub fn loads<'a, B>(py: Python<'a>, data: &B) -> PyResult<&'a PyAny>
where
B: AsRef<[u8]> + ?Sized,
{
let data = data.as_ref();
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)
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::types::PyDict;
#[test]
fn marhshal_roundtrip() {
let gil = Python::acquire_gil();
let py = gil.python();
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 = dumps(py, dict, VERSION)
.expect("marshalling failed")
.as_bytes();
let deserialzed = loads(py, bytes).expect("unmarshalling failed");
assert!(equal(py, dict, deserialzed));
}
fn equal(_py: Python, a: &impl AsPyPointer, b: &impl AsPyPointer) -> bool {
unsafe { ffi::PyObject_RichCompareBool(a.as_ptr(), b.as_ptr(), ffi::Py_EQ) != 0 }
}
}