@@ -7,8 +7,9 @@ use crate::pyutil::ClonePyRef;
7
7
use crate :: { FORCE_STRING , YamlConfigDocument } ;
8
8
use minijinja:: value:: { Object , Value , ValueKind } ;
9
9
use minijinja:: { Environment , Error , ErrorKind , State } ;
10
+ use pyo3:: IntoPyObjectExt ;
11
+ use pyo3:: prelude:: * ;
10
12
use pyo3:: types:: PyTuple ;
11
- use pyo3:: { Bound , IntoPyObject , IntoPyObjectExt , PyAny , PyObject , PyResult , Python } ;
12
13
use serde:: { Serialize , Serializer } ;
13
14
14
15
// https://github.com/rust-lang/rust/issues/70263
@@ -67,7 +68,7 @@ impl<'env> TemplateRenderer<'env> {
67
68
Ok ( Some ( result) )
68
69
}
69
70
70
- pub ( crate ) fn add_helpers ( & mut self , py : Python , helpers : Vec < PyObject > ) {
71
+ pub ( crate ) fn add_helpers ( & mut self , py : Python , helpers : Vec < Py < PyAny > > ) {
71
72
self . globals . extend ( helpers. into_iter ( ) . map ( |f| {
72
73
(
73
74
f. getattr ( py, "__name__" ) . unwrap ( ) . extract ( py) . unwrap ( ) ,
@@ -81,11 +82,11 @@ impl<'env> TemplateRenderer<'env> {
81
82
Value :: from_object ( document)
82
83
}
83
84
84
- pub fn create_helper_fn ( pyf : PyObject ) -> Box < FuncFunc > {
85
+ pub fn create_helper_fn ( pyf : Py < PyAny > ) -> Box < FuncFunc > {
85
86
Box :: new ( typed_closure ! (
86
87
( Fn ( & State , & [ Value ] ) -> Result <Value , Error > + Sync + Send + ' static ) ,
87
88
move |_state: & State , args: & [ Value ] | -> Result <Value , Error > {
88
- Python :: with_gil ( |py| {
89
+ Python :: attach ( |py| {
89
90
let pyargs = PyTuple :: new( py, args. iter( ) . cloned( ) . map( WValue ) )
90
91
. expect( "Failed to construct Python tuple" ) ;
91
92
@@ -104,7 +105,7 @@ impl<'env> TemplateRenderer<'env> {
104
105
105
106
impl Display for PyYamlConfigDocument {
106
107
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
107
- Python :: with_gil (
108
+ Python :: attach (
108
109
|py| match YamlConfigDocument :: __str__ ( self . 0 . clone_ref ( py) , py) {
109
110
Ok ( v) => write ! ( f, "{}" , v) ,
110
111
Err ( _) => write ! ( f, "YCD<?? Error during Display ??>" , ) ,
@@ -168,14 +169,14 @@ impl From<&YcdValueType> for Value {
168
169
match in_v {
169
170
YcdValueType :: Dict ( v) => {
170
171
// TODO: Not ideal
171
- Python :: with_gil ( |py| Value :: from_object ( YHashMap ( v. clone_pyref ( py) ) ) )
172
+ Python :: attach ( |py| Value :: from_object ( YHashMap ( v. clone_pyref ( py) ) ) )
172
173
}
173
174
YcdValueType :: List ( v) => v. iter ( ) . map ( |v| v. into ( ) ) . collect :: < Vec < Value > > ( ) . into ( ) ,
174
175
YcdValueType :: YString ( v) => Value :: from ( v. clone ( ) ) ,
175
176
YcdValueType :: Bool ( v) => Value :: from ( * v) ,
176
177
YcdValueType :: Int ( v) => Value :: from ( * v) ,
177
178
YcdValueType :: Float ( v) => Value :: from ( * v) ,
178
- YcdValueType :: Ycd ( v) => Python :: with_gil ( |py| Value :: from_object ( v. clone_ref ( py) ) ) ,
179
+ YcdValueType :: Ycd ( v) => Python :: attach ( |py| Value :: from_object ( v. clone_ref ( py) ) ) ,
179
180
}
180
181
}
181
182
}
@@ -208,7 +209,7 @@ impl<'py> IntoPyObject<'py> for WValue {
208
209
}
209
210
210
211
#[ derive( Debug ) ]
211
- struct VariableHelper ( PyObject ) ;
212
+ struct VariableHelper ( Py < PyAny > ) ;
212
213
213
214
impl Display for VariableHelper {
214
215
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
@@ -218,14 +219,14 @@ impl Display for VariableHelper {
218
219
219
220
impl Object for VariableHelper {
220
221
fn call ( self : & Arc < Self > , state : & State , args : & [ Value ] ) -> Result < Value , Error > {
221
- Python :: with_gil ( |py| TemplateRenderer :: create_helper_fn ( self . 0 . clone_ref ( py) ) ( state, args) )
222
+ Python :: attach ( |py| TemplateRenderer :: create_helper_fn ( self . 0 . clone_ref ( py) ) ( state, args) )
222
223
}
223
224
}
224
225
225
226
impl Object for PyYamlConfigDocument {
226
227
fn get_value ( self : & Arc < Self > , key : & Value ) -> Option < Value > {
227
228
let name = key. as_str ( ) ?;
228
- Python :: with_gil ( |py| {
229
+ Python :: attach ( |py| {
229
230
let mut bow = self . 0 . borrow ( py) ;
230
231
bow. doc . get ( name) . map ( |x| x. into ( ) ) . or_else ( || {
231
232
if bow. bound_helpers . is_empty ( ) {
@@ -250,7 +251,7 @@ impl Object for PyYamlConfigDocument {
250
251
name : & str ,
251
252
args : & [ Value ] ,
252
253
) -> Result < Value , Error > {
253
- Python :: with_gil ( |py| {
254
+ Python :: attach ( |py| {
254
255
let mut bow = self . 0 . borrow ( py) ;
255
256
if bow. bound_helpers . is_empty ( ) {
256
257
drop ( bow) ;
@@ -303,7 +304,7 @@ impl Object for YHashMap<String, YcdValueType> {
303
304
. map ( |( k, v) | Value :: from_serialize ( YHashMapItem ( k. clone ( ) , v) ) )
304
305
. collect :: < Vec < Value > > ( ) ,
305
306
) ) ,
306
- "values" => Python :: with_gil ( |py| {
307
+ "values" => Python :: attach ( |py| {
307
308
Ok ( Value :: from (
308
309
self . 0
309
310
. values ( )
0 commit comments