From patchwork Tue Feb 21 13:03:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Marczykowski-G=C3=B3recki?= X-Patchwork-Id: 9584533 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A1BA0602A7 for ; Tue, 21 Feb 2017 13:05:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7F684285AE for ; Tue, 21 Feb 2017 13:05:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 748FA2867D; Tue, 21 Feb 2017 13:05:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id CE165285AE for ; Tue, 21 Feb 2017 13:05:19 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cgA60-0005a4-Ml; Tue, 21 Feb 2017 13:03:08 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cgA5z-0005Zt-0V for xen-devel@lists.xen.org; Tue, 21 Feb 2017 13:03:07 +0000 Received: from [85.158.139.211] by server-7.bemta-5.messagelabs.com id BB/96-02154-A8A3CA85; Tue, 21 Feb 2017 13:03:06 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFupgleJIrShJLcpLzFFi42JxymeR0e20WhN h0LxV3GLJx8UsDoweR3f/ZgpgjGLNzEvKr0hgzbjy+RxbwVzViqsv9zA1MF6W72Lk4hASWMUo se/dR9YuRk4gJ1viw9r3rCAJFoFtzBINx86AJSQEPCVmHbjMBmE7S7Rues4C0b2BUeJUexcjS IJFQFVi46vZTCA2m0CwxPUlv8CaRQQUJR68vAlmMwsESrRPbQSrERbQkljyazHYUF4BHYmPH1 +yQ1wRLXFw3lYWiLigxMmZT1ggeksljh97ATSHA8iWllj+jwMkzClgKbHm+3ywElEBZYm/h++ xTGAUmoWkexaS7lkI3RBhdYk/8y4xYwhrSyxb+JoZwraVWLfuPcsCRvZVjOrFqUVlqUW6hnpJ RZnpGSW5iZk5uoYGpnq5qcXFiempOYlJxXrJ+bmbGIGxUs/AwLiDsanX+RCjJAeTkihvttSaC CG+pPyUyozE4oz4otKc1OJDjDIcHEoSvLctgXKCRanpqRVpmTnAqIVJS3DwKInw9oKkeYsLEn OLM9MhUqcYdTlO3Tj9kkmIJS8/L1VKnHc6SJEASFFGaR7cCFgCucQoKyXMy8jAwCDEU5BalJt Zgir/ilGcg1FJmDcYZApPZl4J3KZXQEcwAR1x02MlyBEliQgpqQbG3jldXS92sPoufPAgxXHx lfdHw4NPHdj8h0Fa+rdl7dZLBlqV6Ux/Nn8w6eSv+aO0i8k7PIjTsTNOSDy5Ovb8VE+24lj/K bvqZ1SfeRd090c3p8Da13cWhs/cqb1lxy2f7NsTN/ldDuH78vKNv7/op1d11n6Fmyf9qvx/2V mfeXJFuticaEZVJZbijERDLeai4kQAQ7tQDxsDAAA= X-Env-Sender: marmarek@invisiblethingslab.com X-Msg-Ref: server-13.tower-206.messagelabs.com!1487682184!70450747!1 X-Originating-IP: [66.111.4.28] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.2.3; banners=-,-,- X-VirusChecked: Checked Received: (qmail 42700 invoked from network); 21 Feb 2017 13:03:05 -0000 Received: from out4-smtp.messagingengine.com (HELO out4-smtp.messagingengine.com) (66.111.4.28) by server-13.tower-206.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 21 Feb 2017 13:03:05 -0000 Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 23BFD20AA5; Tue, 21 Feb 2017 08:03:04 -0500 (EST) Received: from frontend2 ([10.202.2.161]) by compute7.internal (MEProxy); Tue, 21 Feb 2017 08:03:04 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc:x-sasl-enc; s=smtpout; bh=Onzr0ahs1e3JLy VmJBCqWPvZSC0=; b=ZcoFu0phwJa4GFigxMSysj7DsALhiSCdYSusl6LfUHksDt EpWvYk9AmdFog52FCtoLQa/8S3F/S9x/yMzTX/sn8BK2PRbS+0vPlwBy/AjCfpK9 rV/fnhkGK6CRTrXOXB5LnI6qv6n8thLq9fIN4bEjDApK4r+rbrI/pqI1GiYDk= X-ME-Sender: X-Sasl-enc: Hm7e/YotxtUxDSC7mPhrlXf+FtBdqQRPSzqHKugbMpLE 1487682183 Received: from mail-itl (89-70-103-23.dynamic.chello.pl [89.70.103.23]) by mail.messagingengine.com (Postfix) with ESMTPA id 474F724357; Tue, 21 Feb 2017 08:03:03 -0500 (EST) Date: Tue, 21 Feb 2017 14:03:00 +0100 From: Marek =?utf-8?Q?Marczykowski-G=C3=B3recki?= To: Wei Liu Message-ID: <20170221130300.GF1146@mail-itl> References: <20170217123601.GF12171@mail-itl> <20170220171844.ifeumcygyk4hglb6@citrix.com> MIME-Version: 1.0 In-Reply-To: <20170220171844.ifeumcygyk4hglb6@citrix.com> User-Agent: Mutt/1.7.1 (2016-10-04) Cc: Ian Jackson , xen-devel Subject: Re: [Xen-devel] Python 3 bindings X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP On Mon, Feb 20, 2017 at 05:18:44PM +0000, Wei Liu wrote: > On Fri, Feb 17, 2017 at 01:36:01PM +0100, Marek Marczykowski-Górecki wrote: > > Hi, > > > > I'm adjusting python bindings to work on python3 too. This will require > > few #if in the code (to compile for both python2 and python3), but it > > isn't that bad. But there are some major changes in python3, which > > require some decision about the bindings API: > > > > 1. Python3 has no longer separate 'int' and 'long' type - old 'long' > > type was renamed to 'int' (but on C-API level, it uses PyLong_*). I see > > two options: > > - switch to PyLong_* everywhere, including python2 bindings - this > > makes the code much cleaner, but it is an API change in python2 > > - switch to PyLong_* only for python3 - this will introduce some > > #ifdefs, but python2 API will be unchanged > > Could you be more specific? Like, provide a code snippet? Here is compile tested only version: https://github.com/marmarek/xen/tree/python3 It uses PyLong_* only for python3, here is how it looks in code (I've skipped s/PyInt_/PyLongOrInt_/ for readability): -----8<----- -----8<----- > > > > > 2. Python3 has no longer separate 'str' and 'unicode' type, new 'str' is > > the same as 'unicode' (PyUnicode_* at C-API level). For things not > > really unicode-aware, 'bytes' type should be used. On the other hand, in > > python2 'bytes' type was the same as 'str'. > > This affects various places, where in most cases 'bytes' type is > > appropriate (for example cpuid). But I'm not sure about xenstore paths - > > those should also be 'bytes', or maybe 'unicode' (which is implicitly > > using 'utf-8' encoding)? I think the only reason to use 'unicode' is > > According to docs/txt/misc/xenstore.txt, paths should be ASCII > alphanumerics plus four punctuation characters. Not sure if this is > relevant to what you describe. It's easy to make function accept both 'bytes' and 'unicode'. The question is what should be return type (read_watch, ls etc) - given limited character set used there, I'm in favor of 'unicode' - easier to handle, but we shouldn't hit any unicode decoding problems. Maybe the same should apply to path arguments (use 'unicode')? Most file-handling methods in python3 use 'unicode' for paths, if that matters. > > convenience for API users - in python3 if you write 'some string' it > > will be unicode type, to create bytes data you need to write b'some > > string'. > > As for python2, it should definitely be still 'str'/'bytes' type. > > > > There is one more little detail - build process. Here I'm going to > > follow popular standard - use $(PYTHON) variable - if that points to > > python3, build for python3. Actually this means no change in the current > > makefile. If someone want to build for both python2 and python3, will > > need to call the build twice - at packaging level. --- a/tools/python/xen/lowlevel/xc/xc.c +++ b/tools/python/xen/lowlevel/xc/xc.c @@ -34,6 +34,17 @@ #define FLASK_CTX_LEN 1024 +/* Python 2 compatibility */ +#if PY_VERSION_HEX >= 0x03000000 +#define PyLongOrInt_FromLong PyLong_FromLong +#define PyLongOrInt_Check PyLong_Check +#define PyLongOrInt_AsLong PyLong_AsLong +#else +#define PyLongOrInt_FromLong PyInt_FromLong +#define PyLongOrInt_Check PyInt_Check +#define PyLongOrInt_AsLong PyInt_AsLong +#endif + static PyObject *xc_error_obj, *zero; typedef struct { --- a/tools/python/xen/lowlevel/xs/xs.c +++ b/tools/python/xen/lowlevel/xs/xs.c @@ -43,6 +43,14 @@ #define PKG "xen.lowlevel.xs" #define CLS "xs" +#if PY_VERSION_HEX < 0x03000000 +/* Python 2 compatibility */ +#define PyLong_FromLong PyInt_FromLong +#undef PyLong_Check +#define PyLong_Check PyInt_Check +#define PyLong_AsLong PyInt_AsLong +#endif + static PyObject *xs_error; /** Python wrapper round an xs handle.