mirror of
https://github.com/MarioSpore/Grinch-AP.git
synced 2025-10-21 20:21:32 -06:00
Core: Add restricted_dumps
helper (#5117)
* Add pickling helper that check unpicklability * Add test condition and generation error handling * Fix incorrect call and make imports consistent * Fix newline padding * Change PicklingError to directly caused by UnpicklingError Co-authored-by: Doug Hoskisson <beauxq@users.noreply.github.com> * Revert to `pickle.dumps` for decompressed multidata * Fix import order * Restore pickle import in main * Re-add for multidata in Main * Remove multisave checks * Update MultiServer.py * Update customserver.py --------- Co-authored-by: Doug Hoskisson <beauxq@users.noreply.github.com> Co-authored-by: NewSoupVi <57900059+NewSoupVi@users.noreply.github.com>
This commit is contained in:
12
Utils.py
12
Utils.py
@@ -483,6 +483,18 @@ def restricted_loads(s: bytes) -> Any:
|
||||
return RestrictedUnpickler(io.BytesIO(s)).load()
|
||||
|
||||
|
||||
def restricted_dumps(obj: Any) -> bytes:
|
||||
"""Helper function analogous to pickle.dumps()."""
|
||||
s = pickle.dumps(obj)
|
||||
# Assert that the string can be successfully loaded by restricted_loads
|
||||
try:
|
||||
restricted_loads(s)
|
||||
except pickle.UnpicklingError as e:
|
||||
raise pickle.PicklingError(e) from e
|
||||
|
||||
return s
|
||||
|
||||
|
||||
class ByValue:
|
||||
"""
|
||||
Mixin for enums to pickle value instead of name (restores pre-3.11 behavior). Use as left-most parent.
|
||||
|
Reference in New Issue
Block a user