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 11, 2021
1 parent 6a9ef54 commit 6ccc0a9
Show file tree
Hide file tree
Showing 2 changed files with 469 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ffi/cpython/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ pub(crate) mod object;
pub(crate) mod pydebug;
#[cfg(all(Py_3_8, not(PyPy)))]
pub(crate) mod pylifecycle;

#[cfg(all(Py_3_8, not(PyPy)))]
pub(crate) mod pystate;
pub(crate) mod unicodeobject;

pub use self::abstract_::*;
#[cfg(not(PyPy))]
Expand All @@ -42,3 +42,4 @@ pub use self::pylifecycle::*;

#[cfg(all(Py_3_8, not(PyPy)))]
pub use self::pystate::*;
pub use self::unicodeobject::*;
Loading

0 comments on commit 6ccc0a9

Please sign in to comment.