← Back to team overview

configglue team mailing list archive

[Merge] lp:~ricardokirkner/configglue/677108-use-SafeConfigParser into lp:configglue

 

Ricardo Kirkner has proposed merging lp:~ricardokirkner/configglue/677108-use-SafeConfigParser into lp:configglue.

Requested reviews:
  Configglue developers (configglue)
Related bugs:
  #677108 Use ConfigParser.SafeConfigParser instead of the broken ConfigParser.ConfigParser
  https://bugs.launchpad.net/bugs/677108

For more details, see:
https://code.launchpad.net/~ricardokirkner/configglue/677108-use-SafeConfigParser/+merge/50536

Use SafeConfigParser instead of ConfigParser as this will get deprecated in
python 3.2.

This also enhances robustness a bit as it is now not possible to store
non-string values in the parser internally.
-- 
https://code.launchpad.net/~ricardokirkner/configglue/677108-use-SafeConfigParser/+merge/50536
Your team Configglue developers is requested to review the proposed merge of lp:~ricardokirkner/configglue/677108-use-SafeConfigParser into lp:configglue.
=== modified file 'configglue/pyschema/glue.py'
--- configglue/pyschema/glue.py	2011-01-10 12:45:09 +0000
+++ configglue/pyschema/glue.py	2011-02-20 23:51:11 +0000
@@ -69,8 +69,9 @@
             value = getattr(options, opt_name(option))
             if parser.get(section.name, option.name) != value:
                 # the value has been overridden by an argument;
-                # update it.
-                parser.set(section.name, option.name, value)
+                # update it, but make sure it's a string, as
+                # SafeConfigParser will complain otherwise.
+                parser.set(section.name, option.name, unicode(value))
 
     return op, options, args
 

=== modified file 'configglue/pyschema/parser.py'
--- configglue/pyschema/parser.py	2010-12-18 22:32:43 +0000
+++ configglue/pyschema/parser.py	2011-02-20 23:51:11 +0000
@@ -23,7 +23,7 @@
 
 from ConfigParser import (
     DEFAULTSECT,
-    ConfigParser as BaseConfigParser,
+    SafeConfigParser as BaseConfigParser,
     InterpolationMissingOptionError,
     NoOptionError,
     NoSectionError,

=== modified file 'tests/pyschema/test_parser.py'
--- tests/pyschema/test_parser.py	2010-12-18 22:32:43 +0000
+++ tests/pyschema/test_parser.py	2011-02-20 23:51:11 +0000
@@ -20,10 +20,18 @@
 import shutil
 import tempfile
 import unittest
+from ConfigParser import (
+    InterpolationDepthError,
+    InterpolationMissingOptionError,
+    InterpolationSyntaxError,
+    NoSectionError,
+)
 from StringIO import StringIO
 
-from ConfigParser import (InterpolationMissingOptionError,
-    InterpolationDepthError, NoSectionError)
+from mock import (
+    Mock,
+    patch_object,
+)
 
 from configglue.pyschema.parser import (
     CONFIG_FILE_ENCODING,
@@ -211,7 +219,7 @@
         rawval = '%(bar)'
         vars = {'foo': '%(bar)s', 'bar': 'pepe'}
         parser = SchemaConfigParser(MySchema())
-        self.assertRaises(ValueError, parser._interpolate, section, option,
+        self.assertRaises(InterpolationSyntaxError, parser._interpolate, section, option,
                           rawval, vars)
 
     def test_interpolate_across_sections(self):
@@ -236,7 +244,7 @@
             baz = ConfigSection()
             baz.wham = IntConfigOption()
 
-        config = StringIO("[foo]\nbar=%(wham)\n[baz]\nwham=42")
+        config = StringIO("[foo]\nbar=%(wham)s\n[baz]\nwham=42")
         parser = SchemaConfigParser(MySchema())
         parser.readfp(config)
         self.assertRaises(InterpolationMissingOptionError, parser.get,
@@ -342,6 +350,22 @@
         value = parser._interpolate_value('__main__', 'foo')
         self.assertEqual(value, expected_value)
 
+    def test_interpolate_value_no_keys(self):
+        class MySchema(Schema):
+            foo = TupleConfigOption(2)
+        config = StringIO("[__main__]\nfoo=%(bar)s,%(bar)s")
+
+        mock_get_interpolation_keys = Mock(return_value=('%(bar)s', None))
+
+        parser = SchemaConfigParser(MySchema())
+        parser.readfp(config)
+        with patch_object(parser, '_get_interpolation_keys',
+                mock_get_interpolation_keys):
+
+            value = parser._interpolate_value('__main__', 'foo')
+            self.assertEqual(value, None)
+
+
     def test_get_with_raw_value(self):
         class MySchema(Schema):
             foo = StringConfigOption(raw=True)