Skip to content

Commit

Permalink
ffi: define some cpython/unicodeobject bindings
Browse files Browse the repository at this point in the history
pyo3 doesn't currently define various Unicode bindings that allow the
retrieval of raw data from Python strings. Said bindings are a
prerequisite to possibly exposing this data in the Rust API (PyO3#1776).
Even if those high-level APIs never materialize, the FFI bindings are
necessary to enable consumers of the raw C API to utilize them.

This commit partially defines the FFI bindings as defined in
CPython's Include/cpython/unicodeobject.h file.

I used the latest CPython 3.9 Git commit for defining the order
of the symbols and the implementation of various inline preprocessor
macros. I tried to be as faithful as possible to the original
implementation, preserving intermediate `#define`s as inline functions.

Missing symbols have been annotated with `skipped` and symbols currently
defined in `src/ffi/unicodeobject.rs` have been annotated with `move`.

The `state` field of `PyASCIIObject` is a bitfield, which Rust doesn't
support. So we've provided accessor functions for retrieving these
fields' values. No accessor functions are present because you shouldn't
be touching these values from Rust code.

Tests of the bitfield APIs and macro implementations have been added.
  • Loading branch information
indygreg committed Aug 14, 2021
1 parent 254ea53 commit 32239fd
Show file tree
Hide file tree
Showing 2 changed files with 469 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ffi/cpython/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub(crate) mod pydebug;
#[cfg(all(Py_3_8, not(PyPy)))]
pub(crate) mod pylifecycle;
pub(crate) mod pystate;
pub(crate) mod unicodeobject;

pub use self::abstract_::*;
#[cfg(not(PyPy))]
Expand All @@ -40,3 +41,4 @@ pub use self::pydebug::*;
#[cfg(all(Py_3_8, not(PyPy)))]
pub use self::pylifecycle::*;
pub use self::pystate::*;
pub use self::unicodeobject::*;
Loading

0 comments on commit 32239fd

Please sign in to comment.