# HG changeset patch # User Dan Villiom Podlaski Christiansen # Date 2010-12-07 15:08:16 # Node ID bf763946f8b09fd23aa7835f44c8699998a8c389 # Parent 8568bbdfbafe2c0bbca984ff284a9a3b95e9c08d make_file: always return a fresh file handle that can be closed Currently, cmdutil.make_file() will return a freshly made file handle, except when given a pattern of '-'. If callers would want to close the handle, they would have to make sure that it's neither sys.stdin or sys.stdout. Instead, returning a duplicate of either of the two ensures that make_file() lives up to its name and creates a new file handle regardless of the input. diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py --- a/mercurial/cmdutil.py +++ b/mercurial/cmdutil.py @@ -233,7 +233,8 @@ def make_file(repo, pat, node=None, writable = 'w' in mode or 'a' in mode if not pat or pat == '-': - return writable and sys.stdout or sys.stdin + fp = writable and sys.stdout or sys.stdin + return os.fdopen(os.dup(fp.fileno()), mode) if hasattr(pat, 'write') and writable: return pat if hasattr(pat, 'read') and 'r' in mode: diff --git a/tests/test-hook.t b/tests/test-hook.t --- a/tests/test-hook.t +++ b/tests/test-hook.t @@ -73,8 +73,8 @@ test generic hooks [1] $ hg cat b pre-cat hook: HG_ARGS=cat b HG_OPTS={'rev': '', 'decode': None, 'exclude': [], 'output': '', 'include': []} HG_PATS=['b'] + b post-cat hook: HG_ARGS=cat b HG_OPTS={'rev': '', 'decode': None, 'exclude': [], 'output': '', 'include': []} HG_PATS=['b'] HG_RESULT=0 - b $ cd ../b $ hg pull ../a