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.
This commit is contained in:
parent
b7ba4d4e70
commit
60ff2e8529
|
@ -236,13 +236,14 @@ class TmpTestdir:
|
||||||
def _makefile(self, ext, args, kwargs):
|
def _makefile(self, ext, args, kwargs):
|
||||||
items = list(kwargs.items())
|
items = list(kwargs.items())
|
||||||
if args:
|
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__
|
basename = self.request.function.__name__
|
||||||
items.insert(0, (basename, source))
|
items.insert(0, (basename, source))
|
||||||
ret = None
|
ret = None
|
||||||
for name, value in items:
|
for name, value in items:
|
||||||
p = self.tmpdir.join(name).new(ext=ext)
|
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")
|
p.write(source.encode("utf-8"), "wb")
|
||||||
if ret is None:
|
if ret is None:
|
||||||
ret = p
|
ret = p
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import py
|
||||||
import pytest
|
import pytest
|
||||||
import os, sys
|
import os, sys
|
||||||
from _pytest.pytester import LineMatcher, LineComp, HookRecorder
|
from _pytest.pytester import LineMatcher, LineComp, HookRecorder
|
||||||
|
@ -113,3 +114,12 @@ def test_functional(testdir, linecomp):
|
||||||
assert res == [42]
|
assert res == [42]
|
||||||
""")
|
""")
|
||||||
reprec.assertoutcome(passed=1)
|
reprec.assertoutcome(passed=1)
|
||||||
|
|
||||||
|
|
||||||
|
def test_makepyfile_unicode(testdir):
|
||||||
|
global unichr
|
||||||
|
try:
|
||||||
|
unichr(65)
|
||||||
|
except NameError:
|
||||||
|
unichr = chr
|
||||||
|
testdir.makepyfile(unichr(0xfffd))
|
||||||
|
|
Loading…
Reference in New Issue