Recursion within collections.defaultdict doesn't work

Issue #47 new
Adam Dinwoodie
created an issue

If you have recursion happening within a colletions.defaultdict object, at least on Python 3.2.3, you'll get errors trying to reload the dumped dictionary.

Lets go with an example:

>>> recursive_list = []
>>> recursive_list.append(recursive_list)
>>> d = collections.defaultdict(None, {0: recursive_list})
>>> print(yaml.dump(d))
!!python/object/apply:collections.defaultdict
dictitems:
  0: &id001
  - *id001

>>> yaml.load(yaml.dump(d))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
<snipping a long stack trace>
yaml.constructor.ConstructorError: found unconstructable recursive node
  in "<unicode string>", line 3, column 6:
      0: &id001
         ^

This works just fine with a regular dictionary:

>>> d = {0: l}
>>> print(yaml.dump(d))
0: &id001
- *id001

>>> yaml.load(yaml.dump(d))
{0: [[...]]}

I've had a bit of a dig in the code, but I can't immediately work out what the recursion detection code is doing, or why it's falling over this particular construction.

Comments (0)

  1. Log in to comment