Repository navigation
Open
Description
I ran on a issue today where I as trying to read a File object from a malformed utf-8 string with an invalid character. I managed to fix my issue creating my own readlines() function with a try/catch block, but I don't think this solution is resistant to future updates from this libary since I cant' guarantee that the file cursor will move to the next line before this error occur.
def readlines(file):
lines = []
eof = False
while not eof:
try:
line = file.readline()
if line == '':
eof = True
else:
lines.append(line)
except UnicodeDecodeError as e:
lines.append(e.object.decode('utf-8',errors='replace'))
return linesOne simple solution to this would be adding a default error handling in line 68.
paramiko/paramiko/py3compat.py
Lines 60 to 71 in cf7d49d
| def u(s, encoding="utf8"): # NOQA | |
| """cast bytes or unicode to unicode""" | |
| if isinstance(s, str): | |
| return s.decode(encoding) | |
| elif isinstance(s, unicode): # NOQA | |
| return s | |
| elif isinstance(s, buffer): # NOQA | |
| return s.decode(encoding) | |
| else: | |
| raise TypeError("Expected unicode or bytes, got {!r}".format(s)) | |
changing to
return s.decode(encoding, errors='replace')or even better giving the user the choice of how she/he would handle the errors via an optional argument.
Reacted by Lovelin B
Reacted by Lovelin B
Metadata
Metadata
Assignees
Labels
No labels
Activity
[-]Hability to ignore unidcode decode errors.[/-][+]Ability to ignore unidcode decode errors.[/+]hakaishi commentedon Mar 14, 2022
continuation of previous pull request at #1995