Skip to content

Commit

Permalink
Do not show warnings by default
Browse files Browse the repository at this point in the history
  • Loading branch information
lexiforest committed Dec 29, 2024
1 parent 3068b48 commit 57c2dcf
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 28 deletions.
6 changes: 4 additions & 2 deletions curl_cffi/aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from weakref import WeakKeyDictionary, WeakSet

from ._wrapper import ffi, lib
from .utils import _SHOW_WARNINGS
from .const import CurlMOpt
from .curl import DEFAULT_CACERT, Curl

Expand Down Expand Up @@ -36,7 +37,8 @@ def _get_selector(asyncio_loop) -> asyncio.AbstractEventLoop:
if not isinstance(asyncio_loop, getattr(asyncio, "ProactorEventLoop", type(None))):
return asyncio_loop

warnings.warn(PROACTOR_WARNING, RuntimeWarning, stacklevel=2)
if _SHOW_WARNINGS:
warnings.warn(PROACTOR_WARNING, RuntimeWarning, stacklevel=2)

from ._asyncio_selector import AddThreadSelectorEventLoop

Expand Down Expand Up @@ -202,7 +204,7 @@ def socket_action(self, sockfd: int, ev_bitmask: int) -> int:

def process_data(self, sockfd: int, ev_bitmask: int):
"""Call curl_multi_info_read to read data for given socket."""
if not self._curlm:
if _SHOW_WARNINGS and not self._curlm:
warnings.warn("Curlm alread closed! quitting from process_data", stacklevel=2)
return

Expand Down
5 changes: 3 additions & 2 deletions curl_cffi/curl.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from ._wrapper import ffi, lib
from .const import CurlECode, CurlHttpVersion, CurlInfo, CurlOpt, CurlWsFlag
from .utils import _SHOW_WARNINGS

DEFAULT_CACERT = certifi.where()
REASON_PHRASE_RE = re.compile(rb"HTTP/\d\.\d [0-9]{3} (.*)")
Expand Down Expand Up @@ -84,7 +85,7 @@ def write_callback(ptr, size, nmemb, userdata):
if wrote == CURL_WRITEFUNC_PAUSE or wrote == CURL_WRITEFUNC_ERROR: # noqa: SIM109
return wrote
# should make this an exception in future versions
if wrote != nmemb * size:
if _SHOW_WARNINGS and wrote != nmemb * size:
warnings.warn("Wrote bytes != received bytes.", RuntimeWarning, stacklevel=2)
return nmemb * size

Expand Down Expand Up @@ -129,7 +130,7 @@ def __init__(self, cacert: str = "", debug: bool = False, handle=None) -> None:

def _set_error_buffer(self) -> None:
ret = lib._curl_easy_setopt(self._curl, CurlOpt.ERRORBUFFER, self._error_buffer)
if ret != 0:
if _SHOW_WARNINGS and ret != 0:
warnings.warn("Failed to set error buffer", stacklevel=2)
if self._debug:
self.setopt(CurlOpt.VERBOSE, 1)
Expand Down
21 changes: 12 additions & 9 deletions curl_cffi/requests/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from typing import Dict, Iterator, List, MutableMapping, Optional, Tuple, Union
from urllib.parse import urlparse

from ..utils import _SHOW_WARNINGS
from .errors import CookieConflict, RequestsError

