From 0756be6d64a686b1dcbef6516f6e1e87d01c43ee Mon Sep 17 00:00:00 2001 From: Filip Salomonsson Date: Fri, 4 May 2018 23:48:11 +0200 Subject: [PATCH] Fix handling of __slots__ --- lib/yaml/constructor.py | 2 +- lib3/yaml/constructor.py | 2 +- tests/data/construct-python-object.code | 2 ++ tests/data/construct-python-object.data | 2 ++ tests/lib/test_constructor.py | 13 ++++++++++++- tests/lib3/test_constructor.py | 13 ++++++++++++- 6 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/yaml/constructor.py b/lib/yaml/constructor.py index 635faac3..b3272c35 100644 --- a/lib/yaml/constructor.py +++ b/lib/yaml/constructor.py @@ -560,7 +560,7 @@ def set_python_instance_state(self, instance, state): elif state: slotstate.update(state) for key, value in slotstate.items(): - setattr(object, key, value) + setattr(instance, key, value) def construct_python_object(self, suffix, node): # Format: diff --git a/lib3/yaml/constructor.py b/lib3/yaml/constructor.py index 981543ae..31fa4694 100644 --- a/lib3/yaml/constructor.py +++ b/lib3/yaml/constructor.py @@ -567,7 +567,7 @@ def set_python_instance_state(self, instance, state): elif state: slotstate.update(state) for key, value in slotstate.items(): - setattr(object, key, value) + setattr(instance, key, value) def construct_python_object(self, suffix, node): # Format: diff --git a/tests/data/construct-python-object.code b/tests/data/construct-python-object.code index 7f1edf12..9e611e43 100644 --- a/tests/data/construct-python-object.code +++ b/tests/data/construct-python-object.code @@ -17,6 +17,8 @@ NewArgsWithState(1, 'two', [3,3,3]), Reduce(1, 'two', [3,3,3]), ReduceWithState(1, 'two', [3,3,3]), +Slots(1, 'two', [3,3,3]), + MyInt(3), MyList(3), MyDict(3), diff --git a/tests/data/construct-python-object.data b/tests/data/construct-python-object.data index bce8b2ed..66797e4c 100644 --- a/tests/data/construct-python-object.data +++ b/tests/data/construct-python-object.data @@ -16,6 +16,8 @@ - !!python/object/apply:test_constructor.Reduce [1, two, [3,3,3]] - !!python/object/apply:test_constructor.ReduceWithState { args: [1, two], state: [3,3,3] } +- !!python/object/new:test_constructor.Slots { state: !!python/tuple [null, { foo: 1, bar: 'two', baz: [3,3,3] } ] } + - !!python/object/new:test_constructor.MyInt [3] - !!python/object/new:test_constructor.MyList { listitems: [~, ~, ~] } - !!python/object/new:test_constructor.MyDict { dictitems: {0, 1, 2} } diff --git a/tests/lib/test_constructor.py b/tests/lib/test_constructor.py index 12d53913..3d475bbd 100644 --- a/tests/lib/test_constructor.py +++ b/tests/lib/test_constructor.py @@ -16,7 +16,7 @@ def execute(code): def _make_objects(): global MyLoader, MyDumper, MyTestClass1, MyTestClass2, MyTestClass3, YAMLObject1, YAMLObject2, \ AnObject, AnInstance, AState, ACustomState, InitArgs, InitArgsWithState, \ - NewArgs, NewArgsWithState, Reduce, ReduceWithState, MyInt, MyList, MyDict, \ + NewArgs, NewArgsWithState, Reduce, ReduceWithState, Slots, MyInt, MyList, MyDict, \ FixedOffset, today, execute class MyLoader(yaml.DangerLoader): @@ -185,6 +185,17 @@ def __reduce__(self): def __setstate__(self, state): self.baz = state + class Slots(object): + __slots__ = ("foo", "bar", "baz") + def __init__(self, foo=None, bar=None, baz=None): + self.foo = foo + self.bar = bar + self.baz = baz + + def __eq__(self, other): + return type(self) is type(other) and \ + (self.foo, self.bar, self.baz) == (other.foo, other.bar, other.baz) + class MyInt(int): def __eq__(self, other): return type(self) is type(other) and int(self) == int(other) diff --git a/tests/lib3/test_constructor.py b/tests/lib3/test_constructor.py index 71caa8e4..0c51db0a 100644 --- a/tests/lib3/test_constructor.py +++ b/tests/lib3/test_constructor.py @@ -13,7 +13,7 @@ def execute(code): def _make_objects(): global MyLoader, MyDumper, MyTestClass1, MyTestClass2, MyTestClass3, YAMLObject1, YAMLObject2, \ AnObject, AnInstance, AState, ACustomState, InitArgs, InitArgsWithState, \ - NewArgs, NewArgsWithState, Reduce, ReduceWithState, MyInt, MyList, MyDict, \ + NewArgs, NewArgsWithState, Reduce, ReduceWithState, Slots, MyInt, MyList, MyDict, \ FixedOffset, today, execute class MyLoader(yaml.DangerLoader): @@ -172,6 +172,17 @@ def __reduce__(self): def __setstate__(self, state): self.baz = state + class Slots: + __slots__ = ("foo", "bar", "baz") + def __init__(self, foo=None, bar=None, baz=None): + self.foo = foo + self.bar = bar + self.baz = baz + + def __eq__(self, other): + return type(self) is type(other) and \ + (self.foo, self.bar, self.baz) == (other.foo, other.bar, other.baz) + class MyInt(int): def __eq__(self, other): return type(self) is type(other) and int(self) == int(other)