|
35 | 35 | # Format: |
36 | 36 | # Primitives: |
37 | 37 | # * None, int, long, float, str, bool - unchanged |
38 | | -# (json.dumps encodes these fine verbatim) |
| 38 | +# (json.dumps encodes these fine verbatim, except for inf, -inf, and nan) |
| 39 | +# |
| 40 | +# exceptions: float('inf') -> ['SPECIAL_FLOAT', 'Infinity'] |
| 41 | +# float('-inf') -> ['SPECIAL_FLOAT', '-Infinity'] |
| 42 | +# float('nan') -> ['SPECIAL_FLOAT', 'NaN'] |
| 43 | +# x == int(x) -> ['SPECIAL_FLOAT', '%.1f' % x] |
| 44 | +# (this way, 3.0 prints as '3.0' and not as 3, which looks like an int) |
39 | 45 | # |
40 | 46 | # If render_heap_primitives is True, then primitive values are rendered |
41 | 47 | # on the heap as ['HEAP_PRIMITIVE', <type name>, <value>] |
42 | 48 | # |
| 49 | +# (for SPECIAL_FLOAT values, <value> is a list like ['SPECIAL_FLOAT', 'Infinity']) |
| 50 | +# |
43 | 51 | # Compound objects: |
44 | 52 | # * list - ['LIST', elt1, elt2, elt3, ..., eltN] |
45 | 53 | # * tuple - ['TUPLE', elt1, elt2, elt3, ..., eltN] |
|
62 | 70 |
|
63 | 71 | import re, types |
64 | 72 | import sys |
| 73 | +import math |
65 | 74 | typeRE = re.compile("<type '(.*)'>") |
66 | 75 | classRE = re.compile("<class '(.*)'>") |
67 | 76 |
|
68 | 77 | import inspect |
69 | 78 |
|
| 79 | +# TODO: maybe use the 'six' library to smooth over Py2 and Py3 incompatibilities? |
70 | 80 | is_python3 = (sys.version_info[0] == 3) |
71 | 81 | if is_python3: |
72 | 82 | long = None # Avoid NameError when evaluating "long" |
@@ -98,6 +108,26 @@ def get_name(obj): |
98 | 108 |
|
99 | 109 | PRIMITIVE_TYPES = (int, long, float, str, bool, type(None)) |
100 | 110 |
|
| 111 | +def encode_primitive(dat): |
| 112 | + if type(dat) is float: |
| 113 | + if math.isinf(dat): |
| 114 | + if dat > 0: |
| 115 | + return ['SPECIAL_FLOAT', 'Infinity'] |
| 116 | + else: |
| 117 | + return ['SPECIAL_FLOAT', '-Infinity'] |
| 118 | + elif math.isnan(dat): |
| 119 | + return ['SPECIAL_FLOAT', 'NaN'] |
| 120 | + else: |
| 121 | + # render floats like 3.0 as '3.0' and not as 3 |
| 122 | + if dat == int(dat): |
| 123 | + return ['SPECIAL_FLOAT', '%.1f' % dat] |
| 124 | + else: |
| 125 | + return round(dat, FLOAT_PRECISION) |
| 126 | + else: |
| 127 | + # return all other primitives verbatim |
| 128 | + return dat |
| 129 | + |
| 130 | + |
101 | 131 | # Note that this might BLOAT MEMORY CONSUMPTION since we're holding on |
102 | 132 | # to every reference ever created by the program without ever releasing |
103 | 133 | # anything! |
@@ -136,11 +166,7 @@ def encode(self, dat, get_parent): |
136 | 166 | """Encode a data value DAT using the GET_PARENT function for parent ids.""" |
137 | 167 | # primitive type |
138 | 168 | if not self.render_heap_primitives and type(dat) in PRIMITIVE_TYPES: |
139 | | - if type(dat) is float: |
140 | | - return round(dat, FLOAT_PRECISION) |
141 | | - else: |
142 | | - return dat |
143 | | - |
| 169 | + return encode_primitive(dat) |
144 | 170 | # compound type - return an object reference and update encoded_heap_objects |
145 | 171 | else: |
146 | 172 | my_id = id(dat) |
@@ -229,7 +255,7 @@ def encode(self, dat, get_parent): |
229 | 255 | new_obj.extend(['module', dat.__name__]) |
230 | 256 | elif typ in PRIMITIVE_TYPES: |
231 | 257 | assert self.render_heap_primitives |
232 | | - new_obj.extend(['HEAP_PRIMITIVE', type(dat).__name__, dat]) |
| 258 | + new_obj.extend(['HEAP_PRIMITIVE', type(dat).__name__, encode_primitive(dat)]) |
233 | 259 | else: |
234 | 260 | typeStr = str(typ) |
235 | 261 | m = typeRE.match(typeStr) |
|
0 commit comments