XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Change event loop policy on windows Support more versions from curl_cffi

7b9ad21d
Heiner Lohaus <heiner@lohaus.eu>
提交于

代码差异

2 个文件 +30 -12
Modified g4f/Provider/helper.py +9 -2
@@ -1,13 +1,20 @@
1 1 from __future__ import annotations
2 2
3 import asyncio
3 import asyncio, sys
4 4 from asyncio import AbstractEventLoop
5 5
6 6 import browser_cookie3
7 7
8 # Change event loop policy on windows
9 if sys.platform == 'win32':
10 if isinstance(
11 asyncio.get_event_loop_policy(), asyncio.WindowsProactorEventLoopPolicy
12 ):
13 asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
14
15 # Local Cookie Storage
8 16 _cookies: dict[str, dict[str, str]] = {}
9 17
10
11 18 # If event loop is already running, handle nested event loops
12 19 # If "nest_asyncio" is installed, patch the event loop.
13 20 def get_event_loop() -> AbstractEventLoop:
Modified g4f/requests.py +21 -10
@@ -8,10 +8,12 @@ from aiohttp.base_protocol import BaseProtocol
8 8
9 9 from curl_cffi.requests import AsyncSession as BaseSession
10 10 from curl_cffi.requests import Response
11 from curl_cffi import AsyncCurl
12 11
13 is_newer_0_5_9 = hasattr(AsyncCurl, "remove_handle")
14 is_newer_0_5_8 = hasattr(BaseSession, "_set_cookies")
12 import curl_cffi
13
14 is_newer_0_5_8 = hasattr(BaseSession, "_set_cookies") or hasattr(curl_cffi.requests.Cookies, "get_cookies_for_curl")
15 is_newer_0_5_9 = hasattr(curl_cffi.AsyncCurl, "remove_handle")
16 is_newer_0_5_10 = hasattr(BaseSession, "release_curl")
15 17
16 18 class StreamResponse:
17 19 def __init__(self, inner: Response, content: StreamReader, request):
@@ -65,13 +67,22 @@ class StreamRequest:
65 67 async def __aenter__(self) -> StreamResponse:
66 68 self.curl = await self.session.pop_curl()
67 69 self.enter = self.loop.create_future()
68 request, _, header_buffer = self.session._set_curl_options(
69 self.curl,
70 self.method,
71 self.url,
72 content_callback=self.on_content,
73 **self.options
74 )
70 if is_newer_0_5_10:
71 request, _, header_buffer, _, _ = self.session._set_curl_options(
72 self.curl,
73 self.method,
74 self.url,
75 content_callback=self.on_content,
76 **self.options
77 )
78 else:
79 request, _, header_buffer = self.session._set_curl_options(
80 self.curl,
81 self.method,
82 self.url,
83 content_callback=self.on_content,
84 **self.options
85 )
75 86 if is_newer_0_5_9:
76 87 self.handle = self.session.acurl.add_handle(self.curl)
77 88 else: