From patchwork Tue Sep 22 21:12:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 273035 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 896B4C2D0E2 for ; Tue, 22 Sep 2020 21:44:56 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2742D22262 for ; Tue, 22 Sep 2020 21:44:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="WYNkLxOb" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2742D22262 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:48620 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKq5n-0008Ph-2a for qemu-devel@archiver.kernel.org; Tue, 22 Sep 2020 17:44:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39860) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKpbJ-0000mu-2J for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:25 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:53693) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKpbE-0005zO-6j for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600809199; 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=l3aFvixgKbhP6pi+lbQ0yIOSVJdkTdzW2xDLUdXZuV8=; b=WYNkLxObc2b41txYaVtHYVGqhVQk96lR9yu4YiiwaolJI/VOSPsT+ZYQ9x+loDZN+BcXa5 WJB4pEJNNBY+d9SZ5cieygr49D2/o9sDfA2SN4QSiEhw/wXQLD7LnJpvw76wpBG21NRGsT iJX7DP6qjGyMWvYxtnH5QoXw6lSRt+c= 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-178-mHUEAdzCMKaeLjYwBMKlvg-1; Tue, 22 Sep 2020 17:13:17 -0400 X-MC-Unique: mHUEAdzCMKaeLjYwBMKlvg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 354D910BBEC3; Tue, 22 Sep 2020 21:13:16 +0000 (UTC) Received: from scv.redhat.com (ovpn-119-140.rdu2.redhat.com [10.10.119.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id F28FC614F5; Tue, 22 Sep 2020 21:13:14 +0000 (UTC) From: John Snow To: Markus Armbruster Subject: [PATCH 01/16] qapi/expr.py: Remove 'info' argument from nested check_if_str Date: Tue, 22 Sep 2020 17:12:58 -0400 Message-Id: <20200922211313.4082880-2-jsnow@redhat.com> In-Reply-To: <20200922211313.4082880-1-jsnow@redhat.com> References: <20200922211313.4082880-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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 Received-SPF: pass client-ip=63.128.21.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 02:07:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: Cleber Rosa , John Snow , qemu-devel@nongnu.org, Eduardo Habkost , Michael Roth Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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 03b31ecfc1..de659a54ad 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -96,7 +96,7 @@ def check_flags(expr, info): def check_if(expr, info, source): - def check_if_str(ifcond, info): + def check_if_str(ifcond): if not isinstance(ifcond, str): raise QAPISemError( info, @@ -116,9 +116,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'] = [ifcond] From patchwork Tue Sep 22 21:12:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 304710 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1A4BDC4727F for ; Tue, 22 Sep 2020 21:46:14 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B7B8522262 for ; Tue, 22 Sep 2020 21:46:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="eKRutyIR" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B7B8522262 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:53596 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKq72-00028w-F4 for qemu-devel@archiver.kernel.org; Tue, 22 Sep 2020 17:46:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39864) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKpbJ-0000n2-3n for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:25 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:41686) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKpbG-000610-RV for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600809201; 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=+WrFPzsy+W9bhbMiGBrTtDEjGrxRPAX22SwGQfV1s7E=; b=eKRutyIRp1ZFZiqL+SRc6gSed+/y7/P8xpek6ylL3IrPsdN9Dyh6As7v9vbJDvBvUNzT8I 4COoUwgB1UWgYAfNS6V4LvnyyhgMh250xT/oZWk7YEScIiAspsFTGY9ROi8Iy4y9BAvOwl 15rWDF8CO//EiHZy9kexI/GY10k8CuA= 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-361-yaT8vAfSPYamZX1BAx-GPw-1; Tue, 22 Sep 2020 17:13:19 -0400 X-MC-Unique: yaT8vAfSPYamZX1BAx-GPw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 631EE64088; Tue, 22 Sep 2020 21:13:17 +0000 (UTC) Received: from scv.redhat.com (ovpn-119-140.rdu2.redhat.com [10.10.119.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7D88273682; Tue, 22 Sep 2020 21:13:16 +0000 (UTC) From: John Snow To: Markus Armbruster Subject: [PATCH 02/16] qapi/expr.py: Check for dict instead of OrderedDict Date: Tue, 22 Sep 2020 17:12:59 -0400 Message-Id: <20200922211313.4082880-3-jsnow@redhat.com> In-Reply-To: <20200922211313.4082880-1-jsnow@redhat.com> References: <20200922211313.4082880-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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 Received-SPF: pass client-ip=216.205.24.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 15:47:47 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: Cleber Rosa , John Snow , qemu-devel@nongnu.org, Eduardo Habkost , Michael Roth Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" OrderedDict is a subtype of dict, so we can check for a more general form. 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 de659a54ad..1872a8a3cc 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -15,7 +15,7 @@ # See the COPYING file in the top-level directory. import re -from collections import OrderedDict + from .common import c_name from .error import QAPISemError @@ -123,7 +123,7 @@ def check_if_str(ifcond): def normalize_members(members): - if isinstance(members, OrderedDict): + if isinstance(members, dict): for key, arg in members.items(): if isinstance(arg, dict): continue @@ -154,7 +154,7 @@ def check_type(value, info, source, if not allow_dict: raise QAPISemError(info, "%s should be a type name" % source) - if not isinstance(value, OrderedDict): + if not isinstance(value, dict): raise QAPISemError(info, "%s should be an object or type name" % source) From patchwork Tue Sep 22 21:13:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 304706 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DC508C4727D for ; Tue, 22 Sep 2020 21:51:16 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7657E2076B for ; Tue, 22 Sep 2020 21:51:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="gy+l62Ar" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7657E2076B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:42076 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKqBv-0000ex-HD for qemu-devel@archiver.kernel.org; Tue, 22 Sep 2020 17:51:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39888) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKpbK-0000qa-Di for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:26 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:44132) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKpbI-00061d-Fh for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:26 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600809203; 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=OviiS+c/KqyWZoTO/aj83Q64YGN4t3Pv8SUuOosZ94s=; b=gy+l62ArpM+Jw4lCgpm6VoVMUdrHmf40H3i75Mzv3w+dZ2lp3XICy8I2YXoEQxy+iMqFmR c3eFAOL55wwgL+/ixxfdrkp9BvPp/E9QllsUabP67Y/ow39Hvvqu+XTTpm+caEESAzuJuN doxeCf0oDBtyoYhr13GC60dls2nXFa4= 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-465-BLZwyfbjPA2zRgtjmq3AQw-1; Tue, 22 Sep 2020 17:13:19 -0400 X-MC-Unique: BLZwyfbjPA2zRgtjmq3AQw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CE04919611A0; Tue, 22 Sep 2020 21:13:18 +0000 (UTC) Received: from scv.redhat.com (ovpn-119-140.rdu2.redhat.com [10.10.119.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id A13FE73689; Tue, 22 Sep 2020 21:13:17 +0000 (UTC) From: John Snow To: Markus Armbruster Subject: [PATCH 03/16] qapi/expr.py: constrain incoming expression types Date: Tue, 22 Sep 2020 17:13:00 -0400 Message-Id: <20200922211313.4082880-4-jsnow@redhat.com> In-Reply-To: <20200922211313.4082880-1-jsnow@redhat.com> References: <20200922211313.4082880-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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 Received-SPF: pass client-ip=216.205.24.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 15:47:47 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: Cleber Rosa , John Snow , qemu-devel@nongnu.org, Eduardo Habkost , Michael Roth Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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 1872a8a3cc..f6b55a87c1 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -15,9 +15,17 @@ # See the COPYING file in the top-level directory. import re +from typing import MutableMapping, Optional 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 forms. +# Minimally, their top-level form must be a mapping of strings to values. +Expression = MutableMapping[str, object] # Names must be letters, numbers, -, and _. They must start with letter, @@ -280,9 +288,20 @@ def check_event(expr, info): def check_exprs(exprs): for expr_elem in exprs: - expr = expr_elem['expr'] - info = expr_elem['info'] - doc = expr_elem.get('doc') + # Expression + assert isinstance(expr_elem['expr'], dict) + expr: Expression = expr_elem['expr'] + for key in expr.keys(): + assert isinstance(key, str) + + # QAPISourceInfo + assert isinstance(expr_elem['info'], QAPISourceInfo) + info: QAPISourceInfo = expr_elem['info'] + + # Optional[QAPIDoc] + tmp = expr_elem.get('doc') + assert tmp is None or isinstance(tmp, QAPIDoc) + doc: Optional[QAPIDoc] = tmp if 'include' in expr: continue From patchwork Tue Sep 22 21:13:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 304708 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 75C71C2D0E2 for ; Tue, 22 Sep 2020 21:48:26 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id EE6CC206FC for ; Tue, 22 Sep 2020 21:48:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="GsgkRqSi" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EE6CC206FC Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:33678 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKq9B-0005YA-3Z for qemu-devel@archiver.kernel.org; Tue, 22 Sep 2020 17:48:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39878) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKpbJ-0000oQ-M9 for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:25 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:36190) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKpbH-00061N-NI for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600809202; 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=kiROWfsWIjfjZEiwsV7fIP+RW6PpLlCUtwamIXrMEmQ=; b=GsgkRqSigQg7OJz2ARHLMbTeY/btcvasSR5SpeN172lM2iphfEq9PWfKX/C/yoh+JPqQ+L yEEtlQZCSlNIrf6R9VZUiL4X2hNtwQkEnieDsNqEp3oUpawKiy38i4wY8pTyW1x3kxAYzj IUQG6a2dOdT5yFS9K0SEwU4FoInJx54= 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-4-8R49_K1jPNSBPcTHcU4WiQ-1; Tue, 22 Sep 2020 17:13:20 -0400 X-MC-Unique: 8R49_K1jPNSBPcTHcU4WiQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DBE2A64086; Tue, 22 Sep 2020 21:13:19 +0000 (UTC) Received: from scv.redhat.com (ovpn-119-140.rdu2.redhat.com [10.10.119.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0661173689; Tue, 22 Sep 2020 21:13:18 +0000 (UTC) From: John Snow To: Markus Armbruster Subject: [PATCH 04/16] qapi/expr.py: Add assertion for union type 'check_dict' Date: Tue, 22 Sep 2020 17:13:01 -0400 Message-Id: <20200922211313.4082880-5-jsnow@redhat.com> In-Reply-To: <20200922211313.4082880-1-jsnow@redhat.com> References: <20200922211313.4082880-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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 Received-SPF: pass client-ip=63.128.21.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 02:07:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: Cleber Rosa , John Snow , qemu-devel@nongnu.org, Eduardo Habkost , Michael Roth Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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 f6b55a87c1..67892502e9 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -166,7 +166,9 @@ def check_type(value, info, source, raise QAPISemError(info, "%s should be an object or type name" % source) - permit_upper = allow_dict in info.pragma.name_case_whitelist + permit_upper = False + if isinstance(allow_dict, str): + permit_upper = allow_dict in info.pragma.name_case_whitelist # value is a dictionary, check that each member is okay for (key, arg) in value.items(): From patchwork Tue Sep 22 21:13:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 304716 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B0F88C2D0E2 for ; Tue, 22 Sep 2020 21:37:02 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5520620757 for ; Tue, 22 Sep 2020 21:37:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="NUrkQbua" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5520620757 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:51838 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKpy9-0005sc-8C for qemu-devel@archiver.kernel.org; Tue, 22 Sep 2020 17:37:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39894) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKpbK-0000rB-LD for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:26 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:52301) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKpbI-00061g-Tm for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:26 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600809204; 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=FMPYL/Fj+O8VtFiDqOGxK2lFd9x8jB/8yhs9MnJy+mE=; b=NUrkQbuaJ4GeIJ04qSf5LpoFwYxCHdPtIRHWZDJFZpmJyez5oZ8MdfCwYdABPwNHuEdD2G WFHgSjeisGx0DPY2kwg0feKNEtucDNhSBiC1JZlMoFCTbnnYzGnZGniUNUsSscAScSQq+J 2hlWUx0eMAfVhG2EtdkhEUYJ8U3j3KE= 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-252-AVT_yaSgP3SnIg_Sf6SG2g-1; Tue, 22 Sep 2020 17:13:22 -0400 X-MC-Unique: AVT_yaSgP3SnIg_Sf6SG2g-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 46D8A64091; Tue, 22 Sep 2020 21:13:21 +0000 (UTC) Received: from scv.redhat.com (ovpn-119-140.rdu2.redhat.com [10.10.119.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1431873689; Tue, 22 Sep 2020 21:13:19 +0000 (UTC) From: John Snow To: Markus Armbruster Subject: [PATCH 05/16] qapi/expr.py: move string check upwards in check_type Date: Tue, 22 Sep 2020 17:13:02 -0400 Message-Id: <20200922211313.4082880-6-jsnow@redhat.com> In-Reply-To: <20200922211313.4082880-1-jsnow@redhat.com> References: <20200922211313.4082880-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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 Received-SPF: pass client-ip=63.128.21.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 02:07:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: Cleber Rosa , John Snow , qemu-devel@nongnu.org, Eduardo Habkost , Michael Roth Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" It's a simple case, shimmy the early return upwards. 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 67892502e9..3f5af5f5e4 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -143,6 +143,10 @@ def check_type(value, info, source, if value is None: return + # Type name + if isinstance(value, str): + return + # Array type if isinstance(value, list): if not allow_array: @@ -153,10 +157,6 @@ def check_type(value, info, source, source) return - # Type name - if isinstance(value, str): - return - # Anonymous type if not allow_dict: From patchwork Tue Sep 22 21:13:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 304714 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 15883C2D0E2 for ; Tue, 22 Sep 2020 21:39:37 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6867922262 for ; Tue, 22 Sep 2020 21:39:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="XC7ZMfpv" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6867922262 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:59912 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKq0d-0001Aj-Fg for qemu-devel@archiver.kernel.org; Tue, 22 Sep 2020 17:39:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39918) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKpbL-0000u7-O0 for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:27 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:39664) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKpbK-00061z-1A for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600809205; 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=s4wL7RgGXtDcB4iuiGvtvZNCWhMHLRgOAJBV493C44w=; b=XC7ZMfpvN6s5ou8xtH1QihlfFVTcbQ5AW4IgMakWCVDYIuucsPsNVq+0pP0QZ9IqqQ5KUu uxfqXofE73pfiLiZAjBeOacPBBujpzIPWwbrypaZYH9mjAkMIMgZY4Fc8UQLhv4g8aXvm5 PceGCGXeVm9J0/khIDXphQQmloAaJ/U= 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-320-Y1P6nmWSMWuEF_9670yniA-1; Tue, 22 Sep 2020 17:13:23 -0400 X-MC-Unique: Y1P6nmWSMWuEF_9670yniA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 75CC88BF320; Tue, 22 Sep 2020 21:13:22 +0000 (UTC) Received: from scv.redhat.com (ovpn-119-140.rdu2.redhat.com [10.10.119.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8BD17614F5; Tue, 22 Sep 2020 21:13:21 +0000 (UTC) From: John Snow To: Markus Armbruster Subject: [PATCH 06/16] qapi/expr.py: Check type of 'data' member Date: Tue, 22 Sep 2020 17:13:03 -0400 Message-Id: <20200922211313.4082880-7-jsnow@redhat.com> In-Reply-To: <20200922211313.4082880-1-jsnow@redhat.com> References: <20200922211313.4082880-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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 Received-SPF: pass client-ip=63.128.21.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 02:07:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: Cleber Rosa , John Snow , qemu-devel@nongnu.org, Eduardo Habkost , Michael Roth Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Iterating over the members of data isn't going to work if it's not some form of dict anyway, but for type safety, 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 3f5af5f5e4..633f9b9172 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -247,6 +247,9 @@ def check_union(expr, info): raise QAPISemError(info, "'discriminator' requires 'base'") check_name_is_str(discriminator, info, "'discriminator'") + if not isinstance(members, dict): + raise QAPISemError(info, "'data' must be an object") + for (key, value) in members.items(): source = "'data' member '%s'" % key check_name_str(key, info, source) @@ -260,6 +263,10 @@ def check_alternate(expr, info): 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 = "'data' member '%s'" % key check_name_str(key, info, source) From patchwork Tue Sep 22 21:13:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 304713 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0CF82C2D0E2 for ; Tue, 22 Sep 2020 21:42:02 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7430F22262 for ; Tue, 22 Sep 2020 21:42:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="PexYHCGK" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7430F22262 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:39830 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKq2y-0004iB-6Z for qemu-devel@archiver.kernel.org; Tue, 22 Sep 2020 17:42:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39938) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKpbM-0000wc-NI for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:28 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:33105) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKpbK-00062b-Tt for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600809206; 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=IaMLC8He4A5gFWw6a7qBuxIZ30CJpWYe7mjR0+qBNoE=; b=PexYHCGKF1Y3oiJPkneztscLoN2GBvOwV/+wx2/joqA8NfzZ4c/18T9Xs1CVfXMPUkwmGK s/ebqiwxfjVpZ7Y+hI1JmMhPoI6bHe1grRcJ3FifDq2PS+I1VoD+P7cwI/SupaLZ6B3q4y RZDh3RpcjBA7zSu2HCzZCKlm8sZ7fk8= 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-515-RcmKZ5txMiuEt9i31lVobQ-1; Tue, 22 Sep 2020 17:13:24 -0400 X-MC-Unique: RcmKZ5txMiuEt9i31lVobQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6D44810BBECA; Tue, 22 Sep 2020 21:13:23 +0000 (UTC) Received: from scv.redhat.com (ovpn-119-140.rdu2.redhat.com [10.10.119.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id A0518614F5; Tue, 22 Sep 2020 21:13:22 +0000 (UTC) From: John Snow To: Markus Armbruster Subject: [PATCH 07/16] qapi/expr.py: Add casts in a few select cases Date: Tue, 22 Sep 2020 17:13:04 -0400 Message-Id: <20200922211313.4082880-8-jsnow@redhat.com> In-Reply-To: <20200922211313.4082880-1-jsnow@redhat.com> References: <20200922211313.4082880-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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 Received-SPF: pass client-ip=216.205.24.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 15:47:47 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: Cleber Rosa , John Snow , qemu-devel@nongnu.org, Eduardo Habkost , Michael Roth Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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 | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index 633f9b9172..4a3352a2bd 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -15,7 +15,11 @@ # See the COPYING file in the top-level directory. import re -from typing import MutableMapping, Optional +from typing import ( + MutableMapping, + Optional, + cast, +) from .common import c_name from .error import QAPISemError @@ -225,7 +229,7 @@ def check_enum(expr, info): def check_struct(expr, info): - name = expr['struct'] + name = cast(str, expr['struct']) # Asserted in check_exprs members = expr['data'] check_type(members, info, "'data'", allow_dict=name) @@ -233,7 +237,7 @@ def check_struct(expr, info): def check_union(expr, info): - name = expr['union'] + name = cast(str, expr['union']) # Asserted in check_exprs base = expr.get('base') discriminator = expr.get('discriminator') members = expr['data'] @@ -330,7 +334,7 @@ def check_exprs(exprs): else: raise QAPISemError(info, "expression is missing metatype") - name = expr[meta] + name = 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) From patchwork Tue Sep 22 21:13:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 273030 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55C7BC2D0E2 for ; Tue, 22 Sep 2020 21:54:22 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 064D8207C3 for ; Tue, 22 Sep 2020 21:54:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="iH9JzcxO" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 064D8207C3 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:53734 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKqEv-0005PE-5Y for qemu-devel@archiver.kernel.org; Tue, 22 Sep 2020 17:54:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39966) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKpbP-00012t-5z for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:31 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:26117) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKpbN-00063I-2L for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600809208; 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=qvntVAw7UwIjWN4n9HMRaHDxuagSIVWP9zNN1NsSEL8=; b=iH9JzcxOiSp0p/vXEwZSPCQsryqSWj5/TGKQnk7Dgeb0bSqSg9ORfh79JJa4ls0osZ5D49 XkhDQ8CriwqbaO2m9RtBWQY81UPjHJbUbNGfxOFZjGSV42J/v+6YfsnS0L7W2einEI+fZE MAuTMNAr7dZ7Nif7HRgMxqYnkPJIYWg= 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-569-ZBVXaUMgO4q0rl6R1uaKrQ-1; Tue, 22 Sep 2020 17:13:26 -0400 X-MC-Unique: ZBVXaUMgO4q0rl6R1uaKrQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0384C805EE4; Tue, 22 Sep 2020 21:13:25 +0000 (UTC) Received: from scv.redhat.com (ovpn-119-140.rdu2.redhat.com [10.10.119.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id 98462614F5; Tue, 22 Sep 2020 21:13:23 +0000 (UTC) From: John Snow To: Markus Armbruster Subject: [PATCH 08/16] qapi/expr.py: add type hint annotations Date: Tue, 22 Sep 2020 17:13:05 -0400 Message-Id: <20200922211313.4082880-9-jsnow@redhat.com> In-Reply-To: <20200922211313.4082880-1-jsnow@redhat.com> References: <20200922211313.4082880-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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 Received-SPF: pass client-ip=216.205.24.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 15:47:47 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_FILL_THIS_FORM_SHORT=0.01 autolearn=ham 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: Cleber Rosa , John Snow , qemu-devel@nongnu.org, Eduardo Habkost , Michael Roth Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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 | 65 +++++++++++++++++++++++++++---------------- scripts/qapi/mypy.ini | 5 ---- 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index 4a3352a2bd..6b064a2138 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -16,8 +16,11 @@ import re from typing import ( + Iterable, + List, MutableMapping, Optional, + Union, cast, ) @@ -27,10 +30,11 @@ from .source import QAPISourceInfo -# Expressions in their raw form are JSON-like structures with arbitrary forms. -# Minimally, their top-level form must be a mapping of strings to values. -Expression = MutableMapping[str, object] +# Arbitrary form for a JSON-like object. +_JSObject = MutableMapping[str, object] +# Expressions in their raw form are (just) JSON-like objects. +Expression = _JSObject # Names must be letters, numbers, -, and _. They must start with letter, # except for downstream extensions which must start with __RFQDN_. @@ -39,14 +43,19 @@ '[a-zA-Z][a-zA-Z0-9_-]*$') -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) -def check_name_str(name, info, source, - allow_optional=False, enum_member=False, - permit_upper=False): +def check_name_str(name: str, + info: QAPISourceInfo, + source: str, + allow_optional: bool = False, + enum_member: bool = False, + permit_upper: bool = False) -> None: membername = name if allow_optional and name.startswith('*'): @@ -66,16 +75,20 @@ def check_name_str(name, info, source, assert not membername.startswith('*') -def check_defn_name_str(name, info, meta): +def check_defn_name_str(name: str, info: QAPISourceInfo, meta: str) -> None: check_name_str(name, info, meta, permit_upper=True) if name.endswith('Kind') or name.endswith('List'): raise QAPISemError( info, "%s name should not end in '%s'" % (meta, name[-4:])) -def check_keys(value, info, source, required, optional): +def check_keys(value: _JSObject, + info: QAPISourceInfo, + source: str, + required: List[str], + optional: List[str]) -> None: - def pprint(elems): + def pprint(elems: Iterable[str]) -> str: return ', '.join("'" + e + "'" for e in sorted(elems)) missing = set(required) - set(value) @@ -95,7 +108,7 @@ def pprint(elems): pprint(unknown), pprint(allowed))) -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( @@ -106,9 +119,9 @@ def check_flags(expr, info): info, "flag '%s' may only use true value" % key) -def check_if(expr, info, source): +def check_if(expr: _JSObject, info: QAPISourceInfo, source: str) -> None: - def check_if_str(ifcond): + def check_if_str(ifcond: object) -> None: if not isinstance(ifcond, str): raise QAPISemError( info, @@ -134,7 +147,7 @@ def check_if_str(ifcond): expr['if'] = [ifcond] -def normalize_members(members): +def normalize_members(members: object) -> None: if isinstance(members, dict): for key, arg in members.items(): if isinstance(arg, dict): @@ -142,8 +155,11 @@ def normalize_members(members): members[key] = {'type': arg} -def check_type(value, info, source, - allow_array=False, allow_dict=False): +def check_type(value: Optional[object], + info: QAPISourceInfo, + source: str, + allow_array: bool = False, + allow_dict: Union[bool, str] = False) -> None: if value is None: return @@ -187,7 +203,8 @@ def check_type(value, info, source, check_type(arg['type'], info, key_source, allow_array=True) -def check_features(features, info): +def check_features(features: Optional[object], + info: QAPISourceInfo) -> None: if features is None: return if not isinstance(features, list): @@ -204,7 +221,7 @@ def check_features(features, info): check_if(f, info, source) -def check_enum(expr, info): +def check_enum(expr: Expression, info: QAPISourceInfo) -> None: name = expr['enum'] members = expr['data'] prefix = expr.get('prefix') @@ -228,7 +245,7 @@ def check_enum(expr, info): check_if(member, info, source) -def check_struct(expr, info): +def check_struct(expr: Expression, info: QAPISourceInfo) -> None: name = cast(str, expr['struct']) # Asserted in check_exprs members = expr['data'] @@ -236,7 +253,7 @@ def check_struct(expr, info): check_type(expr.get('base'), info, "'base'") -def check_union(expr, info): +def check_union(expr: Expression, info: QAPISourceInfo) -> None: name = cast(str, expr['union']) # Asserted in check_exprs base = expr.get('base') discriminator = expr.get('discriminator') @@ -262,7 +279,7 @@ def check_union(expr, info): check_type(value['type'], info, source, allow_array=not base) -def check_alternate(expr, info): +def check_alternate(expr: Expression, info: QAPISourceInfo) -> None: members = expr['data'] if not members: @@ -279,7 +296,7 @@ def check_alternate(expr, info): check_type(value['type'], info, source) -def check_command(expr, info): +def check_command(expr: Expression, info: QAPISourceInfo) -> None: args = expr.get('data') rets = expr.get('returns') boxed = expr.get('boxed', False) @@ -290,7 +307,7 @@ def check_command(expr, info): check_type(rets, info, "'returns'", allow_array=True) -def check_event(expr, info): +def check_event(expr: Expression, info: QAPISourceInfo) -> None: args = expr.get('data') boxed = expr.get('boxed', False) @@ -299,7 +316,7 @@ def check_event(expr, info): check_type(args, info, "'data'", allow_dict=not boxed) -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 5ab3433c5f..0d0111930f 100644 --- a/scripts/qapi/mypy.ini +++ b/scripts/qapi/mypy.ini @@ -14,11 +14,6 @@ disallow_untyped_defs = False disallow_incomplete_defs = False check_untyped_defs = False -[mypy-qapi.expr] -disallow_untyped_defs = False -disallow_incomplete_defs = False -check_untyped_defs = False - [mypy-qapi.parser] disallow_untyped_defs = False disallow_incomplete_defs = False From patchwork Tue Sep 22 21:13:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 273033 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 34312C2D0E2 for ; Tue, 22 Sep 2020 21:47:07 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id CF55D22262 for ; Tue, 22 Sep 2020 21:47:06 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="enDA81Sq" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CF55D22262 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:57034 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKq7t-0003YY-Rx for qemu-devel@archiver.kernel.org; Tue, 22 Sep 2020 17:47:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39974) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKpbP-00014q-QN for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:31 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:59319) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKpbO-00063U-6k for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600809209; 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=3t3h9oDxjRXo+hECtZgRnukmRJ9G3Oo8Skvbxpet8Zc=; b=enDA81SqhnxAgiqjHXsLVQV+e/aOgVTLgW33A+Gi35HJ40ejtUOgwnIl8TUfcdw1Jbx5as AqOJMzOv0aNfsDYqsEeOiv67JeSjynew1bNKpVnSLyUs6x8KS3BLCMQV29aH0xRp+E4NWu PixSodHHHweLfTi1z2AcWKtPKzu5MY8= 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-195-dvWLFTBYO9iIO7oiSEvv-w-1; Tue, 22 Sep 2020 17:13:27 -0400 X-MC-Unique: dvWLFTBYO9iIO7oiSEvv-w-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7E74964088; Tue, 22 Sep 2020 21:13:26 +0000 (UTC) Received: from scv.redhat.com (ovpn-119-140.rdu2.redhat.com [10.10.119.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id 414EF614F5; Tue, 22 Sep 2020 21:13:25 +0000 (UTC) From: John Snow To: Markus Armbruster Subject: [PATCH 09/16] qapi/expr.py: rewrite check_if Date: Tue, 22 Sep 2020 17:13:06 -0400 Message-Id: <20200922211313.4082880-10-jsnow@redhat.com> In-Reply-To: <20200922211313.4082880-1-jsnow@redhat.com> References: <20200922211313.4082880-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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 Received-SPF: pass client-ip=216.205.24.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 15:47:47 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: Cleber Rosa , John Snow , qemu-devel@nongnu.org, Eduardo Habkost , Michael Roth Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" This is a only minor 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. Signed-off-by: John Snow Reviewed-by: Eduardo Habkost Reviewed-by: Cleber Rosa Tested-by: Cleber Rosa --- scripts/qapi/expr.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index 6b064a2138..5d5c3d050d 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -136,15 +136,15 @@ def check_if_str(ifcond: object) -> None: ifcond = expr.get('if') if ifcond is None: return - if isinstance(ifcond, list): - if ifcond == []: - 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'] = [ifcond] + + if not isinstance(ifcond, list): + ifcond = [ifcond] + expr['if'] = ifcond + if not ifcond: + raise QAPISemError( + info, "'if' condition [] of %s is useless" % source) + for elt in ifcond: + check_if_str(elt) def normalize_members(members: object) -> None: From patchwork Tue Sep 22 21:13:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 273032 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 22CD2C4727D for ; Tue, 22 Sep 2020 21:49:34 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id AF8CB206FC for ; Tue, 22 Sep 2020 21:49:33 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="DyW7CMXs" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AF8CB206FC Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:37148 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKqAG-0006yW-Pv for qemu-devel@archiver.kernel.org; Tue, 22 Sep 2020 17:49:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39994) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKpbR-00018B-5I for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:33 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:36388) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKpbP-00063c-EM for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600809210; 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=FttIVxQ9TFGN8XY/kcxNXdK8d88KH8ay5wgIRnTZmA4=; b=DyW7CMXsixBfjPKtb2nVkPH6qT1bjAI3lyAkAP6vvPqv6EWI4pU6eh0PcAx4nCeowvmKRv laOIefqh6KA5ojwcv8HXoLdRTZx/gwxG9X6VQPlmew+/F69eGlF2dmdHlFHrqpCRqXyS0a rqZu5//NDB+HmFXP+6zrClG1Dwo5Y7I= 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-522-WeaGhjP6Nr2gBshNV6Ye4A-1; Tue, 22 Sep 2020 17:13:28 -0400 X-MC-Unique: WeaGhjP6Nr2gBshNV6Ye4A-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D40A310BBEC8; Tue, 22 Sep 2020 21:13:27 +0000 (UTC) Received: from scv.redhat.com (ovpn-119-140.rdu2.redhat.com [10.10.119.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id C26BA614F5; Tue, 22 Sep 2020 21:13:26 +0000 (UTC) From: John Snow To: Markus Armbruster Subject: [PATCH 10/16] qapi/expr.py: Remove single-letter variable Date: Tue, 22 Sep 2020 17:13:07 -0400 Message-Id: <20200922211313.4082880-11-jsnow@redhat.com> In-Reply-To: <20200922211313.4082880-1-jsnow@redhat.com> References: <20200922211313.4082880-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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 Received-SPF: pass client-ip=63.128.21.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 02:07:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: Cleber Rosa , John Snow , qemu-devel@nongnu.org, Eduardo Habkost , Michael Roth Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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 5d5c3d050d..f244e9648c 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -211,14 +211,14 @@ def check_features(features: Optional[object], raise QAPISemError(info, "'features' must be an array") features[:] = [f if isinstance(f, dict) else {'name': f} for f in features] - for f in features: + for feature in features: source = "'features' member" - assert isinstance(f, dict) - check_keys(f, info, source, ['name'], ['if']) - check_name_is_str(f['name'], info, source) - source = "%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 = "%s '%s'" % (source, feature['name']) + check_name_str(feature['name'], info, source) + check_if(feature, info, source) def check_enum(expr: Expression, info: QAPISourceInfo) -> None: From patchwork Tue Sep 22 21:13:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 273029 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0700C2D0E2 for ; Tue, 22 Sep 2020 21:57:47 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8334E208A9 for ; Tue, 22 Sep 2020 21:57:47 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="bv/MLWFC" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8334E208A9 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:33320 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKqIE-0000Hg-BO for qemu-devel@archiver.kernel.org; Tue, 22 Sep 2020 17:57:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40036) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKpbT-0001FN-W6 for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:36 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:37155 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKpbQ-00063r-Tp for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:35 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600809211; 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=F8Tlcu0rBgiQmhsrhc7FiuFZcAK0dpv8XpiYSQEPdQI=; b=bv/MLWFCV2VFbd88AZJEyWj3NWq/SKljV/iNvM4e2dMbPsZWQbJkrBw+jAhR9pYGokDHbS Z/PPkHV0lCo+KQJKuKIH+qYoWiMRkSrVqM0Cf3Hmzn1nsgItiR90+0C1ZMS/ovof4k94Ol iX8BY42ng+BAlBjUD5hDtYE+HOnWXs8= 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-184-9vleCEChPMelxQ12778sRQ-1; Tue, 22 Sep 2020 17:13:29 -0400 X-MC-Unique: 9vleCEChPMelxQ12778sRQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CFBD0802B70; Tue, 22 Sep 2020 21:13:28 +0000 (UTC) Received: from scv.redhat.com (ovpn-119-140.rdu2.redhat.com [10.10.119.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0A5CB73682; Tue, 22 Sep 2020 21:13:27 +0000 (UTC) From: John Snow To: Markus Armbruster Subject: [PATCH 11/16] qapi/expr.py: enable pylint checks Date: Tue, 22 Sep 2020 17:13:08 -0400 Message-Id: <20200922211313.4082880-12-jsnow@redhat.com> In-Reply-To: <20200922211313.4082880-1-jsnow@redhat.com> References: <20200922211313.4082880-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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 Received-SPF: pass client-ip=205.139.110.61; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 17:00:46 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: Cleber Rosa , John Snow , qemu-devel@nongnu.org, Eduardo Habkost , Michael Roth Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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 581755351b..d7adb2ba33 100644 --- a/scripts/qapi/pylintrc +++ b/scripts/qapi/pylintrc @@ -4,7 +4,6 @@ # The regex matches against base names, not paths. ignore-patterns=doc.py, error.py, - expr.py, parser.py, schema.py, From patchwork Tue Sep 22 21:13:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 273048 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 08C33C2D0E2 for ; Tue, 22 Sep 2020 21:23:17 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8F1BE20936 for ; Tue, 22 Sep 2020 21:23:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="DsbMFhUX" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8F1BE20936 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:41438 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKpkp-0004vR-H6 for qemu-devel@archiver.kernel.org; Tue, 22 Sep 2020 17:23:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40038) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKpbU-0001Gg-KN for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:36 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:43756 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKpbS-000648-74 for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600809213; 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=7wkfyJxPT7WMs+xQ5yhM6ldyq5c/1HHIx7xgHgPKhcc=; b=DsbMFhUXDE5V0lTDLpZC2vPRPMuOuiKIX4EcR2LeO63wB3PtKwxp08kZZOWbbRFdrsmhVA I5gbemwpPEVBJimJCppfHCsR9zNKMmzG/Cc8u8arnGPxJbmWDvHNvCWy7Yfa/p6M5ZP4a6 MwM+JYIaxxgynxKtFoZeYFloa23sxFg= 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-354-vE8sbY9yMF67YyODEZxguA-1; Tue, 22 Sep 2020 17:13:31 -0400 X-MC-Unique: vE8sbY9yMF67YyODEZxguA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3AD3910BBEC3; Tue, 22 Sep 2020 21:13:30 +0000 (UTC) Received: from scv.redhat.com (ovpn-119-140.rdu2.redhat.com [10.10.119.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id 07B7973682; Tue, 22 Sep 2020 21:13:28 +0000 (UTC) From: John Snow To: Markus Armbruster Subject: [PATCH 12/16] qapi/expr.py: Add docstrings Date: Tue, 22 Sep 2020 17:13:09 -0400 Message-Id: <20200922211313.4082880-13-jsnow@redhat.com> In-Reply-To: <20200922211313.4082880-1-jsnow@redhat.com> References: <20200922211313.4082880-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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 Received-SPF: pass client-ip=205.139.110.120; envelope-from=jsnow@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 13:33:09 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_FILL_THIS_FORM_SHORT=0.01 autolearn=ham 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: Cleber Rosa , John Snow , qemu-devel@nongnu.org, Eduardo Habkost , Michael Roth Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: John Snow Reviewed-by: Cleber Rosa --- scripts/qapi/expr.py | 157 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 155 insertions(+), 2 deletions(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index f244e9648c..4bba09f6e5 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. +""" +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 also 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, @@ -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) @@ -56,6 +74,24 @@ def check_name_str(name: str, allow_optional: bool = False, enum_member: bool = False, permit_upper: bool = 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 = name if allow_optional and name.startswith('*'): @@ -76,6 +112,17 @@ def check_name_str(name: str, def check_defn_name_str(name: str, info: QAPISourceInfo, meta: str) -> None: + """ + Ensures a name is a legal definition name. [Const] + + A legal definition name: + - Adheres to the criteria in `check_name_str`, with uppercase permitted + - 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=True) if name.endswith('Kind') or name.endswith('List'): raise QAPISemError( @@ -87,6 +134,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. + """ def pprint(elems: Iterable[str]) -> str: return ', '.join("'" + e + "'" for e in sorted(elems)) @@ -109,6 +165,12 @@ def pprint(elems: Iterable[str]) -> str: 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( @@ -120,6 +182,18 @@ def check_flags(expr: Expression, info: QAPISourceInfo) -> None: def check_if(expr: _JSObject, info: QAPISourceInfo, source: str) -> None: + """ + Syntactically validate and normalize the ``if`` field of an object. [RW] + + The ``if`` field may be either a `str` or a `List[str]`. + A `str` element will be normalized to `List[str]`. + + Sugared: `Union[str, List[str]]` + Ifcond: `List[str]` + + :param expr: A `dict`; the ``if`` field, if present, will be validated. + :param info: QAPI source file information. + """ def check_if_str(ifcond: object) -> None: if not isinstance(ifcond, str): @@ -148,6 +222,16 @@ def check_if_str(ifcond: object) -> None: 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}``. + + Sugared: `Dict[str, Union[str, TypeRef]]` + Members: `Dict[str, TypeRef]` + """ if isinstance(members, dict): for key, arg in members.items(): if isinstance(arg, dict): @@ -160,6 +244,18 @@ def check_type(value: Optional[object], source: str, allow_array: bool = False, allow_dict: Union[bool, str] = 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. + """ if value is None: return @@ -205,6 +301,15 @@ def check_type(value: Optional[object], 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}`. + + Sugared: `List[Union[str, Feature]]` + Features: `List[Feature]` + """ if features is None: return if not isinstance(features, list): @@ -222,6 +327,12 @@ def check_features(features: Optional[object], 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 = expr['enum'] members = expr['data'] prefix = expr.get('prefix') @@ -246,6 +357,12 @@ def check_enum(expr: Expression, info: QAPISourceInfo) -> None: 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 = cast(str, expr['struct']) # Asserted in check_exprs members = expr['data'] @@ -254,6 +371,12 @@ def check_struct(expr: Expression, info: QAPISourceInfo) -> None: 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 = cast(str, expr['union']) # Asserted in check_exprs base = expr.get('base') discriminator = expr.get('discriminator') @@ -280,6 +403,12 @@ def check_union(expr: Expression, info: QAPISourceInfo) -> None: 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 = expr['data'] if not members: @@ -297,6 +426,12 @@ def check_alternate(expr: Expression, info: QAPISourceInfo) -> None: 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 = expr.get('data') rets = expr.get('returns') boxed = expr.get('boxed', False) @@ -308,6 +443,16 @@ def check_command(expr: Expression, info: QAPISourceInfo) -> None: 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]` + features: `Optional[Features]` + """ args = expr.get('data') boxed = expr.get('boxed', False) @@ -317,6 +462,14 @@ def check_event(expr: Expression, info: QAPISourceInfo) -> None: 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. + """ for expr_elem in exprs: # Expression assert isinstance(expr_elem['expr'], dict) From patchwork Tue Sep 22 21:13:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 304711 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 42EFBC4727D for ; Tue, 22 Sep 2020 21:44:53 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id BA4732371F for ; Tue, 22 Sep 2020 21:44:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="W87Hwlno" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BA4732371F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:48218 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKq5j-0008Fu-OU for qemu-devel@archiver.kernel.org; Tue, 22 Sep 2020 17:44:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40044) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKpbV-0001J8-T8 for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:37 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:56526) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKpbS-00064M-T0 for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600809214; 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=otO27sep0nJ6bA9EFn+q7sPXFbyUG38myhQs4Z2myn0=; b=W87Hwlnogy3uFwIJZAZR6JoawH3YrS4kpbzHY0SovrKTO279Jn9YbE2R64lUHCGFFihLyx HaUD6orRW/XZLvbAnAEqESUFkhTQOH3It0GlYiJhYf4DkMU5Wf5af2u77WeKKgpgHyyOrU nO1eIT/dgZy6oQHVZOumVxMnnkoLWtM= 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-164-iyCikDqyO3K0c0TFUimbZg-1; Tue, 22 Sep 2020 17:13:32 -0400 X-MC-Unique: iyCikDqyO3K0c0TFUimbZg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4E78C81CBE9; Tue, 22 Sep 2020 21:13:31 +0000 (UTC) Received: from scv.redhat.com (ovpn-119-140.rdu2.redhat.com [10.10.119.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id 64FC173682; Tue, 22 Sep 2020 21:13:30 +0000 (UTC) From: John Snow To: Markus Armbruster Subject: [PATCH 13/16] qapi/expr.py: Modify check_keys to accept any Iterable Date: Tue, 22 Sep 2020 17:13:10 -0400 Message-Id: <20200922211313.4082880-14-jsnow@redhat.com> In-Reply-To: <20200922211313.4082880-1-jsnow@redhat.com> References: <20200922211313.4082880-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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 Received-SPF: pass client-ip=63.128.21.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 02:07:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: Cleber Rosa , John Snow , qemu-devel@nongnu.org, Eduardo Habkost , Michael Roth Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" This is a very minor adjustment. a + b is list-specific behavior, but we can accept a wider variety of types in a more pythonic fashion if we avoid that behavior. Typing it this way allows callers to use things like dict.keys() and other iterables that are not their own discrete lists. Including it just as a statement of practice if nothing else: It's nice to use the least-specific type possible as function input and use the most-specific type for returns. 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 4bba09f6e5..2a1f37ca88 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -132,8 +132,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: Iterable[str] = (), + optional: Iterable[str] = ()) -> None: """ Ensures an object has a specific set of keys. [Const] @@ -154,7 +154,7 @@ def pprint(elems: Iterable[str]) -> str: "%s misses key%s %s" % (source, 's' if len(missing) > 1 else '', pprint(missing))) - allowed = set(required + optional) + allowed = set(required) | set(optional) unknown = set(value) - allowed if unknown: raise QAPISemError( From patchwork Tue Sep 22 21:13:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 273034 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60A7BC4727D for ; Tue, 22 Sep 2020 21:47:02 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 067442371F for ; Tue, 22 Sep 2020 21:47:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="RyUbmmrs" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 067442371F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:56580 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKq7p-0003NH-2B for qemu-devel@archiver.kernel.org; Tue, 22 Sep 2020 17:47:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40066) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKpbX-0001OA-Qt for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:39 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:21925) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKpbW-00064k-24 for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600809217; 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=GDER9rbTV2Nwoc53bdkKQyrGiaz2k0Kuh/paR7LTkj8=; b=RyUbmmrscUJrjJuTaS4uTOW+it3wj+FVxBTdAqzPHEteNFYyRiynU33xZfw6uDCKozjga5 IS4NgO9g2V+IvzoHHNAl6Zdl8b5UxGDi5rKXZWfO/cJyPqgeAkjBsM+yNcnE+NxwzPmBAZ SGYRmgGSL0/9rjGfoCSsRLiTlXI9WmY= 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-367-wq2L9Q8EPgOukuCDyOkBiQ-1; Tue, 22 Sep 2020 17:13:33 -0400 X-MC-Unique: wq2L9Q8EPgOukuCDyOkBiQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7C75881F005; Tue, 22 Sep 2020 21:13:32 +0000 (UTC) Received: from scv.redhat.com (ovpn-119-140.rdu2.redhat.com [10.10.119.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8FEA173682; Tue, 22 Sep 2020 21:13:31 +0000 (UTC) From: John Snow To: Markus Armbruster Subject: [PATCH 14/16] qapi/expr.py: Use tuples instead of lists for static data Date: Tue, 22 Sep 2020 17:13:11 -0400 Message-Id: <20200922211313.4082880-15-jsnow@redhat.com> In-Reply-To: <20200922211313.4082880-1-jsnow@redhat.com> References: <20200922211313.4082880-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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 Received-SPF: pass client-ip=216.205.24.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 15:47:47 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: Cleber Rosa , John Snow , qemu-devel@nongnu.org, Eduardo Habkost , Michael Roth Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" It is -- maybe -- possibly -- three nanoseconds faster. Signed-off-by: John Snow Reviewed-by: Eduardo Habkost Reviewed-by: Cleber Rosa --- 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 2a1f37ca88..69ee9e3f18 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -171,11 +171,11 @@ def check_flags(expr: Expression, info: QAPISourceInfo) -> 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']: + for key in ('boxed', 'allow-oob', 'allow-preconfig'): if key in expr and expr[key] is not True: raise QAPISemError( info, "flag '%s' may only use true value" % key) From patchwork Tue Sep 22 21:13:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 273046 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A0DFC4727E for ; Tue, 22 Sep 2020 21:26:20 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8A08E221E8 for ; Tue, 22 Sep 2020 21:26:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Hot9pERb" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8A08E221E8 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:49894 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKpnm-0000IP-Hy for qemu-devel@archiver.kernel.org; Tue, 22 Sep 2020 17:26:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40064) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKpbX-0001ME-4d for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:39 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:57641) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKpbV-00064d-4B for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:38 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600809216; 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=Je01v8tianvun5dGhcapKD/fWgdPcSCTuzFS6zyvrOM=; b=Hot9pERbAl/29Zt9984dahKmxrx3IOahAiXR3Zg7h7ocDsWaZUQIc9EAlyDZJ157B3gyIV aAFQZoNZY92K3e5mls1zkegSb6YySGN1kqbLmpq5lc/0/zZ1MMk37DlrJrldeFxcnuZSsk r0m+6nRpl8+8Hsrp8LXQUnmQtl539dc= 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-tnwVAhegNAic_vT0-kWgmg-1; Tue, 22 Sep 2020 17:13:34 -0400 X-MC-Unique: tnwVAhegNAic_vT0-kWgmg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8820310BBEC3; Tue, 22 Sep 2020 21:13:33 +0000 (UTC) Received: from scv.redhat.com (ovpn-119-140.rdu2.redhat.com [10.10.119.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id A89DD73670; Tue, 22 Sep 2020 21:13:32 +0000 (UTC) From: John Snow To: Markus Armbruster Subject: [PATCH 15/16] qapi/expr.py: move related checks inside check_xxx functions Date: Tue, 22 Sep 2020 17:13:12 -0400 Message-Id: <20200922211313.4082880-16-jsnow@redhat.com> In-Reply-To: <20200922211313.4082880-1-jsnow@redhat.com> References: <20200922211313.4082880-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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 Received-SPF: pass client-ip=63.128.21.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 02:07:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_FILL_THIS_FORM_SHORT=0.01 autolearn=ham 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: Cleber Rosa , John Snow , qemu-devel@nongnu.org, Eduardo Habkost , Michael Roth Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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. 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 69ee9e3f18..74b2681ef8 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -333,6 +333,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=('enum', 'data'), + optional=('if', 'features', 'prefix')) + name = expr['enum'] members = expr['data'] prefix = expr.get('prefix') @@ -363,6 +367,11 @@ def check_struct(expr: Expression, info: QAPISourceInfo) -> None: :param expr: `Expression` to validate. :param info: QAPI source file information. """ + check_keys(expr, info, 'struct', + required=('struct', 'data'), + optional=('base', 'if', 'features')) + normalize_members(expr['data']) + name = cast(str, expr['struct']) # Asserted in check_exprs members = expr['data'] @@ -377,6 +386,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=('union', 'data'), + optional=('base', 'discriminator', 'if', 'features')) + + normalize_members(expr.get('base')) + normalize_members(expr['data']) + name = cast(str, expr['union']) # Asserted in check_exprs base = expr.get('base') discriminator = expr.get('discriminator') @@ -409,6 +425,11 @@ def check_alternate(expr: Expression, info: QAPISourceInfo) -> None: :param expr: Expression to validate. :param info: QAPI source file information. """ + check_keys(expr, info, 'alternate', + required=('alternate', 'data'), + optional=('if', 'features')) + normalize_members(expr['data']) + members = expr['data'] if not members: @@ -432,6 +453,13 @@ def check_command(expr: Expression, info: QAPISourceInfo) -> None: :param expr: `Expression` to validate. :param info: QAPI source file information. """ + check_keys(expr, info, 'command', + required=['command'], + optional=('data', 'returns', 'boxed', 'if', 'features', + 'gen', 'success-response', 'allow-oob', + 'allow-preconfig')) + normalize_members(expr.get('data')) + args = expr.get('data') rets = expr.get('returns') boxed = expr.get('boxed', False) @@ -453,6 +481,11 @@ def check_event(expr: Expression, info: QAPISourceInfo) -> None: if: `Optional[Ifcond]` features: `Optional[Features]` """ + normalize_members(expr.get('data')) + check_keys(expr, info, 'event', + required=['event'], + optional=('data', 'boxed', 'if', 'features')) + args = expr.get('data') boxed = expr.get('boxed', False) @@ -519,38 +552,16 @@ def check_exprs(exprs: List[_JSObject]) -> List[_JSObject]: "documentation comment required") if meta == 'enum': - check_keys(expr, info, meta, - ['enum', 'data'], ['if', 'features', 'prefix']) check_enum(expr, info) elif meta == '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 == 'alternate': - check_keys(expr, info, meta, - ['alternate', 'data'], ['if', 'features']) - normalize_members(expr['data']) check_alternate(expr, info) elif meta == 'struct': - check_keys(expr, info, meta, - ['struct', 'data'], ['base', 'if', 'features']) - normalize_members(expr['data']) check_struct(expr, info) elif meta == 'command': - check_keys(expr, info, meta, - ['command'], - ['data', 'returns', 'boxed', 'if', 'features', - 'gen', 'success-response', 'allow-oob', - 'allow-preconfig']) - normalize_members(expr.get('data')) check_command(expr, info) elif meta == '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' From patchwork Tue Sep 22 21:13:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 273031 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1D1C0C2D0E2 for ; Tue, 22 Sep 2020 21:52:18 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 706EC20773 for ; Tue, 22 Sep 2020 21:52:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="AWxo4zb+" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 706EC20773 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:45562 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKqCu-00025T-FG for qemu-devel@archiver.kernel.org; Tue, 22 Sep 2020 17:52:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40088) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKpba-0001UM-6v for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:42 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:60781) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKpbY-00064z-7l for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:13:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600809219; 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=JgejwXtms9d7shSYi6P2L9OOZ+k6Lw4Sp1jD2446/rY=; b=AWxo4zb+sXw9yrSbWdWkF64Gj3O+OTIZt4i0eaRNnaZLEkD1gtGEXq64C/7XAAriWvphNY fzZc9hfsYJNKzw4Z1GqviN68G7U33ECphdRS/Ur9czFvtflHy8DmrAiGrPpkNsR0GXnvJC 14KbtxAclf+X5SmaOJlES5qBks0GVzQ= 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-274-4Sr2AnaXMUmHdfcYWhWeKg-1; Tue, 22 Sep 2020 17:13:35 -0400 X-MC-Unique: 4Sr2AnaXMUmHdfcYWhWeKg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A13FF19611A1; Tue, 22 Sep 2020 21:13:34 +0000 (UTC) Received: from scv.redhat.com (ovpn-119-140.rdu2.redhat.com [10.10.119.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id B458673682; Tue, 22 Sep 2020 21:13:33 +0000 (UTC) From: John Snow To: Markus Armbruster Subject: [PATCH 16/16] qapi/expr.py: Use an expression checker dispatch table Date: Tue, 22 Sep 2020 17:13:13 -0400 Message-Id: <20200922211313.4082880-17-jsnow@redhat.com> In-Reply-To: <20200922211313.4082880-1-jsnow@redhat.com> References: <20200922211313.4082880-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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 Received-SPF: pass client-ip=63.128.21.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 02:07:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: Cleber Rosa , John Snow , qemu-devel@nongnu.org, Eduardo Habkost , Michael Roth Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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 Reviewed-by: Eduardo Habkost Reviewed-by: Cleber Rosa --- scripts/qapi/expr.py | 69 ++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index 74b2681ef8..cfd342aa04 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -31,8 +31,11 @@ structures and contextual semantic validation. """ +from enum import Enum import re from typing import ( + Callable, + Dict, Iterable, List, MutableMapping, @@ -494,6 +497,26 @@ def check_event(expr: Expression, info: QAPISourceInfo) -> None: check_type(args, info, "'data'", allow_dict=not boxed) +class ExpressionType(str, Enum): + INCLUDE = 'include' + ENUM = 'enum' + UNION = 'union' + ALTERNATE = 'alternate' + STRUCT = 'struct' + COMMAND = 'command' + EVENT = 'event' + + +_CHECK_FN: Dict[str, Callable[[Expression, QAPISourceInfo], None]] = { + '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] @@ -519,28 +542,20 @@ def check_exprs(exprs: List[_JSObject]) -> List[_JSObject]: assert tmp is None or isinstance(tmp, QAPIDoc) doc: Optional[QAPIDoc] = tmp - if 'include' in expr: - continue - - if 'enum' in expr: - meta = 'enum' - elif 'union' in expr: - meta = 'union' - elif 'alternate' in expr: - meta = 'alternate' - elif 'struct' in expr: - meta = 'struct' - elif 'command' in expr: - meta = 'command' - elif 'event' in expr: - meta = 'event' + for kind in ExpressionType: + if kind in expr: + meta = kind + break else: raise QAPISemError(info, "expression is missing metatype") + if meta == ExpressionType.INCLUDE: + continue + name = 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) + check_name_is_str(name, info, "'%s'" % meta.value) + info.set_defn(meta.value, name) + check_defn_name_str(name, info, meta.value) if doc: if doc.symbol != name: @@ -551,22 +566,8 @@ def check_exprs(exprs: List[_JSObject]) -> List[_JSObject]: raise QAPISemError(info, "documentation comment required") - if meta == 'enum': - check_enum(expr, info) - elif meta == 'union': - check_union(expr, info) - elif meta == 'alternate': - check_alternate(expr, info) - elif meta == 'struct': - check_struct(expr, info) - elif meta == 'command': - check_command(expr, info) - elif meta == 'event': - check_event(expr, info) - else: - assert False, 'unexpected meta type' - - check_if(expr, info, meta) + _CHECK_FN[meta](expr, info) + check_if(expr, info, meta.value) check_features(expr.get('features'), info) check_flags(expr, info)