Bug 1575135 - Make configure sandbox open() look more like python 3's. r=nalexander

As a consequence, we can replace the encoded_open function that did the
same in an opt-in manner.

Differential Revision: https://phabricator.services.mozilla.com/D42605

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Mike Hommey 2019-08-21 04:44:43 +00:00
parent 16c5b7db29
commit 00256d3837
4 changed files with 22 additions and 14 deletions

View File

@ -5,13 +5,6 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
@imports('codecs')
@imports('sys')
@imports(_from='mozbuild.util', _import='system_encoding')
def encoded_open(path, mode):
return codecs.open(path, mode, system_encoding)
option(env='AUTOCONF', nargs=1, help='Path to autoconf 2.13')
@ -148,7 +141,7 @@ def prepare_configure(old_configure, mozconfig, autoconf, build_env, shell,
die('Failed re-creating old-configure: %s' % e.message)
cmd = [shell, old_configure]
with encoded_open('old-configure.vars', 'w') as out:
with open('old-configure.vars', 'w') as out:
log.debug('Injecting the following to old-configure:')
def inject(command):
@ -351,7 +344,7 @@ def old_configure(prepare_configure, prepare_configure_options, altered_path):
if ret:
with log.queue_debug():
if config_log:
with encoded_open(config_log.baseFilename, 'r') as fh:
with open(config_log.baseFilename, 'r') as fh:
fh.seek(log_size)
for line in fh:
log.debug(line.rstrip())
@ -368,7 +361,7 @@ def old_configure(prepare_configure, prepare_configure_options, altered_path):
'split': split,
'unique_list': unique_list,
}
with encoded_open('config.data', 'r') as fh:
with open('config.data', 'r') as fh:
code = compile(fh.read(), 'config.data', 'exec')
# Every variation of the exec() function I tried led to:
# SyntaxError: unqualified exec is not allowed in function 'main' it

View File

@ -170,7 +170,8 @@ class MockedOpen(object):
for name, content in files.items():
self.files[normcase(os.path.abspath(name))] = content
def __call__(self, name, mode='r'):
def __call__(self, name, mode='r', buffering=None):
# buffering is ignored.
absname = normcase(os.path.abspath(name))
if 'w' in mode:
file = MockedFile(self, absname)

View File

@ -4,6 +4,7 @@
from __future__ import absolute_import, print_function, unicode_literals
import codecs
import inspect
import logging
import os
@ -35,6 +36,7 @@ from mozbuild.util import (
memoized_property,
ReadOnlyDict,
ReadOnlyNamespace,
system_encoding,
)
import mozpack.path as mozpath
@ -899,9 +901,21 @@ class ConfigureSandbox(dict):
return self
# Special case for the open() builtin, because otherwise, using it
# fails with "IOError: file() constructor not accessible in
# restricted mode"
# restricted mode". We also make open() look more like python 3's,
# decoding to unicode strings unless the mode says otherwise.
if what == '__builtin__.open':
return lambda *args, **kwargs: open(*args, **kwargs)
def wrapped_open(name, mode=None, buffering=None):
args = (name,)
kwargs = {}
if buffering is not None:
kwargs['buffering'] = buffering
if mode is not None:
args += (mode,)
if 'b' in mode:
return open(*args, **kwargs)
kwargs['encoding'] = system_encoding
return codecs.open(*args, **kwargs)
return wrapped_open
# Special case os and os.environ so that os.environ is our copy of
# the environment.
if what == 'os.environ':

View File

@ -15,7 +15,7 @@ def dies_when_logging(_):
if getpreferredencoding().lower() == 'utf-8':
quote_char = '\u00B4'.encode('utf-8')
try:
with open(test_file, 'w+') as fh:
with open(test_file, 'w+b') as fh:
fh.write(quote_char)
out = check_cmd_output('cat', 'test.txt')
log.info(out)