Skip to content

Commit

Permalink
Add context.h functions (PyContext_New, PyContext*)
Browse files Browse the repository at this point in the history
Formatting
  • Loading branch information
Aviram Hassan committed Oct 22, 2020
1 parent 864eeca commit 0fd500f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
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

0 comments on commit 0fd500f

Please sign in to comment.