diff options
| author | Kenneth Reitz <me@kennethreitz.com> | 2011-03-23 00:38:45 -0400 |
|---|---|---|
| committer | Kenneth Reitz <me@kennethreitz.com> | 2011-03-23 00:38:45 -0400 |
| commit | 65c73dfc42ec2012dcc5c91dff43de25936095b1 (patch) | |
| tree | feb4ab110e7e854a8e78c923334ffd8fe75cd05f /tablib/core.py | |
| parent | 3803a7a21b68633b0e54c6c01824647973ffa95d (diff) | |
| download | tablib-65c73dfc42ec2012dcc5c91dff43de25936095b1.tar.gz | |
do some internal validation for adding formatters
Diffstat (limited to 'tablib/core.py')
| -rw-r--r-- | tablib/core.py | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/tablib/core.py b/tablib/core.py index 6c17bc3..c4efce3 100644 --- a/tablib/core.py +++ b/tablib/core.py @@ -246,12 +246,15 @@ class Dataset(object): if self._formatters: for row_i, row in enumerate(_data): for col, callback in self._formatters: - if col is None: - for j, c in enumerate(row): - _data[row_i][j] = callback(c) - else: - _data[row_i][col] = callback(row[col]) - + try: + if col is None: + for j, c in enumerate(row): + _data[row_i][j] = callback(c) + else: + _data[row_i][col] = callback(row[col]) + except IndexError: + raise InvalidDatasetIndex + if self.headers: if dicts: @@ -501,8 +504,11 @@ class Dataset(object): col = self.headers.index(key) # get 'key' index from each data else: raise KeyError - - self._formatters.append((col, handler)) + + if not col > self.width: + self._formatters.append((col, handler)) + else: + raise InvalidDatasetIndex return True @@ -798,6 +804,9 @@ class InvalidDatasetType(Exception): class InvalidDimensions(Exception): "Invalid size" + +class InvalidDatasetIndex(Exception): + "Outside of Dataset size" class HeadersNeeded(Exception): "Header parameter must be given when appending a column in this Dataset." |
