Skip to content

Commit b90e189

Browse files
authored
use pyo3::intern! (#66)
* pyo3::intern strings * update changelog * update changelog w/ pr num & remove stupid `:wq` (classic)
1 parent da865a7 commit b90e189

File tree

3 files changed

+50
-27
lines changed

3 files changed

+50
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ To see unreleased changes, please see the CHANGELOG on the main branch.
1616
`future_into_py` functions now require future return type to be `Send`.
1717
[#60](https://github.com/PyO3/pyo3-async-runtimes/pull/60)
1818
- Change pyo3 `downcast` calls to `cast` calls [#65](https://github.com/PyO3/pyo3-async-runtimes/pull/65)
19+
- Use `pyo3::intern!` for method calls and `getattr` calls [#66](https://github.com/PyO3/pyo3-async-runtimes/pull/66)
1920

2021
## [0.26.0] - 2025-09-02
2122

src/generic.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ where
219219
},
220220
)?;
221221

222-
event_loop.call_method1("run_until_complete", (coro,))?;
222+
event_loop.call_method1(pyo3::intern!(py, "run_until_complete"), (coro,))?;
223223

224224
let result = result_rx.lock().unwrap().take().unwrap();
225225
Ok(result)
@@ -316,7 +316,7 @@ where
316316
F: Future<Output = PyResult<T>> + Send + 'static,
317317
T: Send + Sync + 'static,
318318
{
319-
let event_loop = asyncio(py)?.call_method0("new_event_loop")?;
319+
let event_loop = asyncio(py)?.call_method0(pyo3::intern!(py, "new_event_loop"))?;
320320

321321
let result = run_until_complete::<R, F, T>(&event_loop, fut);
322322

@@ -326,7 +326,10 @@ where
326326
}
327327

328328
fn cancelled(future: &Bound<PyAny>) -> PyResult<bool> {
329-
future.getattr("cancelled")?.call0()?.is_truthy()
329+
future
330+
.getattr(pyo3::intern!(future.py(), "cancelled"))?
331+
.call0()?
332+
.is_truthy()
330333
}
331334

332335
#[pyclass]
@@ -359,8 +362,14 @@ fn set_result(
359362
let none = py.None().into_bound(py);
360363

361364
let (complete, val) = match result {
362-
Ok(val) => (future.getattr("set_result")?, val.into_pyobject(py)?),
363-
Err(err) => (future.getattr("set_exception")?, err.into_bound_py_any(py)?),
365+
Ok(val) => (
366+
future.getattr(pyo3::intern!(py, "set_result"))?,
367+
val.into_pyobject(py)?,
368+
),
369+
Err(err) => (
370+
future.getattr(pyo3::intern!(py, "set_exception"))?,
371+
err.into_bound_py_any(py)?,
372+
),
364373
};
365374
call_soon_threadsafe(event_loop, &none, (CheckedCompletor, future, complete, val))?;
366375

@@ -608,7 +617,7 @@ where
608617

609618
let py_fut = create_future(locals.0.event_loop.bind(py).clone())?;
610619
py_fut.call_method1(
611-
"add_done_callback",
620+
pyo3::intern!(py, "add_done_callback"),
612621
(PyDoneCallback {
613622
cancel_tx: Some(cancel_tx),
614623
},),
@@ -1027,7 +1036,7 @@ where
10271036

10281037
let py_fut = create_future(locals.0.event_loop.clone_ref(py).into_bound(py))?;
10291038
py_fut.call_method1(
1030-
"add_done_callback",
1039+
pyo3::intern!(py, "add_done_callback"),
10311040
(PyDoneCallback {
10321041
cancel_tx: Some(cancel_tx),
10331042
},),
@@ -1345,7 +1354,8 @@ where
13451354
R: Runtime,
13461355
{
13471356
let (tx, rx) = async_channel::bounded(1);
1348-
let anext: Py<PyAny> = gen.getattr("__anext__")?.into();
1357+
let py = gen.py();
1358+
let anext: Py<PyAny> = gen.getattr(pyo3::intern!(py, "__anext__"))?.into();
13491359

13501360
R::spawn(async move {
13511361
loop {
@@ -1716,11 +1726,13 @@ where
17161726
let (tx, rx) = mpsc::channel(10);
17171727

17181728
locals.event_loop(py).call_method1(
1719-
"call_soon_threadsafe",
1729+
pyo3::intern!(py, "call_soon_threadsafe"),
17201730
(
1721-
locals.event_loop(py).getattr("create_task")?,
1731+
locals
1732+
.event_loop(py)
1733+
.getattr(pyo3::intern!(py, "create_task"))?,
17221734
glue.call_method1(
1723-
"forward",
1735+
pyo3::intern!(py, "forward"),
17241736
(
17251737
gen,
17261738
SenderGlue {

src/lib.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -408,31 +408,34 @@ static GET_RUNNING_LOOP: PyOnceLock<Py<PyAny>> = PyOnceLock::new();
408408
fn 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

417419
fn 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

421423
fn 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

468473
fn 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 {
543548
impl 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

Comments
 (0)