Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ macro_rules! py_format {
static INTERNED: $crate::sync::PyOnceLock<$crate::Py<$crate::types::PyString>> = $crate::sync::PyOnceLock::new();
Ok(
INTERNED
.get_or_init($py, || $crate::types::PyString::intern($py, static_string).unbind())
.get_or_init($py, || {
if let Ok(static_c_string) = ::std::ffi::CString::new(static_string) {
$crate::types::PyString::intern_cstr($py, &static_c_string).unbind()
} else {
$crate::types::PyString::intern($py, static_string).unbind()
}
})
.bind($py)
.to_owned()
)
Expand Down
23 changes: 16 additions & 7 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ use crate::{
internal::state::SuspendAttach,
sealed::Sealed,
types::{PyAny, PyString},
Bound, Py, Python,
Borrowed, Bound, Py, Python,
};
use std::ffi::CStr;
use std::{
cell::UnsafeCell,
marker::PhantomData,
Expand Down Expand Up @@ -229,27 +230,35 @@ impl<T> Drop for GILOnceCell<T> {
#[macro_export]
macro_rules! intern {
($py: expr, $text: expr) => {{
static INTERNED: $crate::sync::Interned = $crate::sync::Interned::new($text);
const _CSTR: &::std::ffi::CStr = {
match ::std::ffi::CStr::from_bytes_with_nul(concat!($text, "\0").as_bytes()) {
::std::result::Result::Ok(s) => s,
::std::result::Result::Err(_) => {
::std::panic!("interned string cannot contain interior null bytes")
}
}
};
static INTERNED: $crate::sync::Interned = $crate::sync::Interned::new(_CSTR);
INTERNED.get($py)
}};
}

/// Implementation detail for `intern!` macro.
#[doc(hidden)]
pub struct Interned(&'static str, PyOnceLock<Py<PyString>>);
pub struct Interned(&'static CStr, PyOnceLock<Py<PyString>>);

impl Interned {
/// Creates an empty holder for an interned `str`.
pub const fn new(value: &'static str) -> Self {
pub const fn new(value: &'static CStr) -> Self {
Interned(value, PyOnceLock::new())
}

/// Gets or creates the interned `str` value.
#[inline]
pub fn get<'py>(&self, py: Python<'py>) -> &Bound<'py, PyString> {
pub fn get<'py>(&self, py: Python<'py>) -> Borrowed<'_, 'py, PyString> {
self.1
.get_or_init(py, || PyString::intern(py, self.0).into())
.bind(py)
.get_or_init(py, || PyString::intern_cstr(py, self.0).unbind())
.bind_borrowed(py)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/types/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,11 @@ impl<'py> PyModuleMethods<'py> for Bound<'py, PyModule> {
}
}

fn __all__(py: Python<'_>) -> &Bound<'_, PyString> {
fn __all__(py: Python<'_>) -> Borrowed<'static, '_, PyString> {
intern!(py, "__all__")
}

fn __name__(py: Python<'_>) -> &Bound<'_, PyString> {
fn __name__(py: Python<'_>) -> Borrowed<'static, '_, PyString> {
intern!(py, "__name__")
}

Expand Down
12 changes: 12 additions & 0 deletions src/types/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@ impl PyString {
}
}

/// Intern the given C string.
///
/// Python may keep a reference to the returned string or make it immortal, preventing it from
/// being garbage collected.
pub fn intern_cstr<'py>(py: Python<'py>, s: &CStr) -> Bound<'py, PyString> {
unsafe {
ffi::PyUnicode_InternFromString(s.as_ptr())
.assume_owned(py)
.cast_into_unchecked()
}
}

/// Attempts to create a Python string from a Python [bytes-like object].
///
/// The `encoding` and `errors` parameters are optional:
Expand Down
Loading