Commit eb16430c authored by Josip Sokcevic's avatar Josip Sokcevic Committed by LUCI CQ

Use io.open for opening files

Use io.open which is consistent in py2 and py3.

Bug: 1357152
Change-Id: I49a3df503026bc6918362a9b5600f57111111111
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3872429Reviewed-by: 's avatarGavin Mak <gavinmak@google.com>
Commit-Queue: Josip Sokcevic <sokcevic@google.com>
parent 38d669fe
......@@ -7,6 +7,7 @@
from __future__ import print_function
import os as _os
import io as _io
from warnings import warn
_HERE = _os.path.dirname(_os.path.abspath(__file__))
......@@ -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, 'rb', 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:
......
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment