From 38c6d942b7c1bd8b01a66bbcd8f9757ce0b6180b Mon Sep 17 00:00:00 2001 From: ijl Date: Fri, 28 Sep 2018 22:01:04 +0000 Subject: [PATCH] Document UTF-8 FFI assumptions --- src/types/string.rs | 2 ++ src/types/string2.rs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/types/string.rs b/src/types/string.rs index b5d4d7d7..da7b75de 100644 --- a/src/types/string.rs +++ b/src/types/string.rs @@ -56,6 +56,8 @@ impl PyString { unsafe { let mut size: ffi::Py_ssize_t = mem::uninitialized(); let data = ffi::PyUnicode_AsUTF8AndSize(self.0.as_ptr(), &mut size) as *const u8; + // PyUnicode_AsUTF8AndSize would return null if the pointer did not reference a valid + // unicode object, but because we have a valid PyString, assume success debug_assert!(!data.is_null()); std::slice::from_raw_parts(data, size as usize) } diff --git a/src/types/string2.rs b/src/types/string2.rs index 2a95f3f2..88a7f4c1 100644 --- a/src/types/string2.rs +++ b/src/types/string2.rs @@ -154,6 +154,8 @@ impl PyUnicode { #[inline] pub fn as_bytes(&self) -> &[u8] { unsafe { + // PyUnicode_AsUTF8String would return null if the pointer did not reference a valid + // unicode object, but because we have a valid PyUnicode, assume success let data: Py = Py::from_owned_ptr( ffi::PyUnicode_AsUTF8String(self.0.as_ptr()), );