[{"id":1817734,"web_url":"http://patchwork.ozlabs.org/comment/1817734/","msgid":"<87zi6uochn.fsf@dusky.pond.sub.org>","list_archive_url":null,"date":"2017-12-07T14:10:12","subject":"Re: [Qemu-devel] [PATCH v3 09/50] qapi: add #if/#endif helpers","submitter":{"id":2645,"url":"http://patchwork.ozlabs.org/api/people/2645/","name":"Markus Armbruster","email":"armbru@redhat.com"},"content":"Marc-André Lureau <marcandre.lureau@redhat.com> writes:\n\n> Add helpers to wrap generated code with #if/#endif lines.\n>\n> Add a function decorator that will be used to wrap visitor methods.\n> The decorator will check if code was generated before adding #if/#endif\n> lines. Used in the following patches.\n>\n> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>\n> ---\n>  scripts/qapi.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++\n>  1 file changed, 55 insertions(+)\n>\n> diff --git a/scripts/qapi.py b/scripts/qapi.py\n> index 2a8e60e975..94b735d8d6 100644\n> --- a/scripts/qapi.py\n> +++ b/scripts/qapi.py\n> @@ -1897,6 +1897,61 @@ def guardend(name):\n>                   name=guardname(name))\n>  \n>  \n> +def gen_if(ifcond):\n> +    if not ifcond:\n> +        return ''\n> +    if isinstance(ifcond, str):\n> +        ifcond = [ifcond]\n\nPerhaps we should take this normalization step in the QAPISchema\nconstructors.\n\n> +    ret = ''\n> +    for ifc in ifcond:\n> +        ret += mcgen('''\n> +#if %(cond)s\n> +''', cond=ifc)\n> +    return ret\n> +\n> +\n> +def gen_endif(ifcond):\n> +    if not ifcond:\n> +        return ''\n> +    if isinstance(ifcond, str):\n> +        ifcond = [ifcond]\n> +    ret = ''\n> +    for ifc in reversed(ifcond):\n> +        ret += mcgen('''\n> +#endif /* %(cond)s */\n> +''', cond=ifc)\n> +    return ret\n> +\n> +\n> +# Wrap a method to add #if / #endif to generated code, only if some\n> +# code was generated. The method must have an 'ifcond' argument.\n> +# self must have 'if_members' listing the attributes to wrap.\n> +def ifcond_decorator(func):\n> +\n> +    def func_wrapper(self, *args, **kwargs):\n> +        import inspect\n\nIs hiding imports in function a good idea?\n\n> +        idx = inspect.getargspec(func).args.index('ifcond')\n> +        ifcond = args[idx - 1]\n> +        save = {}\n> +        for mem in self.if_members:\n> +            save[mem] = getattr(self, mem)\n> +        func(self, *args, **kwargs)\n> +        for mem, val in save.items():\n> +            newval = getattr(self, mem)\n> +            if newval != val:\n> +                assert newval.startswith(val)\n> +                newval = newval[len(val):]\n> +                if newval[0] == '\\n':\n> +                    val += '\\n'\n> +                    newval = newval[1:]\n> +                val += gen_if(ifcond)\n> +                val += newval\n> +                val += gen_endif(ifcond)\n> +            setattr(self, mem, val)\n> +\n> +    return func_wrapper\n> +\n> +\n>  def gen_enum_lookup(name, values, prefix=None):\n>      ret = mcgen('''\n\nMy gut feeling is still \"too clever by half\", but i'm reserving\njudgement until after review of its use, and exploration of\nalternatives.","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":"ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3ysy7g4s8Qz9sCZ\n\tfor <incoming@patchwork.ozlabs.org>;\n\tFri,  8 Dec 2017 01:10:54 +1100 (AEDT)","from localhost ([::1]:60843 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1eMwt0-00018E-N1\n\tfor incoming@patchwork.ozlabs.org; Thu, 07 Dec 2017 09:10:50 -0500","from eggs.gnu.org ([2001:4830:134:3::10]:49804)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <armbru@redhat.com>) id 1eMwse-000187-Vb\n\tfor qemu-devel@nongnu.org; Thu, 07 Dec 2017 09:10:32 -0500","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <armbru@redhat.com>) id 1eMwsa-0007yI-8L\n\tfor qemu-devel@nongnu.org; Thu, 07 Dec 2017 09:10:28 -0500","from mx1.redhat.com ([209.132.183.28]:49714)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <armbru@redhat.com>) id 1eMwsZ-0007wL-T3\n\tfor qemu-devel@nongnu.org; Thu, 07 Dec 2017 09:10:24 -0500","from smtp.corp.redhat.com\n\t(int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mx1.redhat.com (Postfix) with ESMTPS id 4619A30A09B;\n\tThu,  7 Dec 2017 14:10:20 +0000 (UTC)","from blackfin.pond.sub.org (ovpn-116-74.ams2.redhat.com\n\t[10.36.116.74])\n\tby smtp.corp.redhat.com (Postfix) with ESMTPS id 229B177664;\n\tThu,  7 Dec 2017 14:10:14 +0000 (UTC)","by blackfin.pond.sub.org (Postfix, from userid 1000)\n\tid A56641138658; Thu,  7 Dec 2017 15:10:12 +0100 (CET)"],"From":"Markus Armbruster <armbru@redhat.com>","To":"=?utf-8?q?Marc-Andr=C3=A9?= Lureau <marcandre.lureau@redhat.com>","References":"<20170911110623.24981-1-marcandre.lureau@redhat.com>\n\t<20170911110623.24981-10-marcandre.lureau@redhat.com>","Date":"Thu, 07 Dec 2017 15:10:12 +0100","In-Reply-To":"<20170911110623.24981-10-marcandre.lureau@redhat.com> (\n\t=?utf-8?b?Ik1hcmMtQW5kcsOp?= Lureau\"'s message of \"Mon,\n\t11 Sep 2017 \t13:05:42 +0200\")","Message-ID":"<87zi6uochn.fsf@dusky.pond.sub.org>","User-Agent":"Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux)","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Transfer-Encoding":"quoted-printable","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.15","X-Greylist":"Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t(mx1.redhat.com [10.5.110.38]);\n\tThu, 07 Dec 2017 14:10:20 +0000 (UTC)","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]\n\t[fuzzy]","X-Received-From":"209.132.183.28","Subject":"Re: [Qemu-devel] [PATCH v3 09/50] qapi: add #if/#endif helpers","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"qemu-devel@nongnu.org, Michael Roth <mdroth@linux.vnet.ibm.com>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"}},{"id":1837332,"web_url":"http://patchwork.ozlabs.org/comment/1837332/","msgid":"<CAJ+F1CKuG3D5UZDuzpV_z9rzwNu367V8gj9UN5xYhCimMkGcRg@mail.gmail.com>","list_archive_url":null,"date":"2018-01-11T21:21:50","subject":"Re: [Qemu-devel] [PATCH v3 09/50] qapi: add #if/#endif helpers","submitter":{"id":6442,"url":"http://patchwork.ozlabs.org/api/people/6442/","name":"Marc-André Lureau","email":"marcandre.lureau@gmail.com"},"content":"Hi\n\nOn Thu, Dec 7, 2017 at 3:10 PM, Markus Armbruster <armbru@redhat.com> wrote:\n> Marc-André Lureau <marcandre.lureau@redhat.com> writes:\n>\n>> Add helpers to wrap generated code with #if/#endif lines.\n>>\n>> Add a function decorator that will be used to wrap visitor methods.\n>> The decorator will check if code was generated before adding #if/#endif\n>> lines. Used in the following patches.\n>>\n>> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>\n>> ---\n>>  scripts/qapi.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++\n>>  1 file changed, 55 insertions(+)\n>>\n>> diff --git a/scripts/qapi.py b/scripts/qapi.py\n>> index 2a8e60e975..94b735d8d6 100644\n>> --- a/scripts/qapi.py\n>> +++ b/scripts/qapi.py\n>> @@ -1897,6 +1897,61 @@ def guardend(name):\n>>                   name=guardname(name))\n>>\n>>\n>> +def gen_if(ifcond):\n>> +    if not ifcond:\n>> +        return ''\n>> +    if isinstance(ifcond, str):\n>> +        ifcond = [ifcond]\n>\n> Perhaps we should take this normalization step in the QAPISchema\n> constructors.\n\nYes, it's not very convenient though, I added a TODO note\n\n>\n>> +    ret = ''\n>> +    for ifc in ifcond:\n>> +        ret += mcgen('''\n>> +#if %(cond)s\n>> +''', cond=ifc)\n>> +    return ret\n>> +\n>> +\n>> +def gen_endif(ifcond):\n>> +    if not ifcond:\n>> +        return ''\n>> +    if isinstance(ifcond, str):\n>> +        ifcond = [ifcond]\n>> +    ret = ''\n>> +    for ifc in reversed(ifcond):\n>> +        ret += mcgen('''\n>> +#endif /* %(cond)s */\n>> +''', cond=ifc)\n>> +    return ret\n>> +\n>> +\n>> +# Wrap a method to add #if / #endif to generated code, only if some\n>> +# code was generated. The method must have an 'ifcond' argument.\n>> +# self must have 'if_members' listing the attributes to wrap.\n>> +def ifcond_decorator(func):\n>> +\n>> +    def func_wrapper(self, *args, **kwargs):\n>> +        import inspect\n>\n> Is hiding imports in function a good idea?\n\nI believe it's best to restrict the import to the scope it is being\nused, especially if it's very specific to that place. Some code do\nthat already.\n\n>\n>> +        idx = inspect.getargspec(func).args.index('ifcond')\n>> +        ifcond = args[idx - 1]\n>> +        save = {}\n>> +        for mem in self.if_members:\n>> +            save[mem] = getattr(self, mem)\n>> +        func(self, *args, **kwargs)\n>> +        for mem, val in save.items():\n>> +            newval = getattr(self, mem)\n>> +            if newval != val:\n>> +                assert newval.startswith(val)\n>> +                newval = newval[len(val):]\n>> +                if newval[0] == '\\n':\n>> +                    val += '\\n'\n>> +                    newval = newval[1:]\n>> +                val += gen_if(ifcond)\n>> +                val += newval\n>> +                val += gen_endif(ifcond)\n>> +            setattr(self, mem, val)\n>> +\n>> +    return func_wrapper\n>> +\n>> +\n>>  def gen_enum_lookup(name, values, prefix=None):\n>>      ret = mcgen('''\n>\n> My gut feeling is still \"too clever by half\", but i'm reserving\n> judgement until after review of its use, and exploration of\n> alternatives.\n>\n\nCould easily be done as a follow-up.\n\nthanks","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ozlabs.org; dkim=pass (2048-bit key;\n\tunprotected) header.d=gmail.com header.i=@gmail.com\n\theader.b=\"ryxehiC8\"; dkim-atps=neutral"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3zHfB948yHz9sNr\n\tfor <incoming@patchwork.ozlabs.org>;\n\tFri, 12 Jan 2018 08:28:17 +1100 (AEDT)","from localhost ([::1]:43865 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1eZkOV-0007fI-KT\n\tfor incoming@patchwork.ozlabs.org; Thu, 11 Jan 2018 16:28:15 -0500","from eggs.gnu.org ([2001:4830:134:3::10]:43795)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <marcandre.lureau@gmail.com>) id 1eZkIM-0002XI-3L\n\tfor qemu-devel@nongnu.org; Thu, 11 Jan 2018 16:21:55 -0500","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <marcandre.lureau@gmail.com>) id 1eZkIL-00086Z-1p\n\tfor qemu-devel@nongnu.org; Thu, 11 Jan 2018 16:21:54 -0500","from mail-wm0-x22d.google.com ([2a00:1450:400c:c09::22d]:38156)\n\tby eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16)\n\t(Exim 4.71) (envelope-from <marcandre.lureau@gmail.com>)\n\tid 1eZkIK-00085x-QS\n\tfor qemu-devel@nongnu.org; Thu, 11 Jan 2018 16:21:52 -0500","by mail-wm0-x22d.google.com with SMTP id 141so7995356wme.3\n\tfor <qemu-devel@nongnu.org>; Thu, 11 Jan 2018 13:21:52 -0800 (PST)","by 10.223.199.143 with HTTP; Thu, 11 Jan 2018 13:21:50 -0800 (PST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;\n\th=mime-version:in-reply-to:references:from:date:message-id:subject:to\n\t:cc:content-transfer-encoding;\n\tbh=dFQqySxH2J3iwDZ1+sKyfNSCIxFOFEPh2eJIbBr8Tps=;\n\tb=ryxehiC8vZs7H7k0MVVqov5EDnTq+KmKE0sGFG+TBhL/CEn25Zdj7nYhwNlooKXR2U\n\t/dckwS4xTu6Qy8sZJ891A95rUXx8twZvJqn8uSKDroDjIgW5qKZGcIwKm3dWqGIeZdNN\n\tvoc5N4Yjd+nL63q43/yPvor1TKHplmdnPmh23z///t8tB4NYypUHl/923MQJh3Nmh4yU\n\tBBjxMwkuYdESd69uGRNilRzYirRFaaE8v+b1kDpG0VQKDYO9S3lHf7QFsC4vEk2AGlhe\n\ti3h/IIgUZtj9v6fNR13yCYWfZgwrFtchW0ylO8vM4PbQGghX2UDT5+2gGZWNowausvr9\n\tKmhQ==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:mime-version:in-reply-to:references:from:date\n\t:message-id:subject:to:cc:content-transfer-encoding;\n\tbh=dFQqySxH2J3iwDZ1+sKyfNSCIxFOFEPh2eJIbBr8Tps=;\n\tb=AtJ9iUuCENCVsg6JwdTFKoctpwoMGLj1KadICxI+ab09+mk4kuLmiZjFe91CWaLqhg\n\tteMvlOYIKV26jFtOYxq4DlUxfyzmjSggWIY4KyFprwcJ044O8tsNgs6u9wHjMLeSq0u2\n\tdaHYPhHFcy3kOh/7v1ZqeX9/2XQRHXMZ8CDF08Z7SJgxPnrBhbFSag2+8ZkKU6sJ/Kja\n\tArLosCQNGXo1DqwiHA0+iwX1snmRXBmZtbmicWl7NhslgMdzW86yrRq6N3HkoMO5sOOd\n\tA2705YojKIIp9UE4o619KU63u8EoINi4oMS32w05GKRF77I1WwkD051b/d9qLmU4bHe2\n\tyeMA==","X-Gm-Message-State":"AKwxyteL4tU2qkET4pJoF0fqOPFArv8cFqrIMc243gh7+DxD6UqXzmCy\n\tE4k2OCxI7F0FP5k55Mpteg6gBXc7q4u3iq7jb9I=","X-Google-Smtp-Source":"ACJfBotwkJyW8IoePpov4FBxhzT7mYp+le81oQB+JiE+IEjZZvnziHpbhIrY3YJEWS0hD9ObbdVvZXLvhdHJF9Ik03Y=","X-Received":"by 10.28.184.209 with SMTP id i200mr2493358wmf.84.1515705711490; \n\tThu, 11 Jan 2018 13:21:51 -0800 (PST)","MIME-Version":"1.0","In-Reply-To":"<87zi6uochn.fsf@dusky.pond.sub.org>","References":"<20170911110623.24981-1-marcandre.lureau@redhat.com>\n\t<20170911110623.24981-10-marcandre.lureau@redhat.com>\n\t<87zi6uochn.fsf@dusky.pond.sub.org>","From":"=?utf-8?q?Marc-Andr=C3=A9_Lureau?= <marcandre.lureau@gmail.com>","Date":"Thu, 11 Jan 2018 22:21:50 +0100","Message-ID":"<CAJ+F1CKuG3D5UZDuzpV_z9rzwNu367V8gj9UN5xYhCimMkGcRg@mail.gmail.com>","To":"Markus Armbruster <armbru@redhat.com>","Content-Type":"text/plain; charset=\"UTF-8\"","Content-Transfer-Encoding":"quoted-printable","X-detected-operating-system":"by eggs.gnu.org: Genre and OS details not\n\trecognized.","X-Received-From":"2a00:1450:400c:c09::22d","Subject":"Re: [Qemu-devel] [PATCH v3 09/50] qapi: add #if/#endif helpers","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"QEMU <qemu-devel@nongnu.org>, Michael Roth <mdroth@linux.vnet.ibm.com>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"}}]