Fix handling of __slots__ (#161)
diff --git a/lib/yaml/constructor.py b/lib/yaml/constructor.py
index 094b3e0..0c83e43 100644
--- a/lib/yaml/constructor.py
+++ b/lib/yaml/constructor.py
@@ -578,7 +578,7 @@
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 f8d71a1..31e9318 100644
--- a/lib3/yaml/constructor.py
+++ b/lib3/yaml/constructor.py
@@ -585,7 +585,7 @@
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 7f1edf1..9e611e4 100644
--- a/tests/data/construct-python-object.code
+++ b/tests/data/construct-python-object.code
@@ -17,6 +17,8 @@
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 bce8b2e..66797e4 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 beee7b0..9e6c856 100644
--- a/tests/lib/test_constructor.py
+++ b/tests/lib/test_constructor.py
@@ -16,7 +16,7 @@
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.Loader):
@@ -185,6 +185,17 @@
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 427f53c..3d69649 100644
--- a/tests/lib3/test_constructor.py
+++ b/tests/lib3/test_constructor.py
@@ -13,7 +13,7 @@
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.Loader):
@@ -172,6 +172,17 @@
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)