Skip to content

Commit

Permalink
Also change content decode for atext()
Browse files Browse the repository at this point in the history
  • Loading branch information
perklet committed Nov 2, 2023
1 parent be55a66 commit db5a9ed
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions curl_cffi/requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Optional
import queue

from .. import Curl, CurlError
from .. import Curl
from .headers import Headers
from .cookies import Cookies
from .errors import RequestsError
Expand Down Expand Up @@ -63,12 +63,15 @@ def __init__(self, curl: Optional[Curl] = None, request: Optional[Request] = Non
self.queue: Optional[queue.Queue] = None
self.stream_task = None

@property
def text(self) -> str:
def _decode(self, content: bytes) -> str:
try:
return self.content.decode(self.charset, errors="replace")
return content.decode(self.charset, errors="replace")
except (UnicodeDecodeError, LookupError):
return self.content.decode("utf-8-sig")
return content.decode("utf-8-sig")

@property
def text(self) -> str:
return self._decode(self.content)

def raise_for_status(self):
if not self.ok:
Expand Down Expand Up @@ -156,7 +159,7 @@ async def aiter_content(self, chunk_size=None, decode_unicode=False):
yield chunk

async def atext(self) -> str:
return (await self.acontent()).decode(self.charset)
return self._decode(await self.acontent())

async def acontent(self) -> bytes:
chunks = []
Expand Down

0 comments on commit db5a9ed

Please sign in to comment.