changeset 1473:7d66ce9895fa

make readconfig take a filename instead of a file pointer as argument catch parse error while reading a config file add a testcase for parse error
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
date Thu, 27 Oct 2005 13:40:56 -0700
parents 3c909a747d7f
children 1e47f6fc0fdc
files mercurial/hgweb.py mercurial/localrepo.py mercurial/ui.py tests/test-hgrc
diffstat 4 files changed, 15 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/hgweb.py
+++ b/mercurial/hgweb.py
@@ -990,7 +990,7 @@ class hgwebdir:
             for name, path in self.repos:
                 u = ui.ui()
                 try:
-                    u.readconfig(file(os.path.join(path, '.hg', 'hgrc')))
+                    u.readconfig(os.path.join(path, '.hg', 'hgrc'))
                 except IOError:
                     pass
                 get = u.config
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -43,7 +43,7 @@ class localrepository:
 
         self.dirstate = dirstate.dirstate(self.opener, ui, self.root)
         try:
-            self.ui.readconfig(self.opener("hgrc"))
+            self.ui.readconfig(os.path.join(self.path, "hgrc"))
         except IOError: pass
 
     def hook(self, name, **args):
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -15,7 +15,7 @@ class ui:
                  interactive=True):
         self.overlay = {}
         self.cdata = ConfigParser.SafeConfigParser()
-        self.cdata.read(util.rcpath)
+        self.readconfig(util.rcpath)
 
         self.quiet = self.configbool("ui", "quiet")
         self.verbose = self.configbool("ui", "verbose")
@@ -31,8 +31,11 @@ class ui:
         self.debugflag = (self.debugflag or debug)
         self.interactive = (self.interactive and interactive)
 
-    def readconfig(self, fp):
-        self.cdata.readfp(fp)
+    def readconfig(self, fn):
+        try:
+            self.cdata.read(fn)
+        except ConfigParser.ParsingError, inst:
+            raise util.Abort(_("Failed to parse %s\n%s") % (fn, inst))
 
     def setconfig(self, section, name, val):
         self.overlay[(section, name)] = val
new file mode 100755
--- /dev/null
+++ b/tests/test-hgrc
@@ -0,0 +1,7 @@
+#!/bin/sh
+
+mkdir t
+cd t
+hg init
+echo "invalid" > .hg/hgrc
+hg status 2>&1 |sed -e "s:/.*\(/t/.*\):...\1:"