Kirill Simonov avatar Kirill Simonov committed bdc854e

Revamp the inf/nan handling again.

Comments (0)

Files changed (5)

lib/yaml/constructor.py

             return sign*int(value)
 
     inf_value = 1e300
-    while repr(inf_value) != repr(inf_value*inf_value):
+    while inf_value != inf_value*inf_value:
         inf_value *= inf_value
-    nan_value = inf_value/inf_value
+    nan_value = -inf_value/inf_value   # Trying to make a quiet NaN (like C99).
 
     def construct_yaml_float(self, node):
         value = str(self.construct_scalar(node))

lib/yaml/reader.py

             self.determine_encoding()
 
     def peek(self, index=0):
-        if self.pointer+index+1 >= len(self.buffer):
+        try:
+            return self.buffer[self.pointer+index]
+        except IndexError:
             self.update(index+1)
-        return self.buffer[self.pointer+index]
+            return self.buffer[self.pointer+index]
 
     def prefix(self, length=1):
         if self.pointer+length >= len(self.buffer):
     def forward(self, length=1):
         if self.pointer+length+1 >= len(self.buffer):
             self.update(length+1)
-        for k in range(length):
+        while length:
             ch = self.buffer[self.pointer]
             self.pointer += 1
             self.index += 1
                 self.column = 0
             elif ch != u'\uFEFF':
                 self.column += 1
+            length -= 1
 
     def get_mark(self):
         if self.stream is None:

lib/yaml/representer.py

     inf_value = 1e300
     while repr(inf_value) != repr(inf_value*inf_value):
         inf_value *= inf_value
-    nan_value = inf_value/inf_value
-
-    repr_pos_inf = repr(inf_value)
-    repr_neg_inf = repr(-inf_value)
-    repr_nan = repr(inf_value/inf_value)
 
     def represent_float(self, data):
-        repr_data = repr(data)
-        if repr_data == self.repr_pos_inf:
+        if data != data or (data == 0.0 and data == 1.0):
+            value = u'.nan'
+        elif data == self.inf_value:
             value = u'.inf'
-        elif repr_data == self.repr_neg_inf:
+        elif data == -self.inf_value:
             value = u'-.inf'
-        elif repr_data == self.repr_nan:
-            value = u'.nan'
         else:
-            value = unicode(repr_data)
+            value = unicode(repr(data))
         return self.represent_scalar(u'tag:yaml.org,2002:float', value)
 
     def represent_list(self, data):

tests/test_constructor.py

                     data2 = [(repr(key), value) for key, value in data2.items()]
                     data2.sort()
                     data2 = repr(data2)
-                if data1 != data2:
+                    if data1 != data2:
+                        raise
+                elif isinstance(data1, list):
+                    self.failUnlessEqual(type(data1), type(data2))
+                    self.failUnlessEqual(len(data1), len(data2))
+                    for item1, item2 in zip(data1, data2):
+                        if (item1 != item1 or (item1 == 0.0 and item1 == 1.0)) and  \
+                                (item2 != item2 or (item2 == 0.0 and item2 == 1.0)):
+                            continue
+                        self.failUnlessEqual(item1, item2)
+                else:
                     raise
         except:
             print

tests/test_representer.py

                     self.failUnlessEqual(type(data1), type(data2))
                     self.failUnlessEqual(len(data1), len(data2))
                     for item1, item2 in zip(data1, data2):
+                        if (item1 != item1 or (item1 == 0.0 and item1 == 1.0)) and  \
+                                (item2 != item2 or (item2 == 0.0 and item2 == 1.0)):
+                            continue
                         self.failUnlessEqual(item1, item2)
                 else:
                     raise
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.