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 yaml construct a slot object #258

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ AnInstance(1, 'two', [3,3,3]),
AState(1, 'two', [3,3,3]),
ACustomState(1, 'two', [3,3,3]),

AnInstanceWithSlots(1, 'two', [3,3,3]),

InitArgs(1, 'two', [3,3,3]),
InitArgsWithState(1, 'two', [3,3,3]),

Expand Down
2 changes: 2 additions & 0 deletions tests/data/construct-python-object.data
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- !!python/object:test_constructor.AState { _foo: 1, _bar: two, _baz: [3,3,3] }
- !!python/object/new:test_constructor.ACustomState { state: !!python/tuple [1, two, [3,3,3]] }

- !!python/object/new:test_constructor.AnInstanceWithSlots {state: !!python/tuple [null, {bar: two, baz: [3, 3, 3], foo: 1}]}

- !!python/object/new:test_constructor.InitArgs [1, two, [3,3,3]]
- !!python/object/new:test_constructor.InitArgsWithState { args: [1, two], state: [3,3,3] }

Expand Down
18 changes: 17 additions & 1 deletion tests/lib/test_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def execute(code):

def _make_objects():
global MyLoader, MyDumper, MyTestClass1, MyTestClass2, MyTestClass3, YAMLObject1, YAMLObject2, \
AnObject, AnInstance, AState, ACustomState, InitArgs, InitArgsWithState, \
AnObject, AnInstance, AState, ACustomState, AnInstanceWithSlots, InitArgs, InitArgsWithState, \
NewArgs, NewArgsWithState, Reduce, ReduceWithState, MyInt, MyList, MyDict, \
FixedOffset, today, execute

Expand Down Expand Up @@ -147,6 +147,22 @@ def __getstate__(self):
def __setstate__(self, state):
self.foo, self.bar, self.baz = state

class AnInstanceWithSlots(object):
__slots__ = ('foo', 'bar', 'baz')

def __init__(self, foo=None, bar=None, baz=None):
self.foo = foo
self.bar = bar
self.baz = baz

def __cmp__(self, other):
return cmp((type(self), self.foo, self.bar, self.baz),
(type(other), other.foo, other.bar, other.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 InitArgs(AnInstance):
def __getinitargs__(self):
return (self.foo, self.bar, self.baz)
Expand Down
18 changes: 17 additions & 1 deletion tests/lib3/test_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def execute(code):

def _make_objects():
global MyLoader, MyDumper, MyTestClass1, MyTestClass2, MyTestClass3, YAMLObject1, YAMLObject2, \
AnObject, AnInstance, AState, ACustomState, InitArgs, InitArgsWithState, \
AnObject, AnInstance, AState, ACustomState, AnInstanceWithSlots, InitArgs, InitArgsWithState, \
NewArgs, NewArgsWithState, Reduce, ReduceWithState, MyInt, MyList, MyDict, \
FixedOffset, today, execute

Expand Down Expand Up @@ -144,6 +144,22 @@ def __getstate__(self):
def __setstate__(self, state):
self.foo, self.bar, self.baz = state

class AnInstanceWithSlots(object):
__slots__ = ('foo', 'bar', 'baz')

def __init__(self, foo=None, bar=None, baz=None):
self.foo = foo
self.bar = bar
self.baz = baz

def __cmp__(self, other):
return cmp((type(self), self.foo, self.bar, self.baz),
(type(other), other.foo, other.bar, other.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 NewArgs(AnObject):
def __getnewargs__(self):
return (self.foo, self.bar, self.baz)
Expand Down