donate-cpu.py: Fix Python3 compatibility problem with f.read().decode() (#1507)

With Python3 f.read() directly returns a string object that has no
decode() function. As a workaround AttributeError exceptions during
calling the decode() function are ignored and the data read from the file
is left unchanged.
With Python2 calling the decode() function is necessary and still done.
This commit is contained in:
Sebastian 2018-12-07 06:51:07 +01:00 committed by Daniel Marjamäki
parent a68086c959
commit fe6f4193fd
1 changed files with 7 additions and 1 deletions

View File

@ -196,7 +196,13 @@ def hasInclude(path, inc):
filename = os.path.join(root, name)
try:
f = open(filename, 'rt')
filedata = f.read().decode(encoding='utf-8', errors='ignore')
filedata = f.read()
try:
# Python2 needs to decode the data first
filedata = filedata.decode(encoding='utf-8', errors='ignore')
except AttributeError:
# Python3 directly reads the data into a string object that has no decode()
pass
f.close()
if filedata.find('\n#include ' + inc) >= 0:
return True