Skip to content

tmpdir.join("foo").write(...) doesn't work as expected. #605

Closed
@pytestbot

Description

@pytestbot

Originally reported by: Bjorn Pettersen (BitBucket: thebjorn, GitHub: thebjorn)


All of the following testcase

#!python

# -*- coding: utf-8 -*-

def test_1(tmpdir):
    tmpdir.join('foo').write(u'æ')


def test_2(tmpdir):
    tmpdir.join('foo').write(u'æ'.encode('u8'))


def test_3(tmpdir):
    tmpdir.join('foo').write(u'æ'.encode('l1'))

fails with the following errors:

#!python

(dev) w:\>py.test test_tmpdir.py
============================= test session starts =============================
platform win32 -- Python 2.7.8 -- py-1.4.25 -- pytest-2.6.3
plugins: cov, xdist
collected 3 items

test_tmpdir.py FFF

================================== FAILURES ===================================
___________________________________ test_1 ____________________________________

tmpdir = local('c:\\tmp\\pytest-20\\test_10')

    def test_1(tmpdir):
>       tmpdir.join('foo').write(u'æ')

test_tmpdir.py:4:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = local('c:\\tmp\\pytest-20\\test_10\\foo'), data = 'æ', mode = 'w'
ensure = False

    def write(self, data, mode='w', ensure=False):
        """ write data into path.   If ensure is True create
            missing parent directories.
            """
        if ensure:
            self.dirpath().ensure(dir=1)
        if 'b' in mode:
            if not py.builtin._isbytes(data):
                raise ValueError("can only process bytes")
        else:
            if not py.builtin._istext(data):
                if not py.builtin._isbytes(data):
                    data = str(data)
                else:
                    data = py.builtin._totext(data, sys.getdefaultencoding())
        f = self.open(mode)
        try:
>           f.write(data)
E           UnicodeEncodeError: 'ascii' codec can't encode character u'\xe6' in position 0: ordinal not in range(128)

dev\lib\site-packages\py\_path\local.py:476: UnicodeEncodeError
___________________________________ test_2 ____________________________________

tmpdir = local('c:\\tmp\\pytest-20\\test_20')

    def test_2(tmpdir):
>       tmpdir.join('foo').write(u'æ'.encode('u8'))

test_tmpdir.py:8:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = local('c:\\tmp\\pytest-20\\test_20\\foo'), data = '\xc3\xa6', mode = 'w'
ensure = False

    def write(self, data, mode='w', ensure=False):
        """ write data into path.   If ensure is True create
            missing parent directories.
            """
        if ensure:
            self.dirpath().ensure(dir=1)
        if 'b' in mode:
            if not py.builtin._isbytes(data):
                raise ValueError("can only process bytes")
        else:
            if not py.builtin._istext(data):
                if not py.builtin._isbytes(data):
                    data = str(data)
                else:
>                   data = py.builtin._totext(data, sys.getdefaultencoding())
E                   UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)

dev\lib\site-packages\py\_path\local.py:473: UnicodeDecodeError
___________________________________ test_3 ____________________________________

tmpdir = local('c:\\tmp\\pytest-20\\test_30')

    def test_3(tmpdir):
>       tmpdir.join('foo').write(u'æ'.encode('l1'))

test_tmpdir.py:12:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = local('c:\\tmp\\pytest-20\\test_30\\foo'), data = '\xe6', mode = 'w'
ensure = False

    def write(self, data, mode='w', ensure=False):
        """ write data into path.   If ensure is True create
            missing parent directories.
            """
        if ensure:
            self.dirpath().ensure(dir=1)
        if 'b' in mode:
            if not py.builtin._isbytes(data):
                raise ValueError("can only process bytes")
        else:
            if not py.builtin._istext(data):
                if not py.builtin._isbytes(data):
                    data = str(data)
                else:
>                   data = py.builtin._totext(data, sys.getdefaultencoding())
E                   UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)

dev\lib\site-packages\py\_path\local.py:473: UnicodeDecodeError
========================== 3 failed in 0.19 seconds ===========================

(dev) w:\>

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions