Ticket #101 (new defect)

Opened 2 years ago

Last modified 2 years ago

YAMLObject interacts poorly with __slots__

Reported by: Joe Wreschnig Assigned to: xi
Priority: normal Component: pyyaml
Severity: normal Keywords:
Cc:

Description (Last modified by xi)

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/__init__.py", line 177, in dump
    return dump_all([data], stream, Dumper=Dumper, **kwds)
  File "/usr/lib/python2.4/site-packages/yaml/__init__.py", line 167, in dump_all
    dumper.represent(data)
  File "/usr/lib/python2.4/site-packages/yaml/representer.py", line 33, in represent
    node = self.represent_data(data)
  File "/usr/lib/python2.4/site-packages/yaml/representer.py", line 62, in represent_data
    node = self.yaml_representers[data_types[0]](self, data)
  File "/home/piman/code/layer/layer/parse.py", line 84, in __to_yaml
    return dumper.represent_yaml_object(
  File "/usr/lib/python2.4/site-packages/yaml/representer.py", 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.

Attachments

pyyaml-slots.diff (1.2 kB) - added by Joe Wreschnig on 11/09/08 22:20:45.
Patch to support lack of dict

Change History

11/09/08 22:20:45 changed by Joe Wreschnig

  • attachment pyyaml-slots.diff added.

Patch to support lack of dict

11/09/08 22:21:27 changed by Joe Wreschnig

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

11/10/08 09:44:47 changed by xi

  • description changed.

Add/Change #101 (YAMLObject interacts poorly with __slots__)




Change Properties
Action