Patchwork [1,of,2] formatter: verify number of arguments passed to write functions

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 26, 2015, 5:27 a.m.
Message ID <bca566c50dc08a27c25d.1443245276@mimosa>
Download mbox | patch
Permalink /patch/10652/
State Accepted
Headers show

Comments

Yuya Nishihara - Sept. 26, 2015, 5:27 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1443012708 -32400
#      Wed Sep 23 21:51:48 2015 +0900
# Node ID bca566c50dc08a27c25d28cc138bc8197b2b7c5b
# Parent  a672cc61ab1de50d931f215d89fafc22906ba356
formatter: verify number of arguments passed to write functions

zip() takes the shortest length, which can be a source of bug.

Patch

diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -48,11 +48,15 @@  class baseformatter(object):
         self._item.update(data)
     def write(self, fields, deftext, *fielddata, **opts):
         '''do default text output while assigning data to item'''
-        for k, v in zip(fields.split(), fielddata):
+        fieldkeys = fields.split()
+        assert len(fieldkeys) == len(fielddata)
+        for k, v in zip(fieldkeys, fielddata):
             self._item[k] = v
     def condwrite(self, cond, fields, deftext, *fielddata, **opts):
         '''do conditional write (primarily for plain formatter)'''
-        for k, v in zip(fields.split(), fielddata):
+        fieldkeys = fields.split()
+        assert len(fieldkeys) == len(fielddata)
+        for k, v in zip(fieldkeys, fielddata):
             self._item[k] = v
     def plain(self, text, **opts):
         '''show raw text for non-templated mode'''