@@ -3,7 +3,7 @@ use crate::PythonBlock;
3
3
use pyo3:: {
4
4
prelude:: * ,
5
5
types:: { PyCFunction , PyDict } ,
6
- FromPyObject , Py , PyResult , Python , IntoPyObject ,
6
+ FromPyObject , IntoPyObject , Py , PyResult , Python ,
7
7
} ;
8
8
9
9
/// An execution context for Python code.
@@ -45,21 +45,13 @@ pub struct Context {
45
45
impl Context {
46
46
/// Create a new context for running Python code.
47
47
///
48
- /// This function temporarily acquires the GIL.
49
- /// If you already have the GIL, you can use [`Context::new_with_gil`] instead.
50
- ///
51
48
/// This function panics if it fails to create the context.
52
49
#[ allow( clippy:: new_without_default) ]
53
50
pub fn new ( ) -> Self {
54
51
Python :: with_gil ( Self :: new_with_gil)
55
52
}
56
53
57
- /// Create a new context for running Python code.
58
- ///
59
- /// You must acquire the GIL to call this function.
60
- ///
61
- /// This function panics if it fails to create the context.
62
- pub fn new_with_gil ( py : Python ) -> Self {
54
+ pub ( crate ) fn new_with_gil ( py : Python ) -> Self {
63
55
match Self :: try_new ( py) {
64
56
Ok ( x) => x,
65
57
Err ( error) => {
@@ -76,25 +68,15 @@ impl Context {
76
68
}
77
69
78
70
/// Get the globals as dictionary.
79
- pub fn globals < ' p > ( & self , py : Python < ' p > ) -> & Bound < ' p , PyDict > {
80
- self . globals . bind ( py )
71
+ pub fn globals < ' p > ( & self ) -> & Py < PyDict > {
72
+ & self . globals
81
73
}
82
74
83
75
/// Retrieve a global variable from the context.
84
76
///
85
- /// This function temporarily acquires the GIL.
86
- /// If you already have the GIL, you can use [`Context::get_with_gil`] instead.
87
- ///
88
77
/// This function panics if the variable doesn't exist, or the conversion fails.
89
78
pub fn get < T : for < ' p > FromPyObject < ' p > > ( & self , name : & str ) -> T {
90
- Python :: with_gil ( |py| self . get_with_gil ( py, name) )
91
- }
92
-
93
- /// Retrieve a global variable from the context.
94
- ///
95
- /// This function panics if the variable doesn't exist, or the conversion fails.
96
- pub fn get_with_gil < ' p , T : FromPyObject < ' p > > ( & ' p self , py : Python < ' p > , name : & str ) -> T {
97
- match self . globals ( py) . get_item ( name) {
79
+ Python :: with_gil ( |py| match self . globals . bind ( py) . get_item ( name) {
98
80
Err ( _) | Ok ( None ) => panic ! ( "Python context does not contain a variable named `{}`" , name) ,
99
81
Ok ( Some ( value) ) => match FromPyObject :: extract_bound ( & value) {
100
82
Ok ( value) => value,
@@ -103,30 +85,20 @@ impl Context {
103
85
panic ! ( "Unable to convert `{}` to `{}`" , name, std:: any:: type_name:: <T >( ) ) ;
104
86
}
105
87
} ,
106
- }
88
+ } )
107
89
}
108
90
109
91
/// Set a global variable in the context.
110
92
///
111
- /// This function temporarily acquires the GIL.
112
- /// If you already have the GIL, you can use [`Context::set_with_gil`] instead.
113
- ///
114
93
/// This function panics if the conversion fails.
115
94
pub fn set < T : for < ' p > IntoPyObject < ' p > > ( & self , name : & str , value : T ) {
116
- Python :: with_gil ( |py| self . set_with_gil ( py, name, value) ) ;
117
- }
118
-
119
- /// Set a global variable in the context.
120
- ///
121
- /// This function panics if the conversion fails.
122
- pub fn set_with_gil < ' p , T : IntoPyObject < ' p > > ( & self , py : Python < ' p > , name : & str , value : T ) {
123
- match self . globals ( py) . set_item ( name, value) {
95
+ Python :: with_gil ( |py| match self . globals ( ) . bind ( py) . set_item ( name, value) {
124
96
Ok ( ( ) ) => ( ) ,
125
97
Err ( e) => {
126
98
e. print ( py) ;
127
99
panic ! ( "Unable to set `{}` from a `{}`" , name, std:: any:: type_name:: <T >( ) ) ;
128
100
}
129
- }
101
+ } )
130
102
}
131
103
132
104
/// Add a wrapped `#[pyfunction]` or `#[pymodule]` using its own `__name__`.
@@ -152,20 +124,15 @@ impl Context {
152
124
/// });
153
125
/// }
154
126
/// ```
155
- ///
156
- /// This function temporarily acquires the GIL.
157
- /// If you already have the GIL, you can use [`Context::add_wrapped_with_gil`] instead.
158
127
pub fn add_wrapped ( & self , wrapper : & impl Fn ( Python ) -> PyResult < Bound < ' _ , PyCFunction > > ) {
159
- Python :: with_gil ( |py| self . add_wrapped_with_gil ( py, wrapper) ) ;
160
- }
161
-
162
- /// Add a wrapped `#[pyfunction]` or `#[pymodule]` using its own `__name__`.
163
- ///
164
- /// See [Context::add_wrapped].
165
- pub fn add_wrapped_with_gil < ' p > ( & self , py : Python < ' p > , wrapper : & impl Fn ( Python ) -> PyResult < Bound < ' _ , PyCFunction > > ) {
166
- let obj = wrapper ( py) . unwrap ( ) ;
167
- let name = obj. getattr ( "__name__" ) . expect ( "Missing __name__" ) ;
168
- self . set_with_gil ( py, name. extract ( ) . unwrap ( ) , obj) ;
128
+ Python :: with_gil ( |py| {
129
+ let obj = wrapper ( py) . unwrap ( ) ;
130
+ let name = obj. getattr ( "__name__" ) . expect ( "Missing __name__" ) ;
131
+ if let Err ( e) = self . globals ( ) . bind ( py) . set_item ( name, obj) {
132
+ e. print ( py) ;
133
+ panic ! ( "Unable to add wrapped function" ) ;
134
+ }
135
+ } )
169
136
}
170
137
171
138
/// Run Python code using this context.
@@ -181,22 +148,13 @@ impl Context {
181
148
/// });
182
149
/// ```
183
150
///
184
- /// This function temporarily acquires the GIL.
185
- /// If you already have the GIL, you can use [`Context::run_with_gil`] instead.
186
- ///
187
151
/// This function panics if the Python code fails.
188
152
pub fn run < F : FnOnce ( & Bound < PyDict > ) > ( & self , code : PythonBlock < F > ) {
189
153
Python :: with_gil ( |py| self . run_with_gil ( py, code) ) ;
190
154
}
191
155
192
- /// Run Python code using this context.
193
- ///
194
- /// This function should be called using the `python!{}` macro, just like
195
- /// [`Context::run`].
196
- ///
197
- /// This function panics if the Python code fails.
198
- pub fn run_with_gil < F : FnOnce ( & Bound < PyDict > ) > ( & self , py : Python < ' _ > , code : PythonBlock < F > ) {
199
- ( code. set_variables ) ( self . globals ( py) ) ;
156
+ pub ( crate ) fn run_with_gil < F : FnOnce ( & Bound < PyDict > ) > ( & self , py : Python < ' _ > , code : PythonBlock < F > ) {
157
+ ( code. set_variables ) ( self . globals ( ) . bind ( py) ) ;
200
158
match run_python_code ( py, self , code. bytecode ) {
201
159
Ok ( _) => ( ) ,
202
160
Err ( e) => {
0 commit comments