@@ -408,31 +408,34 @@ static GET_RUNNING_LOOP: PyOnceLock<Py<PyAny>> = PyOnceLock::new();
408408fn ensure_future < ' p > ( py : Python < ' p > , awaitable : & Bound < ' p , PyAny > ) -> PyResult < Bound < ' p , PyAny > > {
409409 ENSURE_FUTURE
410410 . get_or_try_init ( py, || -> PyResult < Py < PyAny > > {
411- Ok ( asyncio ( py) ?. getattr ( "ensure_future" ) ?. into ( ) )
411+ Ok ( asyncio ( py) ?
412+ . getattr ( pyo3:: intern!( py, "ensure_future" ) ) ?
413+ . into ( ) )
412414 } ) ?
413415 . bind ( py)
414416 . call1 ( ( awaitable, ) )
415417}
416418
417419fn create_future ( event_loop : Bound < ' _ , PyAny > ) -> PyResult < Bound < ' _ , PyAny > > {
418- event_loop. call_method0 ( "create_future" )
420+ event_loop. call_method0 ( pyo3 :: intern! ( event_loop . py ( ) , "create_future" ) )
419421}
420422
421423fn close ( event_loop : Bound < PyAny > ) -> PyResult < ( ) > {
424+ let py = event_loop. py ( ) ;
422425 event_loop. call_method1 (
423- "run_until_complete" ,
424- ( event_loop. call_method0 ( "shutdown_asyncgens" ) ?, ) ,
426+ pyo3 :: intern! ( py , "run_until_complete" ) ,
427+ ( event_loop. call_method0 ( pyo3 :: intern! ( py , "shutdown_asyncgens" ) ) ?, ) ,
425428 ) ?;
426429
427430 // how to do this prior to 3.9?
428- if event_loop. hasattr ( "shutdown_default_executor" ) ? {
431+ if event_loop. hasattr ( pyo3 :: intern! ( py , "shutdown_default_executor" ) ) ? {
429432 event_loop. call_method1 (
430- "run_until_complete" ,
431- ( event_loop. call_method0 ( "shutdown_default_executor" ) ?, ) ,
433+ pyo3 :: intern! ( py , "run_until_complete" ) ,
434+ ( event_loop. call_method0 ( pyo3 :: intern! ( py , "shutdown_default_executor" ) ) ?, ) ,
432435 ) ?;
433436 }
434437
435- event_loop. call_method0 ( "close" ) ?;
438+ event_loop. call_method0 ( pyo3 :: intern! ( py , "close" ) ) ?;
436439
437440 Ok ( ( ) )
438441}
@@ -453,7 +456,9 @@ pub fn get_running_loop(py: Python) -> PyResult<Bound<PyAny>> {
453456 . get_or_try_init ( py, || -> PyResult < Py < PyAny > > {
454457 let asyncio = asyncio ( py) ?;
455458
456- Ok ( asyncio. getattr ( "get_running_loop" ) ?. into ( ) )
459+ Ok ( asyncio
460+ . getattr ( pyo3:: intern!( py, "get_running_loop" ) ) ?
461+ . into ( ) )
457462 } ) ?
458463 . bind ( py)
459464 . call0 ( )
@@ -466,7 +471,7 @@ fn contextvars(py: Python<'_>) -> PyResult<&Bound<'_, PyAny>> {
466471}
467472
468473fn copy_context ( py : Python ) -> PyResult < Bound < PyAny > > {
469- contextvars ( py) ?. call_method0 ( "copy_context" )
474+ contextvars ( py) ?. call_method0 ( pyo3 :: intern! ( py , "copy_context" ) )
470475}
471476
472477/// Task-local inner structure.
@@ -543,8 +548,9 @@ struct PyTaskCompleter {
543548impl PyTaskCompleter {
544549 #[ pyo3( signature = ( task) ) ]
545550 pub fn __call__ ( & mut self , task : & Bound < PyAny > ) -> PyResult < ( ) > {
546- debug_assert ! ( task. call_method0( "done" ) ?. extract( ) ?) ;
547- let result = match task. call_method0 ( "result" ) {
551+ let py = task. py ( ) ;
552+ debug_assert ! ( task. call_method0( pyo3:: intern!( py, "done" ) ) ?. extract( ) ?) ;
553+ let result = match task. call_method0 ( pyo3:: intern!( py, "result" ) ) {
548554 Ok ( val) => Ok ( val. into ( ) ) ,
549555 Err ( e) => Err ( e) ,
550556 } ;
@@ -575,7 +581,7 @@ impl PyEnsureFuture {
575581 Python :: attach ( |py| {
576582 let task = ensure_future ( py, self . awaitable . bind ( py) ) ?;
577583 let on_complete = PyTaskCompleter { tx : self . tx . take ( ) } ;
578- task. call_method1 ( "add_done_callback" , ( on_complete, ) ) ?;
584+ task. call_method1 ( pyo3 :: intern! ( py , "add_done_callback" ) , ( on_complete, ) ) ?;
579585
580586 Ok ( ( ) )
581587 } )
@@ -590,9 +596,13 @@ fn call_soon_threadsafe<'py>(
590596 let py = event_loop. py ( ) ;
591597
592598 let kwargs = PyDict :: new ( py) ;
593- kwargs. set_item ( "context" , context) ?;
599+ kwargs. set_item ( pyo3 :: intern! ( py , "context" ) , context) ?;
594600
595- event_loop. call_method ( "call_soon_threadsafe" , args, Some ( & kwargs) ) ?;
601+ event_loop. call_method (
602+ pyo3:: intern!( py, "call_soon_threadsafe" ) ,
603+ args,
604+ Some ( & kwargs) ,
605+ ) ?;
596606 Ok ( ( ) )
597607}
598608
@@ -669,7 +679,7 @@ pub fn into_future_with_locals(
669679 Ok ( item) => item,
670680 Err ( _) => Python :: attach ( |py| {
671681 Err ( PyErr :: from_value (
672- asyncio ( py) ?. call_method0 ( "CancelledError" ) ?,
682+ asyncio ( py) ?. call_method0 ( pyo3 :: intern! ( py , "CancelledError" ) ) ?,
673683 ) )
674684 } ) ,
675685 }
0 commit comments