Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of __slots__ #161

Merged
merged 1 commit into from Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/yaml/constructor.py
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib3/yaml/constructor.py
Expand Up @@ -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:
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.DangerLoader):
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.DangerLoader):
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