Ticket #101 (new defect)

Opened 5 years ago

Last modified 5 years ago

YAMLObject interacts poorly with __slots__

Reported by: Joe Wreschnig Owned by: xi
Priority: normal Component: pyyaml
Severity: normal Keywords:

Description (last modified by xi) (diff)

If I try to define a custom __slots__ for a subclass of YAMLObject I get errors during serialization because the code tries to use instance.__dict__.update, but __dict__ doesn't exist on objects with slots.

  File "/usr/lib/python2.4/site-packages/yaml/", line 177, in dump
    return dump_all([data], stream, Dumper=Dumper, **kwds)
  File "/usr/lib/python2.4/site-packages/yaml/", line 167, in dump_all
  File "/usr/lib/python2.4/site-packages/yaml/", line 33, in represent
    node = self.represent_data(data)
  File "/usr/lib/python2.4/site-packages/yaml/", line 62, in represent_data
    node = self.yaml_representers[data_types[0]](self, data)
  File "/home/piman/code/layer/layer/", line 84, in __to_yaml
    return dumper.represent_yaml_object(
  File "/usr/lib/python2.4/site-packages/yaml/", line 248, in represent_yaml_object
    state = data.__dict__.copy()
AttributeError: 'NPoint' object has no attribute '__dict__'

Attached is a patch that makes the constructor use setattr and the representer use __slots__ if __dict__ is not available. I did not write unit tests, as I couldn't figure out how to run them, but it makes the unit tests for my own project (which uses __slots__ for some objects and __dict__ for others) pass.


pyyaml-slots.diff Download (1.2 KB) - added by Joe Wreschnig 5 years ago.
Patch to support lack of dict

Change History

Changed 5 years ago by Joe Wreschnig

Patch to support lack of dict

comment:1 Changed 5 years ago by Joe Wreschnig

I forgot about Trac's horrible formatting - in case it's not obvious, I'm referring to __dict__ and __slots__.

comment:2 Changed 5 years ago by xi

  • Description modified (diff)

Add a comment

Modify Ticket

Change Properties
<Author field>
as new
as The resolution will be set. Next status will be 'closed'
to The owner will be changed from xi. Next status will be 'new'
The owner will be changed from xi to anonymous. Next status will be 'assigned'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.