[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v4 34/46] qapi/gen.py: update write() to be more idiomatic
From: |
John Snow |
Subject: |
[PATCH v4 34/46] qapi/gen.py: update write() to be more idiomatic |
Date: |
Wed, 30 Sep 2020 00:31:38 -0400 |
Make the file handling here just a tiny bit more idiomatic.
(I realize this is heavily subjective.)
Use exist_ok=True for os.makedirs and remove the exception,
use fdopen() to wrap the file descriptor in a File-like object,
and use a context manager for managing the file pointer.
Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
Reviewed-by: Cleber Rosa <crosa@redhat.com>
---
scripts/qapi/gen.py | 25 +++++++++++--------------
1 file changed, 11 insertions(+), 14 deletions(-)
diff --git a/scripts/qapi/gen.py b/scripts/qapi/gen.py
index 5694a00062c..2ad96e396e1 100644
--- a/scripts/qapi/gen.py
+++ b/scripts/qapi/gen.py
@@ -14,7 +14,6 @@
# See the COPYING file in the top-level directory.
from contextlib import contextmanager
-import errno
import os
import re
from typing import (
@@ -67,21 +66,19 @@ def write(self, output_dir: str) -> None:
return
pathname = os.path.join(output_dir, self.fname)
odir = os.path.dirname(pathname)
+
if odir:
- try:
- os.makedirs(odir)
- except os.error as e:
- if e.errno != errno.EEXIST:
- raise
+ os.makedirs(odir, exist_ok=True)
+
+ # use os.open for O_CREAT to create and read a non-existant file
fd = os.open(pathname, os.O_RDWR | os.O_CREAT, 0o666)
- f = open(fd, 'r+', encoding='utf-8')
- text = self.get_content()
- oldtext = f.read(len(text) + 1)
- if text != oldtext:
- f.seek(0)
- f.truncate(0)
- f.write(text)
- f.close()
+ with os.fdopen(fd, 'r+', encoding='utf-8') as fp:
+ text = self.get_content()
+ oldtext = fp.read(len(text) + 1)
+ if text != oldtext:
+ fp.seek(0)
+ fp.truncate(0)
+ fp.write(text)
def _wrap_ifcond(ifcond: List[str], before: str, after: str) -> str:
--
2.26.2
- [PATCH v4 31/46] qapi/gen.py: add type hint annotations, (continued)
- [PATCH v4 31/46] qapi/gen.py: add type hint annotations, John Snow, 2020/09/30
- [PATCH v4 21/46] qapi/common.py: move build_params into gen.py, John Snow, 2020/09/30
- [PATCH v4 23/46] qapi/events.py: add type hint annotations, John Snow, 2020/09/30
- [PATCH v4 22/46] qapi: establish mypy type-checking baseline, John Snow, 2020/09/30
- [PATCH v4 24/46] qapi/events.py: Move comments into docstrings, John Snow, 2020/09/30
- [PATCH v4 26/46] qapi/commands.py: add type hint annotations, John Snow, 2020/09/30
- [PATCH v4 27/46] qapi/commands.py: enable checking with mypy, John Snow, 2020/09/30
- [PATCH v4 29/46] qapi/source.py: delint with pylint, John Snow, 2020/09/30
- [PATCH v4 30/46] qapi/gen.py: Fix edge-case of _is_user_module, John Snow, 2020/09/30
- [PATCH v4 34/46] qapi/gen.py: update write() to be more idiomatic,
John Snow <=
- [PATCH v4 32/46] qapi/gen.py: Enable checking with mypy, John Snow, 2020/09/30
- [PATCH v4 33/46] qapi/gen.py: Remove unused parameter, John Snow, 2020/09/30
- [PATCH v4 35/46] qapi/gen.py: delint with pylint, John Snow, 2020/09/30
- [PATCH v4 36/46] qapi/introspect.py: assert obj is a dict when features are given, John Snow, 2020/09/30
- [PATCH v4 38/46] qapi/introspect.py: add _gen_features helper, John Snow, 2020/09/30
- [PATCH v4 40/46] qapi/introspect.py: replace 'extra' dict with 'comment' argument, John Snow, 2020/09/30
- [PATCH v4 42/46] qapi/types.py: add type hint annotations, John Snow, 2020/09/30