diff options
| -rw-r--r-- | lib/stitch_plan/stitch.py | 12 | ||||
| -rw-r--r-- | lib/stitch_plan/stitch_group.py | 13 |
2 files changed, 25 insertions, 0 deletions
diff --git a/lib/stitch_plan/stitch.py b/lib/stitch_plan/stitch.py index 3bfa7075..67a0fd64 100644 --- a/lib/stitch_plan/stitch.py +++ b/lib/stitch_plan/stitch.py @@ -12,6 +12,10 @@ class Stitch(Point): def __init__(self, x, y=None, color=None, jump=False, stop=False, trim=False, color_change=False, tie_modus=0, force_lock_stitches=False, no_ties=False, tags=None): + # DANGER: if you add new attributes, you MUST also set their default + # values in __new__() below. Otherwise, cached stitch plans can be + # loaded and create objects without those properties defined, because + # unpickling does not call __init__()! base_stitch = None if isinstance(x, Stitch): @@ -42,6 +46,14 @@ class Stitch(Point): if base_stitch is not None: self.add_tags(base_stitch.tags) + def __new__(cls, *args, **kwargs): + instance = super().__new__(cls) + + # Set default values for any new attributes here (see note in __init__() above) + # instance.foo = None + + return instance + def __repr__(self): return "Stitch(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" % (self.x, self.y, diff --git a/lib/stitch_plan/stitch_group.py b/lib/stitch_plan/stitch_group.py index 6dbeb9e5..717bb617 100644 --- a/lib/stitch_plan/stitch_group.py +++ b/lib/stitch_plan/stitch_group.py @@ -19,6 +19,11 @@ class StitchGroup: def __init__(self, color=None, stitches=None, trim_after=False, stop_after=False, tie_modus=0, force_lock_stitches=False, stitch_as_is=False, tags=None): + # DANGER: if you add new attributes, you MUST also set their default + # values in __new__() below. Otherwise, cached stitch plans can be + # loaded and create objects without those properties defined, because + # unpickling does not call __init__()! + self.color = color self.trim_after = trim_after self.stop_after = stop_after @@ -33,6 +38,14 @@ class StitchGroup: if tags: self.add_tags(tags) + def __new__(cls, *args, **kwargs): + instance = super().__new__(cls) + + # Set default values for any new attributes here (see note in __init__() above) + # instance.foo = None + + return instance + def __add__(self, other): if isinstance(other, StitchGroup): return StitchGroup(self.color, self.stitches + other.stitches) |
