1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/// Create [`pyo3::types::PyDict`] from a list of key-value pairs.
///
/// Examples
/// ---------
///
/// - When you have GIL marker `py`, you can pass it and get a Bound pointer `PyResult<Bound<PyDict>>`:
///
/// ```
/// use pyo3::{Python, Bound, types::{PyDict, PyDictMethods, PyAnyMethods}};
/// use serde_pyobject::pydict;
///
/// Python::attach(|py| {
/// let dict: Bound<PyDict> = pydict! {
/// py,
/// "foo" => 42,
/// "bar" => "baz"
/// }
/// .unwrap();
///
/// assert_eq!(
/// dict.get_item("foo")
/// .unwrap()
/// .unwrap()
/// .extract::<i32>()
/// .unwrap(),
/// 42
/// );
/// assert_eq!(
/// dict.get_item("bar")
/// .unwrap()
/// .unwrap()
/// .extract::<String>()
/// .unwrap(),
/// "baz",
/// );
/// })
/// ```
///
/// - When you don't have GIL marker, you get a `PyResult<Py<PyDict>>`:
///
/// ```
/// use pyo3::{Python, Py, types::{PyDict, PyDictMethods, PyAnyMethods}};
/// use serde_pyobject::pydict;
///
/// let dict: Py<PyDict> = pydict! {
/// "foo" => 42,
/// "bar" => "baz"
/// }
/// .unwrap();
///
/// Python::attach(|py| {
/// let dict = dict.into_bound(py);
/// assert_eq!(
/// dict.get_item("foo")
/// .unwrap()
/// .unwrap()
/// .extract::<i32>()
/// .unwrap(),
/// 42
/// );
/// assert_eq!(
/// dict.get_item("bar")
/// .unwrap()
/// .unwrap()
/// .extract::<String>()
/// .unwrap(),
/// "baz",
/// );
/// })
/// ```
///
/// Create [`pyo3::types::PyList`] from a list of values.
///
/// Examples
/// --------
///
/// - When you have GIL marker `py`, you can pass it and get a reference `PyResult<&PyList>`:
///
/// ```
/// use pyo3::{Python, types::{PyList, PyListMethods, PyAnyMethods}};
/// use serde_pyobject::pylist;
///
/// Python::attach(|py| {
/// let list = pylist![py; 1, "two"].unwrap();
/// assert_eq!(list.len(), 2);
/// assert_eq!(list.get_item(0).unwrap().extract::<i32>().unwrap(), 1);
/// assert_eq!(list.get_item(1).unwrap().extract::<String>().unwrap(), "two");
/// })
/// ```
///
/// - When you don't have GIL marker, you get a `PyResult<Py<PyList>>`:
///
/// ```
/// use pyo3::{Python, Py, types::{PyList, PyListMethods, PyAnyMethods}};
/// use serde_pyobject::pylist;
///
/// let list: Py<PyList> = pylist![1, "two"].unwrap();
///
/// Python::attach(|py| {
/// let list = list.into_bound(py);
/// assert_eq!(list.len(), 2);
/// assert_eq!(list.get_item(0).unwrap().extract::<i32>().unwrap(), 1);
/// assert_eq!(list.get_item(1).unwrap().extract::<String>().unwrap(), "two");
/// });
/// ```
///