From 3a8cf869db46d5c0ad792872e6a7fbaef7d4f6e6 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 19 Oct 2020 18:14:06 -0700 Subject: [PATCH] =?UTF-8?q?python:=20Convert=20os.open(=E2=80=A6,=20O=5FEX?= =?UTF-8?q?CL)=20to=20open(=E2=80=A6,=20"x").?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anders Kaseorg --- zerver/lib/cache.py | 9 +++------ zerver/management/commands/export.py | 3 ++- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/zerver/lib/cache.py b/zerver/lib/cache.py index 0b4050fa37..e51f637aed 100644 --- a/zerver/lib/cache.py +++ b/zerver/lib/cache.py @@ -85,13 +85,10 @@ def get_or_create_key_prefix() -> str: filename = os.path.join(settings.DEPLOY_ROOT, "var", "remote_cache_prefix") try: - fd = os.open(filename, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0o444) - prefix = secrets.token_hex(16) + ':' - # This does close the underlying file - with os.fdopen(fd, 'w') as f: + with open(filename, 'x') as f: + prefix = secrets.token_hex(16) + ':' f.write(prefix + "\n") - except OSError: - # The file already exists + except FileExistsError: tries = 1 while tries < 10: with open(filename) as f: diff --git a/zerver/management/commands/export.py b/zerver/management/commands/export.py index c83f5bbd4b..a3e022d293 100644 --- a/zerver/management/commands/export.py +++ b/zerver/management/commands/export.py @@ -173,7 +173,8 @@ class Command(ZulipBaseCommand): tarball_path = output_dir.rstrip("/") + ".tar.gz" try: - os.close(os.open(tarball_path, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o666)) + with open(tarball_path, "x"): + pass except FileExistsError: raise CommandError(f"Refusing to overwrite existing tarball: {tarball_path}. Aborting...")