Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to pyo3 0.21 #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "pyo3-asyncio"
description = "PyO3 utilities for Python's Asyncio library"
version = "0.20.0"
version = "0.21.0"
authors = ["Andrew J Westlake <[email protected]>"]
readme = "README.md"
keywords = ["pyo3", "python", "ffi", "async", "asyncio"]
Expand Down Expand Up @@ -116,11 +116,11 @@ futures = "0.3"
inventory = { version = "0.3", optional = true }
once_cell = "1.14"
pin-project-lite = "0.2"
pyo3 = "0.20"
pyo3 = "0.21"
pyo3-asyncio-macros = { path = "pyo3-asyncio-macros", version = "=0.20.0", optional = true }

[dev-dependencies]
pyo3 = { version = "0.20", features = ["macros"] }
pyo3 = { version = "0.21", features = ["macros"] }

[dependencies.async-std]
version = "1.12"
Expand Down
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ use pyo3::prelude::*;
#[pyo3_asyncio::async_std::main]
async fn main() -> PyResult<()> {
let fut = Python::with_gil(|py| {
let asyncio = py.import("asyncio")?;
let asyncio = py.import_bound("asyncio")?;
// convert asyncio.sleep into a Rust Future
pyo3_asyncio::async_std::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)
})?;
Expand Down Expand Up @@ -95,7 +95,7 @@ use pyo3::prelude::*;
#[pyo3_asyncio::tokio::main]
async fn main() -> PyResult<()> {
let fut = Python::with_gil(|py| {
let asyncio = py.import("asyncio")?;
let asyncio = py.import_bound("asyncio")?;
// convert asyncio.sleep into a Rust Future
pyo3_asyncio::tokio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)
})?;
Expand Down Expand Up @@ -149,7 +149,7 @@ Export an async function that makes use of `async-std`:
use pyo3::{prelude::*, wrap_pyfunction};

