From fe6f4193fdaf3e7b67b250cefd73bfee61744c8b Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 7 Dec 2018 06:51:07 +0100 Subject: [PATCH] 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. --- tools/donate-cpu.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/donate-cpu.py b/tools/donate-cpu.py index 1270a3199..9cd96a222 100644 --- a/tools/donate-cpu.py +++ b/tools/donate-cpu.py @@ -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