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


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


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