From 9821882cdc2c19c81ac9d13e85a5a99278eaca18 Mon Sep 17 00:00:00 2001 From: Gregory Szorc Date: Sat, 6 Mar 2021 10:29:06 -0800 Subject: [PATCH] Define missing import APIs I need some of these APIs for the pyembed crate (used as part of PyOxidizer). I figured I might as well define them all. All these APIs are limited. --- CHANGELOG.md | 1 + src/ffi/import.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 78cc43a1d31..68da091fdb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Add #[pyo3(from_py_with = "...")]` attribute for function arguments and struct fields to override the default from-Python conversion. [#1411](https://github.com/PyO3/pyo3/pull/1411) - 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 definitions for limited APIs `PyImport_ExtendInittab` and missing `_PyImport_*` functions. ### Changed - Change `PyTimeAcces::get_fold()` to return a `bool` instead of a `u8`. [#1397](https://github.com/PyO3/pyo3/pull/1397) diff --git a/src/ffi/import.rs b/src/ffi/import.rs index 81d85c3b5fe..eaa7985509d 100644 --- a/src/ffi/import.rs +++ b/src/ffi/import.rs @@ -1,4 +1,6 @@ use crate::ffi::object::PyObject; +#[cfg(not(Py_LIMITED_API))] +use std::os::raw::c_uchar; use std::os::raw::{c_char, c_int, c_long}; extern "C" { @@ -82,3 +84,68 @@ extern "C" { initfunc: Option *mut PyObject>, ) -> c_int; } + +#[repr(C)] +#[derive(Copy, Clone)] +#[cfg(not(Py_LIMITED_API))] +pub struct _inittab { + pub name: *mut c_char, + pub initfunc: Option, +} + +#[repr(C)] +#[derive(Copy, Clone)] +#[cfg(not(Py_LIMITED_API))] +pub struct _frozen { + pub name: *const c_char, + pub code: *const c_uchar, + pub size: c_int, +} + +#[cfg(not(Py_LIMITED_API))] +extern "C" { + pub static mut PyImport_FrozenModules: *const _frozen; + pub static mut PyImport_Inittab: *mut _inittab; + + pub fn PyImport_ExtendInittab(newtab: *const _inittab) -> 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; + + pub fn _PyImport_FindExtensionObject( + name: *mut PyObject, + filename: *mut PyObject, + ) -> *mut PyObject; + + #[cfg(all(Py_3_7, not(Py_3_9)))] + pub fn _PyImport_FindExtensionObjectEx( + name: *mut PyObject, + filename: *mut PyObject, + modules: *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( + module: *mut PyObject, + name: *mut PyObject, + filename: *mut PyObject, + ) -> c_int; + #[cfg(Py_3_7)] + pub fn _PyImport_FixupExtensionObject( + module: *mut PyObject, + name: *mut PyObject, + filename: *mut PyObject, + modules: *mut PyObject, + ) -> c_int; +}