3333import time
3434import serial
3535
36+ def stdout_write_bytes (b ):
37+ sys .stdout .buffer .write (b )
38+
3639class PyboardError (BaseException ):
3740 pass
3841
@@ -101,7 +104,7 @@ def exec_raw(self, command, timeout=10, data_consumer=None):
101104 if isinstance (command , bytes ):
102105 command_bytes = command
103106 else :
104- command_bytes = bytes (command , encoding = 'ascii ' )
107+ command_bytes = bytes (command , encoding = 'utf8 ' )
105108
106109 # write command
107110 for i in range (0 , len (command_bytes ), 256 ):
@@ -128,19 +131,19 @@ def exec(self, command):
128131 return ret
129132
130133 def execfile (self , filename ):
131- with open (filename ) as f :
134+ with open (filename , 'rb' ) as f :
132135 pyfile = f .read ()
133136 return self .exec (pyfile )
134137
135138 def get_time (self ):
136- t = str (self .eval ('pyb.RTC().datetime()' ), encoding = 'ascii ' )[1 :- 1 ].split (', ' )
139+ t = str (self .eval ('pyb.RTC().datetime()' ), encoding = 'utf8 ' )[1 :- 1 ].split (', ' )
137140 return int (t [4 ]) * 3600 + int (t [5 ]) * 60 + int (t [6 ])
138141
139142def execfile (filename , device = '/dev/ttyACM0' ):
140143 pyb = Pyboard (device )
141144 pyb .enter_raw_repl ()
142145 output = pyb .execfile (filename )
143- print ( str ( output , encoding = 'ascii' ), end = '' )
146+ stdout_write_bytes ( output )
144147 pyb .exit_raw_repl ()
145148 pyb .close ()
146149
@@ -214,24 +217,24 @@ def main():
214217 if len (args .files ) == 0 :
215218 try :
216219 pyb = Pyboard (args .device )
217- ret , ret_err = pyb .follow (timeout = None , data_consumer = lambda d : print ( str ( d , encoding = 'ascii' ), end = '' ) )
220+ ret , ret_err = pyb .follow (timeout = None , data_consumer = stdout_write_bytes )
218221 pyb .close ()
219222 except PyboardError as er :
220223 print (er )
221224 sys .exit (1 )
222225 except KeyboardInterrupt :
223226 sys .exit (1 )
224227 if ret_err :
225- print ( str ( ret_err , encoding = 'ascii' ), end = '' )
228+ stdout_write_bytes ( ret_err )
226229 sys .exit (1 )
227230
228231 for filename in args .files :
229232 try :
230233 pyb = Pyboard (args .device )
231234 pyb .enter_raw_repl ()
232- with open (filename ) as f :
235+ with open (filename , 'rb' ) as f :
233236 pyfile = f .read ()
234- ret , ret_err = pyb .exec_raw (pyfile , timeout = None , data_consumer = lambda d : print ( str ( d , encoding = 'ascii' ), end = '' ) )
237+ ret , ret_err = pyb .exec_raw (pyfile , timeout = None , data_consumer = stdout_write_bytes )
235238 pyb .exit_raw_repl ()
236239 pyb .close ()
237240 except PyboardError as er :
@@ -240,7 +243,7 @@ def main():
240243 except KeyboardInterrupt :
241244 sys .exit (1 )
242245 if ret_err :
243- print ( str ( ret_err , encoding = 'ascii' ), end = '' )
246+ stdout_write_bytes ( ret_err )
244247 sys .exit (1 )
245248
246249if __name__ == "__main__" :
0 commit comments