Skip to content

Commit f243ec8

Browse files
committed
Change file I/O to be compatible with Python3
1 parent 4b755b5 commit f243ec8

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

ev3dev/core.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
# ~autogen
3030

3131
import os
32+
import io
3233
import fnmatch
3334
import numbers
3435
import fcntl
@@ -64,8 +65,8 @@ def list_device_names(class_path, name_pattern, **kwargs):
6465
"""
6566
def matches(attribute, pattern):
6667
try:
67-
with open(attribute) as f:
68-
value = f.read().strip()
68+
with io.FileIO(attribute) as f:
69+
value = f.read().strip().decode()
6970
except:
7071
return False
7172

@@ -153,21 +154,22 @@ def _attribute_file_open( self, name ):
153154
w_ok = mode & stat.S_IWGRP
154155

155156
if r_ok and w_ok:
156-
mode = 'ab+'
157+
mode = 'r+'
157158
elif w_ok:
158-
mode = 'ab'
159+
mode = 'w'
159160
else:
160-
mode = 'rb'
161+
mode = 'r'
161162

162-
return open(path, mode, 0)
163+
return io.FileIO(path, mode)
163164

164165
def _get_attribute(self, attribute, name):
165166
"""Device attribute getter"""
166167
if self.connected:
167168
if None == attribute:
168169
attribute = self._attribute_file_open( name )
169-
attribute.seek(0)
170-
return attribute, attribute.read().strip()
170+
else:
171+
attribute.seek(0)
172+
return attribute, attribute.read().strip().decode()
171173
else:
172174
raise Exception('Device is not connected')
173175

@@ -176,8 +178,10 @@ def _set_attribute(self, attribute, name, value):
176178
if self.connected:
177179
if None == attribute:
178180
attribute = self._attribute_file_open( name )
179-
attribute.seek(0)
180-
attribute.write(value)
181+
else:
182+
attribute.seek(0)
183+
attribute.write(value.encode())
184+
attribute.flush()
181185
return attribute
182186
else:
183187
raise Exception('Device is not connected')
@@ -187,13 +191,13 @@ def get_attr_int(self, attribute, name):
187191
return attribute, int(value)
188192

189193
def set_attr_int(self, attribute, name, value):
190-
return self._set_attribute(attribute, name, '{0:d}'.format(int(value)))
194+
return self._set_attribute(attribute, name, str(int(value)))
191195

192196
def get_attr_string(self, attribute, name):
193197
return self._get_attribute(attribute, name)
194198

195199
def set_attr_string(self, attribute, name, value):
196-
return self._set_attribute(attribute, name, "{0}".format(value))
200+
return self._set_attribute(attribute, name, value)
197201

198202
def get_attr_line(self, attribute, name):
199203
return self._get_attribute(attribute, name)

0 commit comments

Comments
 (0)