Bläddra i källkod

Merge pull request #529 from msabramo/cherry-pick_pr_516_to_3.2-branch

Determine which file an error occurs in
Mike Naberezny 10 år sedan
förälder
incheckning
291ef8f31e
2 ändrade filer med 25 tillägg och 4 borttagningar
  1. 22 1
      supervisor/options.py
  2. 3 3
      supervisor/tests/test_options.py

+ 22 - 1
supervisor/options.py

@@ -811,7 +811,9 @@ class ServerOptions(Options):
             return self._processes_from_section(
                 parser, section, group_name, klass)
         except ValueError, e:
-            raise ValueError('%s in section %r' % (e, section))
+            filename = parser.section_to_file.get(section, '???')
+            raise ValueError('%s in section %r (file: %s)'
+                             % (e, section, filename))
 
     def _processes_from_section(self, parser, section, group_name,
                                 klass=None):
@@ -1616,6 +1618,11 @@ _marker = []
 
 class UnhosedConfigParser(ConfigParser.RawConfigParser):
     mysection = 'supervisord'
+
+    def __init__(self, *args, **kwargs):
+        ConfigParser.RawConfigParser.__init__(self, *args, **kwargs)
+        self.section_to_file = {}
+
     def read_string(self, s):
         from StringIO import StringIO
         s = StringIO(s)
@@ -1641,6 +1648,20 @@ class UnhosedConfigParser(ConfigParser.RawConfigParser):
         return self.saneget(self.mysection, option, default=default,
                             expansions=expansions, **kwargs)
 
+    def read(self, filenames):
+        sections_orig = self._sections.copy()
+        filenames = ConfigParser.RawConfigParser.read(self, filenames)
+
+        if len(filenames) == 1:
+            filename = filenames[0]
+        else:
+            filename = '???'
+
+        for section in frozenset(self._sections) - frozenset(sections_orig):
+            self.section_to_file[section] = filename
+
+        return filenames
+
 
 class Config(object):
     def __ne__(self, other):

+ 3 - 3
supervisor/tests/test_options.py

@@ -1208,10 +1208,10 @@ class ServerOptionsTests(unittest.TestCase):
         try:
             instance.processes_from_section(config, 'program:foo', None)
         except ValueError, e:
-            self.assertEqual(
-                str(e),
+            self.assertTrue(
                 "Unexpected end of key/value pairs in value "
-                "'KEY1=val1,KEY2=val2,KEY3' in section 'program:foo'")
+                "'KEY1=val1,KEY2=val2,KEY3' in section 'program:foo'"
+                in str(e))
         else:
             self.fail('instance.processes_from_section should '
                       'raise a ValueError')