From nobody Fri Jun 14 19:49:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614040592; cv=none; d=zohomail.com; s=zohoarc; b=SDvX84B7/9HeY4LDmI9xMBFw6TuQv8D/oIkT/wODGZLQmcPlqgcKpd7IgorynJPDUWlmv+xnbUVy90RA/RSi5C1IDJ/30dg3tF2PXpuGDsvOcdv2Oiv7wEY7CH91EdGFafl7OawdI7i7qpX91yYglxSmpJc6x3t7m4cA1VYcPSg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614040592; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=sKZiJW6GuHyZD8p1WWoYBotzIQzpfojZ9d8hks1jFBM=; b=VPS1Oy9wufnSCDn84sYAhRbbjGHFGcIU7WQ9llUhmKXeMC+sO/pUO4y84Obfa7PlLchl55h4lNpOskhneaT9TlW3kI5RTqQFDt8ko3zZJoS+9YjP1A7XAP/NU7+Tr/ZSKoSVr7TsNVqpCVTmZ/B2URl5K16FI0R9hJUorvCHXM0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTP id 1614040592212221.07481243670236; Mon, 22 Feb 2021 16:36:32 -0800 (PST) Received: from localhost ([::1]:56016 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lELgZ-0001Lj-Pt for importer@patchew.org; Mon, 22 Feb 2021 19:36:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35454) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lELfG-00006s-0F for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:34:58 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:43979) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lELea-0005Lz-NG for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:34:56 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-103-eVrn8c4ZP9-xoxgTXdwqcA-1; Mon, 22 Feb 2021 19:34:11 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 63C46107ACE3; Tue, 23 Feb 2021 00:34:10 +0000 (UTC) Received: from scv.redhat.com (ovpn-112-247.rdu2.redhat.com [10.10.112.247]) by smtp.corp.redhat.com (Postfix) with ESMTP id A7DA85D6D5; Tue, 23 Feb 2021 00:34:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614040453; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sKZiJW6GuHyZD8p1WWoYBotzIQzpfojZ9d8hks1jFBM=; b=Kz8t4YGOFzK8gZZjz4nwohVG/31chRIkd9n7IZieCuaccrZOUnJ5ELpxoLE6SBuSaeGBOt 4KKZUA+dHO4KVQ03Zl7668Lu66S0IgtQlRS2Z0y8e8AcHVtKH7ezU22OY2qhXz3mi6awuZ gD/cnuT0ONx7I2jrnZf+oimSz1f+Af0= X-MC-Unique: eVrn8c4ZP9-xoxgTXdwqcA-1 From: John Snow To: qemu-devel@nongnu.org, Markus Armbruster Subject: [PATCH v3 01/16] qapi/expr.py: Remove 'info' argument from nested check_if_str Date: Mon, 22 Feb 2021 19:33:53 -0500 Message-Id: <20210223003408.964543-2-jsnow@redhat.com> In-Reply-To: <20210223003408.964543-1-jsnow@redhat.com> References: <20210223003408.964543-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , John Snow , Eduardo Habkost , Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The function can just use the argument from the scope above. Otherwise, we get shadowed argument errors because the parameter name clashes with the name of a variable already in-scope. Signed-off-by: John Snow Reviewed-by: Eduardo Habkost Reviewed-by: Cleber Rosa --- scripts/qapi/expr.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index 2fcaaa2497a..35695c4c653 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -104,7 +104,7 @@ def check_flags(expr, info): =20 def check_if(expr, info, source): =20 - def check_if_str(ifcond, info): + def check_if_str(ifcond): if not isinstance(ifcond, str): raise QAPISemError( info, @@ -124,9 +124,9 @@ def check_if_str(ifcond, info): raise QAPISemError( info, "'if' condition [] of %s is useless" % source) for elt in ifcond: - check_if_str(elt, info) + check_if_str(elt) else: - check_if_str(ifcond, info) + check_if_str(ifcond) expr['if'] =3D [ifcond] =20 =20 --=20 2.29.2 From nobody Fri Jun 14 19:49:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614040593; cv=none; d=zohomail.com; s=zohoarc; b=RTzEczEjWEHPX/OSuNuDNimR7E/j3+opr+13I6pHwYwiitm5OUjy/k9JWbJG9Q8xrgy9KfvDPX+WqInynwRwr3j3nVrc7eqxEjKqHBoDrnlfKx8Tr12WfvGXmc+9RZPg2+EOrXXFQpuC6kG3y4LTunXOKJRQyfHLdLW6GFpq0ps= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614040593; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=tIrFtuBcNrN9o+z9afPNMpNXRC6fen7R/L86hrpB2Ec=; b=Kv+8JPKDrUke6ydb06EI2QLwP+ZX71PgakXg5sNSQSfOSnvRI1y8tzjA0TxFe2s3KGq/LIXF7YMao+OCr6/prNlHt8mEdmRyJ1RDe/QTM7MlbGnEbrjwa12Itpw6AqKZCJRYKo0DJV4o/G7OHOsd0zp4VYUX3ZjaK67XINghT+Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1614040592849543.8846566479807; Mon, 22 Feb 2021 16:36:32 -0800 (PST) Received: from localhost ([::1]:56362 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lELgd-0001UC-2j for importer@patchew.org; Mon, 22 Feb 2021 19:36:23 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35460) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lELfJ-00008P-RI for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:35:01 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:22765) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lELea-0005M7-Me for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:35:01 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-523-OkEyjQ04Nyu-9tmtBemQWQ-1; Mon, 22 Feb 2021 19:34:12 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 50728107ACC7; Tue, 23 Feb 2021 00:34:11 +0000 (UTC) Received: from scv.redhat.com (ovpn-112-247.rdu2.redhat.com [10.10.112.247]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8EBE95D74E; Tue, 23 Feb 2021 00:34:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614040454; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tIrFtuBcNrN9o+z9afPNMpNXRC6fen7R/L86hrpB2Ec=; b=WWnW/tzOz9xjZCrs6fTLBmHDPvqMosJUsAIwvu11RbPOFFfaNIhQmIziHI3TLsuFHGZThe cFUEqAcek/JTrjJG/Z2Xon1t2UEAwH0hhbQyrSJKn7m3KuQl6jmsO0Y0aQdQPx64BEnEP3 jD66JdB5h/rqTJfy9nCFv5eENL/aTVg= X-MC-Unique: OkEyjQ04Nyu-9tmtBemQWQ-1 From: John Snow To: qemu-devel@nongnu.org, Markus Armbruster Subject: [PATCH v3 02/16] qapi/expr.py: Check for dict instead of OrderedDict Date: Mon, 22 Feb 2021 19:33:54 -0500 Message-Id: <20210223003408.964543-3-jsnow@redhat.com> In-Reply-To: <20210223003408.964543-1-jsnow@redhat.com> References: <20210223003408.964543-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , John Snow , Eduardo Habkost , Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" OrderedDict is a subtype of dict, so we can check for a more general form. These functions do not themselves depend on it being any particular type. Signed-off-by: John Snow Reviewed-by: Eduardo Habkost Reviewed-by: Cleber Rosa --- scripts/qapi/expr.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index 35695c4c653..5694c501fa3 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -14,7 +14,6 @@ # This work is licensed under the terms of the GNU GPL, version 2. # See the COPYING file in the top-level directory. =20 -from collections import OrderedDict import re =20 from .common import c_name @@ -131,7 +130,7 @@ def check_if_str(ifcond): =20 =20 def normalize_members(members): - if isinstance(members, OrderedDict): + if isinstance(members, dict): for key, arg in members.items(): if isinstance(arg, dict): continue @@ -162,7 +161,7 @@ def check_type(value, info, source, if not allow_dict: raise QAPISemError(info, "%s should be a type name" % source) =20 - if not isinstance(value, OrderedDict): + if not isinstance(value, dict): raise QAPISemError(info, "%s should be an object or type name" % source) =20 --=20 2.29.2 From nobody Fri Jun 14 19:49:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614040618; cv=none; d=zohomail.com; s=zohoarc; b=bqd+LphjI/EGjer8je52KdIxAFebR46hcxoOimAs+zq0TGm/FVqS9GS3CoP5B5weSpCU4Jm/4nMcM9wxi6gyplXmJj0AYZsQd5G5okjTULuyWlf/QOdlGftNEE0oQFerzNomOddKTsKBABB5/vuL2f4w1emVOjGOcwsP+Z3cNcY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614040618; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=6MQVM0WMlNubOKe/JRZ7iNs/JR2AlIPYYXpSIFmx+hQ=; b=c4P31+yBkip4GYqkdaj3s7u7eA1AX6wj1Frfk5q5v/s+UCbXuQE8a7SC+ubzhdo2HbDj0DCbE4ZwuBnDWomi0uD12NMw9OuvNa555otgbip9dgg8tXey4cItGFQbVqg5NksZd31XBYy70HTtsZcmcdMRf3wFMqjeKJYMs8B9idE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1614040617718909.9184836251503; Mon, 22 Feb 2021 16:36:57 -0800 (PST) Received: from localhost ([::1]:58330 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lELh9-0002In-AD for importer@patchew.org; Mon, 22 Feb 2021 19:36:55 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35510) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lELfe-0000a3-Lr for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:35:22 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:43057) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lELea-0005MO-JW for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:35:22 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-59-J7UyEJNIPJuhiszB8wZtTw-1; Mon, 22 Feb 2021 19:34:13 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 583628030BB; Tue, 23 Feb 2021 00:34:12 +0000 (UTC) Received: from scv.redhat.com (ovpn-112-247.rdu2.redhat.com [10.10.112.247]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7DD855D6D5; Tue, 23 Feb 2021 00:34:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614040455; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6MQVM0WMlNubOKe/JRZ7iNs/JR2AlIPYYXpSIFmx+hQ=; b=MY93FlW/p3QzWqq562T1NrUinI3Urqo6fDyd+W8W3taikpL8C7zSefDIyx1obUjcg9hlUf fGwT41N7/btJAfHjY/WmPbXGn3Byu5qzFYRvW0epRi2uUkx5JeKSZM8+jp5qMc0YRR2ztk zuCC2DtSGaigl97W9rZbRTawFOafeho= X-MC-Unique: J7UyEJNIPJuhiszB8wZtTw-1 From: John Snow To: qemu-devel@nongnu.org, Markus Armbruster Subject: [PATCH v3 03/16] qapi/expr.py: constrain incoming expression types Date: Mon, 22 Feb 2021 19:33:55 -0500 Message-Id: <20210223003408.964543-4-jsnow@redhat.com> In-Reply-To: <20210223003408.964543-1-jsnow@redhat.com> References: <20210223003408.964543-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , John Snow , Eduardo Habkost , Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" mypy does not know the types of values stored in Dicts that masquerade as objects. Help the type checker out by constraining the type. Signed-off-by: John Snow Reviewed-by: Eduardo Habkost Reviewed-by: Cleber Rosa --- scripts/qapi/expr.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index 5694c501fa3..783282b53ce 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -15,9 +15,17 @@ # See the COPYING file in the top-level directory. =20 import re +from typing import MutableMapping, Optional =20 from .common import c_name from .error import QAPISemError +from .parser import QAPIDoc +from .source import QAPISourceInfo + + +# Expressions in their raw form are JSON-like structures with arbitrary fo= rms. +# Minimally, their top-level form must be a mapping of strings to values. +Expression =3D MutableMapping[str, object] =20 =20 # Names must be letters, numbers, -, and _. They must start with letter, @@ -287,9 +295,20 @@ def check_event(expr, info): =20 def check_exprs(exprs): for expr_elem in exprs: - expr =3D expr_elem['expr'] - info =3D expr_elem['info'] - doc =3D expr_elem.get('doc') + # Expression + assert isinstance(expr_elem['expr'], dict) + for key in expr_elem['expr'].keys(): + assert isinstance(key, str) + expr: Expression =3D expr_elem['expr'] + + # QAPISourceInfo + assert isinstance(expr_elem['info'], QAPISourceInfo) + info: QAPISourceInfo =3D expr_elem['info'] + + # Optional[QAPIDoc] + tmp =3D expr_elem.get('doc') + assert tmp is None or isinstance(tmp, QAPIDoc) + doc: Optional[QAPIDoc] =3D tmp =20 if 'include' in expr: continue --=20 2.29.2 From nobody Fri Jun 14 19:49:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614040713; cv=none; d=zohomail.com; s=zohoarc; b=MtN6sO+66qOtdFoSriFqFrpvnUMJY528e5u7nvtFMrhh4ptS0F6BB7zMf/Q1gg4h0SKp6mj6HQDL1b0tpyGRDIMW0YdxtTqDXIREeR6haK+aThKH/zC0JZ1eZRHAmyY3kaWNhyIARCGysfq8wYv6b347b26l9Pn+2a8wQ6KxFh0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614040713; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=IpryRBIBCaqKX6O5ZaLrub7caDgcqzXpJqD2kUlCqjs=; b=GUDDbRUBDc1YwDESZ/ESJP3jCpk5nfWdK2PXqYsc688236ILnXVzOxamlD34qJvcekCEtWv7fxFdyC5DPTTwT/uL/jmpQzaP4Ug09cQaTMoYck3FiE6N70P4t6iJnpXp6D3I+8c4a+dzwYo5icrRKUQam+OVSjp7TL1v2YaC/RA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1614040712887854.3204905720442; Mon, 22 Feb 2021 16:38:32 -0800 (PST) Received: from localhost ([::1]:36606 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lELih-0004yz-Ru for importer@patchew.org; Mon, 22 Feb 2021 19:38:31 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35508) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lELfd-0000Xg-Lj for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:35:21 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:47657) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lELeb-0005MW-Gl for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:35:21 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-294-Xl3mXJCPOxiS7mkz3t07YQ-1; Mon, 22 Feb 2021 19:34:14 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5054D186DD21; Tue, 23 Feb 2021 00:34:13 +0000 (UTC) Received: from scv.redhat.com (ovpn-112-247.rdu2.redhat.com [10.10.112.247]) by smtp.corp.redhat.com (Postfix) with ESMTP id 87F4D5D6D5; Tue, 23 Feb 2021 00:34:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614040456; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IpryRBIBCaqKX6O5ZaLrub7caDgcqzXpJqD2kUlCqjs=; b=H3/YXNemlj+tSEb/aS3qgJISyDLYAx/gGmFyfx97wlQu5haO6dJNSE3rkG5xaLv2ekioPR Cmwo/k1jBbK2UBfU/iwgDj+EiQJZtgDUB2IwoOMfvhj43CjuD+UAEGZpJFpX5+9bZc4Hhh ZxAN46njzfpKF3A4k/YBJyK96i4Riq8= X-MC-Unique: Xl3mXJCPOxiS7mkz3t07YQ-1 From: John Snow To: qemu-devel@nongnu.org, Markus Armbruster Subject: [PATCH v3 04/16] qapi/expr.py: Add assertion for union type 'check_dict' Date: Mon, 22 Feb 2021 19:33:56 -0500 Message-Id: <20210223003408.964543-5-jsnow@redhat.com> In-Reply-To: <20210223003408.964543-1-jsnow@redhat.com> References: <20210223003408.964543-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=63.128.21.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , John Snow , Eduardo Habkost , Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" mypy isn't fond of allowing you to check for bool membership in a collection of str elements. Guard this lookup for precisely when we were given a name. Signed-off-by: John Snow Reviewed-by: Eduardo Habkost Reviewed-by: Cleber Rosa --- scripts/qapi/expr.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index 783282b53ce..138fab0711f 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -173,7 +173,9 @@ def check_type(value, info, source, raise QAPISemError(info, "%s should be an object or type name" % source) =20 - permit_upper =3D allow_dict in info.pragma.name_case_whitelist + permit_upper =3D False + if isinstance(allow_dict, str): + permit_upper =3D allow_dict in info.pragma.name_case_whitelist =20 # value is a dictionary, check that each member is okay for (key, arg) in value.items(): --=20 2.29.2 From nobody Fri Jun 14 19:49:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614040625; cv=none; d=zohomail.com; s=zohoarc; b=I6ciliLbR4XfyVd3eDTQ/5pJeq3iR0VlILvLwAW4uxrA6xc0iAatYqbDyA9sOlkkFyU0uOtfpolnds6ltZ8MIPj4jCOT4oqlg+op7gY6sjOp24vQ5DC0H/TO9pOraSh1w6wZTdn7VBtRotrKkZh0i3n90lKgEjPdfNlmEOecIik= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614040625; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=4H0dmDekCWe0PZwkiXb+jOS9/DVhidudeemDAi+A1cs=; b=Imu836XJqfGn7ZtRiyqSkebwjgfmZwrJtZoMFeBzeSZLHA/dbnZJ2whmCPUxy+nkIRs2wGT05XGy/uhnZKMYR7eCzAP9gwPgUfp+upG9VFjbBIUxJXfYhxNE7Bxbxh8uOW6ErlEItQ1aNV5bL4uM+yYFDF3lQpI2e6dZFCAIulQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1614040625516496.0394330166316; Mon, 22 Feb 2021 16:37:05 -0800 (PST) Received: from localhost ([::1]:58812 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lELhI-0002Vo-G9 for importer@patchew.org; Mon, 22 Feb 2021 19:37:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35550) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lELfo-0000oI-0f for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:35:32 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:53506) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lELec-0005Ma-C1 for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:35:31 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-529-59Q9TNf2MT2y7zmdAQpvTQ-1; Mon, 22 Feb 2021 19:34:15 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5188C8030B7; Tue, 23 Feb 2021 00:34:14 +0000 (UTC) Received: from scv.redhat.com (ovpn-112-247.rdu2.redhat.com [10.10.112.247]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7A8E85D6D5; Tue, 23 Feb 2021 00:34:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614040457; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4H0dmDekCWe0PZwkiXb+jOS9/DVhidudeemDAi+A1cs=; b=U+NeaA8dFsbK5QlbCNPcxeH5Fxo0OauRCcRGPNnN2Hk2ds2h1TLqiRoGgSKgMDPjU/943u dUt7PSJ2pq5xzMS0s0+cmZl1YUSM1mZcTqGT6xMU3HAPY06KlYMFaYxwT9s0mLdiU4XxMh 6EqOqj/o1cj8ovxpEjQoqYInWy8RxM8= X-MC-Unique: 59Q9TNf2MT2y7zmdAQpvTQ-1 From: John Snow To: qemu-devel@nongnu.org, Markus Armbruster Subject: [PATCH v3 05/16] qapi/expr.py: move string check upwards in check_type Date: Mon, 22 Feb 2021 19:33:57 -0500 Message-Id: <20210223003408.964543-6-jsnow@redhat.com> In-Reply-To: <20210223003408.964543-1-jsnow@redhat.com> References: <20210223003408.964543-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , John Snow , Eduardo Habkost , Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" For readability purposes only, shimmy the early return upwards to the top of the function, so cases proceed in order from least to most complex. Signed-off-by: John Snow Reviewed-by: Eduardo Habkost Reviewed-by: Cleber Rosa --- scripts/qapi/expr.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index 138fab0711f..c97e8ce8a4d 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -150,6 +150,10 @@ def check_type(value, info, source, if value is None: return =20 + # Type name + if isinstance(value, str): + return + # Array type if isinstance(value, list): if not allow_array: @@ -160,10 +164,6 @@ def check_type(value, info, source, source) return =20 - # Type name - if isinstance(value, str): - return - # Anonymous type =20 if not allow_dict: --=20 2.29.2 From nobody Fri Jun 14 19:49:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614040711; cv=none; d=zohomail.com; s=zohoarc; b=JT3BqrVR/t+tLtPC8bnxpu5rdN9I5tuMG0THc80iwn2T+E0OXGM6xcxMnczbaQKzjAUYG0mworVqdt/pXpWEWaoyAkzbe2uwKDXizftmpTLttqgz0KeZPS9niRiBMeLTfV0Dukai6y7zDBs2Qgu1/0EgAzVS1jCQ2SwvaujQ6k8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614040711; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=WGYP8qdJK2IAt3UVpRF1NxdiXA6WpnwC2fYEkXiNfcE=; b=Ne7Und8jRvpQUGHxrV7dFpkk6IflTcUWey/UlL3kApliVGYxAm8HYB6fUcWd7Zab76wM+ZPjW4jpsrNw1Ks3VFe0n3zmyi0de2mhvgq7a3z9CkEeQ2pQLsMHCT+Qa1m7RkO3tkE1E6JZFslImoBAQvtQfTgOXHPnZKYGjP+w0zc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1614040710821192.42012355721454; Mon, 22 Feb 2021 16:38:30 -0800 (PST) Received: from localhost ([::1]:36376 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lELif-0004tN-NY for importer@patchew.org; Mon, 22 Feb 2021 19:38:29 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35578) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lELft-0000tL-Af for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:35:38 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:50772) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lELec-0005Mc-Ou for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:35:35 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-406-tcyWUlPNNKqbx51RjNvphQ-1; Mon, 22 Feb 2021 19:34:16 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3FA4F8030BB; Tue, 23 Feb 2021 00:34:15 +0000 (UTC) Received: from scv.redhat.com (ovpn-112-247.rdu2.redhat.com [10.10.112.247]) by smtp.corp.redhat.com (Postfix) with ESMTP id 77D1C5D752; Tue, 23 Feb 2021 00:34:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614040457; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WGYP8qdJK2IAt3UVpRF1NxdiXA6WpnwC2fYEkXiNfcE=; b=OzHAuBKDXJED40+AmgEuWi4Lo5ScIUmNana8f47BAuDyd0GTXIgeD83Jkfvxk8WjkF3et+ vAoC/ECl02eQXGIMu+2ipn5BCxvXzYzYJ3/rbY8CZ1IwuF3/Q6Enbo/45aCvY0kFTtJ2IO 5VRwjPgUbTP+7khDUxqxHlNn0cVv1KY= X-MC-Unique: tcyWUlPNNKqbx51RjNvphQ-1 From: John Snow To: qemu-devel@nongnu.org, Markus Armbruster Subject: [PATCH v3 06/16] qapi/expr.py: Check type of 'data' member Date: Mon, 22 Feb 2021 19:33:58 -0500 Message-Id: <20210223003408.964543-7-jsnow@redhat.com> In-Reply-To: <20210223003408.964543-1-jsnow@redhat.com> References: <20210223003408.964543-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=63.128.21.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , John Snow , Eduardo Habkost , Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Iterating over the members of data isn't going to work if it's not some form of dict anyway, but for the sake of mypy, formalize it. Signed-off-by: John Snow Reviewed-by: Eduardo Habkost --- scripts/qapi/expr.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index c97e8ce8a4d..afa6bd07769 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -254,6 +254,9 @@ def check_union(expr, info): raise QAPISemError(info, "'discriminator' requires 'base'") check_name_is_str(discriminator, info, "'discriminator'") =20 + if not isinstance(members, dict): + raise QAPISemError(info, "'data' must be an object") + for (key, value) in members.items(): source =3D "'data' member '%s'" % key check_name_str(key, info, source) @@ -267,6 +270,10 @@ def check_alternate(expr, info): =20 if not members: raise QAPISemError(info, "'data' must not be empty") + + if not isinstance(members, dict): + raise QAPISemError(info, "'data' must be an object") + for (key, value) in members.items(): source =3D "'data' member '%s'" % key check_name_str(key, info, source) --=20 2.29.2 From nobody Fri Jun 14 19:49:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614041094; cv=none; d=zohomail.com; s=zohoarc; b=WV/MRfPilgKCavFFy544fvOZ2yFPZQgT+RVEaxPqyQ7sdp04E7g0UACPNzj0MKaqkrhzzdQ7VV8IfQo+vtHDxNRo3VQ483zCyJAUlHZoPbIaMA9Nz2I+zqda8O+mvwiErLO5H6qzB6LtPR1AUwivQAhtNfyrAqxQHqAjJzCPSR4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614041094; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=cHXsVwr6YFakxIsE2mMJJNz/dBe9CXnNVC5LNWByBvI=; b=LatvYRy6cspbh7bbcEhoEriKFgoymvbA7019kJjdVqi2Wx01u0X6KWM7HXh20+kc1aHiIqrX/To2WFuL97OX/cgeXdL/g0e3LsyK1qzifBQgRzFewzZLS8UD/jABV3IeaODYA99kqR7iW3t1OqFGUDXhDUBJCWdPRKW6G0xW2Vc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTP id 1614041093689958.7665489117278; Mon, 22 Feb 2021 16:44:53 -0800 (PST) Received: from localhost ([::1]:39292 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lELk0-000672-L2 for importer@patchew.org; Mon, 22 Feb 2021 19:39:52 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35620) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lELgE-0001b6-Im for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:00 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:45929) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lELed-0005Mk-TB for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:35:57 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-485-DxvTdReePk6PeIKVLS0p9Q-1; Mon, 22 Feb 2021 19:34:17 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 438531E564; Tue, 23 Feb 2021 00:34:16 +0000 (UTC) Received: from scv.redhat.com (ovpn-112-247.rdu2.redhat.com [10.10.112.247]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6DC795D6D5; Tue, 23 Feb 2021 00:34:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614040459; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cHXsVwr6YFakxIsE2mMJJNz/dBe9CXnNVC5LNWByBvI=; b=g8O2gMkpe6STJbfmPQxOaiRmIGTipW4xPXzbNEqDGTOsKIGlZyqAYXq2SugcqcmS/DFIL+ gsFdgcdvQfMto3KEWyc8ojBBpaiLgQx00TD+RuQ0SZdvKyu16Cq+BARVRSCHFZeaZ5Nm6R oIhutCbM/aAxOgR2kHJvqSk+7X7ZJBM= X-MC-Unique: DxvTdReePk6PeIKVLS0p9Q-1 From: John Snow To: qemu-devel@nongnu.org, Markus Armbruster Subject: [PATCH v3 07/16] qapi/expr.py: Add casts in a few select cases Date: Mon, 22 Feb 2021 19:33:59 -0500 Message-Id: <20210223003408.964543-8-jsnow@redhat.com> In-Reply-To: <20210223003408.964543-1-jsnow@redhat.com> References: <20210223003408.964543-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , John Snow , Eduardo Habkost , Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Casts are instructions to the type checker only, they aren't "safe" and should probably be avoided in general. In this case, when we perform type checking on a nested structure, the type of each field does not "stick". We don't need to assert that something is a str if we've already checked that it is -- use a cast instead for these cases. Signed-off-by: John Snow Reviewed-by: Eduardo Habkost Reviewed-by: Cleber Rosa --- scripts/qapi/expr.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index afa6bd07769..f45d6be1f4c 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -15,7 +15,7 @@ # See the COPYING file in the top-level directory. =20 import re -from typing import MutableMapping, Optional +from typing import MutableMapping, Optional, cast =20 from .common import c_name from .error import QAPISemError @@ -232,7 +232,7 @@ def check_enum(expr, info): =20 =20 def check_struct(expr, info): - name =3D expr['struct'] + name =3D cast(str, expr['struct']) # Asserted in check_exprs members =3D expr['data'] =20 check_type(members, info, "'data'", allow_dict=3Dname) @@ -240,7 +240,7 @@ def check_struct(expr, info): =20 =20 def check_union(expr, info): - name =3D expr['union'] + name =3D cast(str, expr['union']) # Asserted in check_exprs base =3D expr.get('base') discriminator =3D expr.get('discriminator') members =3D expr['data'] @@ -337,7 +337,7 @@ def check_exprs(exprs): else: raise QAPISemError(info, "expression is missing metatype") =20 - name =3D expr[meta] + name =3D cast(str, expr[meta]) # Asserted right below: check_name_is_str(name, info, "'%s'" % meta) info.set_defn(meta, name) check_defn_name_str(name, info, meta) --=20 2.29.2 From nobody Fri Jun 14 19:49:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614041179; cv=none; d=zohomail.com; s=zohoarc; b=lahyVyxWdb40Z2xRj7tQRnVNgDYc0HgXPtdiQzo9QFpTva9Kqw1FSRoZJa1GDMhupcWBVVeEjE3YADow10jVvump88zVDbnbGpOg9lVEj54aquQFjznk5GZNwwjxkDClHYAsoEblllGjLi2gw0yPZB7GAMeJaA082i0VLt5S+cs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614041179; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=jRBxKMCURslwJ7da45RvGXzhX8eos6ix0VgI3kUYvqs=; b=IIVWVXVXTTcujLMJYTUXr8mSF6Wo/wiFnxaJ+9DC3GVfg72mV7gvoKw72Q9KEl9rod25Blgv2VxiXkS4rNckRUlP4oQJli6K7QeWrnefrYr7cWfpnVK/gjcbefnEKCYBQ5qJSzVdB26bp6I6rgXyRiUrpw1d2bQhSb2dlhD27Kg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1614041178878598.8408630001435; Mon, 22 Feb 2021 16:46:18 -0800 (PST) Received: from localhost ([::1]:59986 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lELqD-00067f-Qg for importer@patchew.org; Mon, 22 Feb 2021 19:46:17 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35666) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lELge-0002Ih-Lb for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:24 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:32953) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lELeg-0005N8-Kv for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:24 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-486-UJqolkQzNOurCJS4rlcQtw-1; Mon, 22 Feb 2021 19:34:18 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 13A5F107ACE8; Tue, 23 Feb 2021 00:34:17 +0000 (UTC) Received: from scv.redhat.com (ovpn-112-247.rdu2.redhat.com [10.10.112.247]) by smtp.corp.redhat.com (Postfix) with ESMTP id 570095D6D5; Tue, 23 Feb 2021 00:34:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614040462; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jRBxKMCURslwJ7da45RvGXzhX8eos6ix0VgI3kUYvqs=; b=CPfYUJBshnEJkaqXvWhVR3NBexXsMM4YLy/G4gGqCDFxJpruBsmMzFxV8oKIGUtGf7ZvPr BfiAI0X14v22DBYCYMDk3i75epqj9ENZu2gupKRIyRXwrgz0y6ySXRxEoAvPbOej6A0hlq H60ZjjhNVd3jS9H/2jRvf3OmYdfF9ck= X-MC-Unique: UJqolkQzNOurCJS4rlcQtw-1 From: John Snow To: qemu-devel@nongnu.org, Markus Armbruster Subject: [PATCH v3 08/16] qapi/expr.py: add type hint annotations Date: Mon, 22 Feb 2021 19:34:00 -0500 Message-Id: <20210223003408.964543-9-jsnow@redhat.com> In-Reply-To: <20210223003408.964543-1-jsnow@redhat.com> References: <20210223003408.964543-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=63.128.21.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , John Snow , Eduardo Habkost , Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Annotations do not change runtime behavior. This commit *only* adds annotations. Signed-off-by: John Snow Reviewed-by: Eduardo Habkost Reviewed-by: Cleber Rosa --- scripts/qapi/expr.py | 71 ++++++++++++++++++++++++++++--------------- scripts/qapi/mypy.ini | 5 --- 2 files changed, 46 insertions(+), 30 deletions(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index f45d6be1f4c..df6c64950fa 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -15,7 +15,14 @@ # See the COPYING file in the top-level directory. =20 import re -from typing import MutableMapping, Optional, cast +from typing import ( + Iterable, + List, + MutableMapping, + Optional, + Union, + cast, +) =20 from .common import c_name from .error import QAPISemError @@ -23,9 +30,10 @@ from .source import QAPISourceInfo =20 =20 -# Expressions in their raw form are JSON-like structures with arbitrary fo= rms. -# Minimally, their top-level form must be a mapping of strings to values. -Expression =3D MutableMapping[str, object] +# Arbitrary form for a JSON-like object. +_JSObject =3D MutableMapping[str, object] +# Expressions in their raw form are (just) JSON-like objects. +Expression =3D _JSObject =20 =20 # Names must be letters, numbers, -, and _. They must start with letter, @@ -35,14 +43,19 @@ '[a-zA-Z][a-zA-Z0-9_-]*$') =20 =20 -def check_name_is_str(name, info, source): +def check_name_is_str(name: object, + info: QAPISourceInfo, + source: str) -> None: if not isinstance(name, str): raise QAPISemError(info, "%s requires a string name" % source) =20 =20 -def check_name_str(name, info, source, - allow_optional=3DFalse, enum_member=3DFalse, - permit_upper=3DFalse): +def check_name_str(name: str, + info: QAPISourceInfo, + source: str, + allow_optional: bool =3D False, + enum_member: bool =3D False, + permit_upper: bool =3D False) -> None: membername =3D name =20 if allow_optional and name.startswith('*'): @@ -62,16 +75,20 @@ def check_name_str(name, info, source, assert not membername.startswith('*') =20 =20 -def check_defn_name_str(name, info, meta): +def check_defn_name_str(name: str, info: QAPISourceInfo, meta: str) -> Non= e: check_name_str(name, info, meta, permit_upper=3DTrue) if name.endswith('Kind') or name.endswith('List'): raise QAPISemError( info, "%s name should not end in '%s'" % (meta, name[-4:])) =20 =20 -def check_keys(value, info, source, required, optional): +def check_keys(value: _JSObject, + info: QAPISourceInfo, + source: str, + required: List[str], + optional: List[str]) -> None: =20 - def pprint(elems): + def pprint(elems: Iterable[str]) -> str: return ', '.join("'" + e + "'" for e in sorted(elems)) =20 missing =3D set(required) - set(value) @@ -91,7 +108,7 @@ def pprint(elems): pprint(unknown), pprint(allowed))) =20 =20 -def check_flags(expr, info): +def check_flags(expr: Expression, info: QAPISourceInfo) -> None: for key in ['gen', 'success-response']: if key in expr and expr[key] is not False: raise QAPISemError( @@ -109,9 +126,9 @@ def check_flags(expr, info): "are incompatible") =20 =20 -def check_if(expr, info, source): +def check_if(expr: _JSObject, info: QAPISourceInfo, source: str) -> None: =20 - def check_if_str(ifcond): + def check_if_str(ifcond: object) -> None: if not isinstance(ifcond, str): raise QAPISemError( info, @@ -137,7 +154,7 @@ def check_if_str(ifcond): expr['if'] =3D [ifcond] =20 =20 -def normalize_members(members): +def normalize_members(members: object) -> None: if isinstance(members, dict): for key, arg in members.items(): if isinstance(arg, dict): @@ -145,8 +162,11 @@ def normalize_members(members): members[key] =3D {'type': arg} =20 =20 -def check_type(value, info, source, - allow_array=3DFalse, allow_dict=3DFalse): +def check_type(value: Optional[object], + info: QAPISourceInfo, + source: str, + allow_array: bool =3D False, + allow_dict: Union[bool, str] =3D False) -> None: if value is None: return =20 @@ -190,7 +210,8 @@ def check_type(value, info, source, check_type(arg['type'], info, key_source, allow_array=3DTrue) =20 =20 -def check_features(features, info): +def check_features(features: Optional[object], + info: QAPISourceInfo) -> None: if features is None: return if not isinstance(features, list): @@ -207,7 +228,7 @@ def check_features(features, info): check_if(f, info, source) =20 =20 -def check_enum(expr, info): +def check_enum(expr: Expression, info: QAPISourceInfo) -> None: name =3D expr['enum'] members =3D expr['data'] prefix =3D expr.get('prefix') @@ -231,7 +252,7 @@ def check_enum(expr, info): check_if(member, info, source) =20 =20 -def check_struct(expr, info): +def check_struct(expr: Expression, info: QAPISourceInfo) -> None: name =3D cast(str, expr['struct']) # Asserted in check_exprs members =3D expr['data'] =20 @@ -239,7 +260,7 @@ def check_struct(expr, info): check_type(expr.get('base'), info, "'base'") =20 =20 -def check_union(expr, info): +def check_union(expr: Expression, info: QAPISourceInfo) -> None: name =3D cast(str, expr['union']) # Asserted in check_exprs base =3D expr.get('base') discriminator =3D expr.get('discriminator') @@ -265,7 +286,7 @@ def check_union(expr, info): check_type(value['type'], info, source, allow_array=3Dnot base) =20 =20 -def check_alternate(expr, info): +def check_alternate(expr: Expression, info: QAPISourceInfo) -> None: members =3D expr['data'] =20 if not members: @@ -282,7 +303,7 @@ def check_alternate(expr, info): check_type(value['type'], info, source) =20 =20 -def check_command(expr, info): +def check_command(expr: Expression, info: QAPISourceInfo) -> None: args =3D expr.get('data') rets =3D expr.get('returns') boxed =3D expr.get('boxed', False) @@ -293,7 +314,7 @@ def check_command(expr, info): check_type(rets, info, "'returns'", allow_array=3DTrue) =20 =20 -def check_event(expr, info): +def check_event(expr: Expression, info: QAPISourceInfo) -> None: args =3D expr.get('data') boxed =3D expr.get('boxed', False) =20 @@ -302,7 +323,7 @@ def check_event(expr, info): check_type(args, info, "'data'", allow_dict=3Dnot boxed) =20 =20 -def check_exprs(exprs): +def check_exprs(exprs: List[_JSObject]) -> List[_JSObject]: for expr_elem in exprs: # Expression assert isinstance(expr_elem['expr'], dict) diff --git a/scripts/qapi/mypy.ini b/scripts/qapi/mypy.ini index 0a000d58b37..7797c834328 100644 --- a/scripts/qapi/mypy.ini +++ b/scripts/qapi/mypy.ini @@ -8,11 +8,6 @@ disallow_untyped_defs =3D False disallow_incomplete_defs =3D False check_untyped_defs =3D False =20 -[mypy-qapi.expr] -disallow_untyped_defs =3D False -disallow_incomplete_defs =3D False -check_untyped_defs =3D False - [mypy-qapi.parser] disallow_untyped_defs =3D False disallow_incomplete_defs =3D False --=20 2.29.2 From nobody Fri Jun 14 19:49:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614040912; cv=none; d=zohomail.com; s=zohoarc; b=g/bV2a2UMmeSyeKs2i5exwZ2bpm8evPRf5E1QqjdfziEXcdGNwN1pco196Cvnb/fdXvtqb3xk8XDHPpVV5SRKOYXVhjj7IXSA9vYxZqQW0mIrg3iOqPqsWnifEvs1MpabKNJ3D2p6RWva/icK6ehtWWmVuUFdu+rB30lE5gUfk8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614040912; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=B71SzLeLid51VPeK1mV5HQktxFjRRGL7XBLLGtod3oc=; b=iYudLpq4OP5vKzglFDNn3oITs3fGlQofI6h5oO0tO0HD2563Uqzb0dARTNRAujbd8w1PARAI66JofU1b+lJZVlak96frKX2KbrcRoqOUXfSXSvQOeyF0P4bxFcrFLnw8AGRr3252KA9iI2KxI6v1GERpdGPTqmbKda/cATqUR40= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 161404091269488.06504790554072; Mon, 22 Feb 2021 16:41:52 -0800 (PST) Received: from localhost ([::1]:45178 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lELlt-00009x-Vz for importer@patchew.org; Mon, 22 Feb 2021 19:41:51 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35642) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lELgT-0001pn-JP for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:13 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:56059) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lELef-0005Mx-Np for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:13 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-99-u2eMckg2MYuNyBBvR92vWQ-1; Mon, 22 Feb 2021 19:34:18 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id ED05F8030BB; Tue, 23 Feb 2021 00:34:17 +0000 (UTC) Received: from scv.redhat.com (ovpn-112-247.rdu2.redhat.com [10.10.112.247]) by smtp.corp.redhat.com (Postfix) with ESMTP id 395165D6D5; Tue, 23 Feb 2021 00:34:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614040461; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=B71SzLeLid51VPeK1mV5HQktxFjRRGL7XBLLGtod3oc=; b=V9Va6jWsOX78Pq/wIcubf4cqmz/P3wffNM9I882ZTOCunFMytPADOTPtxyWdqGCPTKyYoi euBUDlSgfFT2bshsvRh4HLJUtjet/hEBQFJNat8ZCJVuDxyx5SRW7udiggbh6DWGc19Zz9 q5+1G7k5gk2vgXqDF4o3bShbATRugX4= X-MC-Unique: u2eMckg2MYuNyBBvR92vWQ-1 From: John Snow To: qemu-devel@nongnu.org, Markus Armbruster Subject: [PATCH v3 09/16] qapi/expr.py: Consolidate check_if_str calls in check_if Date: Mon, 22 Feb 2021 19:34:01 -0500 Message-Id: <20210223003408.964543-10-jsnow@redhat.com> In-Reply-To: <20210223003408.964543-1-jsnow@redhat.com> References: <20210223003408.964543-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , John Snow , Eduardo Habkost , Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This is a small rewrite to address some minor style nits. Don't compare against the empty list to check for the empty condition, and move the normalization forward to unify the check on the now-normalized structure. With the check unified, the local nested function isn't needed anymore and can be brought down into the normal flow of the function. With the nesting level changed, shuffle the error strings around a bit to get them to fit in 79 columns. Note: though ifcond is typed as Sequence[str] elsewhere, we *know* that the parser will produce real, bona-fide lists. It's okay to check isinstance(ifcond, list) here. Signed-off-by: John Snow --- scripts/qapi/expr.py | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index df6c64950fa..3235a3b809e 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -128,30 +128,26 @@ def check_flags(expr: Expression, info: QAPISourceInf= o) -> None: =20 def check_if(expr: _JSObject, info: QAPISourceInfo, source: str) -> None: =20 - def check_if_str(ifcond: object) -> None: - if not isinstance(ifcond, str): - raise QAPISemError( - info, - "'if' condition of %s must be a string or a list of string= s" - % source) - if ifcond.strip() =3D=3D '': - raise QAPISemError( - info, - "'if' condition '%s' of %s makes no sense" - % (ifcond, source)) - ifcond =3D expr.get('if') if ifcond is None: return - if isinstance(ifcond, list): - if ifcond =3D=3D []: + + # Normalize to a list + if not isinstance(ifcond, list): + ifcond =3D [ifcond] + expr['if'] =3D ifcond + + if not ifcond: + raise QAPISemError(info, f"'if' condition [] of {source} is useles= s") + + for element in ifcond: + if not isinstance(element, str): + raise QAPISemError(info, ( + f"'if' condition of {source}" + " must be a string or a list of strings")) + if element.strip() =3D=3D '': raise QAPISemError( - info, "'if' condition [] of %s is useless" % source) - for elt in ifcond: - check_if_str(elt) - else: - check_if_str(ifcond) - expr['if'] =3D [ifcond] + info, f"'if' condition '{element}' of {source} makes no se= nse") =20 =20 def normalize_members(members: object) -> None: --=20 2.29.2 From nobody Fri Jun 14 19:49:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614041066; cv=none; d=zohomail.com; s=zohoarc; b=GZ1Gwo6TkKDwUfRsWkqlbkb6dRuWnxJiwayvOmxa/CBjIyvqnM7cSIqF2pZEgXliYBrzs2ggEIVrAsUsqjmAall6/1S5c0ih+JRaMSbfHt0OZW4UiIO881WeimEawITlpKqt2VIlU8UzpOpFQD+PZ4f0R1hIHOTPzvZFYfw8zk0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614041066; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=znM4lXZQcNIpQy212wOqWagwMWra20PZb3sCvjr3AX4=; b=VLCvjthvvF6Iw6lX/zqqBsLLoINUIj35fF8KqClH+39lPZxeNp30uSmLEwIre5Lk4RdHDypuSjDfsdfm+cymp+o9yGV2Mcw4fahWVSt2Qcoaf4++razY2xtDucG7IybJxHH4xOus4JKQ1ZTQPwlUjr9HEuJYXTKgTeZ0dhSkSxo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1614041066511157.06235061980829; Mon, 22 Feb 2021 16:44:26 -0800 (PST) Received: from localhost ([::1]:53560 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lELoP-0003Zx-F0 for importer@patchew.org; Mon, 22 Feb 2021 19:44:25 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35654) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lELgc-0002DC-KO for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:22 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:50690) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lELeg-0005N4-75 for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:22 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-161-dg7L6HV0OWGhJXIH2OeQTg-1; Mon, 22 Feb 2021 19:34:19 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D24BA186DD21; Tue, 23 Feb 2021 00:34:18 +0000 (UTC) Received: from scv.redhat.com (ovpn-112-247.rdu2.redhat.com [10.10.112.247]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1FC3A5D6D5; Tue, 23 Feb 2021 00:34:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614040461; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=znM4lXZQcNIpQy212wOqWagwMWra20PZb3sCvjr3AX4=; b=HH99iC052Od62wf3bH8B0JwjlpDWJgHHAI2rqED2eenAOHQVfDbF9/ADMF65Fe2F2ew330 +wOBB/lFEhlYOCEwgcA4JBzTBKtb60c0qPwYhKR/KxWLe6K/TmZySERvKtPpEyGeVtA+xe V6ZfihnBrq00BWuNvmXOyM2/KjvEQkw= X-MC-Unique: dg7L6HV0OWGhJXIH2OeQTg-1 From: John Snow To: qemu-devel@nongnu.org, Markus Armbruster Subject: [PATCH v3 10/16] qapi/expr.py: Remove single-letter variable Date: Mon, 22 Feb 2021 19:34:02 -0500 Message-Id: <20210223003408.964543-11-jsnow@redhat.com> In-Reply-To: <20210223003408.964543-1-jsnow@redhat.com> References: <20210223003408.964543-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , John Snow , Eduardo Habkost , Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Signed-off-by: John Snow Reviewed-by: Eduardo Habkost Reviewed-by: Cleber Rosa --- scripts/qapi/expr.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index 3235a3b809e..473ee4f7f7e 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -214,14 +214,14 @@ def check_features(features: Optional[object], raise QAPISemError(info, "'features' must be an array") features[:] =3D [f if isinstance(f, dict) else {'name': f} for f in features] - for f in features: + for feature in features: source =3D "'features' member" - assert isinstance(f, dict) - check_keys(f, info, source, ['name'], ['if']) - check_name_is_str(f['name'], info, source) - source =3D "%s '%s'" % (source, f['name']) - check_name_str(f['name'], info, source) - check_if(f, info, source) + assert isinstance(feature, dict) + check_keys(feature, info, source, ['name'], ['if']) + check_name_is_str(feature['name'], info, source) + source =3D "%s '%s'" % (source, feature['name']) + check_name_str(feature['name'], info, source) + check_if(feature, info, source) =20 =20 def check_enum(expr: Expression, info: QAPISourceInfo) -> None: --=20 2.29.2 From nobody Fri Jun 14 19:49:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614040907; cv=none; d=zohomail.com; s=zohoarc; b=hF1KiFYF6Fe1+w7wadd9P+IKGjkwwOOIBvw7bdA8Bky7m4CkUO9zA2ng38Otcg1q7pbKba/PXUs4Mk0dEQFXI1Yo8HROq+KDfGbzJXTi20ULmfnCkcn57Rg+IqIdtoc6sNArK5sUhjZ50F5wxc64fsyJYzIoPT/YYOo8t2CRDBU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614040907; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=e8H9kOVXjWamkvFLjCPMgvRiLcHBSMY8RQACkvfTRyc=; b=kVY5jEj8rRp3vMqxlYyoP4SFyRaXn/uX5nirS45l82hUkq89PrhnN9sBeUBKuh5LtxwpzvFuOT+1UN7vg0qTojCDIZ2YsS0wCacZEqa2o4B2HZlw7Orz73XmD7qQmxfggCV7kGLEatTbr47rX2gZAMPPz3TEcc94udcMjVKXVaU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1614040907236234.80365546864323; Mon, 22 Feb 2021 16:41:47 -0800 (PST) Received: from localhost ([::1]:44908 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lELlp-0008V7-Pj for importer@patchew.org; Mon, 22 Feb 2021 19:41:45 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35664) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lELge-0002Id-KW for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:24 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:33031) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lELei-0005ND-6N for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:24 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-43-EPFisN0ZOVynUqz8CGhlBg-1; Mon, 22 Feb 2021 19:34:20 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CECA8107ACE3; Tue, 23 Feb 2021 00:34:19 +0000 (UTC) Received: from scv.redhat.com (ovpn-112-247.rdu2.redhat.com [10.10.112.247]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0A0FC5D6D5; Tue, 23 Feb 2021 00:34:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614040463; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=e8H9kOVXjWamkvFLjCPMgvRiLcHBSMY8RQACkvfTRyc=; b=a56kibra+wpPtt6TjQHBwByr/f2qnVgJU83j9bF9WZBKQcKLFbF917+ShQzpMIf065jc9T /ah2NV5YcOEwHEfCi3Qg+HNxY2tsrO65fznDaUiNBTdhu0q7lnj8msyI2IFTkH+nXNOYUM wSBMy2b8tPCM3OlvTaNc4RZm8XD6VtI= X-MC-Unique: EPFisN0ZOVynUqz8CGhlBg-1 From: John Snow To: qemu-devel@nongnu.org, Markus Armbruster Subject: [PATCH v3 11/16] qapi/expr.py: enable pylint checks Date: Mon, 22 Feb 2021 19:34:03 -0500 Message-Id: <20210223003408.964543-12-jsnow@redhat.com> In-Reply-To: <20210223003408.964543-1-jsnow@redhat.com> References: <20210223003408.964543-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , John Snow , Eduardo Habkost , Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Signed-off-by: John Snow Tested-by: Eduardo Habkost Reviewed-by: Eduardo Habkost Reviewed-by: Cleber Rosa Tested-by: Cleber Rosa --- scripts/qapi/pylintrc | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/qapi/pylintrc b/scripts/qapi/pylintrc index b9e077a1642..fb0386d529a 100644 --- a/scripts/qapi/pylintrc +++ b/scripts/qapi/pylintrc @@ -3,7 +3,6 @@ # Add files or directories matching the regex patterns to the ignore list. # The regex matches against base names, not paths. ignore-patterns=3Derror.py, - expr.py, parser.py, schema.py, =20 --=20 2.29.2 From nobody Fri Jun 14 19:49:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614040953; cv=none; d=zohomail.com; s=zohoarc; b=FHftADEyhA2yQD01X4k1O/NegQlA4+iHf/3xV9GUAEZyhxA2fon4pdcEJnTyISVvG9bSI89s3GsuIwJULkr6NJPddN9pna1GImHPCC+pdIbJ2hZFyTWLLMAhyUp4eZyvnSAR/r7VjC740xnKRXoCvo/i9VyB2BBNpinY1U79DjU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614040953; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=bpGsLWd4+Ca5Vnbh6wA32m5N5z4KyoiDXxdjHYcn5GM=; b=M6frtgWoSzpIjf7bU2YHA5eebXodTUTX13UUhD+4RuC3eqAGEFb112lcz99BDwfdreKXHY9c4iTNX1yv/DrtIGv+k9iPsk7orin5wialINzOrurTYvnu95LtFmpFGk4q4U0NAVlT2NTkvmGSn0LNKWEG7pcOoIHKM2N/mu/JEag= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1614040953261657.291836480515; Mon, 22 Feb 2021 16:42:33 -0800 (PST) Received: from localhost ([::1]:47628 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lELma-0001AW-4W for importer@patchew.org; Mon, 22 Feb 2021 19:42:32 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35668) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lELgf-0002K5-60 for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:25 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:50407) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lELei-0005NH-PL for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:24 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-98-2I8sjkAHNVWTboktL0_OCg-1; Mon, 22 Feb 2021 19:34:21 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B82D88030B7; Tue, 23 Feb 2021 00:34:20 +0000 (UTC) Received: from scv.redhat.com (ovpn-112-247.rdu2.redhat.com [10.10.112.247]) by smtp.corp.redhat.com (Postfix) with ESMTP id 016B25D6D5; Tue, 23 Feb 2021 00:34:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614040463; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bpGsLWd4+Ca5Vnbh6wA32m5N5z4KyoiDXxdjHYcn5GM=; b=Xxutj/TJz3ALSQKi6C+IDlkrs5YHyCLPbIUMto4rQJ7gZjO0cbMSXk5yJFDpyARtuvf2vw ugSsCuuDB2NPXpClGuO8yzVzTAHuXsTzbz1z6TbkPi3fOzsCwywWm9je2pNcfOnIImRBCj WcDRGN/EL3my9D+cxZ7ahoggJLhDlOo= X-MC-Unique: 2I8sjkAHNVWTboktL0_OCg-1 From: John Snow To: qemu-devel@nongnu.org, Markus Armbruster Subject: [PATCH v3 12/16] qapi/expr.py: Add docstrings Date: Mon, 22 Feb 2021 19:34:04 -0500 Message-Id: <20210223003408.964543-13-jsnow@redhat.com> In-Reply-To: <20210223003408.964543-1-jsnow@redhat.com> References: <20210223003408.964543-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , John Snow , Eduardo Habkost , Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" In this patch, I begin to adopt the idea that some functions can be marked as "Const" and others "RW" to distinguish between functions that perform a check-only, and those that perform normilization work and modify the structure under consideration. It is not any kind of doc standard, it was for my own benefit. Signed-off-by: John Snow Reviewed-by: Cleber Rosa --- scripts/qapi/expr.py | 167 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 164 insertions(+), 3 deletions(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index 473ee4f7f7e..2b96bec722f 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- # -# Check (context-free) QAPI schema expression structure -# # Copyright IBM, Corp. 2011 # Copyright (c) 2013-2019 Red Hat Inc. # @@ -14,6 +12,25 @@ # This work is licensed under the terms of the GNU GPL, version 2. # See the COPYING file in the top-level directory. =20 +""" +Normalize and validate (context-free) QAPI schema expression structures. + +After QAPI expressions are parsed from disk, they are stored in +recursively nested Python data structures using Dict, List, str, bool, +and int. This module ensures that those nested structures have the +correct type(s) and key(s) where appropriate for the QAPI context-free +grammar. + +The QAPI schema expression language allows for syntactic sugar; this +module also handles the normalization process of these nested +structures. + +See `check_exprs` for the main entry point. + +See `schema.QAPISchema` for processing into native Python data +structures and contextual semantic validation. +""" + import re from typing import ( Iterable, @@ -32,7 +49,7 @@ =20 # Arbitrary form for a JSON-like object. _JSObject =3D MutableMapping[str, object] -# Expressions in their raw form are (just) JSON-like objects. +#: Expressions in their unvalidated form are JSON-like objects. Expression =3D _JSObject =20 =20 @@ -46,6 +63,7 @@ def check_name_is_str(name: object, info: QAPISourceInfo, source: str) -> None: + """Ensures that ``name`` is a string. [Const]""" if not isinstance(name, str): raise QAPISemError(info, "%s requires a string name" % source) =20 @@ -56,6 +74,25 @@ def check_name_str(name: str, allow_optional: bool =3D False, enum_member: bool =3D False, permit_upper: bool =3D False) -> None: + """ + Ensures a string is a legal name. [Const] + + A name is legal in the default case when: + + - It matches ``(__[a-z0-9.-]+_)?[a-z][a-z0-9_-]*`` + - It does not start with ``q_`` or ``q-`` + + :param name: Name to check. + :param info: QAPI source file information. + :param source: Human-readable str describing "what" this name = is. + :param allow_optional: Allow the very first character to be ``*``. + (Cannot be used with ``enum_member``) + :param enum_member: Allow the very first character to be a digit. + (Cannot be used with ``allow_optional``) + :param permit_upper: Allows upper-case characters wherever + lower-case characters are allowed. + """ + assert not (allow_optional and enum_member) membername =3D name =20 if allow_optional and name.startswith('*'): @@ -76,6 +113,17 @@ def check_name_str(name: str, =20 =20 def check_defn_name_str(name: str, info: QAPISourceInfo, meta: str) -> Non= e: + """ + Ensures a name is a legal definition name. [Const] + + A legal definition name: + - Adheres to the criteria in `check_name_str`, with uppercase permitt= ed + - Does not end with ``Kind`` or ``List``. + + :param name: Name to check. + :param info: QAPI source file information. + :param meta: Type name of the QAPI expression. + """ check_name_str(name, info, meta, permit_upper=3DTrue) if name.endswith('Kind') or name.endswith('List'): raise QAPISemError( @@ -87,6 +135,15 @@ def check_keys(value: _JSObject, source: str, required: List[str], optional: List[str]) -> None: + """ + Ensures an object has a specific set of keys. [Const] + + :param value: The object to check. + :param info: QAPI source file information. + :param source: Human-readable str describing "what" this object is. + :param required: Keys that *must* be present. + :param optional: Keys that *may* be present. + """ =20 def pprint(elems: Iterable[str]) -> str: return ', '.join("'" + e + "'" for e in sorted(elems)) @@ -109,6 +166,12 @@ def pprint(elems: Iterable[str]) -> str: =20 =20 def check_flags(expr: Expression, info: QAPISourceInfo) -> None: + """ + Ensures common fields in an Expression are correct. [Const] + + :param expr: Expression to validate. + :param info: QAPI source file information. + """ for key in ['gen', 'success-response']: if key in expr and expr[key] is not False: raise QAPISemError( @@ -127,6 +190,19 @@ def check_flags(expr: Expression, info: QAPISourceInfo= ) -> None: =20 =20 def check_if(expr: _JSObject, info: QAPISourceInfo, source: str) -> None: + """ + Syntactically validate and normalize the ``if`` field of an object. [R= W] + + The ``if`` field may be either a ``str`` or a ``List[str]``. + A ``str`` element will be normalized to ``List[str]``. + + :param expr: A ``dict``; the ``if`` field, if present, will be validat= ed. + :param info: QAPI source file information. + + :forms: + :sugared: ``Union[str, List[str]]`` + :canonical: ``List[str]`` + """ =20 ifcond =3D expr.get('if') if ifcond is None: @@ -151,6 +227,17 @@ def check_if(expr: _JSObject, info: QAPISourceInfo, so= urce: str) -> None: =20 =20 def normalize_members(members: object) -> None: + """ + Normalize a "members" value. [RW] + + If ``members`` is an object, for every value in that object, if that + value is not itself already an object, normalize it to + ``{'type': value}``. + + :forms: + :sugared: ``Dict[str, Union[str, TypeRef]]`` + :canonical: ``Dict[str, TypeRef]`` + """ if isinstance(members, dict): for key, arg in members.items(): if isinstance(arg, dict): @@ -163,6 +250,21 @@ def check_type(value: Optional[object], source: str, allow_array: bool =3D False, allow_dict: Union[bool, str] =3D False) -> None: + """ + Check the QAPI type of ``value``. [RW] + + Python types of ``str`` or ``None`` are always allowed. + + :param value: The value to check. + :param info: QAPI Source file information. + :param source: Human readable string describing "what" the value = is. + :param allow_array: Allow a ``List[str]`` of length 1, + which indicates an Array type. + :param allow_dict: Allow a dict, treated as an anonymous type. + When given a string, check if that name is allowed= to + have keys that use uppercase letters, and modify + validation accordingly. + """ if value is None: return =20 @@ -208,6 +310,16 @@ def check_type(value: Optional[object], =20 def check_features(features: Optional[object], info: QAPISourceInfo) -> None: + """ + Syntactically validate and normalize the ``features`` field. [RW] + + ``features`` may be a ``list`` of either ``str`` or ``dict``. + Any ``str`` element will be normalized to ``{'name': element}``. + + :forms: + :sugared: ``List[Union[str, Feature]]`` + :canonical: ``List[Feature]`` + """ if features is None: return if not isinstance(features, list): @@ -225,6 +337,12 @@ def check_features(features: Optional[object], =20 =20 def check_enum(expr: Expression, info: QAPISourceInfo) -> None: + """ + Validate this `Expression` as an ``enum`` expression. [RW] + + :param expr: `Expression` to validate. + :param info: QAPI source file information. + """ name =3D expr['enum'] members =3D expr['data'] prefix =3D expr.get('prefix') @@ -249,6 +367,12 @@ def check_enum(expr: Expression, info: QAPISourceInfo)= -> None: =20 =20 def check_struct(expr: Expression, info: QAPISourceInfo) -> None: + """ + Validate this `Expression` as a ``struct`` expression. [RW] + + :param expr: `Expression` to validate. + :param info: QAPI source file information. + """ name =3D cast(str, expr['struct']) # Asserted in check_exprs members =3D expr['data'] =20 @@ -257,6 +381,12 @@ def check_struct(expr: Expression, info: QAPISourceInf= o) -> None: =20 =20 def check_union(expr: Expression, info: QAPISourceInfo) -> None: + """ + Validate this `Expression` as a ``union`` expression. [RW] + + :param expr: `Expression` to validate. + :param info: QAPI source file information. + """ name =3D cast(str, expr['union']) # Asserted in check_exprs base =3D expr.get('base') discriminator =3D expr.get('discriminator') @@ -283,6 +413,12 @@ def check_union(expr: Expression, info: QAPISourceInfo= ) -> None: =20 =20 def check_alternate(expr: Expression, info: QAPISourceInfo) -> None: + """ + Validate this `Expression` as an ``alternate`` expression. [RW] + + :param expr: Expression to validate. + :param info: QAPI source file information. + """ members =3D expr['data'] =20 if not members: @@ -300,6 +436,12 @@ def check_alternate(expr: Expression, info: QAPISource= Info) -> None: =20 =20 def check_command(expr: Expression, info: QAPISourceInfo) -> None: + """ + Validate this `Expression` as a ``command`` expression. [RW] + + :param expr: `Expression` to validate. + :param info: QAPI source file information. + """ args =3D expr.get('data') rets =3D expr.get('returns') boxed =3D expr.get('boxed', False) @@ -311,6 +453,16 @@ def check_command(expr: Expression, info: QAPISourceIn= fo) -> None: =20 =20 def check_event(expr: Expression, info: QAPISourceInfo) -> None: + """ + Normalize and syntactically validate the ``event`` expression. [RW] + + :Event: + :event: ``str`` + :data: ``Optional[Dict[str, Member]]`` + :boxed: ``Optional[bool]`` + :if: ``Optional[Ifcond]`` (see: `check_if`) + :features: ``Optional[Features]`` (see: `check_features`) + """ args =3D expr.get('data') boxed =3D expr.get('boxed', False) =20 @@ -320,6 +472,15 @@ def check_event(expr: Expression, info: QAPISourceInfo= ) -> None: =20 =20 def check_exprs(exprs: List[_JSObject]) -> List[_JSObject]: + """ + Validate and normalize a list of parsed QAPI schema expressions. [RW] + + This function accepts a list of expressions + metadta as returned by + the parser. It destructively normalizes the expressions in-place. + + :param exprs: The list of expressions to normalize/validate. + :return: The same list of expressions (now modified). + """ for expr_elem in exprs: # Expression assert isinstance(expr_elem['expr'], dict) --=20 2.29.2 From nobody Fri Jun 14 19:49:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614041053; cv=none; d=zohomail.com; s=zohoarc; b=ZZtsSzdPQoqDoj0SHoa3q0t8wSCE55PUzaFFoB/tc/ohSRcBdsNaxRdxyEU7wJO2zmMwK54bIfSugFnq71IqyyCMyVuTFVXaQLYHZn/6q+AGZ+HuADLU1n9DdHzw7fw6VP23VT0YkwqJyVhIYoNws3Zs3lHU3v0UNp1wMhDCSXg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614041053; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=doA9yJHacEQkyLpm+WjaQHNzN48+joYVgFx8ILUzw4E=; b=Ua+FUTryvyKhJxAz5sX/uUQlP7Au3TjPz04W+KaZkT0RE2xrdTrSyfLKtwyS93/ioVObCQEvE3NLj4i1xXk3XRJkubS8a6v4emb0P+eQEl3TzMvaalZjbyps8wgwxbI0Mg/XiFelJxLKvE9p9EzatWwcMhpSxCp2tSbhdPKdY6s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1614041053158236.12970982930426; Mon, 22 Feb 2021 16:44:13 -0800 (PST) Received: from localhost ([::1]:53270 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lELoC-0003SY-3F for importer@patchew.org; Mon, 22 Feb 2021 19:44:12 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35676) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lELgg-0002ML-Cx for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:26 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:42067) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lELej-0005NV-S5 for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:26 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-250-eAsAKVpLMwi7rjPOG7pp7w-1; Mon, 22 Feb 2021 19:34:22 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9AB451005501; Tue, 23 Feb 2021 00:34:21 +0000 (UTC) Received: from scv.redhat.com (ovpn-112-247.rdu2.redhat.com [10.10.112.247]) by smtp.corp.redhat.com (Postfix) with ESMTP id DE3A85D74E; Tue, 23 Feb 2021 00:34:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614040465; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=doA9yJHacEQkyLpm+WjaQHNzN48+joYVgFx8ILUzw4E=; b=ea/LpzDm9JRqcn1BkwEhPurikkkRVF2Qv3UI0NOWmZoHlyM+GSepFi0bdFdYa98NPr7DZc jloh/5xuy8UlS8+aHEOYw+ZyoGSMYG6j11bpoU2s4Vlh0JAOq5P9Z+s8axcdcE3ANcDpD2 nTlJOB35Os3SdpjsNNluSVEoShEz7oE= X-MC-Unique: eAsAKVpLMwi7rjPOG7pp7w-1 From: John Snow To: qemu-devel@nongnu.org, Markus Armbruster Subject: [PATCH v3 13/16] qapi/expr.py: Modify check_keys to accept any Collection Date: Mon, 22 Feb 2021 19:34:05 -0500 Message-Id: <20210223003408.964543-14-jsnow@redhat.com> In-Reply-To: <20210223003408.964543-1-jsnow@redhat.com> References: <20210223003408.964543-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=63.128.21.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , John Snow , Eduardo Habkost , Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This is a minor adjustment that allows the 'required' and 'optional' keys fields to take a default value of an empty, immutable sequence (the empty tuple). This reveals a quirk of this function, which is that "a + b" is list-specific behavior. We can accept a wider variety of types if we avoid that behavior. Using Collection allows us to accept things like lists, tuples, sets, and so on. (Iterable would also have worked, but Iterable also includes things like generator expressions which are consumed upon iteration, which would require a rewrite to make sure that each input was only traversed once.) Signed-off-by: John Snow --- scripts/qapi/expr.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index 2b96bec722f..0b841f292d7 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -33,6 +33,7 @@ =20 import re from typing import ( + Collection, Iterable, List, MutableMapping, @@ -133,8 +134,8 @@ def check_defn_name_str(name: str, info: QAPISourceInfo= , meta: str) -> None: def check_keys(value: _JSObject, info: QAPISourceInfo, source: str, - required: List[str], - optional: List[str]) -> None: + required: Collection[str] =3D (), + optional: Collection[str] =3D ()) -> None: """ Ensures an object has a specific set of keys. [Const] =20 @@ -155,7 +156,7 @@ def pprint(elems: Iterable[str]) -> str: "%s misses key%s %s" % (source, 's' if len(missing) > 1 else '', pprint(missing))) - allowed =3D set(required + optional) + allowed =3D set(required) | set(optional) unknown =3D set(value) - allowed if unknown: raise QAPISemError( --=20 2.29.2 From nobody Fri Jun 14 19:49:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614040970; cv=none; d=zohomail.com; s=zohoarc; b=NAepQGmY0IZVSg2MS2rKZnAEl94UIZGQvgLB/XqwaCBSuMLTOvzRZFN9dAu63Y67cAxZdrP1CN2j2RbANHDtkEG1MDxHcN/P/zoJ/l8DTABwGwonCJkBub0Glw5ydYW1taDYcQ5ANOOHVQJQO5gPoT4cermavVN3PdKV4nlcCxU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614040970; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=+Fd+v1opmHyA9PnyOfe6QSG4N81hIkEu9HE+yPuC4O4=; b=On86dOVj02n51fSLM7slrNR/5TwJYamipWiMWq5AdLBb8BP4GL7HBsWA1ahfavoTpOC4n343wBzkrAzK86pQnu5rv5xGfNo4UYqs4S9toftsyDHJ6NwlyRm+uc3XdIlFDm3sjReD4xlrjtitVO51CNwfNXk9445dJ0aQFYhGnUg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1614040970049670.8770122707555; Mon, 22 Feb 2021 16:42:50 -0800 (PST) Received: from localhost ([::1]:47960 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lELmq-0001If-Vv for importer@patchew.org; Mon, 22 Feb 2021 19:42:49 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35674) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lELgg-0002MJ-CY for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:26 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:55752) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lELej-0005NX-QY for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:26 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-189-9n0dvRGsPfGLIeEa3VY8Dw-1; Mon, 22 Feb 2021 19:34:23 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 815F78030C1; Tue, 23 Feb 2021 00:34:22 +0000 (UTC) Received: from scv.redhat.com (ovpn-112-247.rdu2.redhat.com [10.10.112.247]) by smtp.corp.redhat.com (Postfix) with ESMTP id C24565D6D5; Tue, 23 Feb 2021 00:34:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614040465; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+Fd+v1opmHyA9PnyOfe6QSG4N81hIkEu9HE+yPuC4O4=; b=di62+Umt0ifTTYkgZlPIr/+8DdmqmCgYZ9WzvxA70ug/rZeP/oZPk9EjOKtT5pRFcEj6Gn oq+cshVwOQ6K8S6UwYAETcLno6EUd72DZAOvfNNSgy0x7a3MD8DJDvV7LuP+PYHrDionlh MLT6Zy0++RfN4PUrHrFP9TTlU7N4Dxc= X-MC-Unique: 9n0dvRGsPfGLIeEa3VY8Dw-1 From: John Snow To: qemu-devel@nongnu.org, Markus Armbruster Subject: [PATCH v3 14/16] qapi/expr.py: Use tuples instead of lists for static data Date: Mon, 22 Feb 2021 19:34:06 -0500 Message-Id: <20210223003408.964543-15-jsnow@redhat.com> In-Reply-To: <20210223003408.964543-1-jsnow@redhat.com> References: <20210223003408.964543-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , John Snow , Eduardo Habkost , Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" It is -- maybe -- possibly -- three nanoseconds faster. Signed-off-by: John Snow Reviewed-by: Eduardo Habkost Reviewed-by: Cleber Rosa --- This can be dropped if desired; it has no real functional impact I could defend in code review court. I just happened to write it this way. Signed-off-by: John Snow --- scripts/qapi/expr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index 0b841f292d7..61699de8cd5 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -173,11 +173,11 @@ def check_flags(expr: Expression, info: QAPISourceInf= o) -> None: :param expr: Expression to validate. :param info: QAPI source file information. """ - for key in ['gen', 'success-response']: + for key in ('gen', 'success-response'): if key in expr and expr[key] is not False: raise QAPISemError( info, "flag '%s' may only use false value" % key) - for key in ['boxed', 'allow-oob', 'allow-preconfig', 'coroutine']: + for key in ('boxed', 'allow-oob', 'allow-preconfig', 'coroutine'): if key in expr and expr[key] is not True: raise QAPISemError( info, "flag '%s' may only use true value" % key) --=20 2.29.2 From nobody Fri Jun 14 19:49:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614041097; cv=none; d=zohomail.com; s=zohoarc; b=kNRdnRTUxT+hUdWG2fPf8rpSggBRVzAD7Hga2bYLEVRxFBUPYFpiksTXstBwXfYIB1k7Hf9vL92c+ovo9ZQUXl9FS1LfZco9JW2S2CGy9xNMoDj7LvfkgjVKqVpusJW1GzTwMXOo3/+e7JRS8xMIIUDdotOrqDop8dIFZWQ0EC8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614041097; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ODSXElBLvjoiNBfye/ckA5U7plhLxmlHqv/HTpnbeqc=; b=ZxzGKaohqb26R1qscHZEjLmtyAy98fdcIUrxHV/LBctpmks1oDW5NrgUGnZSE2k1xp32iGNJN1kd5OZpYFUXNu4+i9IZFFxzhIucCzsQyH9JBA/ypDwH1DSdIsxrctKv17TsKLF1yiw7WxZ5Bwzxj4Y6Fh+auxLhsI4nyAX7QTk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1614041097366417.98578414424753; Mon, 22 Feb 2021 16:44:57 -0800 (PST) Received: from localhost ([::1]:55504 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lELos-0004KC-95 for importer@patchew.org; Mon, 22 Feb 2021 19:44:54 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35688) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lELgi-0002Rz-Rp for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:28 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:34334) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lELem-0005Nq-DG for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:28 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-243-erwy12NOOPaJ5VUcx-vr9Q-1; Mon, 22 Feb 2021 19:34:24 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 648861E561; Tue, 23 Feb 2021 00:34:23 +0000 (UTC) Received: from scv.redhat.com (ovpn-112-247.rdu2.redhat.com [10.10.112.247]) by smtp.corp.redhat.com (Postfix) with ESMTP id AB3785D6D5; Tue, 23 Feb 2021 00:34:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614040467; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ODSXElBLvjoiNBfye/ckA5U7plhLxmlHqv/HTpnbeqc=; b=DD36fFd4+fNK20Eje4pIy0rr7vEO3T1cjGJTV6VILiTol7ezIiLI8Y6imWBIwgJcKRQdoj vfqBJIzAkniFl8fOOPcQaylL1ZkKp3bmLvcXNhkHRayOKMY5esZY0Ql6P0bzH8JR5ndEcH YyvjQbte7PvpclPKVdNJWPEB8PuQPLg= X-MC-Unique: erwy12NOOPaJ5VUcx-vr9Q-1 From: John Snow To: qemu-devel@nongnu.org, Markus Armbruster Subject: [PATCH v3 15/16] qapi/expr.py: move related checks inside check_xxx functions Date: Mon, 22 Feb 2021 19:34:07 -0500 Message-Id: <20210223003408.964543-16-jsnow@redhat.com> In-Reply-To: <20210223003408.964543-1-jsnow@redhat.com> References: <20210223003408.964543-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , John Snow , Eduardo Habkost , Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" There's not a big obvious difference between the types of checks that happen in the main function versus the kind that happen in the functions. Now they're in one place for each of the main types. As part of the move, spell out the required and optional keywords so they're obvious at a glance. Use tuples instead of lists for immutable data, too. Signed-off-by: John Snow Reviewed-by: Cleber Rosa --- scripts/qapi/expr.py | 55 ++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index 61699de8cd5..3672637487b 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -344,6 +344,10 @@ def check_enum(expr: Expression, info: QAPISourceInfo)= -> None: :param expr: `Expression` to validate. :param info: QAPI source file information. """ + check_keys(expr, info, 'enum', + required=3D('enum', 'data'), + optional=3D('if', 'features', 'prefix')) + name =3D expr['enum'] members =3D expr['data'] prefix =3D expr.get('prefix') @@ -374,6 +378,11 @@ def check_struct(expr: Expression, info: QAPISourceInf= o) -> None: :param expr: `Expression` to validate. :param info: QAPI source file information. """ + check_keys(expr, info, 'struct', + required=3D('struct', 'data'), + optional=3D('base', 'if', 'features')) + normalize_members(expr['data']) + name =3D cast(str, expr['struct']) # Asserted in check_exprs members =3D expr['data'] =20 @@ -388,6 +397,13 @@ def check_union(expr: Expression, info: QAPISourceInfo= ) -> None: :param expr: `Expression` to validate. :param info: QAPI source file information. """ + check_keys(expr, info, 'union', + required=3D('union', 'data'), + optional=3D('base', 'discriminator', 'if', 'features')) + + normalize_members(expr.get('base')) + normalize_members(expr['data']) + name =3D cast(str, expr['union']) # Asserted in check_exprs base =3D expr.get('base') discriminator =3D expr.get('discriminator') @@ -420,6 +436,11 @@ def check_alternate(expr: Expression, info: QAPISource= Info) -> None: :param expr: Expression to validate. :param info: QAPI source file information. """ + check_keys(expr, info, 'alternate', + required=3D('alternate', 'data'), + optional=3D('if', 'features')) + normalize_members(expr['data']) + members =3D expr['data'] =20 if not members: @@ -443,6 +464,13 @@ def check_command(expr: Expression, info: QAPISourceIn= fo) -> None: :param expr: `Expression` to validate. :param info: QAPI source file information. """ + check_keys(expr, info, 'command', + required=3D('command',), + optional=3D('data', 'returns', 'boxed', 'if', 'features', + 'gen', 'success-response', 'allow-oob', + 'allow-preconfig', 'coroutine')) + normalize_members(expr.get('data')) + args =3D expr.get('data') rets =3D expr.get('returns') boxed =3D expr.get('boxed', False) @@ -464,6 +492,11 @@ def check_event(expr: Expression, info: QAPISourceInfo= ) -> None: :if: ``Optional[Ifcond]`` (see: `check_if`) :features: ``Optional[Features]`` (see: `check_features`) """ + check_keys(expr, info, 'event', + required=3D('event',), + optional=3D('data', 'boxed', 'if', 'features')) + normalize_members(expr.get('data')) + args =3D expr.get('data') boxed =3D expr.get('boxed', False) =20 @@ -531,38 +564,16 @@ def check_exprs(exprs: List[_JSObject]) -> List[_JSOb= ject]: "documentation comment required") =20 if meta =3D=3D 'enum': - check_keys(expr, info, meta, - ['enum', 'data'], ['if', 'features', 'prefix']) check_enum(expr, info) elif meta =3D=3D 'union': - check_keys(expr, info, meta, - ['union', 'data'], - ['base', 'discriminator', 'if', 'features']) - normalize_members(expr.get('base')) - normalize_members(expr['data']) check_union(expr, info) elif meta =3D=3D 'alternate': - check_keys(expr, info, meta, - ['alternate', 'data'], ['if', 'features']) - normalize_members(expr['data']) check_alternate(expr, info) elif meta =3D=3D 'struct': - check_keys(expr, info, meta, - ['struct', 'data'], ['base', 'if', 'features']) - normalize_members(expr['data']) check_struct(expr, info) elif meta =3D=3D 'command': - check_keys(expr, info, meta, - ['command'], - ['data', 'returns', 'boxed', 'if', 'features', - 'gen', 'success-response', 'allow-oob', - 'allow-preconfig', 'coroutine']) - normalize_members(expr.get('data')) check_command(expr, info) elif meta =3D=3D 'event': - check_keys(expr, info, meta, - ['event'], ['data', 'boxed', 'if', 'features']) - normalize_members(expr.get('data')) check_event(expr, info) else: assert False, 'unexpected meta type' --=20 2.29.2 From nobody Fri Jun 14 19:49:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614041170; cv=none; d=zohomail.com; s=zohoarc; b=dnA5tr8yQfUhM8ueT7VBylj0qGByUvm4o1sVIkO27LyMJYnH21VG2tcoD9MAH0ribCxdPoedskWHrPMFlw3Va0utYJP7Xuk2g0dz3ZDAB4XMuxhArePOnn4aSZbERbgrfP9eiGl2oJ2zwU2uwxUZ2NS7BahOTBB8JUj0b9/3yPI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614041170; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=J4cuqkJAUQKR5Gdt+K1rgEmNceiXfV/hTv6w2GRuTgE=; b=YNHmC7g1m17ICAhvXvyrpjl2ZcMoJeU/4xtPp9UIvSjAjNJP9OgnZbl8rFkqoAwFyCy04bIhqlW0L4kzUgW2DBre838TlB3GjW9XJKbiIoXLnWUUm2/cSY/n4n1VvdGteQ+uGD1duUUoyXnBbpJL1VsNYvpdEQSPnbiP8uJs7VA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1614041170118461.770903200951; Mon, 22 Feb 2021 16:46:10 -0800 (PST) Received: from localhost ([::1]:59792 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lELq5-00062R-3A for importer@patchew.org; Mon, 22 Feb 2021 19:46:09 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35682) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lELgi-0002Qn-EV for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:28 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:33514) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lELel-0005Nk-QD for qemu-devel@nongnu.org; Mon, 22 Feb 2021 19:36:27 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-276-isTpgk2gMmC380BbHBmlfQ-1; Mon, 22 Feb 2021 19:34:25 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 48F91107ACC7; Tue, 23 Feb 2021 00:34:24 +0000 (UTC) Received: from scv.redhat.com (ovpn-112-247.rdu2.redhat.com [10.10.112.247]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8C7D35D6D5; Tue, 23 Feb 2021 00:34:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614040467; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=J4cuqkJAUQKR5Gdt+K1rgEmNceiXfV/hTv6w2GRuTgE=; b=DAnyEDWUnVeAabG16ZL9DkSHZKHbE3joxDyxIi2nF/uGqgiM3Ze2Y4ZAShaI27gLAXWEZ6 4JUb14ciuBV2m2iAQIkavmZ+K6JTdml8IQI304LlpUTi8g3Ca62n0bkcukBIYXCYjJou3s TnEmdODpnFVujdE4ciFbcspKDlaz5dQ= X-MC-Unique: isTpgk2gMmC380BbHBmlfQ-1 From: John Snow To: qemu-devel@nongnu.org, Markus Armbruster Subject: [PATCH v3 16/16] qapi/expr.py: Use an expression checker dispatch table Date: Mon, 22 Feb 2021 19:34:08 -0500 Message-Id: <20210223003408.964543-17-jsnow@redhat.com> In-Reply-To: <20210223003408.964543-1-jsnow@redhat.com> References: <20210223003408.964543-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=63.128.21.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , John Snow , Eduardo Habkost , Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This enforces a type signature against all of the top-level expression check routines without necessarily needing to create some overcomplicated class hierarchy for them. Signed-off-by: John Snow --- scripts/qapi/expr.py | 64 +++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index 3672637487b..f1c58483915 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -31,9 +31,12 @@ structures and contextual semantic validation. """ =20 +from enum import Enum import re from typing import ( + Callable, Collection, + Dict, Iterable, List, MutableMapping, @@ -505,6 +508,29 @@ def check_event(expr: Expression, info: QAPISourceInfo= ) -> None: check_type(args, info, "'data'", allow_dict=3Dnot boxed) =20 =20 +class ExpressionType(str, Enum): + INCLUDE =3D 'include' + ENUM =3D 'enum' + UNION =3D 'union' + ALTERNATE =3D 'alternate' + STRUCT =3D 'struct' + COMMAND =3D 'command' + EVENT =3D 'event' + + def __str__(self) -> str: + return str(self.value) + + +_CHECK_FN: Dict[str, Callable[[Expression, QAPISourceInfo], None]] =3D { + 'enum': check_enum, + 'union': check_union, + 'alternate': check_alternate, + 'struct': check_struct, + 'command': check_command, + 'event': check_event, +} + + def check_exprs(exprs: List[_JSObject]) -> List[_JSObject]: """ Validate and normalize a list of parsed QAPI schema expressions. [RW] @@ -531,24 +557,16 @@ def check_exprs(exprs: List[_JSObject]) -> List[_JSOb= ject]: assert tmp is None or isinstance(tmp, QAPIDoc) doc: Optional[QAPIDoc] =3D tmp =20 - if 'include' in expr: - continue - - if 'enum' in expr: - meta =3D 'enum' - elif 'union' in expr: - meta =3D 'union' - elif 'alternate' in expr: - meta =3D 'alternate' - elif 'struct' in expr: - meta =3D 'struct' - elif 'command' in expr: - meta =3D 'command' - elif 'event' in expr: - meta =3D 'event' + for kind in ExpressionType: + if kind in expr: + meta =3D kind + break else: raise QAPISemError(info, "expression is missing metatype") =20 + if meta =3D=3D ExpressionType.INCLUDE: + continue + name =3D cast(str, expr[meta]) # Asserted right below: check_name_is_str(name, info, "'%s'" % meta) info.set_defn(meta, name) @@ -563,21 +581,7 @@ def check_exprs(exprs: List[_JSObject]) -> List[_JSObj= ect]: raise QAPISemError(info, "documentation comment required") =20 - if meta =3D=3D 'enum': - check_enum(expr, info) - elif meta =3D=3D 'union': - check_union(expr, info) - elif meta =3D=3D 'alternate': - check_alternate(expr, info) - elif meta =3D=3D 'struct': - check_struct(expr, info) - elif meta =3D=3D 'command': - check_command(expr, info) - elif meta =3D=3D 'event': - check_event(expr, info) - else: - assert False, 'unexpected meta type' - + _CHECK_FN[meta](expr, info) check_if(expr, info, meta) check_features(expr.get('features'), info) check_flags(expr, info) --=20 2.29.2