From 754c27f17d27643974a27bfadd133ebbcbe4f1ab Mon Sep 17 00:00:00 2001 From: Gregory Szorc Date: Sat, 6 Mar 2021 10:29:06 -0800 Subject: [PATCH] ffi: Define missing import APIs Co-authored-by: Nicholas Sim --- CHANGELOG.md | 1 + src/ffi/cpython/import.rs | 71 +++++++++++++++++++++++++++++++++++++++ src/ffi/cpython/mod.rs | 3 +- 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/ffi/cpython/import.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 52b189385c7..0e75f883efb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Add FFI definition `PyCFunction_CheckExact` for Python 3.9 and later. [#1425](https://github.com/PyO3/pyo3/pull/1425) - Add FFI definition `Py_IS_TYPE`. [#1429](https://github.com/PyO3/pyo3/pull/1429) - Add FFI definition `_Py_InitializeMain`. [#1473](https://github.com/PyO3/pyo3/pull/1473) +- Add FFI definitions from `cpython/import.h`.[#1475](https://github.com/PyO3/pyo3/pull/1475) - Add tuple and unit struct support for `#[pyclass]` macro. [#1504](https://github.com/PyO3/pyo3/pull/1504) ### Changed diff --git a/src/ffi/cpython/import.rs b/src/ffi/cpython/import.rs new file mode 100644 index 00000000000..b81247b5f8a --- /dev/null +++ b/src/ffi/cpython/import.rs @@ -0,0 +1,71 @@ +use crate::ffi::{PyInterpreterState, PyObject}; +use std::os::raw::{c_char, c_int, c_uchar}; + +// skipped PyInit__imp + +extern "C" { + pub fn _PyImport_IsInitialized(state: *mut PyInterpreterState) -> c_int; + // skipped _PyImport_GetModuleId + #[cfg(Py_3_7)] + pub fn _PyImport_SetModule(name: *mut PyObject, module: *mut PyObject) -> c_int; + #[cfg(Py_3_7)] + pub fn _PyImport_SetModuleString(name: *const c_char, module: *mut PyObject) -> c_int; + pub fn _PyImport_AcquireLock(); + pub fn _PyImport_ReleaseLock() -> c_int; + #[cfg(not(Py_3_7))] + pub fn _PyImport_FindBuiltin(name: *const c_char) -> *mut PyObject; + #[cfg(all(Py_3_7, not(Py_3_9)))] + pub fn _PyImport_FindBuiltin(name: *const c_char, modules: *mut PyObject) -> *mut PyObject; + #[cfg(not(Py_3_10))] + pub fn _PyImport_FindExtensionObject(a: *mut PyObject, b: *mut PyObject) -> *mut PyObject; + #[cfg(not(Py_3_7))] + pub fn _PyImport_FixupBuiltin(module: *mut PyObject, name: *const c_char) -> c_int; + #[cfg(Py_3_7)] + pub fn _PyImport_FixupBuiltin( + module: *mut PyObject, + name: *const c_char, + modules: *mut PyObject, + ) -> c_int; + #[cfg(not(Py_3_7))] + pub fn _PyImport_FixupExtensionObject( + a: *mut PyObject, + b: *mut PyObject, + c: *mut PyObject, + ) -> c_int; + #[cfg(Py_3_7)] + pub fn _PyImport_FixupExtensionObject( + a: *mut PyObject, + b: *mut PyObject, + c: *mut PyObject, + d: *mut PyObject, + ) -> c_int; +} + +#[repr(C)] +#[derive(Copy, Clone)] +pub struct _inittab { + pub name: *const c_char, + pub initfun: Option *mut PyObject>, +} + +#[cfg_attr(windows, link(name = "pythonXY"))] +extern "C" { + pub static mut PyImport_Inittab: *mut _inittab; +} + +extern "C" { + pub fn PyImport_ExtendInittab(newtab: *mut _inittab) -> c_int; +} + +#[repr(C)] +#[derive(Copy, Clone)] +pub struct _frozen { + name: *const c_char, + code: *const c_uchar, + size: c_int, +} + +#[cfg_attr(windows, link(name = "pythonXY"))] +extern "C" { + pub static mut PyImport_FrozenModules: *const _frozen; +} diff --git a/src/ffi/cpython/mod.rs b/src/ffi/cpython/mod.rs index 62208e82c40..4f7362f5f2e 100644 --- a/src/ffi/cpython/mod.rs +++ b/src/ffi/cpython/mod.rs @@ -9,7 +9,7 @@ pub mod compile; pub mod dictobject; // skipped fileobject.h pub mod frameobject; -// skipped import.h +pub mod import; #[cfg(all(Py_3_8, not(PyPy)))] pub mod initconfig; // skipped interpreteridobject.h @@ -28,6 +28,7 @@ pub use self::compile::*; #[cfg(not(PyPy))] pub use self::dictobject::*; pub use self::frameobject::*; +pub use self::import::*; #[cfg(all(Py_3_8, not(PyPy)))] pub use self::initconfig::*; pub use self::listobject::*;