#[pyfunction]
fn rust_sleep(py: Python) -> PyResult<&PyAny> {
fn rust_sleep(py: Python) -> PyResult<Bound<PyAny>> {
pyo3_asyncio::async_std::future_into_py(py, async {
async_std::task::sleep(std::time::Duration::from_secs(1)).await;
Ok(())
Expand All @@ -173,7 +173,7 @@ If you want to use `tokio` instead, here's what your module should look like:
use pyo3::{prelude::*, wrap_pyfunction};

#[pyfunction]
fn rust_sleep(py: Python) -> PyResult<&PyAny> {
fn rust_sleep(py: Python) -> PyResult<Bound<PyAny>> {
pyo3_asyncio::tokio::future_into_py(py, async {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
Ok(())
Expand Down Expand Up @@ -237,7 +237,7 @@ use pyo3::prelude::*;
async fn main() -> PyResult<()> {
let future = Python::with_gil(|py| -> PyResult<_> {
// import the module containing the py_sleep function
let example = py.import("example")?;
let example = py.import_bound("example")?;

// calling the py_sleep method like a normal function
// returns a coroutine
Expand Down Expand Up @@ -289,7 +289,7 @@ async fn rust_sleep() {
}

#[pyfunction]
fn call_rust_sleep(py: Python) -> PyResult<&PyAny> {
fn call_rust_sleep(py: Python) -> PyResult<Bound<PyAny>> {
pyo3_asyncio::async_std::future_into_py(py, async move {
rust_sleep().await;
Ok(())
Expand Down Expand Up @@ -356,7 +356,7 @@ async fn main() -> PyResult<()> {
// PyO3 is initialized - Ready to go

let fut = Python::with_gil(|py| -> PyResult<_> {
let asyncio = py.import("asyncio")?;
let asyncio = py.import_bound("asyncio")?;

// convert asyncio.sleep into a Rust Future
pyo3_asyncio::async_std::into_future(
Expand Down Expand Up @@ -443,7 +443,7 @@ tokio = "1.9"
use pyo3::{prelude::*, wrap_pyfunction};

#[pyfunction]
fn rust_sleep(py: Python) -> PyResult<&PyAny> {
fn rust_sleep(py: Python) -> PyResult<Bound<PyAny>> {
pyo3_asyncio::tokio::future_into_py(py, async {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
Ok(())
Expand Down Expand Up @@ -504,7 +504,7 @@ fn main() -> PyResult<()> {
pyo3::prepare_freethreaded_python();

Python::with_gil(|py| {
let uvloop = py.import("uvloop")?;
let uvloop = py.import_bound("uvloop")?;
uvloop.call_method0("install")?;

// store a reference for the assertion
Expand All @@ -514,11 +514,11 @@ fn main() -> PyResult<()> {
// verify that we are on a uvloop.Loop
Python::with_gil(|py| -> PyResult<()> {
assert!(uvloop
.as_ref(py)
.bind(py)
.getattr("Loop")?
.downcast::<PyType>()
.unwrap()
.is_instance(pyo3_asyncio::async_std::get_current_loop(py)?)?);
.is_instance(&pyo3_asyncio::async_std::get_current_loop(py)?)?);
Ok(())
})?;

Expand Down Expand Up @@ -601,24 +601,24 @@ To make things a bit easier, I decided to keep most of the old API alongside the
pyo3::prepare_freethreaded_python();

Python::with_gil(|py| {
let asyncio = py.import("asyncio")?;
let asyncio = py.import_bound("asyncio")?;

let event_loop = asyncio.call_method0("new_event_loop")?;
asyncio.call_method1("set_event_loop", (event_loop,))?;
asyncio.call_method1("set_event_loop", (&event_loop,))?;

let event_loop_hdl = PyObject::from(event_loop);
let event_loop_hdl = PyObject::from(event_loop.clone());

pyo3_asyncio::tokio::get_runtime().spawn(async move {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;

// Stop the event loop manually
Python::with_gil(|py| {
event_loop_hdl
.as_ref(py)
.bind(py)
.call_method1(
"call_soon_threadsafe",
(event_loop_hdl
.as_ref(py)
.bind(py)
.getattr("stop")
.unwrap(),),
)
Expand Down
2 changes: 1 addition & 1 deletion examples/async_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pyo3::prelude::*;
#[pyo3_asyncio::async_std::main]
async fn main() -> PyResult<()> {
let fut = Python::with_gil(|py| {
let asyncio = py.import("asyncio")?;
let asyncio = py.import_bound("asyncio")?;

// convert asyncio.sleep into a Rust Future
pyo3_asyncio::async_std::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)
Expand Down
2 changes: 1 addition & 1 deletion examples/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pyo3::prelude::*;
#[pyo3_asyncio::tokio::main]
async fn main() -> PyResult<()> {
let fut = Python::with_gil(|py| {
let asyncio = py.import("asyncio")?;
let asyncio = py.import_bound("asyncio")?;

// convert asyncio.sleep into a Rust Future
pyo3_asyncio::tokio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)
Expand Down
2 changes: 1 addition & 1 deletion examples/tokio_current_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pyo3::prelude::*;
#[pyo3_asyncio::tokio::main(flavor = "current_thread")]
async fn main() -> PyResult<()> {
let fut = Python::with_gil(|py| {
let asyncio = py.import("asyncio")?;
let asyncio = py.import_bound("asyncio")?;

// convert asyncio.sleep into a Rust Future
pyo3_asyncio::tokio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)
Expand Down
2 changes: 1 addition & 1 deletion examples/tokio_multi_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pyo3::prelude::*;
#[pyo3_asyncio::tokio::main(flavor = "multi_thread", worker_threads = 10)]
async fn main() -> PyResult<()> {
let fut = Python::with_gil(|py| {
let asyncio = py.import("asyncio")?;
let asyncio = py.import_bound("asyncio")?;

// convert asyncio.sleep into a Rust Future
pyo3_asyncio::tokio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)
Expand Down
12 changes: 6 additions & 6 deletions pytests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ async def sleep_for_1s(sleep_for):
pub(super) async fn test_into_future(event_loop: PyObject) -> PyResult<()> {
let fut = Python::with_gil(|py| {
let test_mod =
PyModule::from_code(py, TEST_MOD, "test_rust_coroutine/test_mod.py", "test_mod")?;
PyModule::from_code_bound(py, TEST_MOD, "test_rust_coroutine/test_mod.py", "test_mod")?;

pyo3_asyncio::into_future_with_locals(
&TaskLocals::new(event_loop.as_ref(py)),
&TaskLocals::new(event_loop.into_bound(py)),
test_mod.call_method1("py_sleep", (1.into_py(py),))?,
)
})?;
Expand All @@ -36,19 +36,19 @@ pub(super) fn test_blocking_sleep() -> PyResult<()> {

pub(super) async fn test_other_awaitables(event_loop: PyObject) -> PyResult<()> {
let fut = Python::with_gil(|py| {
let functools = py.import("functools")?;
let time = py.import("time")?;
let functools = py.import_bound("functools")?;
let time = py.import_bound("time")?;

// spawn a blocking sleep in the threadpool executor - returns a task, not a coroutine
let task = event_loop.as_ref(py).call_method1(
let task = event_loop.bind(py).call_method1(
"run_in_executor",
(
py.None(),
functools.call_method1("partial", (time.getattr("sleep")?, 1))?,
),
)?;

pyo3_asyncio::into_future_with_locals(&TaskLocals::new(event_loop.as_ref(py)), task)
pyo3_asyncio::into_future_with_locals(&TaskLocals::new(event_loop.into_bound(py)), task)
})?;

fut.await?;
Expand Down
Loading