]> git.phdru.name Git - bookmarks_db.git/commitdiff
Fix(Py3): `urllib` writes its files as bytes
authorOleg Broytman <phd@phdru.name>
Mon, 20 Nov 2023 17:33:42 +0000 (20:33 +0300)
committerOleg Broytman <phd@phdru.name>
Mon, 20 Nov 2023 19:13:26 +0000 (22:13 +0300)
Robots/bkmk_rurllib_py3.py

index 1daec389aebaa27f3eb502cc2ef2016310dbea4c..73f9b77ca3ef19f0106cc3bd3dc3e16902b5a818 100644 (file)
@@ -117,21 +117,19 @@ class robot_urllib_py3(robot_base):
             ):
                 if encoding and encoding not in possible_encodings:
                     possible_encodings.append(encoding)
-            content = None
-            for encoding in possible_encodings:
-                infile = open(fname, 'rt', encoding=encoding)
-                try:
-                    content = infile.read()
-                except UnicodeDecodeError:
-                    infile.close()
-                    continue
-                else:
-                    break
+            content = e = None
+            infile = open(fname, 'rb')
+            try:
+                content = infile.read()
+            except Exception as e:
+                content = None
+            finally:
                 infile.close()
 
             if content is None:
+                e = str(e)
                 return (
-                    'ERROR: File encoding was not recognized',
+                    'ERROR: ' + e,
                     None, None, None, None
                 )
             return None, None, None, headers, content