Travis CI python 2.7.15 unicode issue

When I have run the pytest in my local with 2.7.15 it works fine but in Travis CI it fails to say ‘UnicodeEncodeError’

    $ PYTHONPATH=. pytest -v -s --cov=pingping tests
============================= test session starts ==============================
platform linux2 -- Python 2.7.15, pytest-4.6.6, py-1.7.0, pluggy-0.13.1 -- /home/travis/virtualenv/python2.7.15/bin/python
cachedir: .pytest_cache
rootdir: /home/travis/build/network-tools/pingping
plugins: cov-2.8.1
collecting ... INFO:root:Logging Time : 2020-03-06 03:30:34.992237
collected 6 items

tests/test_ping.py::TestPing::test_ping_en PASSED
tests/test_ping.py::TestPing::test_ping_spanish FAILED
tests/test_ping.py::TestPing::test_ping_french FAILED
tests/test_ping.py::TestPing::test_ping_afrikaans PASSED
tests/test_ping.py::TestPing::test_ping_telugu FAILED
tests/test_ping.py::TestPing::test_ping_hindi FAILED

=================================== FAILURES ===================================
__________________________ TestPing.test_ping_spanish __________________________

self = <tests.test_ping.TestPing instance at 0x7fb04e652d40>
setup = <pingping.ping.Ping instance at 0x7fb04e652b00>

 def test_ping_spanish(self, setup):
> result = self.read_all_inputs(setup, langauge='spanish')

tests/test_ping.py:88:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
tests/test_ping.py:23: in read_all_inputs
 result[os][ip] = obj.fetch_ping_data(each_ping)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <class pingping.ping.Ping at 0x7fb04e48d0b8>
data = 'Haciendo ping a 1.1.1.1 con 32 bytes de datos:
Responder desde 1.1.1.1: bytes = 32 ms = 87ms TTL = 127
Responder de... (0% de pérdida),
Tiempo aproximado de ida y vuelta en milisegundos:
 Mínimo = 68ms, máximo = 99ms, promedio = 81ms '
command = 'ping', loss_percentage_filter = 75

 @classmethod
 def fetch_ping_data(cls, data, command='ping', loss_percentage_filter=75):
 if type(data) is not list:
> data = str(data).split('\n') # .encode('utf-8') issue in 2.7
E UnicodeEncodeError: 'ascii' codec can't encode character u'\xed' in position 281: ordinal not in range(128)

pingping/ping.py:118: UnicodeEncodeError
__________________________ TestPing.test_ping_french ___________________________

https://travis-ci.org/network-tools/pingping Build 57

Thanks, I understood the problem, here is the solution: https://stackoverflow.com/questions/2276200/changing-default-encoding-of-python

I have written the following code in my test class.

import sys        
if sys.version_info.major == 2:
    import imp
    imp.reload(sys)
    sys.setdefaultencoding('utf-8')

I wouldn’t call it a “solution” (at least, not unless you understand its problems and sign up for dealing with them):

The real solution is to always convert str<->unicode explicitly, with explicit encoding. For I/O, you can set PYTHONIOENCODING but then you need to somehow make sure it’s always set.

You are right, but setting PYTHONIOENCODING is not solving the defaultencoding problem.

Error indicates an unicode character cannot be ascii-encoded.

To use a specific encoding (e.g. ‘utf-8’) use the encoding argument for pandas to csv:

df.to_csv(file_name, sep='\t', encoding='utf-8')