Modify ↓
Ticket #221 (new defect)
add_implicit_resolver on a subclass may affect super_class resolvers
| Reported by: | Alex Garel <alex.garel@…> | Owned by: | xi |
|---|---|---|---|
| Priority: | normal | Component: | pyyaml |
| Severity: | major | Keywords: | tags, resolvers |
| Cc: |
Description
Normally if I add a new implicit resolver to a subclass of a loader, super class shall not be affected.
Yet this is the case if some letter of first argument matches an existing implicit resolver in super.
>>> import yaml
>>> import re
>>> class DummyLoader(yaml.SafeLoader):
... pass
...
>>> DummyLoader.add_implicit_resolver(u'!yeah',re.compile(ur'Yeah'),
... first='Y') # first is the same as bool implicit resolver of SafeLoader
>>> yaml.safe_load('Yeah') # I use SafeLoader which shall ignore !yeah
Traceback (most recent call last):
...
yaml.constructor.ConstructorError: could not determine a constructor for the tag '!yeah'
in "<string>", line 1, column 1:
Yeah
This comes from line 26-27 of resolver.py:BaseResolver.add_implicit_resolver:
if not 'yaml_implicit_resolvers' in cls.__dict__:
cls.yaml_implicit_resolvers = cls.yaml_implicit_resolvers.copy()
cls.yaml_implicit_resolvers.copy() is not enough as it will keep existing lists instead of cloning them.
Instead this shall be:
from copy import copy
then:
if not 'yaml_implicit_resolvers' in cls.__dict__:
cls.yaml_implicit_resolvers = {}
for k, v in cls.yaml_implicit_resolvers.items():
cls.yaml_implicit_resolvers[k] = copy(v)
Attachments
Note: See
TracTickets for help on using
tickets.
