@@ -393,6 +393,9 @@ impl Python<'_> {
393393 /// - If the [`auto-initialize`] feature is not enabled and the Python interpreter is not
394394 /// initialized.
395395 ///
396+ /// To avoid possible initialization or panics if calling in a context where the Python
397+ /// interpreter might be unavailable, consider using [`Python::try_attach`].
398+ ///
396399 /// # Examples
397400 ///
398401 /// ```
@@ -419,15 +422,20 @@ impl Python<'_> {
419422 f ( guard. python ( ) )
420423 }
421424
422- /// Variant of [`Python::attach`] which will do no work if the interpreter is in a
423- /// state where it cannot be attached to:
425+ /// Variant of [`Python::attach`] which will return without attaching to the Python
426+ /// interpreter if the interpreter is in a state where it cannot be attached to:
424427 /// - in the middle of GC traversal
425428 /// - not initialized
429+ ///
430+ /// Note that due to the nature of the underlying Python APIs used to implement this,
431+ /// the behavior is currently provided on a best-effort basis; it is expected that a
432+ /// future CPython version will introduce APIs which guarantee this behaviour. This
433+ /// function is still recommended for use in the meanwhile as it provides the best
434+ /// possible behaviour and should transparently change to an optimal implementation
435+ /// once such APIs are available.
426436 #[ inline]
427437 #[ track_caller]
428- #[ cfg( any( not( Py_LIMITED_API ) , Py_3_11 , test) ) ] // only used in buffer.rs for now, allow in test cfg for simplicity
429- // TODO: make this API public?
430- pub ( crate ) fn try_attach < F , R > ( f : F ) -> Option < R >
438+ pub fn try_attach < F , R > ( f : F ) -> Option < R >
431439 where
432440 F : for < ' py > FnOnce ( Python < ' py > ) -> R ,
433441 {
0 commit comments