3 ms·
I was super turned off of Python 3 when it came out and I promptly tried it and ran open("/dev/urandom").read(20) and was confronted with UnicodeDecodeError:
by schoen 12y ago
I was super turned off of Python 3 when it came out and I promptly tried it and ran
open("/dev/urandom").read(20)
and was confronted with
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 2: invalid start byte
If I had realized at the time that this error would be averted with
open("/dev/urandom", "rb").read(20)
I might have felt more charitable and less shocked (although I can see in retrospect that there's quite a good reason for this error, I might have preferred for the default file type to be binary -- presumably something that was debated extensively inside the Python 3 development effort).
- viraptor 12y agoYour first version was already not correct/portable. When reading binary files on Windows, you have to include the binary mode. Otherwise you'll drop bytes semi-randomly. This change only made that mistake visible on other systems.
- ma2rten 12y agoThere is no /dev/random on windows anyway. Not every piece of code has to be portable.
- schoen 12y agoYes, I definitely wouldn't have expected or intended that code to work on Windows.
- viraptor 12y agoIt doesn't have to be, but going for a more correct/portable code (than absolutely necessary minimum) helps avoid such problems. In this case binary file was opened - if the "rb" mode was added anyway, the exception mentioned above wouldn't happen. Edit: Actually python doesn't even mention windows. It's simply "Add a 'b' to the mode for binary files." (in python 2.7 documentation) According to the docs it should be specified regardless.
- thusoy 12y agoThe easiest version is the most portable and easy to read in this case: os.urandom(20) Saves you from having to manage files at all, and works cross-platform.
- schoen 12y agoHuh, I never knew that was there; thanks! In production code I would probably use Crypto.Random.get_random_bytes().