Commit a3a95d43 authored by Gavin Mak's avatar Gavin Mak Committed by LUCI CQ

Revert "Use io.open for opening files"

This reverts commit eb16430c.

Reason for revert: blocking upload of CLs, https://crbug.com/1357152#c8

Original change's description:
> 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/+/3872429
> Reviewed-by: Gavin Mak <gavinmak@google.com>
> Commit-Queue: Josip Sokcevic <sokcevic@google.com>

Bug: 1357152
Change-Id: Iec18fe8fd37f28887507d47d7b35b93ccee72375
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3872487
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Auto-Submit: Gavin Mak <gavinmak@google.com>
Commit-Queue: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
parent eb16430c
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
from __future__ import print_function from __future__ import print_function
import os as _os import os as _os
import io as _io
from warnings import warn from warnings import warn
_HERE = _os.path.dirname(_os.path.abspath(__file__)) _HERE = _os.path.dirname(_os.path.abspath(__file__))
...@@ -171,9 +170,9 @@ def CheckAuthorizedAuthor(input_api, output_api, bot_allowlist=None): ...@@ -171,9 +170,9 @@ def CheckAuthorizedAuthor(input_api, output_api, bot_allowlist=None):
input_api.PresubmitLocalPath(), 'AUTHORS') input_api.PresubmitLocalPath(), 'AUTHORS')
author_re = input_api.re.compile(r'[^#]+\s+\<(.+?)\>\s*$') author_re = input_api.re.compile(r'[^#]+\s+\<(.+?)\>\s*$')
valid_authors = [] valid_authors = []
with _io.open(authors_path, 'rb', encoding='utf-8') as fp: with open(authors_path, 'rb') as fp:
for line in fp: for line in fp:
m = author_re.match(line) m = author_re.match(line.decode('utf8'))
if m: if m:
valid_authors.append(m.group(1).lower()) valid_authors.append(m.group(1).lower())
...@@ -799,7 +798,7 @@ def GetUnitTests(input_api, ...@@ -799,7 +798,7 @@ def GetUnitTests(input_api,
assert run_on_python3 or run_on_python2, ( assert run_on_python3 or run_on_python2, (
'At least one of "run_on_python2" or "run_on_python3" must be set.') 'At least one of "run_on_python2" or "run_on_python3" must be set.')
def has_py3_shebang(test): def has_py3_shebang(test):
with _io.open(test, encoding='utf-8') as f: with open(test) as f:
maybe_shebang = f.readline() maybe_shebang = f.readline()
return maybe_shebang.startswith('#!') and 'python3' in maybe_shebang return maybe_shebang.startswith('#!') and 'python3' in maybe_shebang
...@@ -1900,7 +1899,7 @@ def CheckJsonParses(input_api, output_api, file_filter=None): ...@@ -1900,7 +1899,7 @@ def CheckJsonParses(input_api, output_api, file_filter=None):
file_filter=file_filter) file_filter=file_filter)
warnings = [] warnings = []
for f in affected_files: for f in affected_files:
with _io.open(f.AbsoluteLocalPath(), encoding='utf-8') as j: with open(f.AbsoluteLocalPath()) as j:
try: try:
json.load(j) json.load(j)
except ValueError: except ValueError:
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import functools import functools
import io
import itertools import itertools
import logging import logging
import multiprocessing import multiprocessing
...@@ -27,10 +26,12 @@ if sys.version_info.major == 2: ...@@ -27,10 +26,12 @@ if sys.version_info.major == 2:
from cStringIO import StringIO from cStringIO import StringIO
import mock import mock
import urllib2 as urllib_request import urllib2 as urllib_request
BUILTIN_OPEN = '__builtin__.open'
else: else:
from io import StringIO from io import StringIO
from unittest import mock from unittest import mock
import urllib.request as urllib_request import urllib.request as urllib_request
BUILTIN_OPEN = 'builtins.open'
_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) _ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, _ROOT) sys.path.insert(0, _ROOT)
...@@ -2986,9 +2987,9 @@ the current line as well! ...@@ -2986,9 +2987,9 @@ the current line as well!
self.AssertOwnersWorks(approvers=set(['ben@example.com']), self.AssertOwnersWorks(approvers=set(['ben@example.com']),
is_committing=False) is_committing=False)
@mock.patch('io.open', mock.mock_open()) @mock.patch(BUILTIN_OPEN, mock.mock_open())
def testCannedRunUnitTests(self): def testCannedRunUnitTests(self):
io.open().readline.return_value = '' open().readline.return_value = ''
change = presubmit.Change( change = presubmit.Change(
'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None)
input_api = self.MockInputApi(change, False) input_api = self.MockInputApi(change, False)
...@@ -3032,9 +3033,9 @@ the current line as well! ...@@ -3032,9 +3033,9 @@ the current line as well!
self.checkstdout('') self.checkstdout('')
@mock.patch('io.open', mock.mock_open()) @mock.patch(BUILTIN_OPEN, mock.mock_open())
def testCannedRunUnitTestsWithTimer(self): def testCannedRunUnitTestsWithTimer(self):
io.open().readline.return_value = '' open().readline.return_value = ''
change = presubmit.Change( change = presubmit.Change(
'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None)
input_api = self.MockInputApi(change, False) input_api = self.MockInputApi(change, False)
...@@ -3058,9 +3059,9 @@ the current line as well! ...@@ -3058,9 +3059,9 @@ the current line as well!
self.checkstdout('') self.checkstdout('')
@mock.patch('io.open', mock.mock_open()) @mock.patch(BUILTIN_OPEN, mock.mock_open())
def testCannedRunUnitTestsWithTimerTimesOut(self): def testCannedRunUnitTestsWithTimerTimesOut(self):
io.open().readline.return_value = '' open().readline.return_value = ''
change = presubmit.Change( change = presubmit.Change(
'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None)
input_api = self.MockInputApi(change, False) input_api = self.MockInputApi(change, False)
...@@ -3092,9 +3093,9 @@ the current line as well! ...@@ -3092,9 +3093,9 @@ the current line as well!
input_api.thread_pool.timeout, mock.ANY) input_api.thread_pool.timeout, mock.ANY)
timer_instance.start.assert_called_once_with() timer_instance.start.assert_called_once_with()
@mock.patch('io.open', mock.mock_open()) @mock.patch(BUILTIN_OPEN, mock.mock_open())
def testCannedRunUnitTestsPython3(self): def testCannedRunUnitTestsPython3(self):
io.open().readline.return_value = '#!/usr/bin/env python3' open().readline.return_value = '#!/usr/bin/env python3'
change = presubmit.Change( change = presubmit.Change(
'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None)
input_api = self.MockInputApi(change, False) input_api = self.MockInputApi(change, False)
...@@ -3148,9 +3149,9 @@ the current line as well! ...@@ -3148,9 +3149,9 @@ the current line as well!
self.checkstdout('') self.checkstdout('')
@mock.patch('io.open', mock.mock_open()) @mock.patch(BUILTIN_OPEN, mock.mock_open())
def testCannedRunUnitTestsDontRunOnPython2(self): def testCannedRunUnitTestsDontRunOnPython2(self):
io.open().readline.return_value = '#!/usr/bin/env python3' open().readline.return_value = '#!/usr/bin/env python3'
change = presubmit.Change( change = presubmit.Change(
'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None)
input_api = self.MockInputApi(change, False) input_api = self.MockInputApi(change, False)
...@@ -3192,9 +3193,9 @@ the current line as well! ...@@ -3192,9 +3193,9 @@ the current line as well!
self.checkstdout('') self.checkstdout('')
@mock.patch('io.open', mock.mock_open()) @mock.patch(BUILTIN_OPEN, mock.mock_open())
def testCannedRunUnitTestsDontRunOnPython3(self): def testCannedRunUnitTestsDontRunOnPython3(self):
io.open().readline.return_value = '#!/usr/bin/env python3' open().readline.return_value = '#!/usr/bin/env python3'
change = presubmit.Change( change = presubmit.Change(
'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None)
input_api = self.MockInputApi(change, False) 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