Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: store multidata/multisave enums by value on py3.11 #1783

Merged
merged 1 commit into from
May 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Core: store multidata/multisave enums by value on py3.11
This is what py3.10 did and should be better for AP than the new default
black-sliver committed May 1, 2023

Verified

This commit was signed with the committer’s verified signature.
robert-milan Robert Milan
commit 3e121a3979c1105d226838b704fd63f68ac3eab4
8 changes: 4 additions & 4 deletions NetUtils.py
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@

import websockets

from Utils import Version
from Utils import ByValue, Version


class JSONMessagePart(typing.TypedDict, total=False):
@@ -20,15 +20,15 @@ class JSONMessagePart(typing.TypedDict, total=False):
flags: int


class ClientStatus(enum.IntEnum):
class ClientStatus(ByValue, enum.IntEnum):
CLIENT_UNKNOWN = 0
CLIENT_CONNECTED = 5
CLIENT_READY = 10
CLIENT_PLAYING = 20
CLIENT_GOAL = 30


class SlotType(enum.IntFlag):
class SlotType(ByValue, enum.IntFlag):
spectator = 0b00
player = 0b01
group = 0b10
@@ -39,7 +39,7 @@ def always_goal(self) -> bool:
return self.value != 0b01


class Permission(enum.IntFlag):
class Permission(ByValue, enum.IntFlag):
disabled = 0b000 # 0, completely disables access
enabled = 0b001 # 1, allows manual use
goal = 0b010 # 2, allows manual use after goal completion
9 changes: 9 additions & 0 deletions Utils.py
Original file line number Diff line number Diff line change
@@ -508,6 +508,15 @@ def restricted_loads(s):
return RestrictedUnpickler(io.BytesIO(s)).load()


class ByValue:
"""
Mixin for enums to pickle value instead of name (restores pre-3.11 behavior). Use as left-most parent.
See https://github.com/python/cpython/pull/26658 for why this exists.
"""
def __reduce_ex__(self, prot):
return self.__class__, (self._value_, )


class KeyedDefaultDict(collections.defaultdict):
"""defaultdict variant that uses the missing key as argument to default_factory"""
default_factory: typing.Callable[[typing.Any], typing.Any]