[issue8256] input() doesn't catch _PyUnicode_AsString() exception; io.StringIO().encoding is None

Erik Bray report at bugs.python.org
Mon Nov 23 15:17:46 EST 2015


Erik Bray added the comment:

I just recently discovered this myself.  In the process of debugging the issue I also noticed the same bug that is now fixed via Issue 24402.

While I agree that Issue 24402 mostly mitigates the issue I think this patch is still worthwhile, as the current behavior still leads to cryptic, hard to debug errors.  For example (although this is not great code, bear with me...) one could write a stdout wrapper like:

>>> class WrappedStream:
...     encoding = 'utf8'
...     errors = None
...     def __getattr__(self, attr):
...         return getattr(sys.__stdout__, attr)
... 
>>> sys.stdout = WrappedStream()
>>> sys.stdout.fileno()
1
>>> sys.stdout.isatty()
True
>>> sys.stdout.errors
>>> input('Prompt: ')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad argument type for built-in operation

This still goes down the path for ttys, but because the 'errors' attribute does not defer to the underlying stream it still leads to a hard to debug exception.  To be clear, I think the above code *should* break, just not as cryptically.

----------
nosy: +erik.bray

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8256>
_______________________________________


More information about the Python-bugs-list mailing list