Skip to content

Make the ffi crate no_std#6022

Open
Person-93 wants to merge 4 commits intoPyO3:mainfrom
Person-93:no_std_ffi
Open

Make the ffi crate no_std#6022
Person-93 wants to merge 4 commits intoPyO3:mainfrom
Person-93:no_std_ffi

Conversation

@Person-93
Copy link
Copy Markdown
Contributor

@Person-93 Person-93 commented May 8, 2026

I've added no_std to the python-ffi crate.

Most of the changes in this PR are a search-and-replace from std:: to core::. The only other change I've made is in the datetime file. It used the Once type from std. I've updated it to use AtomicBool instead.

This is the first PR from this converstaion: #5856 (comment)

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 8, 2026

Merging this PR will not alter performance

✅ 125 untouched benchmarks


Comparing Person-93:no_std_ffi (f33c321) with main (65d6a36)

Open in CodSpeed

@Person-93
Copy link
Copy Markdown
Contributor Author

The ffi crate currently uses std::thread::park to work around the interpreter calling pthread_exit for python versions older than 3.14.

#[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
struct HangThread;
#[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
impl Drop for HangThread {
fn drop(&mut self) {
loop {
std::thread::park(); // Block forever.
}
}
}

I can think of two ways to handle this.

  • move the usage into the main crate
  • use a feature-flag to conditionally use a separate crate that can sleep in no_std

The first way is technically a breaking change if anyone is using the PyGILState_Ensure method from the ffi crate as they would have to deal with pinned stack objects some other way, but I think it's simpler, so I will go with that one unless you object.

@Person-93 Person-93 force-pushed the no_std_ffi branch 2 times, most recently from 3f9162a to 48b06f9 Compare May 9, 2026 00:27
Copy link
Copy Markdown
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, overall this looks good. Couple of suggestions / thoughts / questions about the nontrivial bits.

Comment thread pyo3-ffi/src/pystate.rs
Comment thread pyo3-ffi/src/lib.rs
Comment thread pyo3-ffi/src/datetime.rs
@ngoldbaum
Copy link
Copy Markdown
Contributor

Neat, I'm glad this is mostly mechanical. I'll try to give this a once-over.

Copy link
Copy Markdown
Contributor

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with all of David's comments. I also think we need a CI job that compiles the crate with no_std. Might be worth adding that to the nox configuration so all the CI jobs do it if it's a pretty quick build.

@Person-93
Copy link
Copy Markdown
Contributor Author

Person-93 commented May 10, 2026

I agree with all of David's comments. I also think we need a CI job that compiles the crate with no_std. Might be worth adding that to the nox configuration so all the CI jobs do it if it's a pretty quick build.

The ffi crate will always compile with no_std if python >= 3.14 or wasm build. It's not feature-gated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants