Skip to content

Commit

Permalink
Fix #56: PyModuleDef changed "m_slots" to "m_reload" in Python 3.5.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrunwald committed Dec 10, 2016
1 parent 2c89f62 commit 278c1ae
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
49 changes: 49 additions & 0 deletions python3-sys/src/moduleobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ extern "C" {
pub fn PyModule_GetFilenameObject(arg1: *mut PyObject) -> *mut PyObject;
pub fn PyModule_GetDef(arg1: *mut PyObject) -> *mut PyModuleDef;
pub fn PyModule_GetState(arg1: *mut PyObject) -> *mut c_void;

#[cfg(Py_3_5)]
pub fn PyModuleDef_Init(arg1: *mut PyModuleDef) -> *mut PyObject;
#[cfg(Py_3_5)]
pub static mut PyModuleDef_Type: PyTypeObject;
}

#[repr(C)]
Expand All @@ -48,6 +53,23 @@ pub const PyModuleDef_HEAD_INIT: PyModuleDef_Base = PyModuleDef_Base {
m_copy: 0 as *mut PyObject
};

#[repr(C)]
#[derive(Copy)]
#[cfg(Py_3_5)]
pub struct PyModuleDef_Slot {
pub slot: c_int,
pub value: *mut c_void,
}
#[cfg(Py_3_5)]
impl Clone for PyModuleDef_Slot {
fn clone(&self) -> PyModuleDef_Slot { *self }
}

#[cfg(Py_3_5)]
pub const Py_mod_create : c_int = 1;
#[cfg(Py_3_5)]
pub const Py_mod_exec : c_int = 2;

#[repr(C)]
#[derive(Copy)]
pub struct PyModuleDef {
Expand All @@ -56,7 +78,10 @@ pub struct PyModuleDef {
pub m_doc: *const c_char,
pub m_size: Py_ssize_t,
pub m_methods: *mut PyMethodDef,
#[cfg(not(Py_3_5))]
pub m_reload: Option<inquiry>,
#[cfg(Py_3_5)]
pub m_slots: *mut PyModuleDef_Slot,
pub m_traverse: Option<traverseproc>,
pub m_clear: Option<inquiry>,
pub m_free: Option<freefunc>,
Expand All @@ -65,4 +90,28 @@ impl Clone for PyModuleDef {
fn clone(&self) -> PyModuleDef { *self }
}

#[cfg(not(Py_3_5))]
pub const PyModuleDef_INIT: PyModuleDef = PyModuleDef {
m_base: PyModuleDef_HEAD_INIT,
m_name: 0 as *const _,
m_doc: 0 as *const _,
m_size: 0,
m_methods: 0 as *mut _,
m_reload: None,
m_traverse: None,
m_clear: None,
m_free: None
};

#[cfg(Py_3_5)]
pub const PyModuleDef_INIT: PyModuleDef = PyModuleDef {
m_base: PyModuleDef_HEAD_INIT,
m_name: 0 as *const _,
m_doc: 0 as *const _,
m_size: 0,
m_methods: 0 as *mut _,
m_slots: 0 as *mut _,
m_traverse: None,
m_clear: None,
m_free: None
};
1 change: 1 addition & 0 deletions python3-sys/src/pystate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use libc::{c_int, c_long};
use object::PyObject;
use moduleobject::PyModuleDef;

#[cfg(Py_3_6)]
pub const MAX_CO_EXTRA_USERS: c_int = 255;

pub enum PyInterpreterState { }
Expand Down
13 changes: 1 addition & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,7 @@ macro_rules! py_module_initializer {
fn init($py_id: $crate::Python, $m_id: &$crate::PyModule) -> $crate::PyResult<()> {
$body
}
static mut MODULE_DEF: $crate::_detail::ffi::PyModuleDef = $crate::_detail::ffi::PyModuleDef {
m_base: $crate::_detail::ffi::PyModuleDef_HEAD_INIT,
m_name: 0 as *const _,
m_doc: 0 as *const _,
m_size: 0, // we don't use per-module state
m_methods: 0 as *mut _,
m_reload: None,
m_traverse: None,
m_clear: None,
m_free: None
};
static mut MODULE_DEF: $crate::_detail::ffi::PyModuleDef = $crate::_detail::ffi::PyModuleDef_INIT;
// We can't convert &'static str to *const c_char within a static initializer,
// so we'll do it here in the module initialization:
MODULE_DEF.m_name = concat!(stringify!($name), "\0").as_ptr() as *const _;
Expand All @@ -324,7 +314,6 @@ macro_rules! py_module_initializer {
}
}


#[doc(hidden)]
#[cfg(feature="python3-sys")]
pub unsafe fn py_module_initializer_impl(
Expand Down

0 comments on commit 278c1ae

Please sign in to comment.