None has no attribute encoding exception happens in VS when debugging a Python Windows Application that checks sys.stdin.encoding. type(sys.stdin) is <class '_pydev_bundle.pydev_console_utils.DebugConsoleStdIn'>.
(The problem occurs in VS 2017 15.8.1. In VS 2017 15.6.2 this still works correctly.)
Thanks for the report.
I tried this script and I don't get the error. I'm using ptvsd==4.1.4:
import sys
t = type(sys.stdin)
print(t)
<class '_pydev_bundle.pydev_console_utils.DebugConsoleStdIn'>
It's possible the bug has been fixed since 15.8 was released. Please pip install the latest ptvsd in your python environment, VS will use that over the older one that comes bundled with VS.
Thanks for checking.
To exhibit the problem the script should attempt to access sys.stdin.encoding:
import sys
t = type(sys.stdin)
print(t)
print(sys.stdin.encoding)
I'll investigate updating ptvsd.
Actually print won't work, as the problem only occurs when "Windows Application" is checked in the project properties. So I tried this:
import sys
import logging
t = type(sys.stdin)
logging.info(t)
logging.info(sys.stdin.encoding)
After updating to VS 15.9.0 and pip install ptvsd (4.2.0), the problem still occurs.
Thanks for the additional details.
If you add this code at the end:
with open('issue4866.txt', 'wt') as f:
f.write('hello')
You'll see that the file is not created when you run with pythonw.exe, debugging or not.
The legacy debugger doesn't show the unhandled exception like the new debugger does, it only prints the error in VS output window. In that sense, I'd consider the new debugger an improvement.
Is there a reason you are trying to access sys.stdin when running without a console?
https://stackoverflow.com/questions/9705982/pythonw-exe-or-python-exe
The better example (closer to the real world code) to compare to pythonw.exe is:
import sys
with open('issue4866.txt', 'wt') as f:
f.write('hello\n')
f.write(str(type(sys.stdin)) + '\n')
if sys.stdin is not None:
f.write(str(sys.stdin.encoding) + '\n')
f.write('bye\n')
This runs without exception in pythonw.exe. The file contains:
hello
<class 'NoneType'>
bye
But only in the VS debugger there is an exception and the file only contains:
hello
<class '_pydev_bundle.pydev_console_utils.DebugConsoleStdIn'>
_Showing_ unhandled exceptions is good, but _causing_ unhandled exceptions is bad. :)
Is it my fault for not checking hasattr(sys.stdin, 'encoding')? I interpreted the documentation to say this is not required:
These streams are regular text files
And:
Their interfaces are defined in the io module.
Where:
The text stream API is described in detail in the documentation of TextIOBase.
And io.TextIOBase has the encoding attribute.
Thanks! That makes sense.
stdin should probably be None, or have an encoding attribute. Not this in-between thing.
I filed an issue at https://github.com/Microsoft/ptvsd/issues/1023 for ptvsd devs to look at.
I filed an issue at microsoft/ptvsd#1023 for ptvsd devs to look at.
It looks like this upstream issue was fixed one year ago. Does anyone know in which VS version this should be solved?
The problem still occurs in VS2019 16.4.5 / Python 16.4.19310.1.
We had a recent issue with it, so it may have been fixed and then regressed.
https://github.com/microsoft/ptvsd/issues/2052
It should be fixed again in 16.5.