Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/5064.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The following underscore-prefixed fields of FFI definition `PyCodeObject` are now private: `_co_code`, `_co_linearray`, `_co_cached`, `_co_instrumentation_version`, `_co_monitoring` and `_co_firsttraceable`.
1 change: 1 addition & 0 deletions newsfragments/5064.removed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove private types from `pyo3-ffi` (i.e. starting with `_Py`) which are not referenced by public APIs: `_PyLocalMonitors`, `_Py_GlobalMonitors`, `_PyCoCached`, `_PyCoLineInstrumentationData`, `_PyCoMonitoringData`, `_PyCompilerSrcLocation`, `_PyErr_StackItem`.
2 changes: 1 addition & 1 deletion pyo3-ffi/src/bytearrayobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct PyByteArrayObject {
}

#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))]
opaque_struct!(PyByteArrayObject);
opaque_struct!(pub PyByteArrayObject);

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion pyo3-ffi/src/code.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This header doesn't exist in CPython, but Include/cpython/code.h does. We add
// this here so that PyCodeObject has a definition under the limited API.

opaque_struct!(PyCodeObject);
opaque_struct!(pub PyCodeObject);
2 changes: 1 addition & 1 deletion pyo3-ffi/src/cpython/bytesobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct PyBytesObject {
}

#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))]
opaque_struct!(PyBytesObject);
opaque_struct!(pub PyBytesObject);

extern "C" {
#[cfg_attr(PyPy, link_name = "_PyPyBytes_Resize")]
Expand Down
89 changes: 26 additions & 63 deletions pyo3-ffi/src/cpython/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,73 +9,32 @@ use std::ptr::addr_of_mut;
#[cfg(all(Py_3_8, not(any(PyPy, GraalPy)), not(Py_3_11)))]
opaque_struct!(_PyOpcache);

#[cfg(Py_3_12)]
pub const _PY_MONITORING_LOCAL_EVENTS: usize = 10;
#[cfg(Py_3_12)]
pub const _PY_MONITORING_UNGROUPED_EVENTS: usize = 15;
#[cfg(Py_3_12)]
pub const _PY_MONITORING_EVENTS: usize = 17;
// skipped private _PY_MONITORING_LOCAL_EVENTS
// skipped private _PY_MONITORING_UNGROUPED_EVENTS
// skipped private _PY_MONITORING_EVENTS

#[cfg(Py_3_12)]
#[repr(C)]
#[derive(Clone, Copy)]
pub struct _Py_LocalMonitors {
pub tools: [u8; if cfg!(Py_3_13) {
_PY_MONITORING_LOCAL_EVENTS
} else {
_PY_MONITORING_UNGROUPED_EVENTS
}],
}
// skipped private _PyLocalMonitors
// skipped private _Py_GlobalMonitors

#[cfg(Py_3_12)]
#[repr(C)]
#[derive(Clone, Copy)]
pub struct _Py_GlobalMonitors {
pub tools: [u8; _PY_MONITORING_UNGROUPED_EVENTS],
}
// skipped private _Py_CODEUNIT

// skipped _Py_CODEUNIT
// skipped private _Py_OPCODE
// skipped private _Py_OPARG

// skipped _Py_OPCODE
// skipped _Py_OPARG
// skipped private _py_make_codeunit

// skipped _py_make_codeunit
// skipped private _py_set_opcode

// skipped _py_set_opcode

// skipped _Py_MAKE_CODEUNIT
// skipped _Py_SET_OPCODE
// skipped private _Py_MAKE_CODEUNIT
// skipped private _Py_SET_OPCODE

#[cfg(Py_3_12)]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct _PyCoCached {
pub _co_code: *mut PyObject,
pub _co_varnames: *mut PyObject,
pub _co_cellvars: *mut PyObject,
pub _co_freevars: *mut PyObject,
}
opaque_struct!(_PyCoCached);

#[cfg(Py_3_12)]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct _PyCoLineInstrumentationData {
pub original_opcode: u8,
pub line_delta: i8,
}
// skipped private _PyCoLineInstrumentationData

#[cfg(Py_3_12)]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct _PyCoMonitoringData {
pub local_monitors: _Py_LocalMonitors,
pub active_monitors: _Py_LocalMonitors,
pub tools: *mut u8,
pub lines: *mut _PyCoLineInstrumentationData,
pub line_tools: *mut u8,
pub per_instruction_opcodes: *mut u8,
pub per_instruction_tools: *mut u8,
}
opaque_struct!(_PyCoMonitoringData);

