Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit d64d05a

Browse files
Denys Smirnovdennwc
authored andcommitted
handle nan and infinity values properly; fixes #182
Signed-off-by: Denys Smirnov <[email protected]>
1 parent 1735ff9 commit d64d05a

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

fixtures/issue_182.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_POS_INF = 1e10000

fixtures/issue_182.py.native

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
'PY3AST': {
3+
'ast_type': "Module",
4+
body: [
5+
{
6+
'ast_type': "Assign",
7+
'col_offset': 1,
8+
lineno: 1,
9+
targets: [
10+
{
11+
'ast_type': "Name",
12+
'col_offset': 1,
13+
ctx: "Store",
14+
'end_col_offset': 9,
15+
'end_lineno': 1,
16+
id: "_POS_INF",
17+
lineno: 1,
18+
},
19+
],
20+
value: {
21+
'ast_type': "Num",
22+
'col_offset': 12,
23+
lineno: 1,
24+
'n': "inf",
25+
},
26+
},
27+
],
28+
},
29+
}

fixtures/issue_182.py.sem.uast

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{ '@type': "python:Module",
2+
'@role': [File, Module],
3+
'@pos': { '@type': "uast:Positions",
4+
},
5+
body: [
6+
{ '@type': "python:Assign",
7+
'@role': [Assignment, Binary, Expression],
8+
'@pos': { '@type': "uast:Positions",
9+
start: { '@type': "uast:Position",
10+
offset: 0,
11+
line: 1,
12+
col: 1,
13+
},
14+
},
15+
targets: [
16+
{ '@type': "python:BoxedName",
17+
'@role': [Left],
18+
'boxed_value': { '@type': "uast:Identifier",
19+
'@pos': { '@type': "uast:Positions",
20+
start: { '@type': "uast:Position",
21+
offset: 0,
22+
line: 1,
23+
col: 1,
24+
},
25+
end: { '@type': "uast:Position",
26+
offset: 8,
27+
line: 1,
28+
col: 9,
29+
},
30+
},
31+
Name: "_POS_INF",
32+
},
33+
ctx: "Store",
34+
},
35+
],
36+
value: { '@type': "python:Num",
37+
'@token': "inf",
38+
'@role': [Expression, Literal, Number, Primitive, Right],
39+
'@pos': { '@type': "uast:Positions",
40+
start: { '@type': "uast:Position",
41+
offset: 11,
42+
line: 1,
43+
col: 12,
44+
},
45+
},
46+
},
47+
},
48+
],
49+
}

fixtures/issue_182.py.uast

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{ '@type': "Module",
2+
'@role': [File, Module],
3+
'@pos': { '@type': "uast:Positions",
4+
},
5+
body: [
6+
{ '@type': "Assign",
7+
'@role': [Assignment, Binary, Expression],
8+
'@pos': { '@type': "uast:Positions",
9+
start: { '@type': "uast:Position",
10+
offset: 0,
11+
line: 1,
12+
col: 1,
13+
},
14+
},
15+
targets: [
16+
{ '@type': "Name",
17+
'@token': "_POS_INF",
18+
'@role': [Expression, Identifier, Left],
19+
'@pos': { '@type': "uast:Positions",
20+
start: { '@type': "uast:Position",
21+
offset: 0,
22+
line: 1,
23+
col: 1,
24+
},
25+
end: { '@type': "uast:Position",
26+
offset: 8,
27+
line: 1,
28+
col: 9,
29+
},
30+
},
31+
ctx: "Store",
32+
},
33+
],
34+
value: { '@type': "Num",
35+
'@token': "inf",
36+
'@role': [Expression, Literal, Number, Primitive, Right],
37+
'@pos': { '@type': "uast:Positions",
38+
start: { '@type': "uast:Position",
39+
offset: 11,
40+
line: 1,
41+
col: 12,
42+
},
43+
},
44+
},
45+
},
46+
],
47+
}

native/python_package/python_driver/astimprove.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
import tokenize
7+
import math
78
from codecs import encode
89
from copy import deepcopy
910
from io import BytesIO
@@ -125,6 +126,9 @@ def visit_Num(self, node: Node) -> Node:
125126
if isinstance(node["n"], complex):
126127
node.update({"n": {"real": node["n"].real,
127128
"imag": node["n"].imag}})
129+
# infinity and nan are not json-serializable
130+
elif not math.isfinite(node["n"]):
131+
node.update({"n": str(node["n"])})
128132
return node
129133

130134
def visit_NoneType(self, node: Node) -> Node:

0 commit comments

Comments
 (0)