2828if sys .version_info < (3 ,4 ):
2929 raise SystemError ('Must be using Python 3.4 or higher' )
3030
31+ def is_micropython ():
32+ return sys .implementation .name == "micropython"
33+
34+ def chain_exception (exception , cause ):
35+ if is_micropython ():
36+ raise exception
37+ else :
38+ raise exception from cause
39+
3140import os
3241import io
3342import fnmatch
@@ -188,7 +197,8 @@ def get_index(file):
188197 except StopIteration :
189198 self ._path = None
190199 self ._device_index = None
191- raise DeviceNotFound ("%s is not connected." % self ) from None
200+
201+ chain_exception (DeviceNotFound ("%s is not connected." % self ), None )
192202
193203 def __str__ (self ):
194204 if 'address' in self .kwargs :
@@ -206,13 +216,13 @@ def _attribute_file_open(self, name):
206216 w_ok = mode & stat .S_IWGRP
207217
208218 if r_ok and w_ok :
209- mode = 'r+'
219+ mode_str = 'r+'
210220 elif w_ok :
211- mode = 'w'
221+ mode_str = 'w'
212222 else :
213- mode = 'r'
223+ mode_str = 'r'
214224
215- return io .FileIO (path , mode )
225+ return io .FileIO (path , mode_str )
216226
217227 def _get_attribute (self , attribute , name ):
218228 """Device attribute getter"""
@@ -245,20 +255,22 @@ def _raise_friendly_access_error(self, driver_error, attribute):
245255 if not isinstance (driver_error , OSError ):
246256 raise driver_error
247257
248- if driver_error .errno == errno .EINVAL :
258+ driver_errorno = driver_error .args [0 ] if is_micropython () else driver_error .errno
259+
260+ if driver_errorno == errno .EINVAL :
249261 if attribute == "speed_sp" :
250262 try :
251263 max_speed = self .max_speed
252264 except (AttributeError , Exception ):
253- raise ValueError ("The given speed value was out of range" ) from driver_error
265+ chain_exception ( ValueError ("The given speed value was out of range" ), driver_error )
254266 else :
255- raise ValueError ("The given speed value was out of range. Max speed: +/-" + str (max_speed )) from driver_error
256- raise ValueError ("One or more arguments were out of range or invalid" ) from driver_error
257- elif driver_error . errno == errno .ENODEV or driver_error . errno == errno .ENOENT :
267+ chain_exception ( ValueError ("The given speed value was out of range. Max speed: +/-" + str (max_speed )), driver_error )
268+ chain_exception ( ValueError ("One or more arguments were out of range or invalid" ), driver_error )
269+ elif driver_errorno == errno .ENODEV or driver_errorno == errno .ENOENT :
258270 # We will assume that a file-not-found error is the result of a disconnected device
259271 # rather than a library error. If that isn't the case, at a minimum the underlying
260272 # error info will be printed for debugging.
261- raise DeviceNotFound ("%s is no longer connected" % self ) from driver_error
273+ chain_exception ( DeviceNotFound ("%s is no longer connected" % self ), driver_error )
262274 raise driver_error
263275
264276 def get_attr_int (self , attribute , name ):
0 commit comments