From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2368 invoked by alias); 21 Aug 2013 14:29:39 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 2352 invoked by uid 89); 21 Aug 2013 14:29:39 -0000 X-Spam-SWARE-Status: No, score=-8.3 required=5.0 tests=AWL,BAYES_00,KHOP_THREADED,RCVD_IN_HOSTKARMA_W,RCVD_IN_HOSTKARMA_WL,RP_MATCHES_RCVD,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2 Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Wed, 21 Aug 2013 14:29:38 +0000 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r7LETbWQ017342 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 21 Aug 2013 10:29:37 -0400 Received: from localhost.localdomain (ovpn-112-34.ams2.redhat.com [10.36.112.34]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id r7LETZBF022030 for ; Wed, 21 Aug 2013 10:29:36 -0400 Message-ID: <5214CECF.30103@redhat.com> Date: Wed, 21 Aug 2013 14:29:00 -0000 From: Phil Muldoon MIME-Version: 1.0 To: gdb-patches@sourceware.org Subject: Re: [patch] [python] Fix Python 3 build and testsuite issues References: <521230C8.2040803@redhat.com> <878uzxlkl1.fsf@fleche.redhat.com> <52124B8D.6010609@redhat.com> <87ppt9jzrl.fsf@fleche.redhat.com> <5213C6BA.7030703@redhat.com> <87d2p8gmlo.fsf@fleche.redhat.com> <5213D26D.4070003@redhat.com> In-Reply-To: <5213D26D.4070003@redhat.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-SW-Source: 2013-08/txt/msg00578.txt.bz2 On 20/08/13 21:32, Phil Muldoon wrote: > On 20/08/13 20:59, Tom Tromey wrote: >>>>>>> "Phil" == Phil Muldoon writes: >> >> Phil> @@ -236,7 +236,7 @@ class FrameVars(object): >> Phil> # SYM may be a string instead of a symbol in the case of >> Phil> # synthetic local arguments or locals. If that is the case, >> Phil> # always fetch. >> Phil> - if isinstance(sym, basestring): >> Phil> + if isinstance(sym, str): >> Phil> return True >> >> Does this work in all versions? >> I thought perhaps hasattr would be more robust here. > > Tested on both 2.7.3 and 3.3.0 on Fedora 18. basestring seems to > originate from Python 2.3 and was retired in 3.0. I was partially wrong here. Strings in Python 3 are now always encoded and are encapsulated by the "str" class. In Python 2 you had str() and unicode(), where unicode was encoded and str just represented bytes (IE just an unencoded string). So in Python 2: >>> a = "foo" >>> b = u"bar" >>> type(a) >>> type(b) >>> print isinstance(a,str) True >>> print isinstance(b,str) False Whereas in Python 3: >>> a = "foo" >>> b = u"bar" >>> type(a) >>> type(b) >>> print (isinstance(a,str)) True >>> print (isinstance(b,str)) True So the patch hunk: - if isinstance(sym, basestring): + if isinstance(sym, str): Will work in all case for Python 3. Will work in the str() case of Python 2.x (unencoded), but will not work for encoded unicode strings in Python 2.x. Reading around the suggestion seems to be to do this: try: # basestring catches both types of Python 2.x strings if isinstance(sym, basestring) return True except NameError: # If we are here, basestring does not exist, so Python 3.x if isinstance(sym, str) return True # Continue to process objects that are not a string. This kind of sucks. We could come back to a hasattr test for a string, but I am just not sure what is feasible and will remain stable through Python 2.x and 3.x release cycles. We could reverse the condition and hasattr for a symbol like object, but see my previous note on the expectations of the API. Regardless, I think at some point in GDB's future we will have to declare "Python 3.x only from GDB X.X on" as maintaining the two versions of Python is going to become arduous. Cheers, Phil