From: Markus Armbruster <armbru@redhat.com>
To: John Snow <jsnow@redhat.com>
Cc: Michael Roth <michael.roth@amd.com>,
Cleber Rosa <crosa@redhat.com>,
qemu-devel@nongnu.org, Eduardo Habkost <ehabkost@redhat.com>
Subject: Re: [PATCH v2 01/21] qapi/parser: Don't try to handle file errors
Date: Tue, 18 May 2021 11:28:13 +0200 [thread overview]
Message-ID: <87wnrw5r2q.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <20210511220601.2110055-2-jsnow@redhat.com> (John Snow's message of "Tue, 11 May 2021 18:05:41 -0400")
John Snow <jsnow@redhat.com> writes:
> Remove the try/except block that handles file-opening errors in
> QAPISchemaParser.__init__() and add one each to
> QAPISchemaParser._include() and QAPISchema.__init__() respectively.
>
>
> The short-ish version of what motivates this patch is:
>
> - It's hard to write a good error message in the init method,
> because we need to determine the context of our caller to do so.
> It's easier to just let the caller write the message.
I kind of disagree, but that's okay; it's your commit message :)
> - We don't want to allow QAPISourceInfo(None, None, None) to exist.
> - Errors made using such an object are currently incorrect.
> - It's not technically a semantic error if we cannot open the schema
> - There are various typing constraints that make mixing these two cases
> undesirable for a single special case.
>
> Other considerations:
>
> - open() is moved to a 'with' block to ensure file pointers are
> cleaned up deterministically.
Improvement over v1's leak claim. Sold!
> - Python 3.3 deprecated IOError and made it a synonym for OSError.
> Avoid the misleading perception these exception handlers are
> narrower than they really are.
> - Not all QAPIError objects have an 'info' field, so remove that stanza
> from test-qapi. Don't bother to replace the early exit on purpose
> so that we can test its output in the next commit.
To which hunk exactly does the last item refer?
My best guess is the last one. Its rationale is actually "drop code
handling the variant of QAPISourceInfo being removed in this patch".
QAPIError not having .info don't actually exist before this patch.
I'm afraid I don't get the second sentence.
>
>
> The long version:
>
> The error message string here is incorrect (since 52a474180a):
I think this reads slightly better:
The error message here is incorrect (since commit 52a474180a):
>
>> python3 qapi-gen.py 'fake.json'
> qapi-gen.py: qapi-gen.py: can't read schema file 'fake.json': No such file or directory
>
> In pursuing it, we find that QAPISourceInfo has a special accommodation
> for when there's no filename. Meanwhile, the intended typing of
> QAPISourceInfo was (non-optional) 'str'.
Not sure about "intended". When I wrote the code, I intended ".fname is
str means it's a position that file; None means it's not in a file". I
didn't intend typing, because typing wasn't a concern back then.
Do you mean something like "we'd prefer to type .fname as (non-optional)
str"?
>
> To remove this, I'd want to avoid having a "fake" QAPISourceInfo
> object. We also don't want to explicitly begin accommodating
> QAPISourceInfo itself being None, because we actually want to eventually
> prove that this can never happen -- We don't want to confuse "The file
> isn't open yet" with "This error stems from a definition that wasn't
> defined in any file".
>
> (An earlier series tried to create a dummy info object, but it was tough
> to prove in review that it worked correctly without creating new
> regressions. This patch avoids that distraction. We would like to first
> prove that we never raise QAPISemError for any built-in object before we
> add "special" info objects. We aren't ready to do that yet.)
>
> So, which way out of the labyrinth?
>
> Here's one way: Don't try to handle errors at a level with "mixed"
> semantic contexts; i.e. don't mix inclusion errors (should report a
> source line where the include was triggered) and command line errors
> (where we specified a file we couldn't read).
>
> Remove the error handling from the initializer of the parser. Pythonic!
> Now it's the caller's job to figure out what to do about it. Handle the
> error in QAPISchemaParser._include() instead, where we can write a
> targeted error message where we are guaranteed to have an 'info' context
> to report with.
>
> The root level error can similarly move to QAPISchema.__init__(), where
> we know we'll never have an info context to report with, so we use a
> more abstract error type.
>
> Now the error looks sensible again:
>
>> python3 qapi-gen.py 'fake.json'
> qapi-gen.py: can't read schema file 'fake.json': No such file or directory
>
> With these error cases separated, QAPISourceInfo can be solidified as
> never having placeholder arguments that violate our desired types. Clean
> up test-qapi along similar lines.
>
> Fixes: 52a474180a
>
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
> scripts/qapi/parser.py | 18 +++++++++---------
> scripts/qapi/schema.py | 11 +++++++++--
> scripts/qapi/source.py | 3 ---
> tests/qapi-schema/test-qapi.py | 3 ---
> 4 files changed, 18 insertions(+), 17 deletions(-)
>
> diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
> index ca5e8e18e00..a53b735e7de 100644
> --- a/scripts/qapi/parser.py
> +++ b/scripts/qapi/parser.py
> @@ -40,15 +40,9 @@ def __init__(self, fname, previously_included=None, incl_info=None):
> previously_included = previously_included or set()
> previously_included.add(os.path.abspath(fname))
>
> - try:
> - fp = open(fname, 'r', encoding='utf-8')
> + # May raise OSError; allow the caller to handle it.
> + with open(fname, 'r', encoding='utf-8') as fp:
> self.src = fp.read()
> - except IOError as e:
> - raise QAPISemError(incl_info or QAPISourceInfo(None, None, None),
> - "can't read %s file '%s': %s"
> - % ("include" if incl_info else "schema",
> - fname,
> - e.strerror))
>
> if self.src == '' or self.src[-1] != '\n':
> self.src += '\n'
> @@ -129,7 +123,13 @@ def _include(self, include, info, incl_fname, previously_included):
> if incl_abs_fname in previously_included:
> return None
>
> - return QAPISchemaParser(incl_fname, previously_included, info)
> + try:
> + return QAPISchemaParser(incl_fname, previously_included, info)
> + except OSError as err:
> + raise QAPISemError(
> + info,
> + f"can't read include file '{incl_fname}': {err.strerror}"
> + ) from err
>
> def _check_pragma_list_of_str(self, name, value, info):
> if (not isinstance(value, list)
> diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
> index 3a4172fb749..d1d27ff7ee8 100644
> --- a/scripts/qapi/schema.py
> +++ b/scripts/qapi/schema.py
> @@ -20,7 +20,7 @@
> from typing import Optional
>
> from .common import POINTER_SUFFIX, c_name
> -from .error import QAPISemError, QAPISourceError
> +from .error import QAPIError, QAPISemError, QAPISourceError
> from .expr import check_exprs
> from .parser import QAPISchemaParser
>
> @@ -849,7 +849,14 @@ def visit(self, visitor):
> class QAPISchema:
> def __init__(self, fname):
> self.fname = fname
> - parser = QAPISchemaParser(fname)
> +
> + try:
> + parser = QAPISchemaParser(fname)
> + except OSError as err:
> + raise QAPIError(
> + f"can't read schema file '{fname}': {err.strerror}"
> + ) from err
> +
> exprs = check_exprs(parser.exprs)
> self.docs = parser.docs
> self._entity_list = []
> diff --git a/scripts/qapi/source.py b/scripts/qapi/source.py
> index 03b6ede0828..1ade864d7b9 100644
> --- a/scripts/qapi/source.py
> +++ b/scripts/qapi/source.py
> @@ -10,7 +10,6 @@
> # See the COPYING file in the top-level directory.
>
> import copy
> -import sys
> from typing import List, Optional, TypeVar
>
>
> @@ -53,8 +52,6 @@ def next_line(self: T) -> T:
> return info
>
> def loc(self) -> str:
> - if self.fname is None:
> - return sys.argv[0]
> ret = self.fname
> if self.line is not None:
> ret += ':%d' % self.line
> diff --git a/tests/qapi-schema/test-qapi.py b/tests/qapi-schema/test-qapi.py
> index e8db9d09d91..f1c4deb9a51 100755
> --- a/tests/qapi-schema/test-qapi.py
> +++ b/tests/qapi-schema/test-qapi.py
> @@ -128,9 +128,6 @@ def test_and_diff(test_name, dir_name, update):
> try:
> test_frontend(os.path.join(dir_name, test_name + '.json'))
> except QAPIError as err:
> - if err.info.fname is None:
> - print("%s" % err, file=sys.stderr)
> - return 2
> errstr = str(err) + '\n'
> if dir_name:
> errstr = errstr.replace(dir_name + '/', '')
Patch looks good to me.
next prev parent reply other threads:[~2021-05-18 9:54 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-11 22:05 [PATCH v2 00/21] qapi: static typing conversion, pt5a John Snow
2021-05-11 22:05 ` [PATCH v2 01/21] qapi/parser: Don't try to handle file errors John Snow
2021-05-18 9:28 ` Markus Armbruster [this message]
2021-05-18 13:14 ` John Snow
2021-05-19 7:01 ` Markus Armbruster
2021-05-19 17:17 ` John Snow
2021-05-19 17:51 ` John Snow
2021-05-18 19:01 ` John Snow
2021-05-19 6:51 ` Markus Armbruster
2021-05-11 22:05 ` [PATCH v2 02/21] qapi: Add test for nonexistent schema file John Snow
2021-05-11 22:05 ` [PATCH v2 03/21] qapi/source: Remove line number from QAPISourceInfo initializer John Snow
2021-05-11 22:05 ` [PATCH v2 04/21] qapi/parser: factor parsing routine into method John Snow
2021-05-18 9:57 ` Markus Armbruster
2021-05-18 15:11 ` John Snow
2021-05-11 22:05 ` [PATCH v2 05/21] qapi/parser: Assert lexer value is a string John Snow
2021-05-18 10:02 ` Markus Armbruster
2021-05-18 15:19 ` John Snow
2021-05-11 22:05 ` [PATCH v2 06/21] qapi/parser: enforce all top-level expressions must be dict in _parse() John Snow
2021-05-11 22:05 ` [PATCH v2 07/21] qapi/parser: assert object keys are strings John Snow
2021-05-11 22:05 ` [PATCH v2 08/21] qapi/parser: Use @staticmethod where appropriate John Snow
2021-05-11 22:05 ` [PATCH v2 09/21] qapi: add must_match helper John Snow
2021-05-11 22:05 ` [PATCH v2 10/21] qapi/parser: Fix token membership tests when token can be None John Snow
2021-05-11 22:05 ` [PATCH v2 11/21] qapi/parser: Rework _check_pragma_list_of_str as a TypeGuard John Snow
2021-05-11 22:05 ` [PATCH v2 12/21] qapi/parser: add type hint annotations John Snow
2021-05-18 12:01 ` Markus Armbruster
2021-05-18 13:25 ` John Snow
2021-05-11 22:05 ` [PATCH v2 13/21] qapi/parser: Remove superfluous list comprehension John Snow
2021-05-11 22:05 ` [PATCH v2 14/21] qapi/parser: allow 'ch' variable name John Snow
2021-05-11 22:05 ` [PATCH v2 15/21] qapi/parser: add docstrings John Snow
2021-05-19 6:41 ` Markus Armbruster
2021-05-19 18:21 ` John Snow
2021-05-11 22:05 ` [PATCH v2 16/21] CHECKPOINT John Snow
2021-05-11 22:05 ` [PATCH v2 17/21] qapi: [WIP] Rip QAPIDoc out of parser.py John Snow
2021-05-11 22:05 ` [PATCH v2 18/21] qapi: [WIP] Add type ignores for qapidoc.py John Snow
2021-05-11 22:05 ` [PATCH v2 19/21] qapi: [WIP] Import QAPIDoc from qapidoc Signed-off-by: John Snow <jsnow@redhat.com> John Snow
2021-05-11 22:06 ` [PATCH v2 20/21] qapi: [WIP] Add QAPIDocError John Snow
2021-05-11 22:06 ` [PATCH v2 21/21] qapi: [WIP] Enable linters on parser.py John Snow
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87wnrw5r2q.fsf@dusky.pond.sub.org \
--to=armbru@redhat.com \
--cc=crosa@redhat.com \
--cc=ehabkost@redhat.com \
--cc=jsnow@redhat.com \
--cc=michael.roth@amd.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.