[PATCH 1 of 6 py3] config: add sanity assert that files are opened as binary

Augie Fackler raf at durin42.com
Mon Mar 6 23:23:05 UTC 2017


# HG changeset patch
# User Augie Fackler <raf at durin42.com>
# Date 1488563711 18000
#      Fri Mar 03 12:55:11 2017 -0500
# Node ID 428615d799a9115249f3aab8d6481e384e879f6a
# Parent  41a9edc5d00f2b5744e1edaf2edaa10084099f53
config: add sanity assert that files are opened as binary

This helps with some debugging in Python 3, and shouldn't hurt
anything in Python 2. The unusual construction using getattr is done
so that StringIO/BytesIO instances can be used as well as real files.

diff --git a/mercurial/config.py b/mercurial/config.py
--- a/mercurial/config.py
+++ b/mercurial/config.py
@@ -170,5 +170,8 @@ class config(object):
     def read(self, path, fp=None, sections=None, remap=None):
         if not fp:
             fp = util.posixfile(path, 'rb')
+        assert getattr(fp, 'mode', 'rb') == r'rb', (
+            'config files must be opened in binary mode, got fp=%r mode=%r' % (
+                fp, fp.mode))
         self.parse(path, fp.read(),
                    sections=sections, remap=remap, include=self.read)


More information about the Mercurial-devel mailing list