Skip to content

Commit

Permalink
Fix for Python3.10.
Browse files Browse the repository at this point in the history
  • Loading branch information
jzohrab committed Jan 9, 2025
1 parent 2a7fb85 commit 9e33289
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lute/book/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import os
import shutil
from io import StringIO, TextIOWrapper
from io import StringIO, TextIOWrapper, BytesIO
from datetime import datetime
import uuid
from dataclasses import dataclass
Expand Down Expand Up @@ -74,8 +74,26 @@ def get_file_content(self, filename, filestream):

def _get_text_stream_content(self, fstream, encoding="utf-8"):
"Gets content from simple text stream."
with TextIOWrapper(fstream, encoding=encoding) as decoded_stream:
return decoded_stream.read()

usestream = fstream
# May have to convert the fstream to a a BytesIO stream.
# GitHub CI caught this, and per ChatGPT: In Python 3.10,
# SpooledTemporaryFile no longer automatically gains all
# file-like methods when rolled over to a regular temporary
# file. Specifically, it seems that the object lacks the
# readable method required by TextIOWrapper to validate the
# stream ...
#
# I haven't looked into this deeply, but when running Python
# 3.10.16 on my mac, "inv accept -k bad_text_files" failed on
# line "with TextIOWrapper(fstream, encoding=encoding) as
# decoded:" with "AttributeError: 'SpooledTemporaryFile'
# object has no attribute 'readable'. Did you mean:
# 'readline'?".. Converting usestream to BytesIO fixed it.
if not hasattr(fstream, "readable"):
usestream = BytesIO(fstream.read()) # Wrap in BytesIO if needed
with TextIOWrapper(usestream, encoding=encoding) as decoded:
return decoded.read()

def _get_textfile_content(self, filename, filestream):
"Get content as a single string."
Expand Down

0 comments on commit 9e33289

Please sign in to comment.