Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed implicit resolver leak #67 #73

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/yaml/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from error import *
from nodes import *

import itertools
import re

class ResolverError(YAMLError):
Expand Down Expand Up @@ -146,8 +147,8 @@ def resolve(self, kind, value, implicit):
resolvers = self.yaml_implicit_resolvers.get(u'', [])
else:
resolvers = self.yaml_implicit_resolvers.get(value[0], [])
resolvers += self.yaml_implicit_resolvers.get(None, [])
for tag, regexp in resolvers:
none_resolvers = self.yaml_implicit_resolvers.get(None, [])
for tag, regexp in itertools.chain(resolvers, none_resolvers):
if regexp.match(value):
return tag
implicit = implicit[1]
Expand Down
5 changes: 3 additions & 2 deletions lib3/yaml/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .error import *
from .nodes import *

import itertools
import re

class ResolverError(YAMLError):
Expand Down Expand Up @@ -146,8 +147,8 @@ def resolve(self, kind, value, implicit):
resolvers = self.yaml_implicit_resolvers.get('', [])
else:
resolvers = self.yaml_implicit_resolvers.get(value[0], [])
resolvers += self.yaml_implicit_resolvers.get(None, [])
for tag, regexp in resolvers:
none_resolvers = self.yaml_implicit_resolvers.get(None, [])
for tag, regexp in itertools.chain(resolvers, none_resolvers):
if regexp.match(value):
return tag
implicit = implicit[1]
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/test_resolver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import yaml
import pprint
import re

def test_implicit_resolver(data_filename, detect_filename, verbose=False):
correct_tag = None
Expand All @@ -21,6 +22,25 @@ def test_implicit_resolver(data_filename, detect_filename, verbose=False):

test_implicit_resolver.unittest = ['.data', '.detect']

def test_implicit_resolver_leak(data_filename, detect_filename, verbose=False):
# Add any custom resolver
tag, regexp = '!any_resolver', re.compile('AnyResolver')
yaml.add_implicit_resolver(tag, regexp)

none_resolvers = yaml.Dumper.yaml_implicit_resolvers.get(None, [])
assert (tag, regexp) in none_resolvers

old_f_resolvers = yaml.Dumper.yaml_implicit_resolvers.get('f', [])
assert (tag, regexp) not in old_f_resolvers

# Dump at least one ScalarNode
yaml.dump(False)

new_f_resolvers = yaml.Dumper.yaml_implicit_resolvers.get('f', [])
assert (tag, regexp) not in new_f_resolvers

test_implicit_resolver_leak.unittest = ['.data', '.path']

def _make_path_loader_and_dumper():
global MyLoader, MyDumper

Expand Down
20 changes: 20 additions & 0 deletions tests/lib3/test_resolver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import yaml
import pprint
import re

def test_implicit_resolver(data_filename, detect_filename, verbose=False):
correct_tag = None
Expand All @@ -21,6 +22,25 @@ def test_implicit_resolver(data_filename, detect_filename, verbose=False):

test_implicit_resolver.unittest = ['.data', '.detect']

def test_implicit_resolver_leak(data_filename, detect_filename, verbose=False):
# Add any custom resolver
tag, regexp = '!any_resolver', re.compile('AnyResolver')
yaml.add_implicit_resolver(tag, regexp)

none_resolvers = yaml.Dumper.yaml_implicit_resolvers.get(None, [])
assert (tag, regexp) in none_resolvers

old_f_resolvers = yaml.Dumper.yaml_implicit_resolvers.get('f', [])
assert (tag, regexp) not in old_f_resolvers

# Dump at least one ScalarNode
yaml.dump(False)

new_f_resolvers = yaml.Dumper.yaml_implicit_resolvers.get('f', [])
assert (tag, regexp) not in new_f_resolvers

test_implicit_resolver_leak.unittest = ['.data', '.path']

def _make_path_loader_and_dumper():
global MyLoader, MyDumper

Expand Down