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

login
register
mail settings
Submitter Augie Fackler
Date March 6, 2017, 11:23 p.m.
Message ID <428615d799a9115249f3.1488842585@augie-macbookair2.roam.corp.google.com>
Download mbox | patch
Permalink /patch/18945/
State Accepted
Headers show

Comments

Augie Fackler - March 6, 2017, 11:23 p.m.
# HG changeset patch
# User Augie Fackler <raf@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.
Yuya Nishihara - March 7, 2017, 1:57 p.m.
On Mon, 06 Mar 2017 18:23:05 -0500, Augie Fackler wrote:
> # HG changeset patch
> # User Augie Fackler <raf@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

Fixed minor nits and queued the series, thanks.

Patch

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)