Bug 1397428 - Redirection to file results in traceback
Summary: Redirection to file results in traceback
Keywords:
Status: CLOSED WONTFIX
Alias: None
Product: Fedora
Classification: Fedora
Component: python
Version: 24
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: ---
Assignee: Charalampos Stratakis
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2016-11-22 13:55 UTC by Jaroslav Škarvada
Modified: 2016-11-22 15:59 UTC (History)
11 users (show)

Fixed In Version:
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
Last Closed: 2016-11-22 15:59:29 UTC
Type: Bug
Embargoed:


Attachments (Terms of Use)

Description Jaroslav Škarvada 2016-11-22 13:55:54 UTC
Description of problem:
The same code when redirected from STDOUT to file can result in traceback, which is strange.

Version-Release number of selected component (if applicable):
python-2.7.12-6.fc24.x86_64

How reproducible:
Always

Steps to Reproduce:
1.cat > test.py <<:EOF
#!/bin/python

url = 'https://github.com/EttusResearch/uhd/releases'
import requests
text = requests.get(url).text
print(text)
:EOF

2. python test.py
3. python test.py > log.txt

Actual results:
2. content
3. Traceback (most recent call last):
  File "./test0.py", line 10, in <module>
    print text
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 1016: ordinal not in range(128)

Expected results:
2. content
3. content

Additional info:
Redirection to file shouldn't result in different behavior, nor in traceback.

Comment 1 Miro Hrončok 2016-11-22 15:55:10 UTC
Smaller reproducer:

1.cat > test.py <<:EOF
print(u'\xb7')
:EOF

2. python test.py
3. python test.py | head

Actual results:
2. ·
3. Traceback (most recent call last):
  File "test.py", line 1, in <module>
    print(u'\xb7')
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 0: ordinal not in range(128)

Expected results:
2. ·
3. ·

But I'm afraid this is a "feature" of Python 2.

See http://stackoverflow.com/questions/19696652/piping-output-causes-python-program-to-fail

Comment 2 Dave Malcolm 2016-11-22 15:59:29 UTC
The issue is that in CPython 2 the encoding of stdout changes from utf-8 to ascii if stdout is not a tty.

I agree that it's counter-intuitive.

FWIW, I proposed changing this in Fedora 13:

  https://fedoraproject.org/wiki/Features/PythonEncodingUsesSystemLocale

but I was asked to not change this by the upstream Python development community, to ensure consistent behavior with every other CPython 2 implementation.

See that link for more information, and some workarounds.

Closing this out as WONTFIX.


Note You need to log in before you can comment on or make changes to this bug.