22
33use pyo3:: prelude:: * ;
44use pyo3:: types:: { PyDict , PyList } ;
5+ use pyo3:: IntoPyObject ;
56
67use super :: tokenizer:: Loc ;
78
@@ -15,12 +16,13 @@ pub enum Primitive {
1516}
1617
1718impl Primitive {
18- pub fn to_python ( & self , py : Python < ' _ > ) -> PyObject {
19+ pub fn to_python ( & self , py : Python < ' _ > ) -> PyResult < PyObject > {
1920 match self {
20- Primitive :: Int ( i) => i. into_py ( py) ,
21- Primitive :: Float ( f) => f. into_py ( py) ,
22- Primitive :: String ( s) => s. into_py ( py) ,
23- Primitive :: Bool ( b) => b. into_py ( py) ,
21+ Primitive :: Int ( i) => Ok ( i. into_pyobject ( py) ?. into_any ( ) . unbind ( ) ) ,
22+ Primitive :: Float ( f) => Ok ( f. into_pyobject ( py) ?. into_any ( ) . unbind ( ) ) ,
23+ Primitive :: String ( s) => Ok ( s. into_pyobject ( py) ?. into_any ( ) . unbind ( ) ) ,
24+ // Bool returns a Borrowed reference, need to convert to owned
25+ Primitive :: Bool ( b) => Ok ( b. into_pyobject ( py) ?. to_owned ( ) . into_any ( ) . unbind ( ) ) ,
2426 }
2527 }
2628}
@@ -90,7 +92,7 @@ impl CommandNode {
9092
9193 // Create the sub ScalarNode if present
9294 let sub_node: PyObject = if let Some ( ref sub) = self . sub {
93- scalar_node_class. call1 ( ( self . loc , sub) ) ?. into_py ( py )
95+ scalar_node_class. call1 ( ( self . loc , sub) ) ?. unbind ( )
9496 } else {
9597 py. None ( )
9698 } ;
@@ -99,8 +101,8 @@ impl CommandNode {
99101 let args_list = PyList :: empty ( py) ;
100102 for arg in & self . args {
101103 let value: PyObject = if let Some ( ref v) = arg. value {
102- let scalar = scalar_node_class. call1 ( ( arg. loc , v. to_python ( py) ) ) ?;
103- scalar. into_py ( py )
104+ let scalar = scalar_node_class. call1 ( ( arg. loc , v. to_python ( py) ? ) ) ?;
105+ scalar. unbind ( )
104106 } else {
105107 py. None ( )
106108 } ;
@@ -114,14 +116,14 @@ impl CommandNode {
114116 let value: PyObject = match series {
115117 SeriesArgumentNode :: Column ( loc, name) => {
116118 let scalar = scalar_node_class. call1 ( ( * loc, name) ) ?;
117- series_argument_node_class. call1 ( ( * loc, scalar) ) ?. into_py ( py )
119+ series_argument_node_class. call1 ( ( * loc, scalar) ) ?. unbind ( )
118120 }
119121 SeriesArgumentNode :: Directive ( loc, expr) => {
120122 let directive = expr. to_python ( py, commands) ?;
121- series_argument_node_class. call1 ( ( * loc, directive) ) ?. into_py ( py )
123+ series_argument_node_class. call1 ( ( * loc, directive) ) ?. unbind ( )
122124 }
123125 SeriesArgumentNode :: Empty ( loc) => {
124- series_argument_node_class. call1 ( ( * loc, py. None ( ) ) ) ?. into_py ( py )
126+ series_argument_node_class. call1 ( ( * loc, py. None ( ) ) ) ?. unbind ( )
125127 }
126128 } ;
127129 series_list. append ( value) ?;
@@ -149,7 +151,7 @@ impl CommandNode {
149151 let context = context_class. call1 ( ( input_str, cache, commands) ) ?;
150152
151153 let result = node. call_method1 ( "create" , ( context, ) ) ?;
152- Ok ( result. into_py ( py ) )
154+ Ok ( result. unbind ( ) )
153155 }
154156}
155157
@@ -207,7 +209,7 @@ impl ExpressionNode {
207209 pub fn to_python ( & self , py : Python < ' _ > , commands : & Bound < ' _ , PyDict > ) -> PyResult < PyObject > {
208210 match self {
209211 ExpressionNode :: Scalar ( scalar) => {
210- Ok ( scalar. value . to_python ( py) )
212+ scalar. value . to_python ( py)
211213 }
212214 ExpressionNode :: Command ( cmd) => {
213215 cmd. to_python ( py, commands)
@@ -236,7 +238,7 @@ impl ExpressionNode {
236238 // Create context and call create()
237239 let context = create_context ( py, commands, & format ! ( "{}" , self ) ) ?;
238240 let result = node. call_method1 ( "create" , ( context, ) ) ?;
239- Ok ( result. into_py ( py ) )
241+ Ok ( result. unbind ( ) )
240242 }
241243 ExpressionNode :: Unary { loc, operator, expression } => {
242244 let node_module = py. import ( "stock_pandas.directive.node" ) ?;
@@ -261,7 +263,7 @@ impl ExpressionNode {
261263 // Create context and call create()
262264 let context = create_context ( py, commands, & format ! ( "{}" , self ) ) ?;
263265 let result = node. call_method1 ( "create" , ( context, ) ) ?;
264- Ok ( result. into_py ( py ) )
266+ Ok ( result. unbind ( ) )
265267 }
266268 }
267269 }
@@ -282,7 +284,7 @@ impl std::fmt::Display for ExpressionNode {
282284 }
283285}
284286
285- fn get_operator_formula ( py : Python < ' _ > , op_module : & Bound < ' _ , pyo3:: types:: PyModule > , name : & str ) -> PyResult < PyObject > {
287+ fn get_operator_formula ( _py : Python < ' _ > , op_module : & Bound < ' _ , pyo3:: types:: PyModule > , name : & str ) -> PyResult < PyObject > {
286288 // Map operator names to their formula functions
287289 let operators = [
288290 ( "MULTIPLICATION_OPERATORS" , & [ "*" , "/" ] [ ..] ) ,
@@ -304,7 +306,7 @@ fn get_operator_formula(py: Python<'_>, op_module: &Bound<'_, pyo3::types::PyMod
304306 if !entry. is_none ( ) {
305307 // Entry is (formula, priority) tuple
306308 let formula = entry. get_item ( 0 ) ?;
307- return Ok ( formula. into_py ( py ) ) ;
309+ return Ok ( formula. unbind ( ) ) ;
308310 }
309311 }
310312 Err ( _) => continue ,
@@ -318,13 +320,13 @@ fn get_operator_formula(py: Python<'_>, op_module: &Bound<'_, pyo3::types::PyMod
318320 ) ) )
319321}
320322
321- fn get_unary_operator_formula ( py : Python < ' _ > , op_module : & Bound < ' _ , pyo3:: types:: PyModule > , name : & str ) -> PyResult < PyObject > {
323+ fn get_unary_operator_formula ( _py : Python < ' _ > , op_module : & Bound < ' _ , pyo3:: types:: PyModule > , name : & str ) -> PyResult < PyObject > {
322324 let dict = op_module. getattr ( "UNARY_OPERATORS" ) ?;
323325 match dict. call_method1 ( "get" , ( name, ) ) {
324326 Ok ( entry) => {
325327 if !entry. is_none ( ) {
326328 let formula = entry. get_item ( 0 ) ?;
327- return Ok ( formula. into_py ( py ) ) ;
329+ return Ok ( formula. unbind ( ) ) ;
328330 }
329331 }
330332 Err ( _) => { }
@@ -345,5 +347,5 @@ fn create_context(py: Python<'_>, commands: &Bound<'_, PyDict>, input: &str) ->
345347 let cache = cache_class. call0 ( ) ?;
346348
347349 let context = context_class. call1 ( ( input, cache, commands) ) ?;
348- Ok ( context. into_py ( py ) )
350+ Ok ( context. unbind ( ) )
349351}
0 commit comments