|
9 | 9 | //! interpreter. |
10 | 10 | //! |
11 | 11 | //! This module provides synchronization primitives which are able to synchronize under these conditions. |
12 | | -use crate::{ |
13 | | - internal::state::SuspendAttach, |
14 | | - sealed::Sealed, |
15 | | - types::{PyAny, PyString}, |
16 | | - Borrowed, Bound, Py, Python, |
17 | | -}; |
18 | | -use std::ffi::CStr; |
| 12 | +use crate::{internal::state::SuspendAttach, sealed::Sealed, types::PyAny, Bound, Python}; |
19 | 13 | use std::{ |
20 | 14 | cell::UnsafeCell, |
21 | 15 | marker::PhantomData, |
@@ -230,38 +224,27 @@ impl<T> Drop for GILOnceCell<T> { |
230 | 224 | #[macro_export] |
231 | 225 | macro_rules! intern { |
232 | 226 | ($py: expr, $text: expr) => {{ |
233 | | - const _CSTR: &::std::ffi::CStr = { |
| 227 | + const STRING: ::std::result::Result<&::std::ffi::CStr, &str> = { |
234 | 228 | match ::std::ffi::CStr::from_bytes_with_nul(concat!($text, "\0").as_bytes()) { |
235 | | - ::std::result::Result::Ok(s) => s, |
236 | | - ::std::result::Result::Err(_) => { |
237 | | - ::std::panic!("interned string cannot contain interior null bytes") |
238 | | - } |
| 229 | + ::std::result::Result::Ok(c_str) => ::std::result::Result::Ok(c_str), |
| 230 | + ::std::result::Result::Err(_) => ::std::result::Result::Err($text), |
239 | 231 | } |
240 | 232 | }; |
241 | | - static INTERNED: $crate::sync::Interned = $crate::sync::Interned::new(_CSTR); |
242 | | - INTERNED.get($py) |
| 233 | + static INTERNED: $crate::sync::PyOnceLock<$crate::Py<$crate::types::PyString>> = |
| 234 | + $crate::sync::PyOnceLock::new(); |
| 235 | + INTERNED |
| 236 | + .get_or_init($py, || match STRING { |
| 237 | + ::std::result::Result::Ok(c_str) => { |
| 238 | + $crate::types::PyString::intern_cstr($py, c_str).unbind() |
| 239 | + } |
| 240 | + ::std::result::Result::Err(string) => { |
| 241 | + $crate::types::PyString::intern($py, string).unbind() |
| 242 | + } |
| 243 | + }) |
| 244 | + .bind_borrowed($py) |
243 | 245 | }}; |
244 | 246 | } |
245 | 247 |
|
246 | | -/// Implementation detail for `intern!` macro. |
247 | | -#[doc(hidden)] |
248 | | -pub struct Interned(&'static CStr, PyOnceLock<Py<PyString>>); |
249 | | - |
250 | | -impl Interned { |
251 | | - /// Creates an empty holder for an interned `str`. |
252 | | - pub const fn new(value: &'static CStr) -> Self { |
253 | | - Interned(value, PyOnceLock::new()) |
254 | | - } |
255 | | - |
256 | | - /// Gets or creates the interned `str` value. |
257 | | - #[inline] |
258 | | - pub fn get<'py>(&self, py: Python<'py>) -> Borrowed<'_, 'py, PyString> { |
259 | | - self.1 |
260 | | - .get_or_init(py, || PyString::intern_cstr(py, self.0).unbind()) |
261 | | - .bind_borrowed(py) |
262 | | - } |
263 | | -} |
264 | | - |
265 | 248 | /// Extension trait for [`Once`] to help avoid deadlocking when using a [`Once`] when attached to a |
266 | 249 | /// Python thread. |
267 | 250 | pub trait OnceExt: Sealed { |
@@ -735,7 +718,10 @@ mod rwlock_ext_sealed { |
735 | 718 | mod tests { |
736 | 719 | use super::*; |
737 | 720 |
|
738 | | - use crate::types::{PyAnyMethods, PyDict, PyDictMethods}; |
| 721 | + use crate::{ |
| 722 | + types::{PyAnyMethods, PyDict, PyDictMethods}, |
| 723 | + Py, |
| 724 | + }; |
739 | 725 | #[cfg(not(target_arch = "wasm32"))] |
740 | 726 | #[cfg(feature = "macros")] |
741 | 727 | use std::sync::atomic::{AtomicBool, Ordering}; |
|
0 commit comments