diff --git a/ipv8/messaging/lazy_payload.py b/ipv8/messaging/lazy_payload.py index bd417ce95..515ed7678 100644 --- a/ipv8/messaging/lazy_payload.py +++ b/ipv8/messaging/lazy_payload.py @@ -230,22 +230,23 @@ def vp_compile(vp_definition: type[T]) -> type[T]: """ # We use ``exec`` purposefully here, disable the pylint warning: # ruff: noqa: B010, S102 + local_scope = locals() # Load the function definitions into the local scope. exec(_compile_init(vp_definition.names, { k: v.default for k, v in inspect.signature(vp_definition.__init__).parameters.items() if v.default is not inspect.Parameter.empty - }), globals(), locals()) - exec(_compile_from_unpack_list(vp_definition, vp_definition.names), globals(), locals()) - exec(_compile_to_pack_list(vp_definition, vp_definition.format_list, vp_definition.names), globals(), locals()) + }), globals(), local_scope) + exec(_compile_from_unpack_list(vp_definition, vp_definition.names), globals(), local_scope) + exec(_compile_to_pack_list(vp_definition, vp_definition.format_list, vp_definition.names), globals(), local_scope) # Rewrite the class methods from the locally loaded overwrites. # from_unpack_list is a classmethod, so we need to scope it properly. - setattr(vp_definition, '__init__', locals()['__init__']) + setattr(vp_definition, '__init__', local_scope['__init__']) setattr(vp_definition, '__match_args__', tuple(vp_definition.names)) - setattr(vp_definition, 'from_unpack_list', types.MethodType(locals()['from_unpack_list'], vp_definition)) - setattr(vp_definition, 'to_pack_list', locals()['to_pack_list']) + setattr(vp_definition, 'from_unpack_list', types.MethodType(local_scope['from_unpack_list'], vp_definition)) + setattr(vp_definition, 'to_pack_list', local_scope['to_pack_list']) return vp_definition diff --git a/ipv8/test/messaging/test_lazy_payload.py b/ipv8/test/messaging/test_lazy_payload.py index c677b6738..8156dd276 100644 --- a/ipv8/test/messaging/test_lazy_payload.py +++ b/ipv8/test/messaging/test_lazy_payload.py @@ -511,6 +511,7 @@ def test_plain_mismatch_list(self) -> None: If this test fails, you probably screwed up the class-level sub-pattern. """ payload = BitsPayload(False, True, False, True, False, True, False, True) + local_scope = locals() # The following will crash all interpreters < 3.10 if not contained in a string. exec( # noqa: S102 @@ -520,9 +521,9 @@ def test_plain_mismatch_list(self) -> None: matched = True case _: matched = False -""", '', 'exec'), globals(), locals()) +""", '', 'exec'), globals(), local_scope) - self.assertFalse(locals()["matched"]) + self.assertFalse(local_scope["matched"]) @skipUnlessPython310 def test_compiled_mismatch_list(self) -> None: @@ -533,6 +534,7 @@ def test_compiled_mismatch_list(self) -> None: If this test fails, you probably screwed up the class-level sub-pattern. """ payload = CompiledBitsPayload(False, True, False, True, False, True, False, True) + local_scope = locals() # The following will crash all interpreters < 3.10 if not contained in a string. exec( # noqa: S102 @@ -542,9 +544,9 @@ def test_compiled_mismatch_list(self) -> None: matched = True case _: matched = False -""", '', 'exec'), globals(), locals()) +""", '', 'exec'), globals(), local_scope) - self.assertFalse(locals()["matched"]) + self.assertFalse(local_scope["matched"]) @skipUnlessPython310 def test_plain_match_pattern(self) -> None: @@ -552,6 +554,7 @@ def test_plain_match_pattern(self) -> None: Check if a VariablePayload instance matches its own pattern. """ payload = BitsPayload(False, True, False, True, False, True, False, True) + local_scope = locals() # The following will crash all interpreters < 3.10 if not contained in a string. exec( # noqa: S102 @@ -561,9 +564,9 @@ def test_plain_match_pattern(self) -> None: matched = True case _: matched = False -""", '', 'exec'), globals(), locals()) +""", '', 'exec'), globals(), local_scope) - self.assertTrue(locals()["matched"]) + self.assertTrue(local_scope["matched"]) @skipUnlessPython310 def test_compiled_match_pattern(self) -> None: @@ -571,6 +574,7 @@ def test_compiled_match_pattern(self) -> None: Check if a compiled VariablePayload instance matches its own pattern. """ payload = CompiledBitsPayload(False, True, False, True, False, True, False, True) + local_scope = locals() # The following will crash all interpreters < 3.10 if not contained in a string. exec( # noqa: S102 @@ -580,6 +584,6 @@ def test_compiled_match_pattern(self) -> None: matched = True case _: matched = False -""", '', 'exec'), globals(), locals()) +""", '', 'exec'), globals(), local_scope) - self.assertTrue(locals()["matched"]) + self.assertTrue(local_scope["matched"])