From b0410fc20c45227756a7bbdcff65e29eb0bc4d91 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 21 Apr 2024 20:34:59 +0200 Subject: Merging upstream version 115.10.0esr. Signed-off-by: Daniel Baumann --- python/mozbuild/mozbuild/configure/__init__.py | 3 +- python/mozbuild/mozbuild/configure/lint.py | 67 +++++++++++----------- .../mozbuild/test/configure/test_configure.py | 10 +++- .../mozbuild/mozbuild/test/configure/test_lint.py | 34 +++++++---- 4 files changed, 67 insertions(+), 47 deletions(-) (limited to 'python') diff --git a/python/mozbuild/mozbuild/configure/__init__.py b/python/mozbuild/mozbuild/configure/__init__.py index f60f179d6b..0c37532b84 100644 --- a/python/mozbuild/mozbuild/configure/__init__.py +++ b/python/mozbuild/mozbuild/configure/__init__.py @@ -1083,8 +1083,7 @@ class ConfigureSandbox(dict): def _get_one_import(self, _from, _import, _as, glob): """Perform the given import, placing the result into the dict glob.""" if not _from and _import == "__builtin__": - glob[_as or "__builtin__"] = __builtin__ - return + raise Exception("Importing __builtin__ is forbidden") if _from == "__builtin__": _from = "six.moves.builtins" # The special `__sandbox__` module gives access to the sandbox diff --git a/python/mozbuild/mozbuild/configure/lint.py b/python/mozbuild/mozbuild/configure/lint.py index 7ea379b1ef..359b148cc6 100644 --- a/python/mozbuild/mozbuild/configure/lint.py +++ b/python/mozbuild/mozbuild/configure/lint.py @@ -4,6 +4,7 @@ import inspect import re +import sys import types from dis import Bytecode from functools import wraps @@ -23,6 +24,30 @@ from . import ( from .help import HelpFormatter +def code_replace(code, co_filename, co_name, co_firstlineno): + if sys.version_info < (3, 8): + codetype_args = [ + code.co_argcount, + code.co_kwonlyargcount, + code.co_nlocals, + code.co_stacksize, + code.co_flags, + code.co_code, + code.co_consts, + code.co_names, + code.co_varnames, + co_filename, + co_name, + co_firstlineno, + code.co_lnotab, + ] + return types.CodeType(*codetype_args) + else: + return code.replace( + co_filename=co_filename, co_name=co_name, co_firstlineno=co_firstlineno + ) + + class LintSandbox(ConfigureSandbox): def __init__(self, environ=None, argv=None, stdout=None, stderr=None): out = StringIO() @@ -68,49 +93,27 @@ class LintSandbox(ConfigureSandbox): funcname = obj.__name__ filename = obj.__code__.co_filename firstline = obj.__code__.co_firstlineno - line += firstline + line += firstline - 1 elif inspect.isframe(obj): funcname = obj.f_code.co_name filename = obj.f_code.co_filename firstline = obj.f_code.co_firstlineno - line = obj.f_lineno + line = obj.f_lineno - 1 else: # Don't know how to handle the given location, still raise the # exception. raise exception # Create a new function from the above thrower that pretends - # the `def` line is on the first line of the function given as - # argument, and the `raise` line is on the line given as argument. - - offset = line - firstline - # co_lnotab is a string where each pair of consecutive character is - # (chr(byte_increment), chr(line_increment)), mapping bytes in co_code - # to line numbers relative to co_firstlineno. - # If the offset we need to encode is larger than what fits in a 8-bit - # signed integer, we need to split it. - co_lnotab = bytes([0, 127] * (offset // 127) + [0, offset % 127]) - code = thrower.__code__ - codetype_args = [ - code.co_argcount, - code.co_kwonlyargcount, - code.co_nlocals, - code.co_stacksize, - code.co_flags, - code.co_code, - code.co_consts, - code.co_names, - code.co_varnames, - filename, - funcname, - firstline, - co_lnotab, - ] - if hasattr(code, "co_posonlyargcount"): - # co_posonlyargcount was introduced in Python 3.8. - codetype_args.insert(1, code.co_posonlyargcount) + # the `raise` line is on the line given as argument. + + code = code_replace( + thrower.__code__, + co_filename=filename, + co_name=funcname, + co_firstlineno=line, + ) - code = types.CodeType(*codetype_args) thrower = types.FunctionType( code, thrower.__globals__, diff --git a/python/mozbuild/mozbuild/test/configure/test_configure.py b/python/mozbuild/mozbuild/test/configure/test_configure.py index a5e42faae3..110e5db3a3 100644 --- a/python/mozbuild/mozbuild/test/configure/test_configure.py +++ b/python/mozbuild/mozbuild/test/configure/test_configure.py @@ -312,7 +312,9 @@ class TestConfigure(unittest.TestCase): sandbox, ) - self.assertIs(sandbox["foo"](), six.moves.builtins) + with self.assertRaises(Exception) as e: + sandbox["foo"]() + self.assertEqual(str(e.exception), "Importing __builtin__ is forbidden") exec_( textwrap.dedent( @@ -330,7 +332,7 @@ class TestConfigure(unittest.TestCase): self.assertEqual(f.name, os.devnull) f.close() - # This unlocks the sandbox + # This used to unlock the sandbox exec_( textwrap.dedent( """ @@ -343,7 +345,9 @@ class TestConfigure(unittest.TestCase): sandbox, ) - self.assertIs(sandbox["foo"](), sys) + with self.assertRaises(Exception) as e: + sandbox["foo"]() + self.assertEqual(str(e.exception), "Importing __builtin__ is forbidden") exec_( textwrap.dedent( diff --git a/python/mozbuild/mozbuild/test/configure/test_lint.py b/python/mozbuild/mozbuild/test/configure/test_lint.py index 7ecac769c3..5524f3a701 100644 --- a/python/mozbuild/mozbuild/test/configure/test_lint.py +++ b/python/mozbuild/mozbuild/test/configure/test_lint.py @@ -2,9 +2,7 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. -import contextlib import os -import sys import textwrap import traceback import unittest @@ -19,6 +17,28 @@ test_data_path = mozpath.abspath(mozpath.dirname(__file__)) test_data_path = mozpath.join(test_data_path, "data") +class AssertRaisesFromLine: + def __init__(self, test_case, expected, path, line): + self.test_case = test_case + self.expected = expected + self.path = path + self.line = line + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, tb): + if exc_type is None: + raise Exception(f"{self.expected.__name__} not raised") + if not issubclass(exc_type, self.expected): + return False + self.exception = exc_value + self.test_case.assertEqual( + traceback.extract_tb(tb)[-1][:2], (self.path, self.line) + ) + return True + + class TestLint(unittest.TestCase): def lint_test(self, options=[], env={}): sandbox = LintSandbox(env, ["configure"] + options) @@ -30,15 +50,9 @@ class TestLint(unittest.TestCase): {os.path.join(test_data_path, "moz.configure"): textwrap.dedent(source)} ) - @contextlib.contextmanager def assertRaisesFromLine(self, exc_type, line): - with self.assertRaises(exc_type) as e: - yield e - - _, _, tb = sys.exc_info() - self.assertEqual( - traceback.extract_tb(tb)[-1][:2], - (mozpath.join(test_data_path, "moz.configure"), line), + return AssertRaisesFromLine( + self, exc_type, mozpath.join(test_data_path, "moz.configure"), line ) def test_configure_testcase(self): -- cgit v1.2.3