Skip to content

Commit 9d373d8

Browse files
Merge pull request #2741 from nicoddemus/pytester-makepyfile
Encode utf-8 byte strings in pytester's makefile
2 parents 488bbd2 + 221797c commit 9d373d8

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

_pytest/pytester.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -480,29 +480,24 @@ def chdir(self):
480480
if not hasattr(self, '_olddir'):
481481
self._olddir = old
482482

483-
def _makefile(self, ext, args, kwargs, encoding="utf-8"):
483+
def _makefile(self, ext, args, kwargs, encoding='utf-8'):
484484
items = list(kwargs.items())
485+
486+
def to_text(s):
487+
return s.decode(encoding) if isinstance(s, bytes) else six.text_type(s)
488+
485489
if args:
486-
source = six.text_type("\n").join(
487-
map(six.text_type, args)) + six.text_type("\n")
490+
source = u"\n".join(to_text(x) for x in args)
488491
basename = self.request.function.__name__
489492
items.insert(0, (basename, source))
493+
490494
ret = None
491-
for name, value in items:
492-
p = self.tmpdir.join(name).new(ext=ext)
495+
for basename, value in items:
496+
p = self.tmpdir.join(basename).new(ext=ext)
493497
p.dirpath().ensure_dir()
494498
source = Source(value)
495-
496-
def my_totext(s, encoding="utf-8"):
497-
if isinstance(s, six.binary_type):
498-
s = six.text_type(s, encoding=encoding)
499-
return s
500-
501-
source_unicode = "\n".join([my_totext(line) for line in source.lines])
502-
source = six.text_type(source_unicode)
503-
content = source.strip().encode(encoding) # + "\n"
504-
# content = content.rstrip() + "\n"
505-
p.write(content, "wb")
499+
source = u"\n".join(to_text(line) for line in source.lines)
500+
p.write(source.strip().encode(encoding), "wb")
506501
if ret is None:
507502
ret = p
508503
return ret

changelog/2738.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Internal ``pytester`` plugin properly encodes ``bytes`` arguments to ``utf-8``.

testing/test_pytester.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding: utf-8 -*-
12
from __future__ import absolute_import, division, print_function
23
import pytest
34
import os
@@ -120,6 +121,16 @@ def test_makepyfile_unicode(testdir):
120121
testdir.makepyfile(unichr(0xfffd))
121122

122123

124+
def test_makepyfile_utf8(testdir):
125+
"""Ensure makepyfile accepts utf-8 bytes as input (#2738)"""
126+
utf8_contents = u"""
127+
def setup_function(function):
128+
mixed_encoding = u'São Paulo'
129+
""".encode('utf-8')
130+
p = testdir.makepyfile(utf8_contents)
131+
assert u"mixed_encoding = u'São Paulo'".encode('utf-8') in p.read('rb')
132+
133+
123134
def test_inline_run_clean_modules(testdir):
124135
test_mod = testdir.makepyfile("def test_foo(): assert True")
125136
result = testdir.inline_run(str(test_mod))

0 commit comments

Comments
 (0)