Modify

Ticket #221 (new defect)

Opened 2 years ago

Last modified 44 years ago

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

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
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'
Author


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

 
Note: See TracTickets for help on using tickets.