From 9c79b25416f96b769479ff125a5abc3187d81a90 Mon Sep 17 00:00:00 2001 From: Aviram Hassan Date: Thu, 22 Oct 2020 09:58:41 +0300 Subject: [PATCH] Add context.h functions (PyContext_New, PyContext*) --- CHANGELOG.md | 1 + src/ffi/context.rs | 33 +++++++++++++++++++++++++++++++++ src/ffi/mod.rs | 5 +++++ 3 files changed, 39 insertions(+) create mode 100644 src/ffi/context.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 610dd615c33..4b98cb74a1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/ffi/context.rs b/src/ffi/context.rs new file mode 100644 index 00000000000..361ead8d04c --- /dev/null +++ b/src/ffi/context.rs @@ -0,0 +1,33 @@ +use crate::ffi::object::{PyObject, PyTypeObject, Py_TYPE}; +use std::os::raw::{c_int, c_char}; + +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 +} \ No newline at end of file diff --git a/src/ffi/mod.rs b/src/ffi/mod.rs index 5182a695c1c..a7681cef93a 100644 --- a/src/ffi/mod.rs +++ b/src/ffi/mod.rs @@ -9,6 +9,7 @@ pub use self::bytesobject::*; pub use self::ceval::*; pub use self::code::*; pub use self::codecs::*; +pub use self::context::*; pub use self::compile::*; pub use self::complexobject::*; pub use self::datetime::*; @@ -135,6 +136,10 @@ 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. + mod eval; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5 // mod pyctype; TODO excluded by PEP-384