Fix warning for bytestrings with non-ascii content for python3

This commit is contained in:
Daniel Neuhäuser
2010-06-20 19:34:49 +02:00
parent eaa3cb4f1f
commit 710a7d75a1
2 changed files with 16 additions and 5 deletions

View File

@@ -11,12 +11,18 @@
import os
import re
import sys
from os import path
from sphinx.errors import ConfigError
from sphinx.util.osutil import make_filename
nonascii_re = re.compile(r'[\x80-\xff]')
nonascii_re = re.compile(ur'[\x80-\xff]'.encode('ascii'))
try:
bytes
except NameError:
bytes = str
class Config(object):
@@ -187,10 +193,10 @@ class Config(object):
# check all string values for non-ASCII characters in bytestrings,
# since that can result in UnicodeErrors all over the place
for name, value in self._raw_config.iteritems():
if isinstance(value, str) and nonascii_re.search(value):
if isinstance(value, bytes) and nonascii_re.search(value):
warn('the config value %r is set to a string with non-ASCII '
'characters; this can lead to Unicode errors occurring. '
'Please use Unicode strings, e.g. u"Content".' % name)
'Please use Unicode strings, e.g. %r.' % (name, u'Content'))
def init_values(self):
config = self._raw_config

View File

@@ -9,6 +9,7 @@
:copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
import sys
from util import *
@@ -88,8 +89,12 @@ def test_errors_warnings(dir):
raises_msg(ConfigError, 'conf.py', Config, dir, 'conf.py', {}, None)
# test the warning for bytestrings with non-ascii content
write_file(dir / 'conf.py',
u'# -*- coding: latin-1\nproject = "fooä"\n', 'latin-1')
# bytestrings with non-ascii content are a syntax error in python3 so we
# skip the test there
if sys.version_info >= (3, 0):
return
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):