Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions ev3dev/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __del__(self):
for f in self._cache.values():
f.close()

def file_handle(self, path, binary=False):
def file_handle(self, path):
"""Manages the file handle cache and opening the files in the correct mode"""

if path not in self._cache:
Expand All @@ -65,14 +65,11 @@ def file_handle(self, path, binary=False):
w_ok = mode & stat.S_IWGRP

if r_ok and w_ok:
mode = 'a+'
mode = 'ab+'
elif w_ok:
mode = 'a'
mode = 'ab'
else:
mode = 'r'

if binary:
mode += 'b'
mode = 'rb'

f = open(path, mode, 0)
self._cache[path] = f
Expand All @@ -85,13 +82,13 @@ def read(self, path):
f = self.file_handle(path)

f.seek(0)
return f.read().strip()
return f.read().decode().strip()

def write(self, path, value):
f = self.file_handle(path)

f.seek(0)
f.write(value)
f.write(value.encode())


# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -1393,7 +1390,7 @@ def bin_data(self, fmt=None):
"float": 4
}.get(self.bin_data_format, 1) * self.num_values

f = self._attribute_cache.file_handle(abspath(self._path + '/bin_data'), binary=True)
f = self._attribute_cache.file_handle(abspath(self._path + '/bin_data'))
f.seek(0)
raw = bytearray(f.read(self._bin_data_size))

Expand Down