Fix encoding in config test and open configs in binary mode to warn for possible encoding errors

This commit is contained in:
Daniel Neuhäuser 2010-05-30 17:51:14 +02:00
parent 23ef216a15
commit 31275a34c2
3 changed files with 13 additions and 5 deletions

View File

@ -165,7 +165,7 @@ class Config(object):
try: try:
try: try:
os.chdir(dirname) os.chdir(dirname)
f = open(config_file, 'U') f = open(config_file, 'Ub')
try: try:
code = compile(f.read(), config_file, 'exec') code = compile(f.read(), config_file, 'exec')
finally: finally:

View File

@ -84,11 +84,12 @@ def test_extension_values(app):
@with_tempdir @with_tempdir
def test_errors_warnings(dir): def test_errors_warnings(dir):
# test the error for syntax errors in the config file # 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) raises_msg(ConfigError, 'conf.py', Config, dir, 'conf.py', {}, None)
# test the warning for bytestrings with non-ascii content # 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) cfg = Config(dir, 'conf.py', {}, None)
warned = [False] warned = [False]
def warn(msg): def warn(msg):

View File

@ -11,6 +11,7 @@ import sys
import StringIO import StringIO
import tempfile import tempfile
import shutil import shutil
from codecs import open
try: try:
from functools import wraps from functools import wraps
@ -191,8 +192,14 @@ def with_tempdir(func):
return new_func return new_func
def write_file(name, contents): def write_file(name, contents, encoding=None):
f = open(str(name), 'wb') 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.write(contents)
f.close() f.close()