[Mesa-dev] [PATCH 17/26] python: Better check for string types

Mathieu Bridon bochecha at daitauha.fr
Thu Jul 5 13:17:48 UTC 2018


Python 2 byte strings were called "str", and its unicode strings were
called "unicode".

In Python 3, they are called "bytes" and "str".

This commit makes the script compatible with Python 2 and Python 3,
checking for the right types on both.

Signed-off-by: Mathieu Bridon <bochecha at daitauha.fr>
---
 src/compiler/nir/nir_algebraic.py | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/compiler/nir/nir_algebraic.py b/src/compiler/nir/nir_algebraic.py
index fda72d3c69..e17e2d26b9 100644
--- a/src/compiler/nir/nir_algebraic.py
+++ b/src/compiler/nir/nir_algebraic.py
@@ -35,6 +35,13 @@ import traceback
 
 from nir_opcodes import opcodes
 
+try:
+    string_types = (str, unicode)
+
+except NameError:
+    # This is Python 3
+    string_types = (bytes, str)
+
 _type_re = re.compile(r"(?P<type>int|uint|bool|float)?(?P<bits>\d+)?")
 
 def type_bits(type_str):
@@ -70,7 +77,7 @@ class Value(object):
          return Expression(val, name_base, varset)
       elif isinstance(val, Expression):
          return val
-      elif isinstance(val, (str, unicode)):
+      elif isinstance(val, string_types):
          return Variable(val, name_base, varset)
       elif isinstance(val, (bool, int, long, float)):
          return Constant(val, name_base)
-- 
2.17.1



More information about the mesa-dev mailing list