From ab3ec6cc735470caeaa34e80038ea329af380bff Mon Sep 17 00:00:00 2001 From: looooo Date: Fri, 18 May 2018 17:06:04 +0200 Subject: [PATCH] py2 fix: open doesn't accept encoding attribute --- src/Mod/Test/TestPythonSyntax.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Mod/Test/TestPythonSyntax.py b/src/Mod/Test/TestPythonSyntax.py index 0860f89542..f529c5e78b 100644 --- a/src/Mod/Test/TestPythonSyntax.py +++ b/src/Mod/Test/TestPythonSyntax.py @@ -4,28 +4,30 @@ import ast import unittest import FreeCAD as App + def test_python_syntax(rootdir, whitelist=None): whitelist = whitelist or [] log = [] for sub_dir, dirs, files in os.walk(rootdir): for fn in files: + kargs = {} + if sys.version_info.major > 3: + kargs["encoding"] = "utf-8" if (not fn in whitelist) and os.path.splitext(fn)[1] == '.py': - with open(os.path.join(sub_dir, fn), encoding='utf-8') as py_file: + with open(os.path.join(sub_dir, fn), **kargs) as py_file: try: ast.parse(py_file.read()) - except SyntaxError: - log.append(os.path.join(sub_dir, fn)) + except SyntaxError as err: + log.append(str(err).replace("", os.path.join(sub_dir, fn))) message = "\n\n" + "#" * 30 + "\n" message += "{} python files are not parseable:\n\n".format(len(log)) - for i in log: - message += i + "\n" - message += "#" * 30 + "\n\n" + for i, m in enumerate(log): + message += str(i + 1) + " " + m + "\n" if log: raise RuntimeError("there are some files not parse-able with the used python-interpreter" + message) else: return - class PythonSyntaxTestCase(unittest.TestCase): """ Test Case to test python syntax of all python files in FreeCAD @@ -38,4 +40,5 @@ class PythonSyntaxTestCase(unittest.TestCase): def testAll(self): - test_python_syntax(App.getHomePath(), self.whitelist) + mod_dir = os.path.join(App.getHomePath(), "Mod") + test_python_syntax(mod_dir, self.whitelist)