Patchwork [4,of,4] ui: avoid mutable default arguments

login
register
mail settings
Submitter Siddharth Agarwal
Date Sept. 23, 2015, 12:15 a.m.
Message ID <75950e78a828c5994bd3.1442967323@dev6666.prn1.facebook.com>
Download mbox | patch
Permalink /patch/10586/
State Superseded
Commit 60558319ce724e8377c56591af3089380753f6de
Headers show

Comments

Siddharth Agarwal - Sept. 23, 2015, 12:15 a.m.
# HG changeset patch
# User Siddharth Agarwal <sid0@fb.com>
# Date 1442966194 25200
#      Tue Sep 22 16:56:34 2015 -0700
# Node ID 75950e78a828c5994bd36974bdfbc5f62a0009a3
# Parent  2e9d6b73a2a88c044175a05d32626b18afc0e889
ui: avoid mutable default arguments

I almost introduced a bug around this code by accidentally mutating a default
argument. There's no reason for these to exist.

It is OK to not assign {} to environ in ui.system because util.system knows how
to deal with that.
Pierre-Yves David - Sept. 23, 2015, 9:05 a.m.
On 09/22/2015 05:15 PM, Siddharth Agarwal wrote:
> # HG changeset patch
> # User Siddharth Agarwal <sid0@fb.com>
> # Date 1442966194 25200
> #      Tue Sep 22 16:56:34 2015 -0700
> # Node ID 75950e78a828c5994bd36974bdfbc5f62a0009a3
> # Parent  2e9d6b73a2a88c044175a05d32626b18afc0e889
> ui: avoid mutable default arguments

eeeeeeeerk, patches 3 and 4 pushed to the clowncopter.

Patch

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -834,7 +834,9 @@  class ui(object):
         if self.debugflag:
             opts['label'] = opts.get('label', '') + ' ui.debug'
             self.write(*msg, **opts)
-    def edit(self, text, user, extra={}, editform=None):
+    def edit(self, text, user, extra=None, editform=None):
+        if extra is None:
+            extra = {}
         (fd, name) = tempfile.mkstemp(prefix="hg-editor-", suffix=".txt",
                                       text=True)
         try:
@@ -866,7 +868,7 @@  class ui(object):
 
         return t
 
-    def system(self, cmd, environ={}, cwd=None, onerr=None, errprefix=None):
+    def system(self, cmd, environ=None, cwd=None, onerr=None, errprefix=None):
         '''execute shell command with appropriate output stream. command
         output will be redirected if fout is not stdout.
         '''