Skip to content

Commit

Permalink
Merge pull request #822 from kngwyu/fix-tp-doc
Browse files Browse the repository at this point in the history
Fix the case where T::DESCRIPTION is not null-terminated
  • Loading branch information
kngwyu authored Mar 19, 2020
2 parents 6d16d0b + 9f4ea2b commit 5a822bc
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,20 @@ pub(crate) fn initialize_type_object<T>(
where
T: PyClass,
{
// PyPy will segfault if passed only a nul terminator as `tp_doc`.
// ptr::null() is OK though.
if T::DESCRIPTION == "\0" {
type_object.tp_doc = ptr::null();
} else {
type_object.tp_doc = T::DESCRIPTION.as_ptr() as *const _;
type_object.tp_doc = match T::DESCRIPTION {
// PyPy will segfault if passed only a nul terminator as `tp_doc`, ptr::null() is OK though.
"\0" => ptr::null(),
s if s.as_bytes().ends_with(b"\0") => s.as_ptr() as _,
// If the description is not null-terminated, create CString and leak it
s => CString::new(s)?.into_raw(),
};

type_object.tp_base = <T::BaseType as PyTypeInfo>::type_object() as *const _ as _;

let name = match module_name {
Some(module_name) => format!("{}.{}", module_name, T::NAME),
None => T::NAME.to_string(),
type_object.tp_name = match module_name {
Some(module_name) => CString::new(format!("{}.{}", module_name, T::NAME))?.into_raw(),
None => CString::new(T::NAME)?.into_raw(),
};
let name = CString::new(name).expect("Module name/type name must not contain NUL byte");
type_object.tp_name = name.into_raw();

// dealloc
unsafe extern "C" fn tp_dealloc_callback<T>(obj: *mut ffi::PyObject)
Expand Down

0 comments on commit 5a822bc

Please sign in to comment.