From 60ff2e85290072554256bf977e4cd5c0553908b8 Mon Sep 17 00:00:00 2001 From: Floris Bruynooghe Date: Mon, 11 Apr 2011 23:15:56 +0100 Subject: [PATCH] Allow unicode characters in testdir.makepyfile() On python2.x text arguments where passed through str, which meant only ascii-encodable strings could be used. This uses py.builting._totext() to keep unicode until it is written out to the file, which was already UTF-8 encoded. --- _pytest/pytester.py | 5 +++-- testing/test_pytester.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/_pytest/pytester.py b/_pytest/pytester.py index f9da97b18..619bba988 100644 --- a/_pytest/pytester.py +++ b/_pytest/pytester.py @@ -236,13 +236,14 @@ class TmpTestdir: def _makefile(self, ext, args, kwargs): items = list(kwargs.items()) if args: - source = "\n".join(map(str, args)) + "\n" + source = py.builtin._totext("\n").join( + map(py.builtin._totext, args)) + py.builtin._totext("\n") basename = self.request.function.__name__ items.insert(0, (basename, source)) ret = None for name, value in items: p = self.tmpdir.join(name).new(ext=ext) - source = str(py.code.Source(value)).lstrip() + source = py.builtin._totext(py.code.Source(value)).lstrip() p.write(source.encode("utf-8"), "wb") if ret is None: ret = p diff --git a/testing/test_pytester.py b/testing/test_pytester.py index 8b246d7d1..0501ee200 100644 --- a/testing/test_pytester.py +++ b/testing/test_pytester.py @@ -1,3 +1,4 @@ +import py import pytest import os, sys from _pytest.pytester import LineMatcher, LineComp, HookRecorder @@ -113,3 +114,12 @@ def test_functional(testdir, linecomp): assert res == [42] """) reprec.assertoutcome(passed=1) + + +def test_makepyfile_unicode(testdir): + global unichr + try: + unichr(65) + except NameError: + unichr = chr + testdir.makepyfile(unichr(0xfffd))