Skip to content

Commit

Permalink
Merge pull request #784 from kngwyu/py38-ffi
Browse files Browse the repository at this point in the history
Make FFIs Python3.8 compatible
  • Loading branch information
kngwyu committed Mar 3, 2020
2 parents fee755a + 4b9cb70 commit ebb528d
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 69 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* Use `parking_lot::Mutex` instead of `spin::Mutex`. [#734](https://github.com/PyO3/pyo3/pull/734)
* Bumped minimum Rust version to `1.42.0-nightly 2020-01-21`. [#761](https://github.com/PyO3/pyo3/pull/761)
* `PyRef` and `PyRefMut` are renewed for `PyCell`. [#770](https://github.com/PyO3/pyo3/pull/770)
* Some new FFI functions for Python 3.8. [#784](https://github.com/PyO3/pyo3/pull/784)

### Added
* `PyCell`, which has RefCell-like features. [#770](https://github.com/PyO3/pyo3/pull/770)
Expand Down
1 change: 1 addition & 0 deletions src/ffi/ceval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,6 @@ extern "C" {
pub fn PyEval_AcquireThread(tstate: *mut PyThreadState) -> ();
#[cfg_attr(PyPy, link_name = "PyPyEval_ReleaseThread")]
pub fn PyEval_ReleaseThread(tstate: *mut PyThreadState) -> ();
#[cfg(not(Py_3_8))]
pub fn PyEval_ReInitThreads() -> ();
}
60 changes: 45 additions & 15 deletions src/ffi/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use crate::ffi::object::*;
use crate::ffi::pyport::Py_ssize_t;
use std::os::raw::{c_char, c_int, c_uchar, c_void};

#[cfg(Py_3_8)]
pub enum _PyOpcache {}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct PyCodeObject {
Expand Down Expand Up @@ -29,6 +32,14 @@ pub struct PyCodeObject {
pub co_weakreflist: *mut PyObject,
#[cfg(Py_3_6)]
pub co_extra: *mut c_void,
#[cfg(Py_3_8)]
pub co_opcache_map: *mut c_uchar,
#[cfg(Py_3_8)]
pub co_opcache: *mut _PyOpcache,
#[cfg(Py_3_8)]
pub co_opcache_flag: c_int,
#[cfg(Py_3_8)]
pub co_opcache_size: c_uchar,
}

/* Masks for co_flags */
Expand Down Expand Up @@ -77,21 +88,40 @@ extern "C" {

#[cfg_attr(PyPy, link_name = "PyPyCode_New")]
pub fn PyCode_New(
arg1: c_int,
arg2: c_int,
arg3: c_int,
arg4: c_int,
arg5: c_int,
arg6: *mut PyObject,
arg7: *mut PyObject,
arg8: *mut PyObject,
arg9: *mut PyObject,
arg10: *mut PyObject,
arg11: *mut PyObject,
arg12: *mut PyObject,
arg13: *mut PyObject,
arg14: c_int,
arg15: *mut PyObject,
argcount: c_int,
kwonlyargcount: c_int,
nlocals: c_int,
stacksize: c_int,
flags: c_int,
code: *mut PyObject,
consts: *mut PyObject,
names: *mut PyObject,
varnames: *mut PyObject,
freevars: *mut PyObject,
cellvars: *mut PyObject,
filename: *mut PyObject,
name: *mut PyObject,
firstlineno: c_int,
lnotab: *mut PyObject,
) -> *mut PyCodeObject;
#[cfg(Py_3_8)]
pub fn PyCode_NewWithPosOnlyArgs(
argcount: c_int,
posonlyargcount: c_int,
kwonlyargcount: c_int,
nlocals: c_int,
stacksize: c_int,
flags: c_int,
code: *mut PyObject,
consts: *mut PyObject,
names: *mut PyObject,
varnames: *mut PyObject,
freevars: *mut PyObject,
cellvars: *mut PyObject,
filename: *mut PyObject,
name: *mut PyObject,
firstlineno: c_int,
lnotab: *mut PyObject,
) -> *mut PyCodeObject;
#[cfg_attr(PyPy, link_name = "PyPyCode_NewEmpty")]
pub fn PyCode_NewEmpty(
Expand Down
5 changes: 5 additions & 0 deletions src/ffi/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ extern "C" {
) -> *mut PyFutureFeatures;

pub fn PyCompile_OpcodeStackEffect(opcode: c_int, oparg: c_int) -> c_int;

#[cfg(Py_3_8)]
pub fn PyCompile_OpcodeStackEffectWithJump(opcode: c_int, oparg: c_int, jump: c_int) -> c_int;
}

pub const Py_single_input: c_int = 256;
pub const Py_file_input: c_int = 257;
pub const Py_eval_input: c_int = 258;
#[cfg(Py_3_8)]
pub const Py_func_type_input: c_int = 345;
10 changes: 8 additions & 2 deletions src/ffi/dictobject.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::ffi::object::*;
use crate::ffi::pyport::{Py_hash_t, Py_ssize_t};
use crate::ffi::pyport::Py_ssize_t;
use std::os::raw::{c_char, c_int};

#[cfg_attr(windows, link(name = "pythonXY"))]
Expand All @@ -11,6 +11,12 @@ extern "C" {
pub static mut PyDictKeys_Type: PyTypeObject;
pub static mut PyDictItems_Type: PyTypeObject;
pub static mut PyDictValues_Type: PyTypeObject;
#[cfg(Py_3_8)]
pub static mut PyDictRevIterKey_Type: PyTypeObject;
#[cfg(Py_3_8)]
pub static mut PyDictRevIterValue_Type: PyTypeObject;
#[cfg(Py_3_8)]
pub static mut PyDictRevIterItem_Type: PyTypeObject;
}

#[repr(C)]
Expand Down Expand Up @@ -73,7 +79,7 @@ extern "C" {
mp: *mut PyObject,
key: *mut PyObject,
item: *mut PyObject,
hash: Py_hash_t,
hash: crate::ffi::pyport::Py_hash_t,
) -> c_int;
#[cfg_attr(PyPy, link_name = "PyPyDict_DelItem")]
pub fn PyDict_DelItem(mp: *mut PyObject, key: *mut PyObject) -> c_int;
Expand Down
102 changes: 50 additions & 52 deletions src/ffi/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ pub type newfunc = unsafe extern "C" fn(
) -> *mut PyObject;
pub type allocfunc =
unsafe extern "C" fn(arg1: *mut PyTypeObject, arg2: Py_ssize_t) -> *mut PyObject;
#[cfg(Py_3_8)]
pub type vectorcallfunc = unsafe extern "C" fn(
callable: *mut PyObject,
args: *const *mut PyObject,
nargsf: libc::size_t,
kwnames: *mut PyObject,
) -> *mut PyObject;

#[cfg(Py_LIMITED_API)]
mod typeobject {
Expand Down Expand Up @@ -492,23 +499,25 @@ mod typeobject {
pub tp_members: *mut ffi::structmember::PyMemberDef,
pub tp_getset: *mut ffi::descrobject::PyGetSetDef,
pub tp_base: *mut PyTypeObject,
pub tp_dict: *mut ffi::object::PyObject,
pub tp_descr_get: Option<ffi::object::descrgetfunc>,
pub tp_descr_set: Option<ffi::object::descrsetfunc>,
pub tp_dict: *mut object::PyObject,
pub tp_descr_get: Option<object::descrgetfunc>,
pub tp_descr_set: Option<object::descrsetfunc>,
pub tp_dictoffset: Py_ssize_t,
pub tp_init: Option<ffi::object::initproc>,
pub tp_alloc: Option<ffi::object::allocfunc>,
pub tp_new: Option<ffi::object::newfunc>,
pub tp_free: Option<ffi::object::freefunc>,
pub tp_is_gc: Option<ffi::object::inquiry>,
pub tp_bases: *mut ffi::object::PyObject,
pub tp_mro: *mut ffi::object::PyObject,
pub tp_cache: *mut ffi::object::PyObject,
pub tp_subclasses: *mut ffi::object::PyObject,
pub tp_weaklist: *mut ffi::object::PyObject,
pub tp_del: Option<ffi::object::destructor>,
pub tp_init: Option<object::initproc>,
pub tp_alloc: Option<object::allocfunc>,
pub tp_new: Option<object::newfunc>,
pub tp_free: Option<object::freefunc>,
pub tp_is_gc: Option<object::inquiry>,
pub tp_bases: *mut object::PyObject,
pub tp_mro: *mut object::PyObject,
pub tp_cache: *mut object::PyObject,
pub tp_subclasses: *mut object::PyObject,
pub tp_weaklist: *mut object::PyObject,
pub tp_del: Option<object::destructor>,
pub tp_version_tag: c_uint,
pub tp_finalize: Option<ffi::object::destructor>,
pub tp_finalize: Option<object::destructor>,
#[cfg(Py_3_8)]
pub tp_vectorcall: Option<object::vectorcallfunc>,
#[cfg(PyPy)]
pub tp_pypy_flags: ::std::os::raw::c_long,
#[cfg(py_sys_config = "COUNT_ALLOCS")]
Expand All @@ -524,7 +533,7 @@ mod typeobject {
}

macro_rules! _type_object_init {
({$($head:tt)*} $tp_as_async:ident, $($tail:tt)*) => {
({$($head:tt)*}, $($tail:tt)*) => {
as_expr! {
PyTypeObject {
$($head)*
Expand All @@ -538,7 +547,7 @@ mod typeobject {
tp_vectorcall_offset: 0,
tp_getattr: None,
tp_setattr: None,
$tp_as_async: ptr::null_mut(),
tp_as_async: ptr::null_mut(),
tp_repr: None,
tp_as_number: ptr::null_mut(),
tp_as_sequence: ptr::null_mut(),
Expand All @@ -549,7 +558,7 @@ mod typeobject {
tp_getattro: None,
tp_setattro: None,
tp_as_buffer: ptr::null_mut(),
tp_flags: ffi::object::Py_TPFLAGS_DEFAULT,
tp_flags: object::Py_TPFLAGS_DEFAULT,
tp_doc: ptr::null(),
tp_traverse: None,
tp_clear: None,
Expand Down Expand Up @@ -577,65 +586,54 @@ mod typeobject {
tp_weaklist: ptr::null_mut(),
tp_del: None,
tp_version_tag: 0,
tp_finalize: None,
#[cfg(Py_3_8)]
tp_vectorcall: None,
$($tail)*
}
}
}
}

#[cfg(PyPy)]
macro_rules! py_type_object_init {
($tp_as_async:ident, $($tail:tt)*) => {
macro_rules! type_object_init {
($($tail:tt)*) => {
_type_object_init!({
ob_refcnt: 1,
ob_pypy_link: 0,
ob_type: ptr::null_mut(),
ob_size: 0,
}
$tp_as_async,
},
tp_pypy_flags: 0,
$($tail)*
)
}
}

#[cfg(not(PyPy))]
macro_rules! py_type_object_init {
($tp_as_async:ident, $($tail:tt)*) => {
macro_rules! type_object_init {
($($tail:tt)*) => {
_type_object_init!({
ob_base: ffi::object::PyVarObject {
ob_base: ffi::object::PyObject_HEAD_INIT,
ob_base: object::PyVarObject {
ob_base: object::PyObject_HEAD_INIT,
ob_size: 0
},}
$tp_as_async,
},},
$($tail)*
)
}
}

#[cfg(py_sys_config = "COUNT_ALLOCS")]
macro_rules! py_type_object_init_with_count_allocs {
($tp_as_async:ident, $($tail:tt)*) => {
py_type_object_init!($tp_as_async,
$($tail)*
tp_allocs: 0,
tp_frees: 0,
tp_maxalloc: 0,
tp_prev: ptr::null_mut(),
tp_next: ptr::null_mut(),
)
}
}
pub const PyTypeObject_INIT: PyTypeObject = type_object_init! {
tp_allocs: 0,
tp_frees: 0,
tp_maxalloc: 0,
tp_prev: ptr::null_mut(),
tp_next: ptr::null_mut(),
};

#[cfg(not(py_sys_config = "COUNT_ALLOCS"))]
macro_rules! py_type_object_init_with_count_allocs {
($tp_as_async:ident, $($tail:tt)*) => {
py_type_object_init!($tp_as_async, $($tail)*)
}
}

pub const PyTypeObject_INIT: PyTypeObject =
py_type_object_init_with_count_allocs!(tp_as_async, tp_finalize: None,);
pub const PyTypeObject_INIT: PyTypeObject = type_object_init!();

#[repr(C)]
#[derive(Copy, Clone)]
Expand All @@ -646,9 +644,9 @@ mod typeobject {
pub as_mapping: PyMappingMethods,
pub as_sequence: PySequenceMethods,
pub as_buffer: PyBufferProcs,
pub ht_name: *mut ffi::object::PyObject,
pub ht_slots: *mut ffi::object::PyObject,
pub ht_qualname: *mut ffi::object::PyObject,
pub ht_name: *mut object::PyObject,
pub ht_slots: *mut object::PyObject,
pub ht_qualname: *mut object::PyObject,
pub ht_cached_keys: *mut c_void,
}

Expand All @@ -663,7 +661,7 @@ mod typeobject {
pub unsafe fn PyHeapType_GET_MEMBERS(
etype: *mut PyHeapTypeObject,
) -> *mut ffi::structmember::PyMemberDef {
let py_type = ffi::object::Py_TYPE(etype as *mut ffi::object::PyObject);
let py_type = object::Py_TYPE(etype as *mut object::PyObject);
let ptr = etype.offset((*py_type).tp_basicsize);
ptr as *mut ffi::structmember::PyMemberDef
}
Expand Down

0 comments on commit ebb528d

Please sign in to comment.