This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [patch] [python] Fix Python 3 build and testsuite issues


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]