Ejemplo n.º 1
0
    def __init__(self, method: str, url: URL, *,
                 writer: 'asyncio.Task[None]',
                 continue100: Optional['asyncio.Future[bool]'],
                 timer: BaseTimerContext,
                 request_info: RequestInfo,
                 traces: List['Trace'],
                 loop: asyncio.AbstractEventLoop,
                 session: 'ClientSession') -> None:
        assert isinstance(url, URL)

        self.method = method
        self.cookies = SimpleCookie()

        self._real_url = url
        self._url = url.with_fragment(None)
        self._body = None  # type: Any
        self._writer = writer  # type: Optional[asyncio.Task[None]]
        self._continue = continue100  # None by default
        self._closed = True
        self._history = ()  # type: Tuple[ClientResponse, ...]
        self._request_info = request_info
        self._timer = timer if timer is not None else TimerNoop()
        self._cache = {}  # type: Dict[str, Any]
        self._traces = traces
        self._loop = loop
        # store a reference to session #1985
        self._session = session  # type: Optional[ClientSession]
        if loop.get_debug():
            self._source_traceback = traceback.extract_stack(sys._getframe(1))
Ejemplo n.º 2
0
    def __init__(self, method: str, url: URL, *, writer: 'asyncio.Task[None]',
                 continue100: Optional['asyncio.Future[bool]'],
                 timer: BaseTimerContext, request_info: RequestInfo,
                 traces: List['Trace'], loop: asyncio.AbstractEventLoop,
                 session: 'ClientSession') -> None:
        assert isinstance(url, URL)

        self.method = method
        self.cookies = SimpleCookie()

        self._real_url = url
        self._url = url.with_fragment(None)
        self._body = None  # type: Any
        self._writer = writer  # type: Optional[asyncio.Task[None]]
        self._continue = continue100  # None by default
        self._closed = True
        self._history = ()  # type: Tuple[ClientResponse, ...]
        self._request_info = request_info
        self._timer = timer if timer is not None else TimerNoop()
        self._cache = {}  # type: Dict[str, Any]
        self._traces = traces
        self._loop = loop
        # store a reference to session #1985
        self._session = session  # type: Optional[ClientSession]
        if loop.get_debug():
            self._source_traceback = traceback.extract_stack(sys._getframe(1))
        def _do_waitpid(
            self,
            loop: asyncio.AbstractEventLoop,
            expected_pid: int,
            callback: _Callback,
            args: List[Any],
        ) -> None:
            assert expected_pid > 0

            try:
                pid, status = os.waitpid(expected_pid, 0)
            except ChildProcessError:
                # The child process is already reaped
                # (may happen if waitpid() is called elsewhere).
                pid = expected_pid
                returncode = 255
                logger.warning(
                    "Unknown child process pid %d, will report returncode 255",
                    pid)
            else:
                returncode = _compute_returncode(status)
                if loop.get_debug():
                    logger.debug("process %s exited with returncode %s",
                                 expected_pid, returncode)

            if loop.is_closed():
                logger.warning("Loop %r that handles pid %r is closed", loop,
                               pid)
            else:
                loop.call_soon_threadsafe(callback, pid, returncode, *args)

            self._threads.pop(expected_pid)
Ejemplo n.º 4
0
    def __init__(
        self,
        method: str,
        url: URL,
        *,
        writer: "asyncio.Task[None]",
        continue100: Optional["asyncio.Future[bool]"],
        timer: BaseTimerContext,
        request_info: RequestInfo,
        traces: List["Trace"],
        loop: asyncio.AbstractEventLoop,
        session: "ClientSession",
    ) -> None:
        assert isinstance(url, URL)
        super().__init__()

        self.method = method
        self.cookies: SimpleCookie[str] = SimpleCookie()

        self._real_url = url
        self._url = url.with_fragment(None)
        self._body: Optional[bytes] = None
        self._writer: Optional[asyncio.Task[None]] = writer
        self._continue = continue100  # None by default
        self._closed = True
        self._history: Tuple[ClientResponse, ...] = ()
        self._request_info = request_info
        self._timer = timer if timer is not None else TimerNoop()
        self._cache: Dict[str, Any] = {}
        self._traces = traces
        self._loop = loop
        # store a reference to session #1985
        self._session: Optional[ClientSession] = session
        if loop.get_debug():
            self._source_traceback = traceback.extract_stack(sys._getframe(1))
Ejemplo n.º 5
0
    def __init__(self, connector: 'BaseConnector', key: 'ConnectionKey',
                 protocol: ResponseHandler,
                 loop: asyncio.AbstractEventLoop) -> None:
        self._key = key
        self._connector = connector
        self._loop = loop
        self._protocol = protocol  # type: Optional[ResponseHandler]
        self._callbacks = []  # type: List[Callable[[], None]]

        if loop.get_debug():
            self._source_traceback = traceback.extract_stack(sys._getframe(1))
