Skip to content

Commit 2c2f39f

Browse files
codeguru42dsal3389davidhewitt
authored
Frame ffi (#5154)
* add missing pyframe ffi functions * Rearrange pyframe stuff to match CPython structure * Remove duplicate declaration * Clean up duplicate declaration of PyFrameObject * Fix imports * Update changelog * Conditional compilation for `use` statement that matches its usage * Match `use` conditional compilation with usage * Fix another `use` statement * Don't need cfg_attr() * Fix another conditional compilation * Conditional compilation for `use` * Another conditional compile * More conditional compile checks * One more * Remove unused import Co-authored-by: David Hewitt <[email protected]> * Changelog for removed items * Remove unnecessary wording in comments Co-authored-by: David Hewitt <[email protected]> --------- Co-authored-by: daniel sonbolian <[email protected]> Co-authored-by: David Hewitt <[email protected]>
1 parent 9e2f033 commit 2c2f39f

File tree

9 files changed

+79
-57
lines changed

9 files changed

+79
-57
lines changed

newsfragments/5154.added.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* added missing ffi functions for `PyFrameObject` but without
2+
including unstable API from python 3.13
3+
* moved ffi functions to correct files to mach CPython as of 3.13

newsfragments/5154.removed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Removed access to internals of PyFrameObject

pyo3-ffi/src/cpython/frameobject.rs

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#[cfg(not(GraalPy))]
22
use crate::cpython::code::PyCodeObject;
3+
#[cfg(not(GraalPy))]
34
use crate::object::*;
45
#[cfg(not(GraalPy))]
56
use crate::pystate::PyThreadState;
7+
use crate::PyFrameObject;
68
#[cfg(not(any(PyPy, GraalPy, Py_3_11)))]
79
use std::os::raw::c_char;
810
use std::os::raw::c_int;
9-
use std::ptr::addr_of_mut;
1011

1112
#[cfg(not(any(PyPy, GraalPy, Py_3_11)))]
1213
pub type PyFrameState = c_char;
@@ -20,55 +21,10 @@ pub struct PyTryBlock {
2021
pub b_level: c_int,
2122
}
2223

23-
#[repr(C)]
24-
#[cfg(not(any(PyPy, GraalPy, Py_3_11)))]
25-
pub struct PyFrameObject {
26-
pub ob_base: PyVarObject,
27-
pub f_back: *mut PyFrameObject,
28-
pub f_code: *mut PyCodeObject,
29-
pub f_builtins: *mut PyObject,
30-
pub f_globals: *mut PyObject,
31-
pub f_locals: *mut PyObject,
32-
pub f_valuestack: *mut *mut PyObject,
33-
34-
#[cfg(not(Py_3_10))]
35-
pub f_stacktop: *mut *mut PyObject,
36-
pub f_trace: *mut PyObject,
37-
#[cfg(Py_3_10)]
38-
pub f_stackdepth: c_int,
39-
pub f_trace_lines: c_char,
40-
pub f_trace_opcodes: c_char,
41-
42-
pub f_gen: *mut PyObject,
43-
44-
pub f_lasti: c_int,
45-
pub f_lineno: c_int,
46-
pub f_iblock: c_int,
47-
#[cfg(not(Py_3_10))]
48-
pub f_executing: c_char,
49-
#[cfg(Py_3_10)]
50-
pub f_state: PyFrameState,
51-
pub f_blockstack: [PyTryBlock; crate::CO_MAXBLOCKS],
52-
pub f_localsplus: [*mut PyObject; 1],
53-
}
54-
55-
#[cfg(any(PyPy, GraalPy, Py_3_11))]
56-
opaque_struct!(pub PyFrameObject);
57-
5824
// skipped _PyFrame_IsRunnable
5925
// skipped _PyFrame_IsExecuting
6026
// skipped _PyFrameHasCompleted
6127

62-
#[cfg_attr(windows, link(name = "pythonXY"))]
63-
extern "C" {
64-
pub static mut PyFrame_Type: PyTypeObject;
65-
}
66-
67-
#[inline]
68-
pub unsafe fn PyFrame_Check(op: *mut PyObject) -> c_int {
69-
(Py_TYPE(op) == addr_of_mut!(PyFrame_Type)) as c_int
70-
}
71-
7228
extern "C" {
7329
#[cfg(not(GraalPy))]
7430
#[cfg_attr(PyPy, link_name = "PyPyFrame_New")]
@@ -89,8 +45,6 @@ extern "C" {
8945
pub fn PyFrame_FastToLocals(f: *mut PyFrameObject);
9046

9147
// skipped _PyFrame_DebugMallocStats
92-
#[cfg(all(Py_3_9, not(PyPy)))]
93-
pub fn PyFrame_GetBack(f: *mut PyFrameObject) -> *mut PyFrameObject;
9448

9549
#[cfg(not(Py_3_9))]
9650
pub fn PyFrame_ClearFreeList() -> c_int;

pyo3-ffi/src/cpython/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ pub use self::object::*;
7272
pub use self::objimpl::*;
7373
pub use self::pydebug::*;
7474
pub use self::pyerrors::*;
75-
#[cfg(all(Py_3_11, not(PyPy)))]
7675
pub use self::pyframe::*;
7776
#[cfg(any(not(PyPy), Py_3_13))]
7877
pub use self::pyhash::*;

pyo3-ffi/src/cpython/pyframe.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,62 @@
1+
#[cfg(any(Py_3_11, all(Py_3_9, not(PyPy))))]
2+
use crate::PyFrameObject;
3+
use crate::{PyObject, PyTypeObject, Py_TYPE};
4+
#[cfg(Py_3_12)]
5+
use std::os::raw::c_char;
6+
use std::os::raw::c_int;
7+
use std::ptr::addr_of_mut;
8+
19
// NB used in `_PyEval_EvalFrameDefault`, maybe we remove this too.
210
#[cfg(all(Py_3_11, not(PyPy)))]
311
opaque_struct!(pub _PyInterpreterFrame);
12+
13+
#[cfg_attr(windows, link(name = "pythonXY"))]
14+
extern "C" {
15+
pub static mut PyFrame_Type: PyTypeObject;
16+
17+
#[cfg(Py_3_13)]
18+
pub static mut PyFrameLocalsProxy_Type: PyTypeObject;
19+
}
20+
21+
#[inline]
22+
pub unsafe fn PyFrame_Check(op: *mut PyObject) -> c_int {
23+
(Py_TYPE(op) == addr_of_mut!(PyFrame_Type)) as c_int
24+
}
25+
26+
#[cfg(Py_3_13)]
27+
#[inline]
28+
pub unsafe fn PyFrameLocalsProxy_Check(op: *mut PyObject) -> c_int {
29+
(Py_TYPE(op) == addr_of_mut!(PyFrameLocalsProxy_Type)) as c_int
30+
}
31+
32+
extern "C" {
33+
#[cfg(all(Py_3_9, not(PyPy)))]
34+
pub fn PyFrame_GetBack(frame: *mut PyFrameObject) -> *mut PyFrameObject;
35+
36+
#[cfg(Py_3_11)]
37+
pub fn PyFrame_GetLocals(frame: *mut PyFrameObject) -> *mut PyObject;
38+
39+
#[cfg(Py_3_11)]
40+
pub fn PyFrame_GetGlobals(frame: *mut PyFrameObject) -> *mut PyObject;
41+
42+
#[cfg(Py_3_11)]
43+
pub fn PyFrame_GetBuiltins(frame: *mut PyFrameObject) -> *mut PyObject;
44+
45+
#[cfg(Py_3_11)]
46+
pub fn PyFrame_GetGenerator(frame: *mut PyFrameObject) -> *mut PyObject;
47+
48+
#[cfg(Py_3_11)]
49+
pub fn PyFrame_GetLasti(frame: *mut PyFrameObject) -> c_int;
50+
51+
#[cfg(Py_3_12)]
52+
pub fn PyFrame_GetVar(frame: *mut PyFrameObject, name: *mut PyObject) -> *mut PyObject;
53+
54+
#[cfg(Py_3_12)]
55+
pub fn PyFrame_GetVarString(frame: *mut PyFrameObject, name: *mut c_char) -> *mut PyObject;
56+
57+
// skipped PyUnstable_InterpreterFrame_GetCode
58+
// skipped PyUnstable_InterpreterFrame_GetLasti
59+
// skipped PyUnstable_InterpreterFrame_GetLine
60+
// skipped PyUnstable_ExecutableKinds
61+
62+
}

pyo3-ffi/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ pub use self::pyport::*;
447447
pub use self::pystate::*;
448448
pub use self::pystrtod::*;
449449
pub use self::pythonrun::*;
450+
pub use self::pytypedefs::*;
450451
pub use self::rangeobject::*;
451452
pub use self::refcount::*;
452453
pub use self::setobject::*;
@@ -540,6 +541,7 @@ mod pythonrun;
540541
mod pystrtod;
541542
// skipped pythread.h
542543
// skipped pytime.h
544+
mod pytypedefs;
543545
mod rangeobject;
544546
mod refcount;
545547
mod setobject;

pyo3-ffi/src/pyframe.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1+
#[allow(unused_imports)]
2+
use crate::object::PyObject;
13
#[cfg(not(GraalPy))]
24
#[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))]
35
use crate::PyCodeObject;
4-
#[cfg(not(Py_LIMITED_API))]
56
use crate::PyFrameObject;
67
use std::os::raw::c_int;
78

8-
#[cfg(Py_LIMITED_API)]
9-
opaque_struct!(pub PyFrameObject);
10-
119
extern "C" {
12-
pub fn PyFrame_GetLineNumber(f: *mut PyFrameObject) -> c_int;
10+
pub fn PyFrame_GetLineNumber(frame: *mut PyFrameObject) -> c_int;
11+
1312
#[cfg(not(GraalPy))]
1413
#[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))]
15-
pub fn PyFrame_GetCode(f: *mut PyFrameObject) -> *mut PyCodeObject;
14+
pub fn PyFrame_GetCode(frame: *mut PyFrameObject) -> *mut PyCodeObject;
1615
}

pyo3-ffi/src/pystate.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
#[cfg(all(Py_3_10, not(PyPy), not(Py_LIMITED_API)))]
2-
use crate::frameobject::PyFrameObject;
31
use crate::moduleobject::PyModuleDef;
42
use crate::object::PyObject;
53
use std::os::raw::c_int;
64

5+
#[cfg(all(Py_3_10, not(PyPy), not(Py_LIMITED_API)))]
6+
use crate::PyFrameObject;
7+
78
#[cfg(not(PyPy))]
89
use std::os::raw::c_long;
910

pyo3-ffi/src/pytypedefs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// TODO: Created this file as part of fixing pyframe.rs and cpython/pyframe.rs
2+
// TODO: Finish defining or moving declarations now in Include/pytypedefs.h
3+
4+
opaque_struct!(pub PyFrameObject);

0 commit comments

Comments
 (0)