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

Eric Engestrom eric.engestrom at intel.com
Thu Jul 5 16:55:10 UTC 2018


On Thursday, 2018-07-05 09:33:24 -0700, Dylan Baker wrote:
> 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.

Agreed, but the mix was already there, this patch doesn't change it.
I think it should be cleaned up at some point, but for now this patch is
ok with me, although I'd like to replace the exception with a version
check like you suggested in 18/26; with that:
Reviewed-by: Eric Engestrom <eric.engestrom at intel.com>

> 
> 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


More information about the mesa-dev mailing list