Skip to content

Commit

Permalink
Add back IntoPyDict::into_py_dict
Browse files Browse the repository at this point in the history
  • Loading branch information
bschoenmaeckers committed Jul 28, 2024
1 parent efbb6cb commit b8bb1e4
Show file tree
Hide file tree
Showing 36 changed files with 99 additions and 92 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn main() -> PyResult<()> {
let sys = py.import_bound("sys")?;
let version: String = sys.getattr("version")?.extract()?;

let locals = [("os", py.import_bound("os")?)].into_py_dict_bound(py);
let locals = [("os", py.import_bound("os")?)].into_py_dict(py);
let code = "os.getenv('USER') or os.getenv('USERNAME') or 'Unknown'";
let user: String = py.eval_bound(code, None, Some(&locals))?.extract()?;

Expand Down
2 changes: 1 addition & 1 deletion guide/src/exception.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use pyo3::exceptions::PyException;
create_exception!(mymodule, CustomError, PyException);

Python::with_gil(|py| {
let ctx = [("CustomError", py.get_type_bound::<CustomError>())].into_py_dict_bound(py);
let ctx = [("CustomError", py.get_type_bound::<CustomError>())].into_py_dict(py);
pyo3::py_run!(
py,
*ctx,
Expand Down
2 changes: 1 addition & 1 deletion guide/src/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn func() -> String {
# use pyo3::wrap_pymodule;
# use pyo3::types::IntoPyDict;
# let parent_module = wrap_pymodule!(parent_module)(py);
# let ctx = [("parent_module", parent_module)].into_py_dict_bound(py);
# let ctx = [("parent_module", parent_module)].into_py_dict(py);
#
# py.run_bound("assert parent_module.child_module.func() == 'func'", None, Some(&ctx)).unwrap();
# })
Expand Down
2 changes: 1 addition & 1 deletion guide/src/python-from-rust/calling-existing-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def leaky_relu(x, slope=0.01):
let relu_result: f64 = activators.getattr("relu")?.call1((-1.0,))?.extract()?;
assert_eq!(relu_result, 0.0);

let kwargs = [("slope", 0.2)].into_py_dict_bound(py);
let kwargs = [("slope", 0.2)].into_py_dict(py);
let lrelu_result: f64 = activators
.getattr("leaky_relu")?
.call((-1.0,), Some(&kwargs))?
Expand Down
6 changes: 3 additions & 3 deletions guide/src/python-from-rust/function-calls.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ fn main() -> PyResult<()> {
.into();

// call object with PyDict
let kwargs = [(key1, val1)].into_py_dict_bound(py);
let kwargs = [(key1, val1)].into_py_dict(py);
fun.call_bound(py, (), Some(&kwargs))?;

// pass arguments as Vec
let kwargs = vec![(key1, val1), (key2, val2)];
fun.call_bound(py, (), Some(&kwargs.into_py_dict_bound(py)))?;
fun.call_bound(py, (), Some(&kwargs.into_py_dict(py)))?;

// pass arguments as HashMap
let mut kwargs = HashMap::<&str, i32>::new();
kwargs.insert(key1, 1);
fun.call_bound(py, (), Some(&kwargs.into_py_dict_bound(py)))?;
fun.call_bound(py, (), Some(&kwargs.into_py_dict(py)))?;

Ok(())
})
Expand Down
14 changes: 7 additions & 7 deletions pyo3-benches/benches/bench_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use pyo3::{prelude::*, types::PyMapping};
fn iter_dict(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict_bound(py);
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
let mut sum = 0;
b.iter(|| {
for (k, _v) in &dict {
Expand All @@ -23,14 +23,14 @@ fn iter_dict(b: &mut Bencher<'_>) {
fn dict_new(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 50_000;
b.iter_with_large_drop(|| (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict_bound(py));
b.iter_with_large_drop(|| (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py));
});
}

fn dict_get_item(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 50_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict_bound(py);
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
let mut sum = 0;
b.iter(|| {
for i in 0..LEN {
Expand All @@ -48,15 +48,15 @@ fn dict_get_item(b: &mut Bencher<'_>) {
fn extract_hashmap(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict_bound(py);
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| HashMap::<u64, u64>::extract_bound(&dict));
});
}

fn extract_btreemap(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict_bound(py);
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| BTreeMap::<u64, u64>::extract_bound(&dict));
});
}
Expand All @@ -65,15 +65,15 @@ fn extract_btreemap(b: &mut Bencher<'_>) {
fn extract_hashbrown_map(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict_bound(py);
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| hashbrown::HashMap::<u64, u64>::extract_bound(&dict));
});
}

fn mapping_from_dict(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 100_000;
let dict = &(0..LEN as u64).map(|i| (i, i * 2)).into_py_dict_bound(py);
let dict = &(0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| black_box(dict).downcast::<PyMapping>().unwrap());
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/conversions/anyhow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ mod test_anyhow {
let pyerr = PyErr::from(err);

Python::with_gil(|py| {
let locals = [("err", pyerr)].into_py_dict_bound(py);
let locals = [("err", pyerr)].into_py_dict(py);
let pyerr = py.run_bound("raise err", None, Some(&locals)).unwrap_err();
assert_eq!(pyerr.value_bound(py).to_string(), expected_contents);
})
Expand All @@ -163,7 +163,7 @@ mod test_anyhow {
let pyerr = PyErr::from(err);

Python::with_gil(|py| {
let locals = [("err", pyerr)].into_py_dict_bound(py);
let locals = [("err", pyerr)].into_py_dict(py);
let pyerr = py.run_bound("raise err", None, Some(&locals)).unwrap_err();
assert_eq!(pyerr.value_bound(py).to_string(), expected_contents);
})
Expand Down
2 changes: 1 addition & 1 deletion src/conversions/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ mod tests {
fn test_pyo3_offset_fixed_frompyobject_created_in_python(timestamp in 0..(i32::MAX as i64), timedelta in -86399i32..=86399i32) {
Python::with_gil(|py| {

let globals = [("datetime", py.import_bound("datetime").unwrap())].into_py_dict_bound(py);
let globals = [("datetime", py.import_bound("datetime").unwrap())].into_py_dict(py);
let code = format!("datetime.datetime.fromtimestamp({}).replace(tzinfo=datetime.timezone(datetime.timedelta(seconds={})))", timestamp, timedelta);
let t = py.eval_bound(&code, Some(&globals), None).unwrap();

Expand Down
4 changes: 2 additions & 2 deletions src/conversions/eyre.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ mod tests {
let pyerr = PyErr::from(err);

Python::with_gil(|py| {
let locals = [("err", pyerr)].into_py_dict_bound(py);
let locals = [("err", pyerr)].into_py_dict(py);
let pyerr = py.run_bound("raise err", None, Some(&locals)).unwrap_err();
assert_eq!(pyerr.value_bound(py).to_string(), expected_contents);
})
Expand All @@ -168,7 +168,7 @@ mod tests {
let pyerr = PyErr::from(err);

Python::with_gil(|py| {
let locals = [("err", pyerr)].into_py_dict_bound(py);
let locals = [("err", pyerr)].into_py_dict(py);
let pyerr = py.run_bound("raise err", None, Some(&locals)).unwrap_err();
assert_eq!(pyerr.value_bound(py).to_string(), expected_contents);
})
Expand Down
6 changes: 3 additions & 3 deletions src/conversions/hashbrown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ where
H: hash::BuildHasher,
{
fn to_object(&self, py: Python<'_>) -> PyObject {
IntoPyDict::into_py_dict_bound(self, py).into()
IntoPyDict::into_py_dict(self, py).into()
}
}

Expand All @@ -47,7 +47,7 @@ where
let iter = self
.into_iter()
.map(|(k, v)| (k.into_py(py), v.into_py(py)));
IntoPyDict::into_py_dict_bound(iter, py).into()
IntoPyDict::into_py_dict(iter, py).into()
}
}

Expand Down Expand Up @@ -163,7 +163,7 @@ mod tests {
let mut map = hashbrown::HashMap::<i32, i32>::new();
map.insert(1, 1);

let py_map = map.into_py_dict_bound(py);
let py_map = map.into_py_dict(py);

assert_eq!(py_map.len(), 1);
assert_eq!(
Expand Down
8 changes: 4 additions & 4 deletions src/conversions/indexmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ where
H: hash::BuildHasher,
{
fn to_object(&self, py: Python<'_>) -> PyObject {
IntoPyDict::into_py_dict_bound(self, py).into()
IntoPyDict::into_py_dict(self, py).into()
}
}

Expand All @@ -112,7 +112,7 @@ where
let iter = self
.into_iter()
.map(|(k, v)| (k.into_py(py), v.into_py(py)));
IntoPyDict::into_py_dict_bound(iter, py).into()
IntoPyDict::into_py_dict(iter, py).into()
}
}

Expand Down Expand Up @@ -192,7 +192,7 @@ mod test_indexmap {
let mut map = indexmap::IndexMap::<i32, i32>::new();
map.insert(1, 1);

let py_map = map.into_py_dict_bound(py);
let py_map = map.into_py_dict(py);

assert_eq!(py_map.len(), 1);
assert_eq!(
Expand Down Expand Up @@ -221,7 +221,7 @@ mod test_indexmap {
}
}

let py_map = map.clone().into_py_dict_bound(py);
let py_map = map.clone().into_py_dict(py);

let trip_map = py_map.extract::<indexmap::IndexMap<i32, i32>>().unwrap();

Expand Down
8 changes: 4 additions & 4 deletions src/conversions/std/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ where
H: hash::BuildHasher,
{
fn to_object(&self, py: Python<'_>) -> PyObject {
IntoPyDict::into_py_dict_bound(self, py).into()
IntoPyDict::into_py_dict(self, py).into()
}
}

Expand All @@ -26,7 +26,7 @@ where
V: ToPyObject,
{
fn to_object(&self, py: Python<'_>) -> PyObject {
IntoPyDict::into_py_dict_bound(self, py).into()
IntoPyDict::into_py_dict(self, py).into()
}
}

Expand All @@ -40,7 +40,7 @@ where
let iter = self
.into_iter()
.map(|(k, v)| (k.into_py(py), v.into_py(py)));
IntoPyDict::into_py_dict_bound(iter, py).into()
IntoPyDict::into_py_dict(iter, py).into()
}

#[cfg(feature = "experimental-inspect")]
Expand All @@ -58,7 +58,7 @@ where
let iter = self
.into_iter()
.map(|(k, v)| (k.into_py(py), v.into_py(py)));
IntoPyDict::into_py_dict_bound(iter, py).into()
IntoPyDict::into_py_dict(iter, py).into()
}

#[cfg(feature = "experimental-inspect")]
Expand Down
10 changes: 5 additions & 5 deletions src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ macro_rules! impl_exception_boilerplate_bound {
/// import_exception!(socket, gaierror);
///
/// Python::with_gil(|py| {
/// let ctx = [("gaierror", py.get_type_bound::<gaierror>())].into_py_dict_bound(py);
/// let ctx = [("gaierror", py.get_type_bound::<gaierror>())].into_py_dict(py);
/// pyo3::py_run!(py, *ctx, "import socket; assert gaierror is socket.gaierror");
/// });
///
Expand Down Expand Up @@ -874,7 +874,7 @@ mod tests {

Python::with_gil(|py| {
let error_type = py.get_type_bound::<CustomError>();
let ctx = [("CustomError", error_type)].into_py_dict_bound(py);
let ctx = [("CustomError", error_type)].into_py_dict(py);
let type_description: String = py
.eval_bound("str(CustomError)", None, Some(&ctx))
.unwrap()
Expand All @@ -897,7 +897,7 @@ mod tests {
create_exception!(mymodule.exceptions, CustomError, PyException);
Python::with_gil(|py| {
let error_type = py.get_type_bound::<CustomError>();
let ctx = [("CustomError", error_type)].into_py_dict_bound(py);
let ctx = [("CustomError", error_type)].into_py_dict(py);
let type_description: String = py
.eval_bound("str(CustomError)", None, Some(&ctx))
.unwrap()
Expand All @@ -916,7 +916,7 @@ mod tests {

Python::with_gil(|py| {
let error_type = py.get_type_bound::<CustomError>();
let ctx = [("CustomError", error_type)].into_py_dict_bound(py);
let ctx = [("CustomError", error_type)].into_py_dict(py);
let type_description: String = py
.eval_bound("str(CustomError)", None, Some(&ctx))
.unwrap()
Expand Down Expand Up @@ -949,7 +949,7 @@ mod tests {

Python::with_gil(|py| {
let error_type = py.get_type_bound::<CustomError>();
let ctx = [("CustomError", error_type)].into_py_dict_bound(py);
let ctx = [("CustomError", error_type)].into_py_dict(py);
let type_description: String = py
.eval_bound("str(CustomError)", None, Some(&ctx))
.unwrap()
Expand Down
4 changes: 2 additions & 2 deletions src/impl_/extract_argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ mod tests {

Python::with_gil(|py| {
let args = PyTuple::empty(py);
let kwargs = [("foo", 0u8)].into_py_dict_bound(py);
let kwargs = [("foo", 0u8)].into_py_dict(py);
let err = unsafe {
function_description
.extract_arguments_tuple_dict::<NoVarargs, NoVarkeywords>(
Expand Down Expand Up @@ -839,7 +839,7 @@ mod tests {

Python::with_gil(|py| {
let args = PyTuple::empty(py);
let kwargs = [(1u8, 1u8)].into_py_dict_bound(py);
let kwargs = [(1u8, 1u8)].into_py_dict(py);
let err = unsafe {
function_description
.extract_arguments_tuple_dict::<NoVarargs, NoVarkeywords>(
Expand Down
2 changes: 1 addition & 1 deletion src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1818,7 +1818,7 @@ mod tests {

assert_repr(obj.call1(py, ((('x', 1),),)).unwrap().bind(py), "{'x': 1}");
assert_repr(
obj.call_bound(py, (), Some(&[('x', 1)].into_py_dict_bound(py)))
obj.call_bound(py, (), Some(&[('x', 1)].into_py_dict(py)))
.unwrap()
.bind(py),
"{'x': 1}",
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@
//! let sys = py.import_bound("sys")?;
//! let version: String = sys.getattr("version")?.extract()?;
//!
//! let locals = [("os", py.import_bound("os")?)].into_py_dict_bound(py);
//! let locals = [("os", py.import_bound("os")?)].into_py_dict(py);
//! let code = "os.getenv('USER') or os.getenv('USERNAME') or 'Unknown'";
//! let user: String = py.eval_bound(code, None, Some(&locals))?.extract()?;
//!
Expand Down
4 changes: 2 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
/// }
///
/// Python::with_gil(|py| {
/// let locals = [("C", py.get_type_bound::<MyClass>())].into_py_dict_bound(py);
/// let locals = [("C", py.get_type_bound::<MyClass>())].into_py_dict(py);
/// pyo3::py_run!(py, *locals, "c = C()");
/// });
/// ```
Expand All @@ -99,7 +99,7 @@ macro_rules! py_run_impl {
($py:expr, $($val:ident)+, $code:expr) => {{
use $crate::types::IntoPyDict;
use $crate::ToPyObject;
let d = [$((stringify!($val), $val.to_object($py)),)+].into_py_dict_bound($py);
let d = [$((stringify!($val), $val.to_object($py)),)+].into_py_dict($py);
$crate::py_run_impl!($py, *d, $code)
}};
($py:expr, *$dict:expr, $code:expr) => {{
Expand Down
2 changes: 1 addition & 1 deletion src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ mod tests {
.unwrap();
assert_eq!(v, 1);

let d = [("foo", 13)].into_py_dict_bound(py);
let d = [("foo", 13)].into_py_dict(py);

// Inject our own global namespace
let v: i32 = py
Expand Down
4 changes: 2 additions & 2 deletions src/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod inner {
// Case1: idents & no err_msg
($py:expr, $($val:ident)+, $code:expr, $err:ident) => {{
use pyo3::types::IntoPyDict;
let d = [$((stringify!($val), $val.to_object($py)),)+].into_py_dict_bound($py);
let d = [$((stringify!($val), $val.to_object($py)),)+].into_py_dict($py);
py_expect_exception!($py, *d, $code, $err)
}};
// Case2: dict & no err_msg
Expand Down Expand Up @@ -119,7 +119,7 @@ mod inner {
f: impl FnOnce(&Bound<'py, PyList>) -> PyResult<R>,
) -> PyResult<R> {
let warnings = py.import_bound("warnings")?;
let kwargs = [("record", true)].into_py_dict_bound(py);
let kwargs = [("record", true)].into_py_dict(py);
let catch_warnings = warnings
.getattr("catch_warnings")?
.call((), Some(&kwargs))?;
Expand Down
2 changes: 1 addition & 1 deletion src/types/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1623,7 +1623,7 @@ class NonHeapNonDescriptorInt:
fn test_call_with_kwargs() {
Python::with_gil(|py| {
let list = vec![3, 6, 5, 4, 7].to_object(py);
let dict = vec![("reverse", true)].into_py_dict_bound(py);
let dict = vec![("reverse", true)].into_py_dict(py);
list.call_method_bound(py, "sort", (), Some(&dict)).unwrap();
assert_eq!(list.extract::<Vec<i32>>(py).unwrap(), vec![7, 6, 5, 4, 3]);
});
Expand Down
Loading

0 comments on commit b8bb1e4

Please sign in to comment.