From 31275a34c2a80c6243833f658a465c1a3338f4e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Neuh=C3=A4user?= Date: Sun, 30 May 2010 17:51:14 +0200 Subject: [PATCH] Fix encoding in config test and open configs in binary mode to warn for possible encoding errors --- sphinx/config.py | 2 +- tests/test_config.py | 5 +++-- tests/util.py | 11 +++++++++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/sphinx/config.py b/sphinx/config.py index 2ec769871..07c3d63af 100644 --- a/sphinx/config.py +++ b/sphinx/config.py @@ -165,7 +165,7 @@ class Config(object): try: try: os.chdir(dirname) - f = open(config_file, 'U') + f = open(config_file, 'Ub') try: code = compile(f.read(), config_file, 'exec') finally: diff --git a/tests/test_config.py b/tests/test_config.py index cb4e11056..23d92e39c 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -84,11 +84,12 @@ def test_extension_values(app): @with_tempdir def test_errors_warnings(dir): # test the error for syntax errors in the config file - write_file(dir / 'conf.py', 'project = \n') + write_file(dir / 'conf.py', u'project = \n', 'ascii') raises_msg(ConfigError, 'conf.py', Config, dir, 'conf.py', {}, None) # test the warning for bytestrings with non-ascii content - write_file(dir / 'conf.py', '# -*- coding: latin-1\nproject = "foo\xe4"\n') + write_file(dir / 'conf.py', + u'# -*- coding: latin-1\nproject = "fooƤ"\n', 'latin-1') cfg = Config(dir, 'conf.py', {}, None) warned = [False] def warn(msg): diff --git a/tests/util.py b/tests/util.py index 1b24af0e2..2cf4a775b 100644 --- a/tests/util.py +++ b/tests/util.py @@ -11,6 +11,7 @@ import sys import StringIO import tempfile import shutil +from codecs import open try: from functools import wraps @@ -191,8 +192,14 @@ def with_tempdir(func): return new_func -def write_file(name, contents): - f = open(str(name), 'wb') +def write_file(name, contents, encoding=None): + if encoding is None: + mode = 'wb' + if isinstance(contents, unicode): + contents = contents.encode('ascii') + else: + mode = 'w' + f = open(str(name), 'wb', encoding=encoding) f.write(contents) f.close()