Ejemplo n.º 6
0
    def __init__(self, connector: 'BaseConnector',
                 key: 'ConnectionKey',
                 protocol: ResponseHandler,
                 loop: asyncio.AbstractEventLoop) -> None:
        self._key = key
        self._connector = connector
        self._loop = loop
        self._protocol = protocol  # type: Optional[ResponseHandler]
        self._callbacks = []  # type: List[Callable[[], None]]

        if loop.get_debug():
            self._source_traceback = traceback.extract_stack(sys._getframe(1))
Ejemplo n.º 7
0
    def __init__(
        self,
        connector: "BaseConnector",
        key: "ConnectionKey",
        protocol: ResponseHandler,
        loop: asyncio.AbstractEventLoop,
    ) -> None:
        self._key = key
        self._connector = connector
        self._loop = loop
        self._protocol: Optional[ResponseHandler] = protocol
        self._callbacks: List[Callable[[], None]] = []

        if loop.get_debug():
            self._source_traceback = traceback.extract_stack(sys._getframe(1))
Ejemplo n.º 8
0
    def handler(loop: asyncio.AbstractEventLoop,
                context: Dict[str, Any]) -> None:  # pragma: no cover
        message = context["message"]
        exception = context.get("exception", Exception)
        if sys.version_info < (3, 7, 4) and message in (
                "SSL error in data received",
                "Fatal error on transport",
        ):
            # Ignore aiohttp #3535 / cpython #13548 issue with SSL data after close
            # Adapted from https://github.com/aio-libs/aiohttp/issues/3535#issuecomment-483268542
            import ssl
            from asyncio.sslproto import SSLProtocol

            protocol = context.get("protocol")
            if (isinstance(exception, ssl.SSLError)
                    and exception.reason == "KRB5_S_INIT"
                    and isinstance(protocol, SSLProtocol)):
                return

        # Our main interest here is minimising the other various errors and tracebacks that
        # drown out the politely formatted errors from cli.py when things go wrong
        if "exception was never retrieved" in message:
            from .request import MissingSession

            # While closing down, we remove the global session, causing other requests to fail. This
            # just causes noise, so ignore if that's the exception.
            if not isinstance(exception, MissingSession):
                print(
                    f"ERROR (while closing down): {type(exception).__name__}: {exception}",
                    file=sys.stderr,
                )
        else:
            print(
                f"ERROR (from event loop): {type(exception).__name__}: {message}",
                file=sys.stderr)
        if loop.get_debug():
            loop.default_exception_handler(context)
Ejemplo n.º 9
0
    def __init__(
        self,
        method: str,
        url: URL,
        *,
        params: Optional[Mapping[str, str]] = None,
        headers: Optional[LooseHeaders] = None,
        skip_auto_headers: Iterable[str] = frozenset(),
        data: Any = None,
        cookies: Optional[LooseCookies] = None,
        auth: Optional[BasicAuth] = None,
        version: http.HttpVersion = http.HttpVersion11,
        compress: Optional[str] = None,
        chunked: Optional[bool] = None,
        expect100: bool = False,
        loop: asyncio.AbstractEventLoop,
        response_class: Optional[Type["ClientResponse"]] = None,
        proxy: Optional[URL] = None,
        proxy_auth: Optional[BasicAuth] = None,
        timer: Optional[BaseTimerContext] = None,
        session: Optional["ClientSession"] = None,
        ssl: Union[SSLContext, bool, Fingerprint, None] = None,
        proxy_headers: Optional[LooseHeaders] = None,
        traces: Optional[List["Trace"]] = None,
    ):

        assert isinstance(url, URL), url
        assert isinstance(proxy, (URL, type(None))), proxy
        # FIXME: session is None in tests only, need to fix tests
        # assert session is not None
        self._session = cast("ClientSession", session)
        if params:
            q = MultiDict(url.query)
            url2 = url.with_query(params)
            q.extend(url2.query)
            url = url.with_query(q)
        self.original_url = url
        self.url = url.with_fragment(None)
        self.method = method.upper()
        self.chunked = chunked
        self.compress = compress
        self.loop = loop
        self.length = None
        if response_class is None:
            real_response_class = ClientResponse
        else:
            real_response_class = response_class
        self.response_class = real_response_class  # type: Type[ClientResponse]
        self._timer = timer if timer is not None else TimerNoop()
        self._ssl = ssl

        if loop.get_debug():
            self._source_traceback = traceback.extract_stack(sys._getframe(1))

        self.update_version(version)
        self.update_host(url)
        self.update_headers(headers)
        self.update_auto_headers(skip_auto_headers)
        self.update_cookies(cookies)
        self.update_content_encoding(data)
        self.update_auth(auth)
        self.update_proxy(proxy, proxy_auth, proxy_headers)

        self.update_body_from_data(data)
        if data is not None or self.method not in self.GET_METHODS:
            self.update_transfer_encoding()
        self.update_expect_continue(expect100)
        if traces is None:
            traces = []
        self._traces = traces