From 32b03c0c5a7443b2c7ecff00b18470aed2f75aa0 Mon Sep 17 00:00:00 2001
From: Gabriel Studer <gabriel.studer@stud.unibas.ch>
Date: Fri, 10 Aug 2012 13:46:40 +0200
Subject: [PATCH] force user to provide data with same length as number of rows
 in the table when adding a new column

---
 modules/base/pymod/table.py      | 5 +++++
 modules/base/tests/test_table.py | 5 +++++
 2 files changed, 10 insertions(+)

diff --git a/modules/base/pymod/table.py b/modules/base/pymod/table.py
index e3e9a56cb..77951c634 100644
--- a/modules/base/pymod/table.py
+++ b/modules/base/pymod/table.py
@@ -654,6 +654,11 @@ class Table(object):
         for row in self.rows:
           row.append(data)
       else:
+        if len(data)!=len(self.rows):
+          self.col_names.pop()
+          self.col_types.pop()
+          raise ValueError('Length of data (%i) must correspond to number of '%len(data) +\
+                           'existing rows (%i)'%len(self.rows))
         for row, d in zip(self.rows, data):
           row.append(d)
 
diff --git a/modules/base/tests/test_table.py b/modules/base/tests/test_table.py
index 9afefb578..13afa0437 100644
--- a/modules/base/tests/test_table.py
+++ b/modules/base/tests/test_table.py
@@ -592,6 +592,11 @@ class TestTable(unittest.TestCase):
                                    'foo': [True, None, True],
                                    'bar': [1, 2, 3]})
 
+  def testRaiseErrorOnWrongDataLengthAddCol(self):
+    tab = Table()
+    tab.AddCol('a','f',[4.2,4.2,4.2])
+    self.assertRaises(ValueError, tab.AddCol, 'b', 'f', [4.2,4.2])
+
 
   def testRaiseErrorOnWrongColumnTypes(self):
     # wrong columns types in init
-- 
GitLab