make_file: always return a fresh file handle that can be closed
authorDan Villiom Podlaski Christiansen <danchr@gmail.com>
Tue, 07 Dec 2010 16:08:16 +0100
changeset 13121 bf763946f8b0
parent 13120 8568bbdfbafe
child 13126 e76701bf4480
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.
mercurial/cmdutil.py
tests/test-hook.t
--- a/mercurial/cmdutil.py	Fri Dec 10 19:20:11 2010 -0600
+++ b/mercurial/cmdutil.py	Tue Dec 07 16:08:16 2010 +0100
@@ -233,7 +233,8 @@
     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:
--- a/tests/test-hook.t	Fri Dec 10 19:20:11 2010 -0600
+++ b/tests/test-hook.t	Tue Dec 07 16:08:16 2010 +0100
@@ -73,8 +73,8 @@
   [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