From d6e157b74d84da461cfb0039dc193299f96d5f0d Mon Sep 17 00:00:00 2001 From: Andrey Somov Date: Fri, 2 Mar 2018 10:18:32 +0100 Subject: [PATCH] Revert "Allow colon in a plain scalar in a flow context (#45)" This reverts commit c5b135fe39d41cffbdc006f28ccb2032df6005e0. --- lib/yaml/scanner.py | 16 +++++++++++----- lib3/yaml/scanner.py | 16 +++++++++++----- tests/data/colon-in-flow-context.loader-error | 1 + 3 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 tests/data/colon-in-flow-context.loader-error diff --git a/lib/yaml/scanner.py b/lib/yaml/scanner.py index f86cb80b..d191c905 100644 --- a/lib/yaml/scanner.py +++ b/lib/yaml/scanner.py @@ -1272,7 +1272,7 @@ def scan_flow_scalar_breaks(self, double, start_mark): def scan_plain(self): # See the specification for details. # We add an additional restriction for the flow context: - # plain scalars in the flow context cannot contain ',' or '?'. + # plain scalars in the flow context cannot contain ',', ':' and '?'. # We also keep track of the `allow_simple_key` flag here. # Indentation rules are loosed for the flow context. chunks = [] @@ -1291,12 +1291,18 @@ def scan_plain(self): while True: ch = self.peek(length) if ch in u'\0 \t\r\n\x85\u2028\u2029' \ - or (ch == u':' and - self.peek(length+1) in u'\0 \t\r\n\x85\u2028\u2029' - + (u',[]{}' if self.flow_level else u''))\ - or (self.flow_level and ch in u',?[]{}'): + or (not self.flow_level and ch == u':' and + self.peek(length+1) in u'\0 \t\r\n\x85\u2028\u2029') \ + or (self.flow_level and ch in u',:?[]{}'): break length += 1 + # It's not clear what we should do with ':' in the flow context. + if (self.flow_level and ch == u':' + and self.peek(length+1) not in u'\0 \t\r\n\x85\u2028\u2029,[]{}'): + self.forward(length) + raise ScannerError("while scanning a plain scalar", start_mark, + "found unexpected ':'", self.get_mark(), + "Please check http://pyyaml.org/wiki/YAMLColonInFlowContext for details.") if length == 0: break self.allow_simple_key = False diff --git a/lib3/yaml/scanner.py b/lib3/yaml/scanner.py index 3b494072..1107f107 100644 --- a/lib3/yaml/scanner.py +++ b/lib3/yaml/scanner.py @@ -1266,7 +1266,7 @@ def scan_flow_scalar_breaks(self, double, start_mark): def scan_plain(self): # See the specification for details. # We add an additional restriction for the flow context: - # plain scalars in the flow context cannot contain ',' or '?'. + # plain scalars in the flow context cannot contain ',', ':' and '?'. # We also keep track of the `allow_simple_key` flag here. # Indentation rules are loosed for the flow context. chunks = [] @@ -1285,12 +1285,18 @@ def scan_plain(self): while True: ch = self.peek(length) if ch in '\0 \t\r\n\x85\u2028\u2029' \ - or (ch == ':' and - self.peek(length+1) in '\0 \t\r\n\x85\u2028\u2029' - + (u',[]{}' if self.flow_level else u''))\ - or (self.flow_level and ch in ',?[]{}'): + or (not self.flow_level and ch == ':' and + self.peek(length+1) in '\0 \t\r\n\x85\u2028\u2029') \ + or (self.flow_level and ch in ',:?[]{}'): break length += 1 + # It's not clear what we should do with ':' in the flow context. + if (self.flow_level and ch == ':' + and self.peek(length+1) not in '\0 \t\r\n\x85\u2028\u2029,[]{}'): + self.forward(length) + raise ScannerError("while scanning a plain scalar", start_mark, + "found unexpected ':'", self.get_mark(), + "Please check http://pyyaml.org/wiki/YAMLColonInFlowContext for details.") if length == 0: break self.allow_simple_key = False diff --git a/tests/data/colon-in-flow-context.loader-error b/tests/data/colon-in-flow-context.loader-error new file mode 100644 index 00000000..13d50875 --- /dev/null +++ b/tests/data/colon-in-flow-context.loader-error @@ -0,0 +1 @@ +{ foo:bar }