File tree Expand file tree Collapse file tree 1 file changed +9
-5
lines changed
Expand file tree Collapse file tree 1 file changed +9
-5
lines changed Original file line number Diff line number Diff line change @@ -104,13 +104,17 @@ pub fn print(s: String) {
104104#[ cfg( feature = "python-bindings" ) ]
105105pub fn print ( s : String ) {
106106 use pyo3:: prelude:: * ;
107- use pyo3:: types:: { PyDict , PyDictMethods } ;
108- let _ = Python :: attach ( |py| -> PyResult < ( ) > {
109- let locals = PyDict :: new ( py) ;
110- locals. set_item ( "s" , s) ?;
111- py. run ( pyo3:: ffi:: c_str!( "import sys\n print(s, end='')\n sys.stdout.flush()" ) , None , Some ( & locals) ) ?;
107+ let result = Python :: attach ( |py| -> PyResult < ( ) > {
108+ let stdout = py. import ( "sys" ) ?. getattr ( "stdout" ) ?;
109+ stdout. call_method1 ( "write" , ( s, ) ) ?;
110+ stdout. call_method0 ( "flush" ) ?;
112111 Ok ( ( ) )
113112 } ) ;
113+ if cfg ! ( debug_assertions) {
114+ if let Err ( e) = result {
115+ eprintln ! ( "Python print failed: {e}" ) ;
116+ }
117+ }
114118}
115119
116120/// Prompts the user and returns the response, if any.
You can’t perform that action at this time.
0 commit comments