[Mesa-dev] [PATCH v2 5/9] python: Better check for integer types

Mathieu Bridon bochecha at daitauha.fr
Thu Aug 9 08:27:22 UTC 2018


Python 3 lost the long type: now everything is an int, with the right
size.

This commit makes the script compatible with Python 2 (where we check
for both int and long) and Python 3 (where we only check for int).

Signed-off-by: Mathieu Bridon <bochecha at daitauha.fr>
---
 src/compiler/nir/nir_algebraic.py           |  8 +++++---
 src/gallium/auxiliary/util/u_format_pack.py | 13 +++++++++++--
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/src/compiler/nir/nir_algebraic.py b/src/compiler/nir/nir_algebraic.py
index 5baeea88af..3055937029 100644
--- a/src/compiler/nir/nir_algebraic.py
+++ b/src/compiler/nir/nir_algebraic.py
@@ -36,9 +36,11 @@ import traceback
 from nir_opcodes import opcodes
 
 if sys.version_info < (3, 0):
+    integer_types = (int, long)
     string_type = unicode
 
 else:
+    integer_types = (int, )
     string_type = str
 
 _type_re = re.compile(r"(?P<type>int|uint|bool|float)?(?P<bits>\d+)?")
@@ -81,7 +83,7 @@ class Value(object):
          return val
       elif isinstance(val, string_type):
          return Variable(val, name_base, varset)
-      elif isinstance(val, (bool, int, long, float)):
+      elif isinstance(val, (bool, float) + integer_types):
          return Constant(val, name_base)
 
    __template = mako.template.Template("""
@@ -145,7 +147,7 @@ class Constant(Value):
    def hex(self):
       if isinstance(self.value, (bool)):
          return 'NIR_TRUE' if self.value else 'NIR_FALSE'
-      if isinstance(self.value, (int, long)):
+      if isinstance(self.value, integer_types):
          return hex(self.value)
       elif isinstance(self.value, float):
          i = struct.unpack('Q', struct.pack('d', self.value))[0]
@@ -164,7 +166,7 @@ class Constant(Value):
    def type(self):
       if isinstance(self.value, (bool)):
          return "nir_type_bool32"
-      elif isinstance(self.value, (int, long)):
+      elif isinstance(self.value, integer_types):
          return "nir_type_int"
       elif isinstance(self.value, float):
          return "nir_type_float"
diff --git a/src/gallium/auxiliary/util/u_format_pack.py b/src/gallium/auxiliary/util/u_format_pack.py
index ad2e49281f..c1307d30c2 100644
--- a/src/gallium/auxiliary/util/u_format_pack.py
+++ b/src/gallium/auxiliary/util/u_format_pack.py
@@ -38,9 +38,18 @@
 
 from __future__ import division, print_function
 
+import sys
+
 from u_format_parse import *
 
 
+if sys.version_info < (3, 0):
+    integer_types = (int, long)
+
+else:
+    integer_types = (int, )
+
+
 def inv_swizzles(swizzles):
     '''Return an array[4] of inverse swizzle terms'''
     '''Only pick the first matching value to avoid l8 getting blue and i8 getting alpha'''
@@ -212,7 +221,7 @@ def truncate_mantissa(x, bits):
     '''Truncate an integer so it can be represented exactly with a floating
     point mantissa'''
 
-    assert isinstance(x, (int, long))
+    assert isinstance(x, integer_types)
 
     s = 1
     if x < 0:
@@ -236,7 +245,7 @@ def value_to_native(type, value):
     '''Get the value of unity for this type.'''
     if type.type == FLOAT:
         if type.size <= 32 \
-            and isinstance(value, (int, long)):
+            and isinstance(value, integer_types):
             return truncate_mantissa(value, 23)
         return value
     if type.type == FIXED:
-- 
2.17.1



More information about the mesa-dev mailing list