summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2017-08-05 12:28:35 +0200
committerAnthon van der Neut <anthon@mnt.org>2017-08-05 12:28:35 +0200
commitea1cd43722d6e8cc6ddf9190b09a5ad818313be6 (patch)
tree8e486f2180f2b67b21b067203ee53a202089b875
parentc4dda520edb15b010c9134d6f81b26abbb1c8a90 (diff)
downloadruamel.yaml-ea1cd43722d6e8cc6ddf9190b09a5ad818313be6.tar.gz
warn on incorrect YAML 1.1 floats
-rw-r--r--constructor.py12
-rw-r--r--error.py26
2 files changed, 35 insertions, 3 deletions
diff --git a/constructor.py b/constructor.py
index c957cd0..aa77b47 100644
--- a/constructor.py
+++ b/constructor.py
@@ -11,7 +11,8 @@ import sys
import types
import warnings
-from ruamel.yaml.error import (MarkedYAMLError, MarkedYAMLFutureWarning)
+from ruamel.yaml.error import (MarkedYAMLError, MarkedYAMLFutureWarning,
+ MantissaNoDotYAML1_1Warning)
from ruamel.yaml.nodes import * # NOQA
from ruamel.yaml.nodes import (SequenceNode, MappingNode, ScalarNode)
from ruamel.yaml.compat import (utf8, builtins_module, to_str, PY2, PY3, # NOQA
@@ -411,8 +412,8 @@ class SafeConstructor(BaseConstructor):
def construct_yaml_float(self, node):
# type: (Any) -> float
- value_s = to_str(self.construct_scalar(node))
- value_s = value_s.replace('_', '').lower()
+ value_so = to_str(self.construct_scalar(node))
+ value_s = value_so.replace('_', '').lower()
sign = +1
if value_s[0] == '-':
sign = -1
@@ -432,6 +433,11 @@ class SafeConstructor(BaseConstructor):
base *= 60
return sign * value
else:
+ if self.resolver.processing_version != (1, 2) and 'e' in value_s:
+ # value_s is lower case independent of input
+ mantissa, exponent = value_s.split('e')
+ if '.' not in mantissa:
+ warnings.warn(MantissaNoDotYAML1_1Warning(node, value_so))
return sign * float(value_s)
if PY3:
diff --git a/error.py b/error.py
index 4faab94..4658f8e 100644
--- a/error.py
+++ b/error.py
@@ -193,6 +193,32 @@ In most other cases you should consider using 'safe_load(stream)'"""
warnings.simplefilter('once', UnsafeLoaderWarning)
+class MantissaNoDotYAML1_1Warning(YAMLWarning):
+ def __init__(self, node, flt_str):
+ self.node = node
+ self.flt = flt_str
+
+ def __str__(self):
+ line = self.node.start_mark.line
+ col = self.node.start_mark.column
+ return """
+In YAML 1.1 floating point values should have a dot ('.') in their mantissa.
+See the Floating-Point Language-Independent Type for YAML™ Version 1.1 specification
+( http://yaml.org/type/float.html ). This dot is not required for JSON nor for YAML 1.2
+
+Correct your float: "{}" on line: {}, column: {}
+
+or alternatively include the following in your code:
+
+ import warnings
+ warnings.simplefilter('ignore', ruamel.yaml.error.MantissaNoDotYAML1_1Warning)
+
+""".format(self.flt, line, col)
+
+
+warnings.simplefilter('once', MantissaNoDotYAML1_1Warning)
+
+
class YAMLFutureWarning(Warning):
pass