Merge pull request #2741 from nicoddemus/pytester-makepyfile

Encode utf-8 byte strings in pytester's makefile
This commit is contained in:
Ronny Pfannschmidt 2017-09-01 08:31:35 +02:00 committed by GitHub
commit 9d373d83ac
3 changed files with 23 additions and 16 deletions

View File

@ -480,29 +480,24 @@ class Testdir:
if not hasattr(self, '_olddir'): if not hasattr(self, '_olddir'):
self._olddir = old self._olddir = old
def _makefile(self, ext, args, kwargs, encoding="utf-8"): def _makefile(self, ext, args, kwargs, encoding='utf-8'):
items = list(kwargs.items()) items = list(kwargs.items())
def to_text(s):
return s.decode(encoding) if isinstance(s, bytes) else six.text_type(s)
if args: if args:
source = six.text_type("\n").join( source = u"\n".join(to_text(x) for x in args)
map(six.text_type, args)) + six.text_type("\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 basename, value in items:
p = self.tmpdir.join(name).new(ext=ext) p = self.tmpdir.join(basename).new(ext=ext)
p.dirpath().ensure_dir() p.dirpath().ensure_dir()
source = Source(value) source = Source(value)
source = u"\n".join(to_text(line) for line in source.lines)
def my_totext(s, encoding="utf-8"): p.write(source.strip().encode(encoding), "wb")
if isinstance(s, six.binary_type):
s = six.text_type(s, encoding=encoding)
return s
source_unicode = "\n".join([my_totext(line) for line in source.lines])
source = six.text_type(source_unicode)
content = source.strip().encode(encoding) # + "\n"
# content = content.rstrip() + "\n"
p.write(content, "wb")
if ret is None: if ret is None:
ret = p ret = p
return ret return ret

1
changelog/2738.bugfix Normal file
View File

@ -0,0 +1 @@
Internal ``pytester`` plugin properly encodes ``bytes`` arguments to ``utf-8``.

View File

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function from __future__ import absolute_import, division, print_function
import pytest import pytest
import os import os
@ -120,6 +121,16 @@ def test_makepyfile_unicode(testdir):
testdir.makepyfile(unichr(0xfffd)) testdir.makepyfile(unichr(0xfffd))
def test_makepyfile_utf8(testdir):
"""Ensure makepyfile accepts utf-8 bytes as input (#2738)"""
utf8_contents = u"""
def setup_function(function):
mixed_encoding = u'São Paulo'
""".encode('utf-8')
p = testdir.makepyfile(utf8_contents)
assert u"mixed_encoding = u'São Paulo'".encode('utf-8') in p.read('rb')
def test_inline_run_clean_modules(testdir): def test_inline_run_clean_modules(testdir):
test_mod = testdir.makepyfile("def test_foo(): assert True") test_mod = testdir.makepyfile("def test_foo(): assert True")
result = testdir.inline_run(str(test_mod)) result = testdir.inline_run(str(test_mod))