scripts: Fixed CsvInt(CsvFloat(mt.inf)) bypassing int/float cast
Turns out mt.isinf is happy to accept non-primitive floats (such as CsvFloat) as long as __float__ is defined. But CsvInt expects a float inf, not a CsvFloat, so things explode later. Fixed by explicitly casting to float if mt.isinf, instead of passing as-is. Also tweaked CsvInt/CsvFloat constructors to not bother checking isinstance, unconditional int/float casts are probably cheaper than the condition in Python.
This commit is contained in:
+1
-3
@@ -51,9 +51,7 @@ class CsvInt(co.namedtuple('CsvInt', 'a')):
|
|||||||
a = -mt.inf
|
a = -mt.inf
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
if not (isinstance(a, int) or mt.isinf(a)):
|
return super().__new__(cls, float(a) if mt.isinf(a) else int(a))
|
||||||
a = int(a)
|
|
||||||
return super().__new__(cls, a)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '%s(%r)' % (self.__class__.__name__, self.a)
|
return '%s(%r)' % (self.__class__.__name__, self.a)
|
||||||
|
|||||||
+1
-3
@@ -53,9 +53,7 @@ class CsvInt(co.namedtuple('CsvInt', 'a')):
|
|||||||
a = -mt.inf
|
a = -mt.inf
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
if not (isinstance(a, int) or mt.isinf(a)):
|
return super().__new__(cls, float(a) if mt.isinf(a) else int(a))
|
||||||
a = int(a)
|
|
||||||
return super().__new__(cls, a)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '%s(%r)' % (self.__class__.__name__, self.a)
|
return '%s(%r)' % (self.__class__.__name__, self.a)
|
||||||
|
|||||||
+2
-6
@@ -76,9 +76,7 @@ class CsvInt(co.namedtuple('CsvInt', 'a')):
|
|||||||
a = -mt.inf
|
a = -mt.inf
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
if not (isinstance(a, int) or mt.isinf(a)):
|
return super().__new__(cls, float(a) if mt.isinf(a) else int(a))
|
||||||
a = int(a)
|
|
||||||
return super().__new__(cls, a)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '%s(%r)' % (self.__class__.__name__, self.a)
|
return '%s(%r)' % (self.__class__.__name__, self.a)
|
||||||
@@ -186,9 +184,7 @@ class CsvFloat(co.namedtuple('CsvFloat', 'a')):
|
|||||||
a = -mt.inf
|
a = -mt.inf
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
if not isinstance(a, float):
|
return super().__new__(cls, float(a))
|
||||||
a = float(a)
|
|
||||||
return super().__new__(cls, a)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '%s(%r)' % (self.__class__.__name__, self.a)
|
return '%s(%r)' % (self.__class__.__name__, self.a)
|
||||||
|
|||||||
+1
-3
@@ -47,9 +47,7 @@ class CsvInt(co.namedtuple('CsvInt', 'a')):
|
|||||||
a = -mt.inf
|
a = -mt.inf
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
if not (isinstance(a, int) or mt.isinf(a)):
|
return super().__new__(cls, float(a) if mt.isinf(a) else int(a))
|
||||||
a = int(a)
|
|
||||||
return super().__new__(cls, a)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '%s(%r)' % (self.__class__.__name__, self.a)
|
return '%s(%r)' % (self.__class__.__name__, self.a)
|
||||||
|
|||||||
+1
-3
@@ -51,9 +51,7 @@ class CsvInt(co.namedtuple('CsvInt', 'a')):
|
|||||||
a = -mt.inf
|
a = -mt.inf
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
if not (isinstance(a, int) or mt.isinf(a)):
|
return super().__new__(cls, float(a) if mt.isinf(a) else int(a))
|
||||||
a = int(a)
|
|
||||||
return super().__new__(cls, a)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '%s(%r)' % (self.__class__.__name__, self.a)
|
return '%s(%r)' % (self.__class__.__name__, self.a)
|
||||||
|
|||||||
+1
-3
@@ -61,9 +61,7 @@ class CsvInt(co.namedtuple('CsvInt', 'a')):
|
|||||||
a = -mt.inf
|
a = -mt.inf
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
if not (isinstance(a, int) or mt.isinf(a)):
|
return super().__new__(cls, float(a) if mt.isinf(a) else int(a))
|
||||||
a = int(a)
|
|
||||||
return super().__new__(cls, a)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '%s(%r)' % (self.__class__.__name__, self.a)
|
return '%s(%r)' % (self.__class__.__name__, self.a)
|
||||||
|
|||||||
+1
-3
@@ -52,9 +52,7 @@ class CsvInt(co.namedtuple('CsvInt', 'a')):
|
|||||||
a = -mt.inf
|
a = -mt.inf
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
if not (isinstance(a, int) or mt.isinf(a)):
|
return super().__new__(cls, float(a) if mt.isinf(a) else int(a))
|
||||||
a = int(a)
|
|
||||||
return super().__new__(cls, a)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '%s(%r)' % (self.__class__.__name__, self.a)
|
return '%s(%r)' % (self.__class__.__name__, self.a)
|
||||||
|
|||||||
+1
-3
@@ -47,9 +47,7 @@ class CsvInt(co.namedtuple('CsvInt', 'a')):
|
|||||||
a = -mt.inf
|
a = -mt.inf
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
if not (isinstance(a, int) or mt.isinf(a)):
|
return super().__new__(cls, float(a) if mt.isinf(a) else int(a))
|
||||||
a = int(a)
|
|
||||||
return super().__new__(cls, a)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '%s(%r)' % (self.__class__.__name__, self.a)
|
return '%s(%r)' % (self.__class__.__name__, self.a)
|
||||||
|
|||||||
+1
-3
@@ -47,9 +47,7 @@ class CsvInt(co.namedtuple('CsvInt', 'a')):
|
|||||||
a = -mt.inf
|
a = -mt.inf
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
if not (isinstance(a, int) or mt.isinf(a)):
|
return super().__new__(cls, float(a) if mt.isinf(a) else int(a))
|
||||||
a = int(a)
|
|
||||||
return super().__new__(cls, a)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '%s(%r)' % (self.__class__.__name__, self.a)
|
return '%s(%r)' % (self.__class__.__name__, self.a)
|
||||||
|
|||||||
Reference in New Issue
Block a user