byroot / pysrt

Python parser for SubRip (srt) files
GNU General Public License v3.0
453 stars 69 forks source link

error_handling=SubRipFile.ERROR_LOG mode causes exceptions in Python3 #98

Open muravjov opened 6 days ago

muravjov commented 6 days ago

Hi, I open .srt files with error_handling=SubRipFile.ERROR_LOG, and it works fine in Py2 but not in Py3.

In case of a wrongly formatted srt file pysrt tries to write bytes into sys.stderr:

            sys.stderr.write(error.args[0].encode('ascii', 'replace'))

So we get in Py3:

  File "/Users/ilya/opt/programming/catbo/backend-env11/lib/python3.11/site-packages/pysrt/srtfile.py", line 153, in open
    new_file.read(source_file, error_handling=error_handling)
  File "/Users/ilya/opt/programming/catbo/backend-env11/lib/python3.11/site-packages/pysrt/srtfile.py", line 181, in read
    self.extend(self.stream(source_file, error_handling=error_handling))
  File "/opt/homebrew/Cellar/python@3.11/3.11.7/Frameworks/Python.framework/Versions/3.11/lib/python3.11/collections/__init__.py", line 1324, in extend
    self.data.extend(other)
  File "/Users/ilya/opt/programming/catbo/backend-env11/lib/python3.11/site-packages/pysrt/srtfile.py", line 215, in stream
    cls._handle_error(error, error_handling, index)
  File "/Users/ilya/opt/programming/catbo/backend-env11/lib/python3.11/site-packages/pysrt/srtfile.py", line 311, in _handle_error
    sys.stderr.write(error.args[0].encode('ascii', 'replace'))
TypeError: write() argument must be str, not bytes
muravjov commented 6 days ago

See also #71