Skip to content

Commit

Permalink
Fix 3.13 locals() compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
qstokkink committed Nov 18, 2024
1 parent 432c238 commit d78dbac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
13 changes: 7 additions & 6 deletions ipv8/messaging/lazy_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
20 changes: 12 additions & 8 deletions ipv8/test/messaging/test_lazy_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -520,9 +521,9 @@ def test_plain_mismatch_list(self) -> None:
matched = True
case _:
matched = False
""", '<string>', 'exec'), globals(), locals())
""", '<string>', 'exec'), globals(), local_scope)

self.assertFalse(locals()["matched"])
self.assertFalse(local_scope["matched"])

@skipUnlessPython310
def test_compiled_mismatch_list(self) -> None:
Expand All @@ -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
Expand All @@ -542,16 +544,17 @@ def test_compiled_mismatch_list(self) -> None:
matched = True
case _:
matched = False
""", '<string>', 'exec'), globals(), locals())
""", '<string>', 'exec'), globals(), local_scope)

self.assertFalse(locals()["matched"])
self.assertFalse(local_scope["matched"])

@skipUnlessPython310
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
Expand All @@ -561,16 +564,17 @@ def test_plain_match_pattern(self) -> None:
matched = True
case _:
matched = False
""", '<string>', 'exec'), globals(), locals())
""", '<string>', 'exec'), globals(), local_scope)

self.assertTrue(locals()["matched"])
self.assertTrue(local_scope["matched"])

@skipUnlessPython310
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
Expand All @@ -580,6 +584,6 @@ def test_compiled_match_pattern(self) -> None:
matched = True
case _:
matched = False
""", '<string>', 'exec'), globals(), locals())
""", '<string>', 'exec'), globals(), local_scope)

self.assertTrue(locals()["matched"])
self.assertTrue(local_scope["matched"])

0 comments on commit d78dbac

Please sign in to comment.