From 045d26e83cafc87e1da5d7960f1cadc88696b20e Mon Sep 17 00:00:00 2001 From: fuzzyman Date: Sat, 27 Feb 2010 19:05:24 +0000 Subject: Values refering missing interpolation options can now be repr'd. Issue 9. --- configobj.py | 14 ++++++++++++-- functionaltests/test_configobj.py | 7 +++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/configobj.py b/configobj.py index 3827cee..7e3adee 100644 --- a/configobj.py +++ b/configobj.py @@ -753,7 +753,12 @@ class Section(dict): def __repr__(self): """x.__repr__() <==> repr(x)""" - return '{%s}' % ', '.join([('%s: %s' % (repr(key), repr(self[key]))) + def _getval(key): + try: + return self[key] + except MissingInterpolationOption: + return dict.__getitem__(self, key) + return '{%s}' % ', '.join([('%s: %s' % (repr(key), repr(_getval(key)))) for key in (self.scalars + self.sections)]) __str__ = __repr__ @@ -1367,8 +1372,13 @@ class ConfigObj(Section): def __repr__(self): + def _getval(key): + try: + return self[key] + except MissingInterpolationOption: + return dict.__getitem__(self, key) return ('ConfigObj({%s})' % - ', '.join([('%s: %s' % (repr(key), repr(self[key]))) + ', '.join([('%s: %s' % (repr(key), repr(_getval(key)))) for key in (self.scalars + self.sections)])) diff --git a/functionaltests/test_configobj.py b/functionaltests/test_configobj.py index 8528028..31e63d9 100644 --- a/functionaltests/test_configobj.py +++ b/functionaltests/test_configobj.py @@ -76,13 +76,16 @@ item1 = 1234 item2 = '$item1'""".splitlines() c = ConfigObj(cfg, interpolation='Template') - # This raises an exception in 4.7.1 and earlier + # This raises an exception in 4.7.1 and earlier due to the section + # being found as the interpolation value repr(c) def test_interoplation_repr(self): c = ConfigObj(['foo = $bar'], interpolation='Template') + c['baz'] = {} + c['baz']['spam'] = '%(bar)s' - # This raises an exception in 4.7.1 and earlier + # This raises a MissingInterpolationOption exception in 4.7.1 and earlier repr(c) -- cgit v1.2.1