Skip to content

Commit

Permalink
Merge pull request #1387 from nw0/ffi-4
Browse files Browse the repository at this point in the history
ffi module cleanup: listobject.h to memoryobject.h
  • Loading branch information
davidhewitt committed Jan 15, 2021
2 parents bf701fe + d023338 commit b6f595b
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 51 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed
- `prepare_freethreaded_python` will no longer register an `atexit` handler to call `Py_Finalize`. [#1355](https://github.com/PyO3/pyo3/pull/1355)
- Mark FFI definitions `PyMarshal_WriteObjectToString`, `PyMarshal_ReadObjectFromString` as available in limited API.
- Mark FFI definitions `PyListObject` and those from `funcobject.h` as requiring non-limited API. [#1387](https://github.com/PyO3/pyo3/pull/1387)
- Fix typo in FFI definition `PyFunction_Code` to `PyFunction_GetCode`. [#1387](https://github.com/PyO3/pyo3/pull/1387)


### Fixed
- Fix support for using `r#raw_idents` as argument names in pyfunctions. [#1383](https://github.com/PyO3/pyo3/pull/1383)
Expand Down
31 changes: 31 additions & 0 deletions src/ffi/cpython/listobject.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::ffi::object::*;
use crate::ffi::pyport::Py_ssize_t;

#[repr(C)]
#[derive(Copy, Clone)]
pub struct PyListObject {
pub ob_base: PyVarObject,
pub ob_item: *mut *mut PyObject,
pub allocated: Py_ssize_t,
}

// skipped _PyList_Extend
// skipped _PyList_DebugMallocStats
// skipped _PyList_CAST (used inline below)

/// Macro, trading safety for speed
#[inline]
pub unsafe fn PyList_GET_ITEM(op: *mut PyObject, i: Py_ssize_t) -> *mut PyObject {
*(*(op as *mut PyListObject)).ob_item.offset(i as isize)
}

/// Macro, *only* to be used to fill in brand new lists
#[inline]
pub unsafe fn PyList_SET_ITEM(op: *mut PyObject, i: Py_ssize_t, v: *mut PyObject) {
*(*(op as *mut PyListObject)).ob_item.offset(i as isize) = v;
}

#[inline]
pub unsafe fn PyList_GET_SIZE(op: *mut PyObject) -> Py_ssize_t {
Py_SIZE(op)
}
4 changes: 4 additions & 0 deletions src/ffi/cpython/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pub mod dictobject;
// skipped fileobject.h
pub mod frameobject;
// skipped import.h
// skipped initconfig.h
// skipped interpreteridobject.h
pub mod listobject;

pub use self::abstract_::*;
#[cfg(not(PyPy))]
Expand All @@ -18,3 +21,4 @@ pub use self::code::*;
#[cfg(not(PyPy))]
pub use self::dictobject::*;
pub use self::frameobject::*;
pub use self::listobject::*;
22 changes: 18 additions & 4 deletions src/ffi/funcobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ use std::os::raw::c_int;

use crate::ffi::object::{PyObject, PyTypeObject, Py_TYPE};

// skipped PyFunctionObject

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
#[cfg(not(Py_LIMITED_API))]
#[cfg_attr(PyPy, link_name = "PyPyFunction_Type")]
pub static mut PyFunction_Type: PyTypeObject;
}

#[cfg(not(Py_LIMITED_API))]
#[inline]
pub unsafe fn PyFunction_Check(op: *mut PyObject) -> c_int {
(Py_TYPE(op) == &mut PyFunction_Type) as c_int
}

extern "C" {
pub fn PyFunction_New(code: *mut PyObject, globals: *mut PyObject) -> *mut PyObject;
pub fn PyFunction_NewWithQualName(
code: *mut PyObject,
globals: *mut PyObject,
qualname: *mut PyObject,
) -> *mut PyObject;
pub fn PyFunction_New(code: *mut PyObject, globals: *mut PyObject) -> *mut PyObject;
pub fn PyFunction_Code(op: *mut PyObject) -> *mut PyObject;
pub fn PyFunction_GetCode(op: *mut PyObject) -> *mut PyObject;
pub fn PyFunction_GetGlobals(op: *mut PyObject) -> *mut PyObject;
pub fn PyFunction_GetModule(op: *mut PyObject) -> *mut PyObject;
pub fn PyFunction_GetDefaults(op: *mut PyObject) -> *mut PyObject;
Expand All @@ -34,3 +34,17 @@ extern "C" {
pub fn PyFunction_GetAnnotations(op: *mut PyObject) -> *mut PyObject;
pub fn PyFunction_SetAnnotations(op: *mut PyObject, annotations: *mut PyObject) -> c_int;
}

// skipped _PyFunction_Vectorcall
// skipped PyFunction_GET_CODE
// skipped PyFunction_GET_GLOBALS
// skipped PyFunction_GET_MODULE
// skipped PyFunction_GET_DEFAULTS
// skipped PyFunction_GET_KW_DEFAULTS
// skipped PyFunction_GET_CLOSURE
// skipped PyFunction_GET_ANNOTATIONS

// skipped PyClassMethod_Type
// skipped PyStaticMethod_Type
// skipped PyClassMethod_New
// skipped PyStaticMethod_New
28 changes: 0 additions & 28 deletions src/ffi/listobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ use crate::ffi::object::*;
use crate::ffi::pyport::Py_ssize_t;
use std::os::raw::c_int;

#[repr(C)]
#[derive(Copy, Clone)]
pub struct PyListObject {
pub ob_base: PyVarObject,
pub ob_item: *mut *mut PyObject,
pub allocated: Py_ssize_t,
}

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
#[cfg_attr(PyPy, link_name = "PyPyList_Type")]
Expand All @@ -28,26 +20,6 @@ pub unsafe fn PyList_CheckExact(op: *mut PyObject) -> c_int {
(Py_TYPE(op) == &mut PyList_Type) as c_int
}

/// Macro, trading safety for speed
#[cfg(not(Py_LIMITED_API))]
#[inline]
pub unsafe fn PyList_GET_ITEM(op: *mut PyObject, i: Py_ssize_t) -> *mut PyObject {
*(*(op as *mut PyListObject)).ob_item.offset(i as isize)
}

#[cfg(not(Py_LIMITED_API))]
#[inline]
pub unsafe fn PyList_GET_SIZE(op: *mut PyObject) -> Py_ssize_t {
Py_SIZE(op)
}

/// Macro, *only* to be used to fill in brand new lists
#[cfg(not(Py_LIMITED_API))]
#[inline]
pub unsafe fn PyList_SET_ITEM(op: *mut PyObject, i: Py_ssize_t, v: *mut PyObject) {
*(*(op as *mut PyListObject)).ob_item.offset(i as isize) = v;
}

extern "C" {
#[cfg_attr(PyPy, link_name = "PyPyList_New")]
pub fn PyList_New(size: Py_ssize_t) -> *mut PyObject;
Expand Down
45 changes: 38 additions & 7 deletions src/ffi/longobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ use std::os::raw::{
c_char, c_double, c_int, c_long, c_longlong, c_uchar, c_ulong, c_ulonglong, c_void,
};

/// This is an opaque type in the python c api
#[repr(C)]
pub struct PyLongObject {
_unused: [u8; 0],
}
opaque_struct!(PyLongObject);

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
Expand Down Expand Up @@ -50,7 +46,24 @@ extern "C" {
pub fn PyLong_AsUnsignedLong(arg1: *mut PyObject) -> c_ulong;
#[cfg_attr(PyPy, link_name = "PyPyLong_AsUnsignedLongMask")]
pub fn PyLong_AsUnsignedLongMask(arg1: *mut PyObject) -> c_ulong;
// skipped non-limited _PyLong_AsInt
pub fn PyLong_GetInfo() -> *mut PyObject;
// skipped PyLong_AS_LONG

// skipped PyLong_FromPid
// skipped PyLong_AsPid
// skipped _Py_PARSE_INTPTR
// skipped _Py_PARSE_UINTPTR

// skipped non-limited _PyLong_UnsignedShort_Converter
// skipped non-limited _PyLong_UnsignedInt_Converter
// skipped non-limited _PyLong_UnsignedLong_Converter
// skipped non-limited _PyLong_UnsignedLongLong_Converter
// skipped non-limited _PyLong_Size_t_Converter

// skipped non-limited _PyLong_DigitValue
// skipped non-limited _PyLong_Frexp

#[cfg_attr(PyPy, link_name = "PyPyLong_AsDouble")]
pub fn PyLong_AsDouble(arg1: *mut PyObject) -> c_double;
#[cfg_attr(PyPy, link_name = "PyPyLong_FromVoidPtr")]
Expand All @@ -75,15 +88,19 @@ extern "C" {
arg2: *mut *mut c_char,
arg3: c_int,
) -> *mut PyObject;
pub fn PyOS_strtoul(arg1: *const c_char, arg2: *mut *mut c_char, arg3: c_int) -> c_ulong;
pub fn PyOS_strtol(arg1: *const c_char, arg2: *mut *mut c_char, arg3: c_int) -> c_long;
}
// skipped non-limited PyLong_FromUnicodeObject
// skipped non-limited _PyLong_FromBytes

#[cfg(not(Py_LIMITED_API))]
extern "C" {
// skipped non-limited _PyLong_Sign

#[cfg(not(PyPy))]
pub fn _PyLong_NumBits(obj: *mut PyObject) -> c_int;

// skipped _PyLong_DivmodNear

#[cfg_attr(PyPy, link_name = "_PyPyLong_FromByteArray")]
pub fn _PyLong_FromByteArray(
bytes: *const c_uchar,
Expand All @@ -101,3 +118,17 @@ extern "C" {
is_signed: c_int,
) -> c_int;
}

// skipped non-limited _PyLong_Format
// skipped non-limited _PyLong_FormatWriter
// skipped non-limited _PyLong_FormatBytesWriter
// skipped non-limited _PyLong_FormatAdvancedWriter

extern "C" {
pub fn PyOS_strtoul(arg1: *const c_char, arg2: *mut *mut c_char, arg3: c_int) -> c_ulong;
pub fn PyOS_strtol(arg1: *const c_char, arg2: *mut *mut c_char, arg3: c_int) -> c_long;
}

// skipped non-limited _PyLong_GCD
// skipped non-limited _PyLong_Rshift
// skipped non-limited _PyLong_Lshift
14 changes: 11 additions & 3 deletions src/ffi/marshal.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use super::PyObject;
use super::{PyObject, Py_ssize_t};
use std::os::raw::{c_char, c_int};

#[cfg(not(Py_LIMITED_API))]
// skipped Py_MARSHAL_VERSION
// skipped PyMarshal_WriteLongToFile
// skipped PyMarshal_WriteObjectToFile

extern "C" {
#[cfg_attr(PyPy, link_name = "PyPyMarshal_WriteObjectToString")]
pub fn PyMarshal_WriteObjectToString(object: *mut PyObject, version: c_int) -> *mut PyObject;

// skipped non-limited PyMarshal_ReadLongFromFile
// skipped non-limited PyMarshal_ReadShortFromFile
// skipped non-limited PyMarshal_ReadObjectFromFile
// skipped non-limited PyMarshal_ReadLastObjectFromFile

#[cfg_attr(PyPy, link_name = "PyPyMarshal_ReadObjectFromString")]
pub fn PyMarshal_ReadObjectFromString(data: *const c_char, len: isize) -> *mut PyObject;
pub fn PyMarshal_ReadObjectFromString(data: *const c_char, len: Py_ssize_t) -> *mut PyObject;
}
11 changes: 11 additions & 0 deletions src/ffi/memoryobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::ffi::object::*;
use crate::ffi::pyport::Py_ssize_t;
use std::os::raw::{c_char, c_int};

// skipped non-limited _PyManagedBuffer_Type

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
#[cfg_attr(PyPy, link_name = "PyPyMemoryView_Type")]
Expand All @@ -13,6 +15,9 @@ pub unsafe fn PyMemoryView_Check(op: *mut PyObject) -> c_int {
(Py_TYPE(op) == &mut PyMemoryView_Type) as c_int
}

// skipped non-limited PyMemoryView_GET_BUFFER
// skipped non-limited PyMemeryView_GET_BASE

extern "C" {
#[cfg_attr(PyPy, link_name = "PyPyMemoryView_FromObject")]
pub fn PyMemoryView_FromObject(base: *mut PyObject) -> *mut PyObject;
Expand All @@ -22,9 +27,15 @@ extern "C" {
size: Py_ssize_t,
flags: c_int,
) -> *mut PyObject;
// skipped non-limited PyMemoryView_FromBuffer
pub fn PyMemoryView_GetContiguous(
base: *mut PyObject,
buffertype: c_int,
order: c_char,
) -> *mut PyObject;
}

// skipped remainder of file with comment:
/* The structs are declared here so that macros can work, but they shouldn't
be considered public. Don't access their fields directly, use the macros
and functions instead! */
16 changes: 7 additions & 9 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub use self::enumobject::*;
pub use self::eval::*;
pub use self::fileobject::*;
pub use self::floatobject::*;
#[cfg(not(Py_LIMITED_API))]
pub use self::funcobject::*;
#[cfg(not(Py_LIMITED_API))]
pub use self::genobject::*;
Expand Down Expand Up @@ -111,6 +112,8 @@ mod fileobject; // TODO: incomplete
mod floatobject; // TODO supports PEP-384 only

// skipped empty frameobject.h
#[cfg(not(Py_LIMITED_API))]
pub(crate) mod funcobject;
// skipped genericaliasobject.h
#[cfg(not(Py_LIMITED_API))]
mod genobject; // TODO: incomplete
Expand All @@ -119,9 +122,11 @@ mod import; // TODO: incomplete
// skipped interpreteridobject.h
mod intrcheck; // TODO supports PEP-384 only
mod iterobject;
mod listobject; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5

mod listobject;
// skipped longintrepr.h
mod longobject;
pub(crate) mod marshal;
mod memoryobject;
// skipped namespaceobject.h
// skipped odictobject.h
// skipped opcode.h
Expand Down Expand Up @@ -164,17 +169,14 @@ mod pyhash;
mod pymem;
mod typeslots;

mod longobject;
mod unicodeobject; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5
// mod longintrepr; TODO excluded by PEP-384
mod memoryobject; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5
mod rangeobject; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5
mod tupleobject; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5
// mod odictobject; TODO new in 3.5
mod methodobject; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5
mod moduleobject;
mod setobject; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5
// mod funcobject; TODO excluded by PEP-384
// mod classobject; TODO excluded by PEP-384
mod pycapsule; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5
mod sliceobject;
Expand Down Expand Up @@ -209,9 +211,5 @@ mod pystrtod; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and
// Additional headers that are not exported by Python.h
pub mod structmember; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5

pub(crate) mod marshal;

pub(crate) mod funcobject;

#[cfg(not(Py_LIMITED_API))]
mod cpython;

0 comments on commit b6f595b

Please sign in to comment.