blob: f766ed358777d1dbc9b9fa857c3d71e90e28e440 [file] [log] [blame]
# Copyright (c) 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Miscellaneous Python 2-3 compatibility functions.
Seven is an extension to the compatibility layer six.
It contains utilities that ease migration from Python 2
to Python 3, but aren't present in the six library.
import six
import six.moves.configparser
import socket
if six.PY3:
import builtins
SOCKET_ERRORS = (builtins.ConnectionError, socket.timeout, socket.gaierror,
SOCKET_ERRORS = (socket.error, )
def exec_file(filename, globals_, locals_):
"""exec_file compiles and runs a file with globals and locals.
exec_file does not exactly mimic all the edge cases in Python 2's
execfile function. Rather, it does only what is necessary to execute
control files in autotest and prevent compiler-wide settings like
'from __future__ import ...' from spilling into control files that
have not yet been made Python 3-compatible.
filename: path to a file
globals_: dictionary of globals
locals_: dictionary of locals
None (output of six.exec_)
with open(filename, "rb") as fh:
code_obj = compile(,
return six.exec_(code_obj, globals_, locals_)
def config_parser():
"""config_parser returns a non-strict config parser.
Unfortunately, in six configparser is not same between 2/3. For our .ini's
we do not want it to be strict (ie, error upon duplicates).
if six.PY3:
return six.moves.configparser.ConfigParser(strict=False)
return six.moves.configparser.ConfigParser()