[PATCH] read configuration files in universal newline mode

Martin Geisler mg at daimi.au.dk
Tue Jan 27 14:56:10 CST 2009


# HG changeset patch
# User Martin Geisler <mg at daimi.au.dk>
# Date 1233089757 -3600
# Node ID 09e152c5c220ca53ab92276a629b0ae2130fa743
# Parent  752325f2208d4279a4fc892adddd1bfbc61b0ad1
read configuration files in universal newline mode

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -131,7 +131,9 @@
             fn = [fn]
         for f in fn:
             try:
-                fp = open(f)
+                # open the file in universal newline mode, otherwise
+                # ConfigParser cannot read a file with \r line endings
+                fp = open(f, 'U')
             except IOError:
                 continue
             cdata = self.cdata
@@ -176,7 +178,8 @@
         cdata = util.configparser()
         try:
             try:
-                fp = open(filename)
+                # open the file in universal newline mode
+                fp = open(filename, 'U')
             except IOError, inst:
                 raise util.Abort(_("unable to open %s: %s") %
                                  (filename, getattr(inst, "strerror", inst)))
diff --git a/tests/test-hgrc b/tests/test-hgrc
--- a/tests/test-hgrc
+++ b/tests/test-hgrc
@@ -16,3 +16,17 @@
 cat .hg/hgrc |sed -e "s:$p:...:"
 hg paths |sed -e "s:$p:...:"
 hg showconfig |sed -e "s:$p:...:"
+
+# test line endings, this could fail if Python is not compiled with
+# universal newline support
+echo % Unix line endings
+echo -n -e '[paths]\nfoo = /\n' > .hg/hgrc
+hg paths
+
+echo % Mac line endings
+echo -n -e '[paths]\rfoo = /\r' > .hg/hgrc
+hg paths
+
+echo % Windows line endings
+echo -n -e '[paths]\r\nfoo = /\r\n' > .hg/hgrc
+hg paths
diff --git a/tests/test-hgrc.out b/tests/test-hgrc.out
--- a/tests/test-hgrc.out
+++ b/tests/test-hgrc.out
@@ -14,3 +14,9 @@
 defaults.tag=-d "0 0"
 paths.default=.../foo%bar
 ui.slash=True
+% Unix line endings
+foo = /
+% Mac line endings
+foo = /
+% Windows line endings
+foo = /


More information about the Mercurial-devel mailing list