Modify ↓
Ticket #22 (closed defect: fixed)
Aliases break if there are temporary objects
| Reported by: | tim.hochberg@… | Owned by: | xi |
|---|---|---|---|
| Priority: | normal | Component: | pyyaml |
| Severity: | normal | Keywords: | |
| Cc: |
Description
If an object being passed to represet_data has a shorter lifespan than the representer, the results are unpredictable, but generally bad. In my case, I'm trying to represent a custom omap class. I register a representer like so:
def omap_representer(dumper, data):
items = [[x, y] for (x, y) in data.iteritems()]
return dumper.represent_sequence(u'!omap', items)
yaml.add_representer(omap, omap_representer)
If I then dump something that contains multiple omaps, such as [one_omap,another_omap], the representer get's confused because it sees distinct objects that have the same id. Here's an actual example:
>>> a # Note that these are omaps not dictionaries, despite appearances.
[{1: 2, 2: 4}, {1: 99, 2: 88}]
>>> print yaml.dump(a)
- !omap
- &id001 [1, 2]
- &id002 [2, 4]
- !omap
- *id001
- *id002
Two approaches come to mind to fix this.
- Hold onto a reference to the original object as well as to the serialized object in represented_objects. This is what I did as a temporary fix.
- Do some sort of weak reference magic to track if an object dies and then delete it from represented_objects. This is more complicated and I'm not entirely sure it would work well. The upside is it's possibly more frugal with memory.
-tim
Attachments
Change History
Note: See
TracTickets for help on using
tickets.

Fixed in [222].
It's a subtle bug. Thanks for the report.
I've made representer to keep references to the represented objects. Perhaps, not very efficient, but should always work.