Skip to content

Commit

Permalink
Merge pull request #19 from mrMakaronka/feature/python-3.8-support
Browse files Browse the repository at this point in the history
Python 3.8 support: Only dicts can be assigned to Pickler#memo
  • Loading branch information
faucct authored Apr 13, 2021
2 parents a134250 + 7261020 commit 250f5a8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 31 deletions.
27 changes: 0 additions & 27 deletions src/main/python/ipystate/impl/memo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Dict

class ChunkedFile:
def __init__(self):
self._ba = bytearray()
Expand All @@ -12,28 +10,3 @@ def current_chunk(self) -> bytearray:

def reset(self) -> None:
self._ba = bytearray()


class TransactionalDict(dict):
def __init__(self, original: Dict):
self._dirty = set()
self._prev = {}
super().__init__(original)

def __setitem__(self, key, value) -> None:
if key not in self._dirty:
self._dirty.add(key)
self._prev[key] = super().get(key, None)
super().__setitem__(key, value)

def rollback(self) -> None:
for k, v in self._prev.items():
if v is None:
super().__delitem__(k)
else:
super().__setitem__(k, v)
self.commit()

def commit(self) -> None:
self._dirty = set()
self._prev = {}
8 changes: 4 additions & 4 deletions src/main/python/ipystate/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from .impl.components_fuser import ComponentsFuser
from .impl.dispatch.common import CommonDispatcher
from .impl.memo import ChunkedFile, TransactionalDict
from .impl.memo import ChunkedFile


class Dump:
Expand Down Expand Up @@ -194,7 +194,7 @@ def _dump_pickle_component(self, component: Set[str], ns: Dict[str, object]) ->

cf = ChunkedFile()
pickler = Pickler(ns, self.configurable_dispatch_table, cf)
pickler.memo = TransactionalDict(pickler.memo)
committed_memo = pickler.memo.copy()

comp_sorted_vars = self._sort_component_vars(component, ns)
for var_name in comp_sorted_vars:
Expand All @@ -203,11 +203,11 @@ def _dump_pickle_component(self, component: Set[str], ns: Dict[str, object]) ->
# TODO and report to non serialized
try:
pickler.dump(var_value)
pickler.memo.commit()
committed_memo = pickler.memo.copy()
chunk = cf.current_chunk()
serialized_vars.append((var_name, chunk))
except Exception as e:
pickler.memo.rollback()
pickler.memo = committed_memo
non_serialized_var_names.add(var_name)
self._on_var_serialize_error(var_name, var_value, e)
finally:
Expand Down

0 comments on commit 250f5a8

Please sign in to comment.