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
12 changes: 8 additions & 4 deletions src/impl_/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ use std::future::Future;

use crate::{
coroutine::{cancel::ThrowCallback, Coroutine},
instance::Bound,
types::PyString,
Py, PyAny, PyResult, Python,
Borrowed, Py, PyAny, PyResult, Python,
};

pub fn new_coroutine<'py, F>(
name: &Bound<'py, PyString>,
name: Borrowed<'_, 'py, PyString>,
qualname_prefix: Option<&'static str>,
throw_callback: Option<ThrowCallback>,
future: F,
) -> Coroutine
where
F: Future<Output = PyResult<Py<PyAny>>> + Send + 'static,
{
Coroutine::new(Some(name.clone()), qualname_prefix, throw_callback, future)
Coroutine::new(
Some(name.to_owned()),
qualname_prefix,
throw_callback,
future,
)
}

/// Handle which assumes that the coroutine is attached to the thread. Unlike `Python<'_>`, this is `Send`.
Expand Down
48 changes: 21 additions & 27 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
//! interpreter.
//!
//! This module provides synchronization primitives which are able to synchronize under these conditions.
use crate::{
internal::state::SuspendAttach,
sealed::Sealed,
types::{PyAny, PyString},
Bound, Py, Python,
};
use crate::{internal::state::SuspendAttach, sealed::Sealed, types::PyAny, Bound, Python};
use std::{
cell::UnsafeCell,
marker::PhantomData,
Expand Down Expand Up @@ -229,30 +224,27 @@ 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);
INTERNED.get($py)
const STRING: ::std::result::Result<&::std::ffi::CStr, &str> = {
match ::std::ffi::CStr::from_bytes_with_nul(concat!($text, "\0").as_bytes()) {
::std::result::Result::Ok(c_str) => ::std::result::Result::Ok(c_str),
::std::result::Result::Err(_) => ::std::result::Result::Err($text),
}
};
static INTERNED: $crate::sync::PyOnceLock<$crate::Py<$crate::types::PyString>> =
$crate::sync::PyOnceLock::new();
INTERNED
.get_or_init($py, || match STRING {
::std::result::Result::Ok(c_str) => {
$crate::types::PyString::intern_cstr($py, c_str).unbind()
}
::std::result::Result::Err(string) => {
$crate::types::PyString::intern($py, string).unbind()
}
})
.bind_borrowed($py)
}};
}

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

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

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

/// Extension trait for [`Once`] to help avoid deadlocking when using a [`Once`] when attached to a
/// Python thread.
pub trait OnceExt: Sealed {
Expand Down Expand Up @@ -727,6 +719,8 @@ mod tests {
use super::*;

use crate::types::{PyAnyMethods, PyDict, PyDictMethods};
#[cfg(feature = "macros")]
use crate::Py;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "macros")]
use std::sync::atomic::{AtomicBool, Ordering};
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