From 437c6ccebe96e022a112057df7db6a4bbbfeaba8 Mon Sep 17 00:00:00 2001 From: Ulf Magnusson Date: Wed, 29 May 2019 02:19:14 +0200 Subject: Use os.replace() if available in _save_old() Gives *nix rename() semantics on Windows, with overwriting of the target file. Also limit the scope of the catch-all try-except. --- kconfiglib.py | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/kconfiglib.py b/kconfiglib.py index 1527a00..98c39b9 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -1373,7 +1373,8 @@ class Kconfig(object): written. Errors are silently ignored if .old cannot be written (e.g. - due to being a directory). + due to being a directory, or being something like + /dev/null). verbose (default: True): If True and filename is None (automatically infer configuration @@ -6057,24 +6058,34 @@ def _touch_dep_file(path, sym_name): def _save_old(path): - # See write_config(). os.replace() would be nice here, but it's Python 3 - # (3.3+) only. + # See write_config() + + def copy(src, dst): + # Import as needed, to save some startup time + import shutil + shutil.copyfile(src, dst) + + if islink(path): + # Preserve symlinks + copy_fn = copy + elif hasattr(os, "replace"): + # Python 3 (3.3+) only. Best choice when available, because it + # removes .old on both *nix and Windows. + copy_fn = os.replace + elif os.name == "posix": + # Removes .old on POSIX systems + copy_fn = os.rename + else: + # Fall back on copying + copy_fn = copy try: - if os.name == "posix" and not islink(path): - # Will remove .old if it already exists on POSIX - # systems - os.rename(path, path + ".old") - else: - # Use copyfile() if 'path' is a symlink. The intention is probably - # to overwrite the target in that case. Only import shutil as - # needed, to save some startup time. - import shutil - shutil.copyfile(path, path + ".old") + copy_fn(path, path + ".old") except: - # Ignore errors from 'filename' missing as well as other errors. The + # Ignore errors from 'path' missing as well as other errors. # .old file is usually more of a nice-to-have, and not worth - # erroring out over e.g. if .old happens to be a directory. + # erroring out over e.g. if .old happens to be a directory or + # is something like /dev/null. pass -- cgit v1.2.3