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: Tom Tromey <tromey at redhat dot com>
- To: Phil Muldoon <pmuldoon at redhat dot com>
- Cc: gdb-patches at sourceware dot org
- Date: Wed, 21 Aug 2013 08:59:36 -0600
- 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>
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.
Tom