Handle NaN and infinity values

Right now, NaN/infinity values will produce "nan" and "inf", but
Python doesn't allow those in source code. This change will wrap
those values in float(''), which is allowed.

Tests for Python 2.7 and 3.8 have been added as well.

Fixes #136
This commit is contained in:
John Richards
2020-10-15 20:57:56 -04:00
parent 36d93bd1a5
commit 870ecdc430
5 changed files with 26 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
#include "pyc_numeric.h"
#include "bytecode.h"
#include <cmath>
#ifdef _MSC_VER
#define snprintf _snprintf
@@ -268,7 +269,15 @@ void print_const(PycRef<PycObject> obj, PycModule* mod)
obj.cast<PycComplex>()->imag());
break;
case PycObject::TYPE_BINARY_FLOAT:
fprintf(pyc_output, "%g", obj.cast<PycCFloat>()->value());
{
// Wrap any nan/inf values in float('').
double value = obj.cast<PycCFloat>()->value();
if (std::isnan(value) || std::isinf(value)) {
fprintf(pyc_output, "float('%g')", value);
} else {
fprintf(pyc_output, "%g", value);
}
}
break;
case PycObject::TYPE_BINARY_COMPLEX:
fprintf(pyc_output, "(%g+%gj)", obj.cast<PycCComplex>()->value(),