diff options
Diffstat (limited to 'tests/util.py')
-rw-r--r-- | tests/util.py | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/tests/util.py b/tests/util.py index 1b24af0e..b81e15b6 100644 --- a/tests/util.py +++ b/tests/util.py @@ -11,6 +11,8 @@ import sys import StringIO import tempfile import shutil +import re +from codecs import open try: from functools import wraps @@ -31,7 +33,7 @@ __all__ = [ 'raises', 'raises_msg', 'Struct', 'ListOutput', 'TestApp', 'with_app', 'gen_with_app', 'path', 'with_tempdir', 'write_file', - 'sprint', + 'sprint', 'remove_unicode_literals', ] @@ -191,11 +193,21 @@ 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() def sprint(*args): sys.stderr.write(' '.join(map(str, args)) + '\n') + +_unicode_literals_re = re.compile(r'u(".*?")|u(\'.*?\')') +def remove_unicode_literals(s): + return _unicode_literals_re.sub(lambda x: x.group(1) or x.group(2), s) |