Skip to content

Commit 1a8c4f9

Browse files
committed
Prepare for snapshotting
1 parent 8e2f550 commit 1a8c4f9

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

coconut/compiler/compiler.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@
3737
from functools import partial, wraps
3838
from collections import defaultdict
3939
from threading import Lock
40+
from copy import copy
4041

4142
from coconut._pyparsing import (
4243
USE_COMPUTATION_GRAPH,
4344
USE_CACHE,
44-
USE_LINE_BY_LINE,
4545
ParseBaseException,
4646
ParseResults,
4747
col as getcol,
@@ -108,6 +108,7 @@
108108
assert_remove_suffix,
109109
dictset,
110110
noop_ctx,
111+
create_method,
111112
)
112113
from coconut.exceptions import (
113114
CoconutException,
@@ -484,12 +485,16 @@ def get_cli_args(self):
484485
args.append("--no-wrap-types")
485486
return args
486487

487-
def __copy__(self):
488-
"""Create a new, blank copy of the compiler."""
489-
cls, args = self.__reduce__()
490-
return cls(*args)
491-
492-
copy = __copy__
488+
def copy(self, snapshot=False):
489+
"""Create a blank copy of the compiler, or a non-blank copy if snapshot=True."""
490+
if snapshot:
491+
old_reduce, self.__reduce__ = self.__reduce__, create_method(object.__reduce__, self, self.__class__)
492+
try:
493+
return copy(self)
494+
finally:
495+
self.__reduce__ = old_reduce
496+
else:
497+
return copy(self)
493498

494499
def genhash(self, code, package_level=-1):
495500
"""Generate a hash from code."""
@@ -1357,7 +1362,7 @@ def run_final_checks(self, original, keep_state=False):
13571362

13581363
def parse_line_by_line(self, init_parser, line_parser, original):
13591364
"""Apply init_parser then line_parser repeatedly."""
1360-
if not USE_LINE_BY_LINE:
1365+
if not USE_COMPUTATION_GRAPH:
13611366
raise CoconutException("line-by-line parsing not supported", extra="run 'pip install --upgrade cPyparsing' to fix")
13621367
with ComputationNode.using_overrides():
13631368
ComputationNode.override_original = original

coconut/util.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ def __call__(self, *args, **kwargs):
107107
return self.value
108108

109109

110+
def create_method(func, obj, objtype):
111+
"""Universally create a new method object."""
112+
if PY2:
113+
return MethodType(func, obj, objtype)
114+
else:
115+
return MethodType(func, obj)
116+
117+
110118
class override(pickleable_obj):
111119
"""Implementation of Coconut's @override for use within Coconut."""
112120
__slots__ = ("func",)
@@ -129,10 +137,7 @@ def __get__(self, obj, objtype=None):
129137
return self.func.__get__(obj, objtype)
130138
if obj is None:
131139
return self.func
132-
if PY2:
133-
return MethodType(self.func, obj, objtype)
134-
else:
135-
return MethodType(self.func, obj)
140+
return create_method(self.func, obj, objtype)
136141

137142
def __set_name__(self, obj, name):
138143
if not hasattr(super(obj, obj), name):

0 commit comments

Comments
 (0)