[issue29809] TypeError in traceback.print_exc - unicode does not have the buffer interface

2017-03-14 Thread Martin Panter

Martin Panter added the comment:

FWIW I tend to use cStringIO.StringIO as a Python 2 replacement for io.StringIO 
to avoid this str vs unicode problem. But that only accepts ASCII, so won't 
help you if you really need the UTF-8 encoding step.

--
nosy: +martin.panter

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue29809] TypeError in traceback.print_exc - unicode does not have the buffer interface

2017-03-14 Thread Jason R. Coombs

Jason R. Coombs added the comment:

As a workaround, I wrapped BytesIO thus and it works for our needs.

class LenientIO(io.BytesIO):
"""
A version of BytesIO that can accept unicode or
bytes. See http://bugs.python.org/issue29809
"""
def write(self, value):
if isinstance(value, six.text_type):
value = value.encode('utf-8')
super(LenientIO, self).write(value)


I'll recommend we close as wontfix, given the limited impact, unless it's 
demonstrated elsewhere.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue29809] TypeError in traceback.print_exc - unicode does not have the buffer interface

2017-03-14 Thread Jason R. Coombs

Changes by Jason R. Coombs :


--
resolution:  -> wont fix
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue29809] TypeError in traceback.print_exc - unicode does not have the buffer interface

2017-03-14 Thread Jason R. Coombs

Jason R. Coombs added the comment:

> How you got a Unicode source line in SyntaxError?

Good question. Took me a while to replicate the exact conditions, but here's an 
example:

#coding: utf-8

from __future__ import unicode_literals

import tokenize
import io

code = """
   //
  //
"""
buf = io.StringIO(code)

try:
  list(tokenize.generate_tokens(buf.readline))
except Exception as exc:
  print(exc.args)


It outputs:
('unindent does not match any outer indentation level', ('', 3, 2, u' 
 //\n'))


> Yeah, it's now time to upgrade to Python 3 ;-)

Love to. Currently, we're blocked in that our code relies on SimpleParse, which 
doesn't support Python 3.


> You should write an helper function (class with a write method) encoding 
> Unicode strings.


Good idea. I was pondering what I might use as a workaround and your suggestion 
sounds suitable. I'll report back how well that works.

--
type: behavior -> 

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue29809] TypeError in traceback.print_exc - unicode does not have the buffer interface

2017-03-14 Thread STINNER Victor

STINNER Victor added the comment:

sys.stderr.write() accepts Unicode (encoded to sys.stderr.encoding), whereas 
io.BytesIO.write() requires bytes:

>>> import sys; sys.stderr.write(u'\xe9\n')
é

>>> import io; io.BytesIO().write(u'\xe9\n')
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'unicode' does not have the buffer interface


You should write an helper function (class with a write method) encoding 
Unicode strings.


> The same test runs without error on Python 3.

Yeah, it's now time to upgrade to Python 3 ;-)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue29809] TypeError in traceback.print_exc - unicode does not have the buffer interface

2017-03-14 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

format_exception_only() produces a Unicode string for SyntaxError with Unicode 
source line.

>>> traceback.format_exception_only(SyntaxError, SyntaxError('failed', 
>>> ('', 7, 2, u'  // test')))
['  File "", line 7\n', u'// test\n', '^\n', 'SyntaxError: 
failed\n']

But io.BytesIO() accepts only binary strings.

The similar issue is caused by Unicode file name:

>>> traceback.format_exception_only(SyntaxError, SyntaxError('failed', 
>>> (u'', 7, 2, '  // test')))
[u'  File "", line 7\n', '// test\n', '^\n', 'SyntaxError: 
failed\n']

But Unicode error message doesn't produce Unicode output:

>>> traceback.format_exception_only(SyntaxError, SyntaxError(u'failed', 
>>> ('', 7, 2, '  // test')))
['  File "", line 7\n', '// test\n', '^\n', 'SyntaxError: 
failed\n']

How you got a Unicode source line in SyntaxError?

--
components: +Library (Lib), Unicode
nosy: +ezio.melotti, haypo, serhiy.storchaka
type:  -> behavior

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue29809] TypeError in traceback.print_exc - unicode does not have the buffer interface

2017-03-14 Thread Jason R. Coombs

Jason R. Coombs added the comment:

A complimentary error will occur on Python 3 if a bytes member exists in the 
SyntaxError, though I suspect that would be an invalid usage.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue29809] TypeError in traceback.print_exc - unicode does not have the buffer interface

2017-03-14 Thread Jason R. Coombs

New submission from Jason R. Coombs:

I'm writing a routine that captures exceptions and logs them to a database. In 
doing so, I encountered a situation that when parsing a Unicode file that has 
an IndentationError (SyntaxError), print_exc will fail when it tries to render 
the unicode line. Here's a script that replicates the failure.

# coding: utf-8

from __future__ import unicode_literals

import io
import sys
import traceback

PY3 = sys.version_info > (3,)

print(sys.version)

buffer = io.StringIO() if PY3 else io.BytesIO()

try:
args = str(''), 7, 2, '  // test'
raise IndentationError('failed', args)
except Exception:
traceback.print_exc(file=buffer)


And the output


$ python2 test-unicode-tb.py   
2.7.13 (default, Dec 24 2016, 21:20:02) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]
Traceback (most recent call last):
  File "test-unicode-tb.py", line 19, in 
traceback.print_exc(file=buffer)
  File 
"/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/traceback.py",
 line 233, in print_exc
print_exception(etype, value, tb, limit, file)
  File 
"/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/traceback.py",
 line 128, in print_exception
_print(file, line, '')
  File 
"/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/traceback.py",
 line 13, in _print
file.write(str+terminator)
TypeError: 'unicode' does not have the buffer interface


The same test runs without error on Python 3. It's surprising to me that I'm 
the first person to encounter this issue. Is it possible I'm abusing the 
tokenize module and a unicode value shouldn't be present in the args for the 
IndentationError?

--
messages: 289592
nosy: jason.coombs
priority: normal
severity: normal
status: open
title: TypeError in traceback.print_exc - unicode does not have the buffer 
interface
versions: Python 2.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com