Skip to content

Commit

Permalink
Also add reuse_anchors to libyaml backend
Browse files Browse the repository at this point in the history
  • Loading branch information
perlpunk committed Sep 24, 2021
1 parent bd96e40 commit 737ad62
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/yaml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def parse(stream, Loader=Loader, reuse_anchors=False):
finally:
loader.dispose()

def compose(stream, Loader=Loader):
def compose(stream, Loader=Loader, reuse_anchors=False):
"""
Parse the first YAML document in a stream
and produce the corresponding representation tree.
Expand Down
20 changes: 10 additions & 10 deletions lib/yaml/cyaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,36 @@

class CBaseLoader(CParser, BaseConstructor, BaseResolver):

def __init__(self, stream):
CParser.__init__(self, stream)
def __init__(self, stream, reuse_anchors=False):
CParser.__init__(self, stream, reuse_anchors=reuse_anchors)
BaseConstructor.__init__(self)
BaseResolver.__init__(self)

class CSafeLoader(CParser, SafeConstructor, Resolver):

def __init__(self, stream):
CParser.__init__(self, stream)
def __init__(self, stream, reuse_anchors=False):
CParser.__init__(self, stream, reuse_anchors=reuse_anchors)
SafeConstructor.__init__(self)
Resolver.__init__(self)

class CFullLoader(CParser, FullConstructor, Resolver):

def __init__(self, stream):
CParser.__init__(self, stream)
def __init__(self, stream, reuse_anchors=False):
CParser.__init__(self, stream, reuse_anchors=reuse_anchors)
FullConstructor.__init__(self)
Resolver.__init__(self)

class CUnsafeLoader(CParser, UnsafeConstructor, Resolver):

def __init__(self, stream):
CParser.__init__(self, stream)
def __init__(self, stream, reuse_anchors=False):
CParser.__init__(self, stream, reuse_anchors=reuse_anchors)
UnsafeConstructor.__init__(self)
Resolver.__init__(self)

class CLoader(CParser, Constructor, Resolver):

def __init__(self, stream):
CParser.__init__(self, stream)
def __init__(self, stream, reuse_anchors=False):
CParser.__init__(self, stream, reuse_anchors=reuse_anchors)
Constructor.__init__(self)
Resolver.__init__(self)

Expand Down
3 changes: 3 additions & 0 deletions tests/data/duplicate-anchor-1.loader-error
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- &foo bar
- &bar bar
- &foo bar
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
30 changes: 29 additions & 1 deletion tests/lib/test_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def test_constructor_types(data_filename, code_filename, verbose=False):
native2 = None
try:
with open(data_filename, 'rb') as file:
native1 = list(yaml.load_all(file, Loader=MyLoader, reuse_anchors=True))
native1 = list(yaml.load_all(file, Loader=MyLoader))
if len(native1) == 1:
native1 = native1[0]
with open(code_filename, 'rb') as file:
Expand Down Expand Up @@ -296,6 +296,34 @@ def test_subclass_blacklist_types(data_filename, verbose=False):

test_subclass_blacklist_types.unittest = ['.subclass_blacklist']

def test_reuse_anchors(data_filename, code_filename, verbose=False):
try:
with open(data_filename, 'rb') as file:
native1 = list(yaml.load_all(file, Loader=yaml.SafeLoader, reuse_anchors=True))
if len(native1) == 1:
native1 = native1[0]
with open(code_filename, 'rb') as file:
native2 = _load_code(file.read())
try:
if native1 == native2:
return
except TypeError:
pass
if verbose:
print("SERIALIZED NATIVE1:")
print(_serialize_value(native1))
print("SERIALIZED NATIVE2:")
print(_serialize_value(native2))
assert _serialize_value(native1) == _serialize_value(native2), (native1, native2)
finally:
if verbose:
print("NATIVE1:")
pprint.pprint(native1)
print("NATIVE2:")
pprint.pprint(native2)

test_reuse_anchors.unittest = ['.reuse-anchors-data', '.reuse-anchors-code']

if __name__ == '__main__':
import sys, test_constructor
sys.modules['test_constructor'] = sys.modules['__main__']
Expand Down
24 changes: 12 additions & 12 deletions tests/lib/test_yaml_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ def new_parse(stream, Loader=yaml.CLoader):
return old_parse(stream, Loader)

old_compose = yaml.compose
def new_compose(stream, Loader=yaml.CLoader):
return old_compose(stream, Loader)
def new_compose(stream, Loader=yaml.CLoader, reuse_anchors=False):
return old_compose(stream, Loader, reuse_anchors=reuse_anchors)

old_compose_all = yaml.compose_all
def new_compose_all(stream, Loader=yaml.CLoader):
return old_compose_all(stream, Loader)
def new_compose_all(stream, Loader=yaml.CLoader, reuse_anchors=False):
return old_compose_all(stream, Loader, reuse_anchors=reuse_anchors)

old_load = yaml.load
def new_load(stream, Loader=yaml.CLoader):
return old_load(stream, Loader)
def new_load(stream, Loader=yaml.CLoader, reuse_anchors=False):
return old_load(stream, Loader, reuse_anchors=reuse_anchors)

old_load_all = yaml.load_all
def new_load_all(stream, Loader=yaml.CLoader):
return old_load_all(stream, Loader)
def new_load_all(stream, Loader=yaml.CLoader, reuse_anchors=False):
return old_load_all(stream, Loader, reuse_anchors=reuse_anchors)

old_safe_load = yaml.safe_load
def new_safe_load(stream):
return old_load(stream, yaml.CSafeLoader)
def new_safe_load(stream, reuse_anchors=False):
return old_load(stream, yaml.CSafeLoader, reuse_anchors=reuse_anchors)

old_safe_load_all = yaml.safe_load_all
def new_safe_load_all(stream):
return old_load_all(stream, yaml.CSafeLoader)
def new_safe_load_all(stream, reuse_anchors=False):
return old_load_all(stream, yaml.CSafeLoader, reuse_anchors=reuse_anchors)

old_emit = yaml.emit
def new_emit(events, stream=None, Dumper=yaml.CDumper, **kwds):
Expand Down
5 changes: 3 additions & 2 deletions yaml/_yaml.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ cdef class CParser:
cdef int stream_cache_pos
cdef int unicode_source

def __init__(self, stream):
def __init__(self, stream, reuse_anchors=False):
self.reuse_anchors=reuse_anchors
cdef is_readable
if yaml_parser_initialize(&self.parser) == 0:
raise MemoryError
Expand Down Expand Up @@ -714,7 +715,7 @@ cdef class CParser:
and self.parsed_event.data.mapping_start.anchor != NULL:
anchor = PyUnicode_FromYamlString(self.parsed_event.data.mapping_start.anchor)
if anchor is not None:
if anchor in self.anchors:
if anchor in self.anchors and not self.reuse_anchors:
mark = Mark(self.stream_name,
self.parsed_event.start_mark.index,
self.parsed_event.start_mark.line,
Expand Down

0 comments on commit 737ad62

Please sign in to comment.