From 33704bc7172ca602128767756fa49e09684a06fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=A1zquez=20Acosta?= Date: Thu, 26 Apr 2018 03:14:29 -0400 Subject: [PATCH] [FIX] web: Explicitly decode the JS files in tests. When in Python 3, then function `open` defaults to open the file in text mode with and the encoding (if not passed) is taken from `locale.getpreferredencoding`. Using `getpreferredencoding` is not necessarily a good practice. Because files might no be encoded that way. When testing Odoo not in runbot (which has LANG set to C.UTF-8) you may get: Traceback (most recent call last): File "/home/gitlab-runner/src/odoo11/addons/web/tests/test_js.py", line 24, in test_check_suite self._check_only_call('web.qunit_suite') File "/home/gitlab-runner/src/odoo11/addons/web/tests/test_js.py", line 37, in _check_only_call if RE_ONLY.search(fp.read()): File "/home/gitlab-runner/virtualenvs/odoo11-MlnlRiTaP2/lib/python3.5/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4757: ordinal not in range(128) The proposed solution is to read the file in binary mode, and decode to unicode/str ourselves using utf-8. We don't use the encoding argument of `open`, so that the code works in both Python 3 and Python 2. PR #24434 --- addons/web/tests/test_js.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addons/web/tests/test_js.py b/addons/web/tests/test_js.py index ab0eefb360b..81927bb49c5 100644 --- a/addons/web/tests/test_js.py +++ b/addons/web/tests/test_js.py @@ -33,6 +33,6 @@ class WebSuite(odoo.tests.HttpCase): filename = asset['filename'] if not filename or asset['atype'] != 'text/javascript': continue - with open(filename, 'r') as fp: - if RE_ONLY.search(fp.read()): + with open(filename, 'rb') as fp: + if RE_ONLY.search(fp.read().decode('utf-8')): self.fail("`QUnit.only()` used in file %r" % asset['url'])