This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [patch] [python] Fix Python 3 build and testsuite issues
- From: <Paul_Koning at Dell dot com>
- To: <tromey at redhat dot com>
- Cc: <pmuldoon at redhat dot com>, <gdb-patches at sourceware dot org>
- Date: Wed, 21 Aug 2013 15:37:06 +0000
- Subject: Re: [patch] [python] Fix Python 3 build and testsuite issues
- References: <521230C8 dot 2040803 at redhat dot com> <878uzxlkl1 dot fsf at fleche dot redhat dot com> <52124B8D dot 6010609 at redhat dot com> <87ppt9jzrl dot fsf at fleche dot redhat dot com> <5213C6BA dot 7030703 at redhat dot com> <87d2p8gmlo dot fsf at fleche dot redhat dot com> <5213D26D dot 4070003 at redhat dot com> <5214CECF dot 30103 at redhat dot com> <8761uzf5t3 dot fsf at fleche dot redhat dot com>
On Aug 21, 2013, at 10:59 AM, Tom Tromey <tromey@redhat.com> wrote:
> Phil> Strings in Python 3 are now always encoded and are encapsulated by the
> Phil> "str" class.
>
> Phil> In Python 2 you had str() and unicode(), where unicode was encoded and
> Phil> str just represented bytes (IE just an unencoded string).
>
> Phil> Reading around the suggestion seems to be to do this:
>
> Phil> try:
> Phil> # basestring catches both types of Python 2.x strings
> Phil> if isinstance(sym, basestring)
> Phil> return True
> Phil> except NameError:
> Phil> # If we are here, basestring does not exist, so Python 3.x
> Phil> if isinstance(sym, str)
> Phil> return True
> Phil> # Continue to process objects that are not a string.
>
> We can do this check once, at top-level:
>
>
> try:
> if isinstance('hi', basestring):
> def is_string(x):
> return isinstance(x, basestring)
> except NameError:
> def isinstance(x):
> return isinstance(x, str)
>
>
> Maybe duck typing is still preferable though.
>
> Not sure if this needs a third def in case the 'if' fails without throwing.
> Probably not.
I would write it this way:
try:
basestring
except NameError:
basestring = str
Similar techniques can be used for type "long" which isn't in Python 3 either.
--paul