Skip to content
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

Add context.h functions (PyContext_New, PyContext*) #1259

Merged
merged 1 commit into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- Add argument names to `TypeError` messages generated by pymethod wrappers. [#1212](https://github.com/PyO3/pyo3/pull/1212)
- Add `PyEval_SetProfile` and `PyEval_SetTrace` to FFI. [#1255](https://github.com/PyO3/pyo3/pull/1255)
- Add context.h functions (`PyContext_New`, etc) to FFI. [#1259](https://github.com/PyO3/pyo3/pull/1259)

### Changed
- Change `PyIterator` to be consistent with other native types: it is now used as `&PyIterator` instead of `PyIterator<'a>`. [#1176](https://github.com/PyO3/pyo3/pull/1176)
Expand Down
36 changes: 36 additions & 0 deletions src/ffi/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::ffi::object::{PyObject, PyTypeObject, Py_TYPE};
use std::os::raw::{c_char, c_int};

extern "C" {
pub static mut PyContext_Type: PyTypeObject;
pub static mut PyContextVar_Type: PyTypeObject;
pub static mut PyContextToken_Type: PyTypeObject;
pub fn PyContext_New() -> *mut PyObject;
pub fn PyContext_Copy(ctx: *mut PyObject) -> *mut PyObject;
pub fn PyContext_CopyCurrent() -> *mut PyObject;
pub fn PyContext_Enter(ctx: *mut PyObject) -> c_int;
pub fn PyContext_Exit(ctx: *mut PyObject) -> c_int;
pub fn PyContextVar_New(name: *const c_char, def: *mut PyObject) -> *mut PyObject;
pub fn PyContextVar_Get(
var: *mut PyObject,
default_value: *mut PyObject,
value: *mut *mut PyObject,
) -> c_int;
pub fn PyContextVar_Set(var: *mut PyObject, value: *mut PyObject) -> *mut PyObject;
pub fn PyContextVar_Reset(var: *mut PyObject, token: *mut PyObject) -> c_int;
}

#[inline]
pub unsafe fn PyContext_CheckExact(op: *mut PyObject) -> c_int {
(Py_TYPE(op) == &mut PyContext_Type) as c_int
}

#[inline]
pub unsafe fn PyContextVar_CheckExact(op: *mut PyObject) -> c_int {
(Py_TYPE(op) == &mut PyContextVar_Type) as c_int
}

#[inline]
pub unsafe fn PyContextToken_CheckExact(op: *mut PyObject) -> c_int {
(Py_TYPE(op) == &mut PyContextToken_Type) as c_int
}
8 changes: 8 additions & 0 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub use self::code::*;
pub use self::codecs::*;
pub use self::compile::*;
pub use self::complexobject::*;
pub use self::context::*;
pub use self::datetime::*;
pub use self::descrobject::*;
pub use self::dictobject::*;
Expand Down Expand Up @@ -135,6 +136,13 @@ mod code {}
mod code;

mod compile; // TODO: incomplete

#[cfg(all(Py_3_8, not(Py_LIMITED_API)))]
mod context; // It's actually 3.7.1, but no cfg for patches.

#[cfg(not(all(Py_3_8, not(Py_LIMITED_API))))]
mod context {}

mod eval; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5

// mod pyctype; TODO excluded by PEP-384
Expand Down