Skip to content

Commit

Permalink
Support overriding anchors
Browse files Browse the repository at this point in the history
In YAML 1.2, anchors can be reused.
https://yaml.org/spec/1.2/spec.html#id2786196
  • Loading branch information
perlpunk committed Sep 22, 2021
1 parent f8e89b3 commit 57cfaae
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 21 deletions.
5 changes: 0 additions & 5 deletions lib/yaml/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ def compose_node(self, parent, index):
return self.anchors[anchor]
event = self.peek_event()
anchor = event.anchor
if anchor is not None:
if anchor in self.anchors:
raise ComposerError("found duplicate anchor %r; first occurrence"
% anchor, self.anchors[anchor].start_mark,
"second occurrence", event.start_mark)
self.descend_resolver(parent, index)
if self.check_event(ScalarEvent):
node = self.compose_scalar_node(anchor)
Expand Down
3 changes: 0 additions & 3 deletions tests/data/duplicate-anchor-1.loader-error

This file was deleted.

1 change: 1 addition & 0 deletions tests/data/override-anchor-1.code
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(['bar', 'bar', 'baz', 'baz'])
4 changes: 4 additions & 0 deletions tests/data/override-anchor-1.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- &foo bar
- *foo
- &foo baz
- *foo
1 change: 1 addition & 0 deletions tests/data/override-anchor-2.code
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1, 2, 3, 4]
File renamed without changes.
13 changes: 0 additions & 13 deletions yaml/_yaml.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -754,19 +754,6 @@ cdef class CParser:
elif self.parsed_event.type == YAML_MAPPING_START_EVENT \
and self.parsed_event.data.mapping_start.anchor != NULL:
anchor = PyUnicode_FromString(self.parsed_event.data.mapping_start.anchor)
if anchor is not None:
if anchor in self.anchors:
mark = Mark(self.stream_name,
self.parsed_event.start_mark.index,
self.parsed_event.start_mark.line,
self.parsed_event.start_mark.column,
None, None)
if PY_MAJOR_VERSION < 3:
raise ComposerError("found duplicate anchor; first occurrence",
self.anchors[anchor].start_mark, "second occurrence", mark)
else:
raise ComposerError(u"found duplicate anchor; first occurrence",
self.anchors[anchor].start_mark, u"second occurrence", mark)
self.descend_resolver(parent, index)
if self.parsed_event.type == YAML_SCALAR_EVENT:
node = self._compose_scalar_node(anchor)
Expand Down

0 comments on commit 57cfaae

Please sign in to comment.