-
Notifications
You must be signed in to change notification settings - Fork 784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement ffi/cpython/pystate #1687
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7c51084
dont reexport cpython items twice
mejrs 0c8ecec
add ffi module level doc
mejrs 121808e
reorder to header file order
mejrs c720ef2
cargo fmt
mejrs af64427
implement cpython/pystate
mejrs ca826c7
fix import errors
mejrs 377911e
make PyInterpreterState opaque
mejrs f97c7f5
Merge branch 'main' into ffi2
mejrs 5d31748
update changelog
mejrs 955e1b2
resolve merge conflicts
mejrs 1db36bd
fix formatting
mejrs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
use crate::ffi::object::{freefunc, PyObject}; | ||
use crate::ffi::pystate::Py_tracefunc; | ||
use crate::ffi::Py_tracefunc; | ||
|
||
use std::os::raw::c_int; | ||
|
||
extern "C" { | ||
pub fn PyEval_SetProfile(trace_func: Py_tracefunc, arg1: *mut PyObject); | ||
|
||
pub fn _PyEval_EvalFrameDefault( | ||
arg1: *mut crate::ffi::PyFrameObject, | ||
exc: c_int, | ||
) -> *mut PyObject; | ||
pub fn _PyEval_RequestCodeExtraIndex(func: freefunc) -> c_int; | ||
pub fn PyEval_SetProfile(trace_func: Py_tracefunc, arg1: *mut PyObject); | ||
|
||
pub fn PyEval_SetTrace(trace_func: Py_tracefunc, arg1: *mut PyObject); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use crate::ffi::pystate::{PyInterpreterState, PyThreadState}; | ||
use std::os::raw::c_int; | ||
|
||
// Py_tracefunc is defined in ffi::pystate | ||
|
||
pub const PyTrace_CALL: c_int = 0; | ||
pub const PyTrace_EXCEPTION: c_int = 1; | ||
pub const PyTrace_LINE: c_int = 2; | ||
pub const PyTrace_RETURN: c_int = 3; | ||
pub const PyTrace_C_CALL: c_int = 4; | ||
pub const PyTrace_C_EXCEPTION: c_int = 5; | ||
pub const PyTrace_C_RETURN: c_int = 6; | ||
pub const PyTrace_OPCODE: c_int = 7; | ||
|
||
extern "C" { | ||
// PyGILState_Check is defined in ffi::pystate | ||
pub fn PyInterpreterState_Main() -> *mut PyInterpreterState; | ||
pub fn PyInterpreterState_Head() -> *mut PyInterpreterState; | ||
pub fn PyInterpreterState_Next(interp: *mut PyInterpreterState) -> *mut PyInterpreterState; | ||
pub fn PyInterpreterState_ThreadHead(interp: *mut PyInterpreterState) -> *mut PyThreadState; | ||
pub fn PyThreadState_Next(tstate: *mut PyThreadState) -> *mut PyThreadState; | ||
} | ||
|
||
#[cfg(Py_3_9)] | ||
#[cfg_attr(docsrs, doc(cfg(Py_3_9)))] | ||
pub type _PyFrameEvalFunction = extern "C" fn( | ||
*mut crate::ffi::PyThreadState, | ||
*mut crate::ffi::PyFrameObject, | ||
c_int, | ||
) -> *mut crate::ffi::object::PyObject; | ||
|
||
#[cfg(Py_3_9)] | ||
extern "C" { | ||
/// Get the frame evaluation function. | ||
#[cfg_attr(docsrs, doc(cfg(Py_3_9)))] | ||
pub fn _PyInterpreterState_GetEvalFrameFunc( | ||
interp: *mut PyInterpreterState, | ||
) -> _PyFrameEvalFunction; | ||
|
||
///Set the frame evaluation function. | ||
#[cfg_attr(docsrs, doc(cfg(Py_3_9)))] | ||
pub fn _PyInterpreterState_SetEvalFrameFunc( | ||
interp: *mut PyInterpreterState, | ||
eval_frame: _PyFrameEvalFunction, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
use crate::ffi::ceval::_PyFrameEvalFunction; | ||
#[cfg(all(Py_3_9, not(Py_LIMITED_API)))] | ||
use crate::ffi::cpython::pystate::_PyFrameEvalFunction; | ||
use crate::ffi::moduleobject::PyModuleDef; | ||
use crate::ffi::object::PyObject; | ||
use crate::ffi::PyFrameObject; | ||
|
@@ -7,10 +8,29 @@ use std::os::raw::{c_int, c_long}; | |
pub const MAX_CO_EXTRA_USERS: c_int = 255; | ||
|
||
#[repr(C)] | ||
#[derive(Copy, Clone)] | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct PyInterpreterState { | ||
/// Not actually a public field | ||
pub ob_base: PyObject, | ||
|
||
/// Not actually a public field | ||
/// Also, it has an additional argument in 3.9 | ||
#[cfg(not(Py_3_9))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure that this is a good way to handle this, any suggestions? |
||
pub eval_frame: extern "C" fn(*mut crate::ffi::PyFrameObject, c_int) -> *mut PyObject, | ||
|
||
/// Not actually a public field | ||
/// Also, it has an additional argument in 3.9 | ||
#[cfg(all(Py_3_9, not(Py_LIMITED_API)))] | ||
pub eval_frame: _PyFrameEvalFunction, | ||
|
||
/// Not actually a public field | ||
/// Also, it has an additional argument in 3.9 | ||
#[cfg(all(Py_3_9, Py_LIMITED_API))] | ||
pub eval_frame: extern "C" fn( | ||
*mut crate::ffi::PyThreadState, | ||
*mut crate::ffi::PyFrameObject, | ||
c_int, | ||
) -> *mut PyObject, | ||
} | ||
|
||
#[repr(C)] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this looks dangerously broken, because it doesn't match at all the full contents of the corresponding struct in the Python headers: https://github.com/python/cpython/blob/af5fb6706219d7949c1db5c9f2b7da53198123f3/Include/internal/pycore_pystate.h#L67
Appreciate that this existed before you wrote this PR, but I don't think editing this struct in this PR achieves much.
As it's an internal type (and probably very different on PyPy),I think we should just replace with an opaque type:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.