Skip to content

Commit 027cfbc

Browse files
ddemidovrhempel
authored andcommitted
Restore Python3 compatibility
Open all files in binary mode, then decode/encode strings after reading/before writing to the files.
1 parent 6f835c9 commit 027cfbc

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

ev3dev/core.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def __del__(self):
5555
for f in self._cache.values():
5656
f.close()
5757

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

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

6767
if r_ok and w_ok:
68-
mode = 'a+'
68+
mode = 'ab+'
6969
elif w_ok:
70-
mode = 'a'
70+
mode = 'ab'
7171
else:
72-
mode = 'r'
73-
74-
if binary:
75-
mode += 'b'
72+
mode = 'rb'
7673

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

8784
f.seek(0)
88-
return f.read().strip()
85+
return f.read().decode().strip()
8986

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

9390
f.seek(0)
94-
f.write(value)
91+
f.write(value.encode())
9592

9693

9794
# -----------------------------------------------------------------------------
@@ -1312,7 +1309,7 @@ def bin_data(self, fmt=None):
13121309
"float": 4
13131310
}.get(self.bin_data_format, 1) * self.num_values
13141311

1315-
f = self._attribute_cache.file_handle(abspath(self._path + '/bin_data'), binary=True)
1312+
f = self._attribute_cache.file_handle(abspath(self._path + '/bin_data'))
13161313
f.seek(0)
13171314
raw = bytearray(f.read(self._bin_data_size))
13181315

0 commit comments

Comments
 (0)