cpython-ext: serialize Rust str into Python str type

Summary:
Previously Rust str was serialized into bytes. To be Python 3 friendly, let's
serialize it into `str`.

Reviewed By: markbt

Differential Revision: D19797706

fbshipit-source-id: 388eb044dc7e25cdc438f0c3d6fa5a5740f22e3d
This commit is contained in:
Jun Wu 2020-03-12 12:13:53 -07:00 committed by Facebook GitHub Bot
parent 3376363721
commit ad8190713b

View File

@ -160,7 +160,11 @@ impl<'a, 'b> ser::Serializer for &'a Serializer<'b> {
}
fn serialize_str(self, v: &str) -> Result<PyObject> {
self.serialize_bytes(v.as_bytes())
if cfg!(feature = "python2") {
Ok(PyBytes::new(self.py, v.as_bytes()).into_object())
} else {
Ok(PyUnicode::new(self.py, v).into_object())
}
}
fn serialize_bytes(self, v: &[u8]) -> Result<PyObject> {