diff --git a/presubmit_canned_checks.py b/presubmit_canned_checks.py index 4120f45cf..0998ddd32 100644 --- a/presubmit_canned_checks.py +++ b/presubmit_canned_checks.py @@ -6,6 +6,7 @@ from __future__ import print_function +import io as _io import os as _os from warnings import warn @@ -170,9 +171,9 @@ def CheckAuthorizedAuthor(input_api, output_api, bot_allowlist=None): input_api.PresubmitLocalPath(), 'AUTHORS') author_re = input_api.re.compile(r'[^#]+\s+\<(.+?)\>\s*$') valid_authors = [] - with open(authors_path, 'rb') as fp: + with _io.open(authors_path, encoding='utf-8') as fp: for line in fp: - m = author_re.match(line.decode('utf8')) + m = author_re.match(line) if m: valid_authors.append(m.group(1).lower()) @@ -798,7 +799,7 @@ def GetUnitTests(input_api, assert run_on_python3 or run_on_python2, ( 'At least one of "run_on_python2" or "run_on_python3" must be set.') def has_py3_shebang(test): - with open(test) as f: + with _io.open(test, encoding='utf-8') as f: maybe_shebang = f.readline() return maybe_shebang.startswith('#!') and 'python3' in maybe_shebang @@ -1899,7 +1900,7 @@ def CheckJsonParses(input_api, output_api, file_filter=None): file_filter=file_filter) warnings = [] for f in affected_files: - with open(f.AbsoluteLocalPath()) as j: + with _io.open(f.AbsoluteLocalPath(), encoding='utf-8') as j: try: json.load(j) except ValueError: diff --git a/tests/presubmit_unittest.py b/tests/presubmit_unittest.py index 803e87c0c..6f4c9cf34 100755 --- a/tests/presubmit_unittest.py +++ b/tests/presubmit_unittest.py @@ -10,6 +10,7 @@ from __future__ import unicode_literals import functools +import io import itertools import logging import multiprocessing @@ -26,12 +27,10 @@ if sys.version_info.major == 2: from cStringIO import StringIO import mock import urllib2 as urllib_request - BUILTIN_OPEN = '__builtin__.open' else: from io import StringIO from unittest import mock import urllib.request as urllib_request - BUILTIN_OPEN = 'builtins.open' _ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, _ROOT) @@ -2987,9 +2986,9 @@ the current line as well! self.AssertOwnersWorks(approvers=set(['ben@example.com']), is_committing=False) - @mock.patch(BUILTIN_OPEN, mock.mock_open()) + @mock.patch('io.open', mock.mock_open()) def testCannedRunUnitTests(self): - open().readline.return_value = '' + io.open().readline.return_value = '' change = presubmit.Change( 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) input_api = self.MockInputApi(change, False) @@ -3033,9 +3032,9 @@ the current line as well! self.checkstdout('') - @mock.patch(BUILTIN_OPEN, mock.mock_open()) + @mock.patch('io.open', mock.mock_open()) def testCannedRunUnitTestsWithTimer(self): - open().readline.return_value = '' + io.open().readline.return_value = '' change = presubmit.Change( 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) input_api = self.MockInputApi(change, False) @@ -3059,9 +3058,9 @@ the current line as well! self.checkstdout('') - @mock.patch(BUILTIN_OPEN, mock.mock_open()) + @mock.patch('io.open', mock.mock_open()) def testCannedRunUnitTestsWithTimerTimesOut(self): - open().readline.return_value = '' + io.open().readline.return_value = '' change = presubmit.Change( 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) input_api = self.MockInputApi(change, False) @@ -3093,9 +3092,9 @@ the current line as well! input_api.thread_pool.timeout, mock.ANY) timer_instance.start.assert_called_once_with() - @mock.patch(BUILTIN_OPEN, mock.mock_open()) + @mock.patch('io.open', mock.mock_open()) def testCannedRunUnitTestsPython3(self): - open().readline.return_value = '#!/usr/bin/env python3' + io.open().readline.return_value = '#!/usr/bin/env python3' change = presubmit.Change( 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) input_api = self.MockInputApi(change, False) @@ -3149,9 +3148,9 @@ the current line as well! self.checkstdout('') - @mock.patch(BUILTIN_OPEN, mock.mock_open()) + @mock.patch('io.open', mock.mock_open()) def testCannedRunUnitTestsDontRunOnPython2(self): - open().readline.return_value = '#!/usr/bin/env python3' + io.open().readline.return_value = '#!/usr/bin/env python3' change = presubmit.Change( 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) input_api = self.MockInputApi(change, False) @@ -3193,9 +3192,9 @@ the current line as well! self.checkstdout('') - @mock.patch(BUILTIN_OPEN, mock.mock_open()) + @mock.patch('io.open', mock.mock_open()) def testCannedRunUnitTestsDontRunOnPython3(self): - open().readline.return_value = '#!/usr/bin/env python3' + io.open().readline.return_value = '#!/usr/bin/env python3' change = presubmit.Change( 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) input_api = self.MockInputApi(change, False)