#[cfg(all(not(any(PyPy, GraalPy)), not(Py_3_7)))]
opaque_struct!(PyCodeObject);
Expand Down Expand Up @@ -177,20 +136,24 @@ pub struct PyCodeObject {
pub co_linetable: *mut PyObject,
pub co_weakreflist: *mut PyObject,
#[cfg(not(Py_3_12))]
pub _co_code: *mut PyObject,
_co_code: *mut PyObject,
#[cfg(not(Py_3_12))]
pub _co_linearray: *mut c_char,
_co_linearray: *mut c_char,
#[cfg(Py_3_13)]
#[allow(
private_interfaces,
reason = "field is public, but the type is opaque and private"
)]
pub co_executors: *mut _PyExecutorArray,
#[cfg(Py_3_12)]
pub _co_cached: *mut _PyCoCached,
_co_cached: *mut _PyCoCached,
#[cfg(all(Py_3_12, not(Py_3_13)))]
pub _co_instrumentation_version: u64,
_co_instrumentation_version: u64,
#[cfg(Py_3_13)]
pub _co_instrumentation_version: libc::uintptr_t,
_co_instrumentation_version: libc::uintptr_t,
#[cfg(Py_3_12)]
pub _co_monitoring: *mut _PyCoMonitoringData,
pub _co_firsttraceable: c_int,
_co_monitoring: *mut _PyCoMonitoringData,
_co_firsttraceable: c_int,
pub co_extra: *mut c_void,
pub co_code_adaptive: [c_char; 1],
}
Expand Down
2 changes: 1 addition & 1 deletion pyo3-ffi/src/cpython/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct PyCompilerFlags {
#[cfg(all(Py_3_12, not(any(Py_3_13, PyPy, GraalPy))))]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct _PyCompilerSrcLocation {
struct _PyCompilerSrcLocation {
pub lineno: c_int,
pub end_lineno: c_int,
pub col_offset: c_int,
Expand Down
4 changes: 2 additions & 2 deletions pyo3-ffi/src/cpython/critical_section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ pub struct PyCriticalSection2 {
}

#[cfg(not(Py_GIL_DISABLED))]
opaque_struct!(PyCriticalSection);
opaque_struct!(pub PyCriticalSection);

#[cfg(not(Py_GIL_DISABLED))]
opaque_struct!(PyCriticalSection2);
opaque_struct!(pub PyCriticalSection2);

extern "C" {
pub fn PyCriticalSection_Begin(c: *mut PyCriticalSection, op: *mut PyObject);
Expand Down
4 changes: 2 additions & 2 deletions pyo3-ffi/src/cpython/dictobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use crate::object::*;
use crate::pyport::Py_ssize_t;
use std::os::raw::c_int;

opaque_struct!(PyDictKeysObject);
opaque_struct!(pub PyDictKeysObject);

#[cfg(Py_3_11)]
opaque_struct!(PyDictValues);
opaque_struct!(pub PyDictValues);

#[cfg(not(GraalPy))]
#[repr(C)]
Expand Down
2 changes: 1 addition & 1 deletion pyo3-ffi/src/cpython/frameobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub struct PyFrameObject {
}

#[cfg(any(PyPy, GraalPy, Py_3_11))]
opaque_struct!(PyFrameObject);
opaque_struct!(pub PyFrameObject);

// skipped _PyFrame_IsRunnable
// skipped _PyFrame_IsExecuting
Expand Down
5 changes: 2 additions & 3 deletions pyo3-ffi/src/cpython/genobject.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::object::*;
use crate::PyFrameObject;
#[cfg(not(any(PyPy, GraalPy)))]
use crate::_PyErr_StackItem;
#[cfg(all(Py_3_11, not(any(PyPy, GraalPy))))]
use std::os::raw::c_char;
use std::os::raw::c_int;
Expand All @@ -20,7 +18,8 @@ pub struct PyGenObject {
pub gi_weakreflist: *mut PyObject,
pub gi_name: *mut PyObject,
pub gi_qualname: *mut PyObject,
pub gi_exc_state: _PyErr_StackItem,
#[allow(private_interfaces, reason = "field is public but type is private")]
pub gi_exc_state: crate::cpython::pystate::_PyErr_StackItem,
#[cfg(Py_3_11)]
pub gi_origin_or_finalizer: *mut PyObject,
#[cfg(Py_3_11)]
Expand Down
3 changes: 2 additions & 1 deletion pyo3-ffi/src/cpython/pyframe.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// NB used in `_PyEval_EvalFrameDefault`, maybe we remove this too.
#[cfg(all(Py_3_11, not(PyPy)))]
opaque_struct!(_PyInterpreterFrame);
opaque_struct!(pub _PyInterpreterFrame);
11 changes: 6 additions & 5 deletions pyo3-ffi/src/cpython/pystate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ pub const PyTrace_OPCODE: c_int = 7;
// skipped PyTraceInfo
// skipped CFrame

/// Private structure used inline in `PyGenObject`
#[cfg(not(PyPy))]
#[repr(C)]
#[derive(Clone, Copy)]
pub struct _PyErr_StackItem {
pub(crate) struct _PyErr_StackItem {
#[cfg(not(Py_3_11))]
pub exc_type: *mut PyObject,
pub exc_value: *mut PyObject,
exc_type: *mut PyObject,
exc_value: *mut PyObject,
#[cfg(not(Py_3_11))]
pub exc_traceback: *mut PyObject,
pub previous_item: *mut _PyErr_StackItem,
exc_traceback: *mut PyObject,
previous_item: *mut _PyErr_StackItem,
}

// skipped _PyStackChunk
Expand Down
1 change: 1 addition & 0 deletions pyo3-ffi/src/cpython/weakrefobject.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// NB publicly re-exported in `src/weakrefobject.rs`
#[cfg(not(any(PyPy, GraalPy)))]
pub struct _PyWeakReference {
pub ob_base: crate::PyObject,
Expand Down
2 changes: 1 addition & 1 deletion pyo3-ffi/src/dictobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@ extern "C" {

#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))]
// TODO: remove (see https://github.com/PyO3/pyo3/pull/1341#issuecomment-751515985)
opaque_struct!(PyDictObject);
opaque_struct!(pub PyDictObject);
2 changes: 1 addition & 1 deletion pyo3-ffi/src/floatobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ptr::addr_of_mut;

#[cfg(Py_LIMITED_API)]
// TODO: remove (see https://github.com/PyO3/pyo3/pull/1341#issuecomment-751515985)
opaque_struct!(PyFloatObject);
opaque_struct!(pub PyFloatObject);

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
Expand Down
4 changes: 2 additions & 2 deletions pyo3-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@
// model opaque types:
// https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs
macro_rules! opaque_struct {
($name:ident) => {
($pub:vis $name:ident) => {
#[repr(C)]
pub struct $name([u8; 0]);
$pub struct $name([u8; 0]);
};
}

Expand Down
2 changes: 1 addition & 1 deletion pyo3-ffi/src/longobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use libc::size_t;
use std::os::raw::{c_char, c_double, c_int, c_long, c_longlong, c_ulong, c_ulonglong, c_void};
use std::ptr::addr_of_mut;

opaque_struct!(PyLongObject);
opaque_struct!(pub PyLongObject);

#[inline]
pub unsafe fn PyLong_Check(op: *mut PyObject) -> c_int {
Expand Down
2 changes: 1 addition & 1 deletion pyo3-ffi/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::ptr;
use std::sync::atomic::{AtomicIsize, AtomicU32, AtomicU8, Ordering::Relaxed};

#[cfg(Py_LIMITED_API)]
opaque_struct!(PyTypeObject);
opaque_struct!(pub PyTypeObject);

#[cfg(not(Py_LIMITED_API))]
pub use crate::cpython::object::PyTypeObject;
Expand Down
2 changes: 1 addition & 1 deletion pyo3-ffi/src/pyarena.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
opaque_struct!(PyArena);
opaque_struct!(pub PyArena);
2 changes: 1 addition & 1 deletion pyo3-ffi/src/pyframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::PyFrameObject;
use std::os::raw::c_int;

#[cfg(Py_LIMITED_API)]
opaque_struct!(PyFrameObject);
opaque_struct!(pub PyFrameObject);

extern "C" {
pub fn PyFrame_GetLineNumber(f: *mut PyFrameObject) -> c_int;
Expand Down
4 changes: 2 additions & 2 deletions pyo3-ffi/src/pystate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use std::os::raw::c_long;

pub const MAX_CO_EXTRA_USERS: c_int = 255;

opaque_struct!(PyThreadState);
opaque_struct!(PyInterpreterState);
opaque_struct!(pub PyThreadState);
opaque_struct!(pub PyInterpreterState);

extern "C" {
#[cfg(not(PyPy))]
Expand Down
6 changes: 3 additions & 3 deletions pyo3-ffi/src/pythonrun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ pub const PYOS_STACK_MARGIN: c_int = 2048;
// skipped PyOS_CheckStack under Microsoft C

#[cfg(not(any(PyPy, Py_LIMITED_API, Py_3_10)))]
opaque_struct!(_mod);
opaque_struct!(pub _mod);

#[cfg(not(any(PyPy, Py_3_10)))]
opaque_struct!(symtable);
opaque_struct!(pub symtable);
#[cfg(not(any(PyPy, Py_3_10)))]
opaque_struct!(_node);
opaque_struct!(pub _node);

#[cfg(not(any(PyPy, Py_LIMITED_API, Py_3_10)))]
#[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))]
Expand Down
2 changes: 1 addition & 1 deletion pyo3-ffi/src/weakrefobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::os::raw::c_int;
use std::ptr::addr_of_mut;

#[cfg(all(not(PyPy), Py_LIMITED_API, not(GraalPy)))]
opaque_struct!(PyWeakReference);
opaque_struct!(pub PyWeakReference);

#[cfg(all(not(PyPy), not(Py_LIMITED_API), not(GraalPy)))]
pub use crate::_PyWeakReference as PyWeakReference;
Expand Down
Loading