mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Fix encoding in config test and open configs in binary mode to warn for possible encoding errors
This commit is contained in:
parent
23ef216a15
commit
31275a34c2
@ -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:
|
||||
|
@ -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):
|
||||
|
@ -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()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user