Skip to content

Commit

Permalink
make Value objects picklable by rebuilding the memoryview on a new by…
Browse files Browse the repository at this point in the history
…tearray.

note that this breaks the link of any stored view to the original variable when unpickling.
remove stored view from FOR-stack.
(fix issue #30)
  • Loading branch information
robhagemans committed Dec 28, 2017
1 parent 0e6318d commit 35e5640
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pcbasic/basic/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,7 @@ def for_(self, args):
# initialise loop variable
self._scalars.set(varname, start)
# obtain a view of the loop variable
counter_view = self._scalars.view(varname)
self.for_stack.append((varname, counter_view, stop, step, step.sign(), forpos, nextpos,))
self.for_stack.append((varname, stop, step, step.sign(), forpos, nextpos,))
# empty loop: jump to NEXT without executing block
if (start.gt(stop) if step.sign() > 0 else stop.gt(start)):
ins.seek(nextpos)
Expand Down Expand Up @@ -427,7 +426,7 @@ def iterate_loop(self, varname=None):
# find the matching NEXT record
num = len(self.for_stack)
for depth in range(num):
varname2, counter_view, stop, step, sgn, forpos, nextpos = self.for_stack[-depth-1]
varname2, stop, step, sgn, forpos, nextpos = self.for_stack[-depth-1]
if pos == nextpos:
if varname is not None and varname2 != self._memory.complete_name(varname):
# check once more for matches
Expand All @@ -439,6 +438,7 @@ def iterate_loop(self, varname=None):
else:
raise error.RunError(error.NEXT_WITHOUT_FOR)
# increment counter
counter_view = self._scalars.view(varname2)
counter_view.iadd(step)
# check condition
loop_ends = counter_view.gt(stop) if sgn > 0 else stop.gt(counter_view)
Expand Down
11 changes: 11 additions & 0 deletions pcbasic/basic/values/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ def __str__(self):

__repr__ = __str__


def __getstate__(self):
# can't pickle memoryview
self._buffer = bytearray(self._buffer)
return self.__dict__

def __setstate__(self, pickle_dict):
# can't pickle memoryview
self.__dict__ = pickle_dict
self._buffer = memoryview(self._buffer)

def to_value(self):
"""Convert to Python value."""

Expand Down

0 comments on commit 35e5640

Please sign in to comment.