Skip to content

Commit

Permalink
Fix handling of __slots__ (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipsalomonsson authored and perlpunk committed Dec 7, 2019
1 parent eb459f8 commit 5a0cfab
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/yaml/constructor.py
Expand Up @@ -578,7 +578,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:
Expand Down
2 changes: 1 addition & 1 deletion lib3/yaml/constructor.py
Expand Up @@ -585,7 +585,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:
Expand Down
2 changes: 2 additions & 0 deletions tests/data/construct-python-object.code
Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions tests/data/construct-python-object.data
Expand Up @@ -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} }
13 changes: 12 additions & 1 deletion tests/lib/test_constructor.py
Expand Up @@ -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.Loader):
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion tests/lib3/test_constructor.py
Expand Up @@ -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.Loader):
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5a0cfab

Please sign in to comment.