Anonymous avatar Anonymous committed 190be39

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

Comments (0)

Files changed (3)

             try:
                 try:
                     os.chdir(dirname)
-                    f = open(config_file, 'U')
+                    f = open(config_file, 'Ub')
                     try:
                         code = compile(f.read(), config_file, 'exec')
                     finally:

tests/test_config.py

 @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):
 import StringIO
 import tempfile
 import shutil
+from codecs import open
 
 try:
     from functools import wraps
     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()
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.