CookieTypes = Union["Cookies", CookieJar, Dict[str, str], List[Tuple[str, str]]]
Expand Down Expand Up @@ -191,17 +192,19 @@ def set(self, name: str, value: str, domain: str = "", path: str = "/", secure=F
"""
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
if name.startswith("__Secure-") and secure is False:
warnings.warn(
"`secure` changed to True for `__Secure-` prefixed cookies",
stacklevel=2,
)
if _SHOW_WARNINGS:
warnings.warn(
"`secure` changed to True for `__Secure-` prefixed cookies",
stacklevel=2,
)
secure = True
elif name.startswith("__Host-") and (secure is False or domain or path != "/"):
warnings.warn(
"`host` changed to True, `domain` removed, `path` changed to `/` "
"for `__Host-` prefixed cookies",
stacklevel=2,
)
if _SHOW_WARNINGS:
warnings.warn(
"`host` changed to True, `domain` removed, `path` changed to `/` "
"for `__Host-` prefixed cookies",
stacklevel=2,
)
secure = True
domain = ""
path = "/"
Expand Down
3 changes: 2 additions & 1 deletion curl_cffi/requests/impersonate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import List, Literal, Optional, TypedDict

from ..const import CurlOpt, CurlSslVersion
from ..utils import _SHOW_WARNINGS

BrowserTypeLiteral = Literal[
# Edge
Expand Down Expand Up @@ -303,7 +304,7 @@ def toggle_extension(curl, extension_id: int, enable: bool):
curl.setopt(CurlOpt.ECH, "")
# compress certificate
elif extension_id == 27:
if enable:
if _SHOW_WARNINGS and enable:
warnings.warn(
"Cert compression setting to brotli, "
"you had better specify which to use: zlib/brotli",
Expand Down
7 changes: 4 additions & 3 deletions curl_cffi/requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from concurrent.futures import Future
from typing import Any, Awaitable, Callable, Dict, List, Optional, Union

from .. import Curl
from ..curl import Curl
from ..utils import _SHOW_WARNINGS
from .cookies import Cookies
from .exceptions import HTTPError, RequestException
from .headers import Headers
Expand Down Expand Up @@ -173,7 +174,7 @@ def iter_content(self, chunk_size=None, decode_unicode=False):
"""
iterate streaming content chunk by chunk in bytes.
"""
if chunk_size:
if _SHOW_WARNINGS and chunk_size:
warnings.warn("chunk_size is ignored, there is no way to tell curl that.", stacklevel=2)
if decode_unicode:
raise NotImplementedError()
Expand Down Expand Up @@ -236,7 +237,7 @@ async def aiter_content(self, chunk_size=None, decode_unicode=False):
"""
iterate streaming content chunk by chunk in bytes.
"""
if chunk_size:
if _SHOW_WARNINGS and chunk_size:
warnings.warn("chunk_size is ignored, there is no way to tell curl that.", stacklevel=2)
if decode_unicode:
raise NotImplementedError()
Expand Down
3 changes: 2 additions & 1 deletion curl_cffi/requests/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from ..aio import AsyncCurl
from ..const import CurlHttpVersion, CurlInfo, CurlOpt
from ..curl import Curl, CurlError, CurlMime
from ..utils import _SHOW_WARNINGS
from .cookies import Cookies, CookieTypes, CurlMorsel
from .exceptions import RequestException, SessionClosed, code2error
from .headers import Headers, HeaderTypes
Expand Down Expand Up @@ -319,7 +320,7 @@ def __init__(
@property
def curl(self):
if self._use_thread_local_curl:
if self._is_customized_curl:
if _SHOW_WARNINGS and self._is_customized_curl:
warnings.warn("Creating fresh curl handle in different thread.", stacklevel=2)
if not getattr(self._local, "curl", None):
self._local.curl = Curl(debug=self.debug)
Expand Down
22 changes: 12 additions & 10 deletions curl_cffi/requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

from ..const import CurlHttpVersion, CurlOpt, CurlSslVersion
from ..curl import CURL_WRITEFUNC_ERROR, CurlMime
from ..utils import _SHOW_WARNINGS
from .cookies import Cookies
from .exceptions import ImpersonateError, InvalidURL
from .headers import Headers
Expand Down Expand Up @@ -246,12 +247,13 @@ def set_ja3_options(curl: Curl, ja3: str, permute: bool = False):

if extensions.endswith("-21"):
extensions = extensions[:-3]
warnings.warn(
"Padding(21) extension found in ja3 string, whether to add it should "
"be managed by the SSL engine. The TLS client hello packet may contain "
"or not contain this extension, any of which should be correct.",
stacklevel=1,
)
if _SHOW_WARNINGS:
warnings.warn(
"Padding(21) extension found in ja3 string, whether to add it should "
"be managed by the SSL engine. The TLS client hello packet may contain "
"or not contain this extension, any of which should be correct.",
stacklevel=1,
)
extension_ids = set(int(e) for e in extensions.split("-"))
toggle_extensions_by_ids(curl, extension_ids)

Expand Down Expand Up @@ -521,7 +523,7 @@ def set_curl_options(
c.setopt(CurlOpt.PROXY, proxy)

if parts.scheme == "https":
if proxy.startswith("https://"):
if _SHOW_WARNINGS and proxy.startswith("https://"):
warnings.warn(
"Make sure you are using https over https proxy, otherwise, "
"the proxy prefix should be 'http://' not 'https://', "
Expand Down Expand Up @@ -579,7 +581,7 @@ def set_curl_options(

# ja3 string
if ja3:
if impersonate:
if _SHOW_WARNINGS and impersonate:
warnings.warn("JA3 was altered after browser version was set.", stacklevel=1)
permute = False
if isinstance(extra_fp, ExtraFingerprints) and extra_fp.tls_permute_extensions:
Expand All @@ -590,15 +592,15 @@ def set_curl_options(

# akamai string
if akamai:
if impersonate:
if _SHOW_WARNINGS and impersonate:
warnings.warn("Akamai was altered after browser version was set.", stacklevel=1)
set_akamai_options(c, akamai)

# extra_fp options
if extra_fp:
if isinstance(extra_fp, dict):
extra_fp = ExtraFingerprints(**extra_fp)
if impersonate:
if _SHOW_WARNINGS and impersonate:
warnings.warn(
"Extra fingerprints was altered after browser version was set.",
stacklevel=1,
Expand Down

0 comments on commit 57c2dcf

Please sign in to comment.