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

Dylan Baker dylan at pnwbakers.com
Thu Jul 5 17:14:03 UTC 2018


Quoting Eric Engestrom (2018-07-05 09:55:10)
> 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.

The problem is that python 2 tries to be helpful and coerces bytes and unicode
for you. Python 3 does the right thing and dies in a fire when you try to do
operations one both types.

> 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
-------------- 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/473ef108/attachment.sig>


More information about the mesa-dev mailing list