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

Dylan Baker dylan at pnwbakers.com
Thu Jul 5 16:33:24 UTC 2018


I've done enough python 2 -> 3 porting to feel very nervous about this, my
experience tells me that mixing bytes and unicode always leads to subtle and
hard to track down bugs. I'd much rather enforce that we're always getting
unicode or bytes, but not mixing them.

Quoting Mathieu Bridon (2018-07-05 06:17:48)
> 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
> 
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: signature
URL: <https://lists.freedesktop.org/archives/mesa-dev/attachments/20180705/bba4cb2b/attachment.sig>


More information about the mesa-dev mailing list