Procházet zdrojové kódy

Determine which file an error occurs in

Marc Abramowitz před 10 roky
rodič
revize
aaecdfd71f
2 změnil soubory, kde provedl 24 přidání a 4 odebrání
  1. 21 1
      supervisor/options.py
  2. 3 3
      supervisor/tests/test_options.py

+ 21 - 1
supervisor/options.py

@@ -834,7 +834,9 @@ class ServerOptions(Options):
             return self._processes_from_section(
                 parser, section, group_name, klass)
         except ValueError as 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):
@@ -1660,6 +1662,10 @@ _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):
         s = StringIO(s)
         try:
@@ -1687,6 +1693,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

@@ -1399,10 +1399,10 @@ class ServerOptionsTests(unittest.TestCase):
         try:
             instance.processes_from_section(config, 'program:foo', None)
         except ValueError as 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')