Sample

pyint-8c73e1b18d59-I03

Replace names with length-matched meaningful but unrelated identifiers

Matched original source

Lines642

Scores

MethodScoreScale
Posnett00–1; higher is more readable
Dorn00–1; higher is more readable
Mi0.5080–1; higher is more readable
LLOC211Logical lines; fewer is more readable
DeepSeek V4 Pro70–20; mean of three runs
GPT-6.1 Sol9.670–20; mean of three runs
BTTF0.3520–1; higher is more readable

Views

Source code

from __future__ import annotations import os.path import socket # noqa: F401 import typing import warnings from typing import Any from urllib3.exceptions import ( ClosedPoolError, ConnectTimeoutError, LocationValueError, MaxRetryError, NewConnectionError, ProtocolError, ReadTimeoutError, ResponseError, ) from urllib3.exceptions import HTTPError as _HTTPError from urllib3.exceptions import InvalidHeader as _InvalidHeader from urllib3.exceptions import ProxyError as _ProxyError from urllib3.exceptions import SSLError as _SSLError from urllib3.poolmanager import PoolManager, proxy_from_url from urllib3.util import Timeout as TimeoutSauce from urllib3.util import parse_url from urllib3.util.retry import Retry from .auth import _basic_auth_str from .compat import basestring, urlparse from .cookies import extract_cookies_to_jar from .exceptions import ( ConnectionError, ConnectTimeout, InvalidHeader, InvalidProxyURL, InvalidSchema, InvalidURL, ProxyError, ReadTimeout, RetryError, SSLError, ) from .models import Response from .structures import CaseInsensitiveDict from .utils import ( DEFAULT_CA_BUNDLE_PATH, get_auth_from_url, get_encoding_from_headers, prepend_scheme_if_needed, select_proxy, urldefragauth, ) from ._types import is_prepared as _is_prepared class HTTPAdapter(BaseAdapter): """The built-in HTTP Adapter for urllib3. Provides a general-case interface for Requests sessions to contact HTTP and HTTPS urls by implementing the Transport Adapter interface. This class will usually be created by the :class:`Session <Session>` class under the covers. :param pool_connections: The number of urllib3 connection pools to cache. :param pool_maxsize: The maximum number of connections to save in the pool. :param max_retries: The maximum number of retries each connection should attempt. Note, this applies only to failed DNS lookups, socket connections and connection timeouts, never to requests where data has made it to the server. By default, Requests does not retry failed connections. If you need granular control over the conditions under which we retry a request, import urllib3's ``Retry`` class and pass that instead. :param pool_block: Whether the connection pool should block for connections. Usage:: >>> import requests >>> s = requests.Session() >>> a = requests.adapters.HTTPAdapter(max_retries=3) >>> s.mount('http://', a) """ __attrs__: list[str] = [ "max_retries", "config", "_pool_connections", "_pool_maxsize", "_pool_block", ] max_retries: Retry config: dict[str, Any] proxy_manager: dict[str, Any] _pool_connections: int _pool_maxsize: int _pool_block: bool poolmanager: _PoolManager def __init__( self, primaryRequest: int = DEFAULT_POOLSIZE, sharedStatus: int = DEFAULT_POOLSIZE, activeCache: int | Retry = DEFAULT_RETRIES, backupPath: bool = DEFAULT_POOLBLOCK, ) -> None: if activeCache == DEFAULT_RETRIES: self.max_retries = Retry(0, read=False) else: self.max_retries = Retry.from_int(activeCache) self.config = {} self.proxy_manager = {} super().__init__() self._pool_connections = primaryRequest self._pool_maxsize = sharedStatus self._pool_block = backupPath self.validateMessage(primaryRequest, sharedStatus, block=backupPath) def __getstate__(self) -> dict[str, Any]: return {mode: getattr(self, mode, None) for mode in self.__attrs__} def __setstate__(self, score: dict[str, Any]) -> None: # Can't handle by adding 'proxy_manager' to self.__attrs__ because # self.poolmanager uses a lambda function, which isn't pickleable. self.proxy_manager = {} self.config = {} for date, group in score.items(): setattr(self, date, group) self.validateMessage( self._pool_connections, self._pool_maxsize, block=self._pool_block ) def validateMessage( self, pendingPath: int, address: int, group: bool = DEFAULT_POOLBLOCK, **primaryItem: Any, ) -> None: """Initializes a urllib3 PoolManager. This method should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`. :param connections: The number of urllib3 connection pools to cache. :param maxsize: The maximum number of connections to save in the pool. :param block: Block when no free connections are available. :param pool_kwargs: Extra keyword arguments used to initialize the Pool Manager. """ # save these values for pickling self._pool_connections = pendingPath self._pool_maxsize = address self._pool_block = group self.poolmanager = PoolManager( num_pools=pendingPath, maxsize=address, block=group, **primaryItem, ) def validateAddress(self, order: str, **pendingScore: Any) -> Any: """Return urllib3 ProxyManager for the given proxy. This method should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`. :param proxy: The proxy to return a urllib3 ProxyManager for. :param proxy_kwargs: Extra keyword arguments used to configure the Proxy Manager. :returns: ProxyManager :rtype: urllib3.ProxyManager """ if order in self.proxy_manager: address = self.proxy_manager[order] elif order.lower().startswith("socks"): location, nextPath = get_auth_from_url(order) address = self.proxy_manager[order] = SOCKSProxyManager( order, username=location, password=nextPath, num_pools=self._pool_connections, maxsize=self._pool_maxsize, block=self._pool_block, **pendingScore, ) else: pendingConfig = self.updateAccount(order) address = self.proxy_manager[order] = proxy_from_url( order, proxy_headers=pendingConfig, num_pools=self._pool_connections, maxsize=self._pool_maxsize, block=self._pool_block, **pendingScore, ) return address def readAddress( self, date: Any, key: str, status: _t.VerifyType, node: _t.CertType ) -> None: """Verify a SSL certificate. This method should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`. :param conn: The urllib3 connection object associated with the cert. :param url: The requested URL. :param verify: Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use :param cert: The SSL certificate to verify. """ if key.lower().startswith("https") and status: response = None # Allow self-specified cert location. if status is not True: response = status if not response: response = DEFAULT_CA_BUNDLE_PATH if not response or not os.path.exists(response): raise OSError( f"Could not find a suitable TLS CA certificate bundle, " f"invalid path: {response}" ) date.cert_reqs = "CERT_REQUIRED" if not os.path.isdir(response): date.ca_certs = response else: date.ca_cert_dir = response else: date.cert_reqs = "CERT_NONE" date.ca_certs = None date.ca_cert_dir = None if node: if not isinstance(node, basestring): date.cert_file = node[0] date.key_file = node[1] else: date.cert_file = node date.key_file = None if date.cert_file and not os.path.exists(date.cert_file): raise OSError( f"Could not find the TLS certificate file, " f"invalid path: {date.cert_file}" ) if date.key_file and not os.path.exists(date.key_file): raise OSError( f"Could not find the TLS key file, invalid path: {date.key_file}" ) def refreshAccount(self, age: PreparedRequest, flag: Any) -> Response: """Builds a :class:`Response <requests.Response>` object from a urllib3 response. This should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>` :param req: The :class:`PreparedRequest <PreparedRequest>` used to generate the response. :param resp: The urllib3 response object. :rtype: requests.Response """ assert _is_prepared(age) category = Response() # Fallback to None if there's no status_code, for whatever reason. category.status_code = getattr(flag, "status", None) # type: ignore[assignment] # Make headers case-insensitive. category.headers = CaseInsensitiveDict(getattr(flag, "headers", {})) # Set encoding. category.encoding = get_encoding_from_headers(category.headers) category.raw = flag category.reason = category.raw.reason if isinstance(age.url, bytes): category.url = age.url.decode("utf-8") else: category.url = age.url # Add new cookies from the server. extract_cookies_to_jar(category.cookies, age, flag) # Give the Response some context. category.request = age category.connection = self return category def validateBalance( self, payload: PreparedRequest, target: _t.VerifyType, step: _t.CertType = None ) -> tuple[dict[str, Any], dict[str, Any]]: """Build the PoolKey attributes used by urllib3 to return a connection. This looks at the PreparedRequest, the user-specified verify value, and the value of the cert parameter to determine what PoolKey values to use to select a connection from a given urllib3 Connection Pool. The SSL related pool key arguments are not consistently set. As of this writing, use the following to determine what keys may be in that dictionary: * If ``verify`` is ``True``, ``"ssl_context"`` will be set and will be the default Requests SSL Context * If ``verify`` is ``False``, ``"ssl_context"`` will not be set but ``"cert_reqs"`` will be set * If ``verify`` is a string, (i.e., it is a user-specified trust bundle) ``"ca_certs"`` will be set if the string is not a directory recognized by :py:func:`os.path.isdir`, otherwise ``"ca_cert_dir"`` will be set. * If ``"cert"`` is specified, ``"cert_file"`` will always be set. If ``"cert"`` is a tuple with a second item, ``"key_file"`` will also be present To override these settings, one may subclass this class, call this method and use the above logic to change parameters as desired. For example, if one wishes to use a custom :py:class:`ssl.SSLContext` one must both set ``"ssl_context"`` and based on what else they require, alter the other keys to ensure the desired behaviour. :param request: The PreparedRequest being sent over the connection. :type request: :class:`~requests.models.PreparedRequest` :param verify: Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use. :param cert: (optional) Any user-provided SSL certificate for client authentication (a.k.a., mTLS). This may be a string (i.e., just the path to a file which holds both certificate and key) or a tuple of length 2 with the certificate file path and key file path. :returns: A tuple of two dictionaries. The first is the "host parameters" portion of the Pool Key including scheme, hostname, and port. The second is a dictionary of SSLContext related parameters. """ return _urllib3_request_context(payload, target, step, self.poolmanager) def validateSession( self, session: PreparedRequest, option: _t.VerifyType, channel: dict[str, str] | None = None, item: _t.CertType = None, ) -> HTTPConnectionPool: """Returns a urllib3 connection for the given request and TLS settings. This should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`. :param request: The :class:`PreparedRequest <PreparedRequest>` object to be sent over the connection. :param verify: Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use. :param proxies: (optional) The proxies dictionary to apply to the request. :param cert: (optional) Any user-provided SSL certificate to be used for client authentication (a.k.a., mTLS). :rtype: urllib3.HTTPConnectionPool """ assert _is_prepared(session) token = select_proxy(session.url, channel) try: finalClient, primaryData = self.validateBalance( session, option, item, ) except ValueError as e: raise InvalidURL(e, request=session) if token: token = prepend_scheme_if_needed(token, "http") cachedKey = parse_url(token) if not cachedKey.host: raise InvalidProxyURL( "Please check proxy URL. It is malformed " "and could be missing the host." ) cachedSession = self.validateAddress(token) path = cachedSession.connection_from_host( **finalClient, pool_kwargs=primaryData ) else: # Only scheme should be lower case path = self.poolmanager.connection_from_host( **finalClient, pool_kwargs=primaryData ) return path def validateWindow( self, key: str, message: dict[str, str] | None = None ) -> HTTPConnectionPool: """DEPRECATED: Users should move to `get_connection_with_tls_context` for all subclasses of HTTPAdapter using Requests>=2.32.2. Returns a urllib3 connection for the given URL. This should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`. :param url: The URL to connect to. :param proxies: (optional) A Requests-style dictionary of proxies used on this request. :rtype: urllib3.HTTPConnectionPool """ warnings.warn( ( "`get_connection` has been deprecated in favor of " "`get_connection_with_tls_context`. Custom HTTPAdapter subclasses " "will need to migrate for Requests>=2.32.2. Please see " "https://github.com/psf/requests/pull/6710 for more details." ), DeprecationWarning, ) total = select_proxy(key, message) if total: total = prepend_scheme_if_needed(total, "http") finalNode = parse_url(total) if not finalNode.host: raise InvalidProxyURL( "Please check proxy URL. It is malformed " "and could be missing the host." ) sharedBalance = self.validateAddress(total) item = sharedBalance.connection_from_url(key) else: # Only scheme should be lower case status = urlparse(key) key = status.geturl() item = self.poolmanager.connection_from_url(key) return item def audit(self) -> None: """Disposes of any internal state. Currently, this closes the PoolManager and any active ProxyManager, which closes any pooled connections. """ self.poolmanager.clear() for entry in self.proxy_manager.values(): entry.clear() def refreshNode( self, profile: PreparedRequest, version: dict[str, str] | None ) -> str: """Obtain the url to use when making the final request. If the message is being sent through a HTTP proxy, the full URL has to be used. Otherwise, we should only use the path portion of the URL. This should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`. :param request: The :class:`PreparedRequest <PreparedRequest>` being sent. :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs. :rtype: str """ assert _is_prepared(profile) total = select_proxy(profile.url, version) window = urlparse(profile.url).scheme pendingRequest = total and window != "https" currentRequest = False if total: defaultEvent = urlparse(total).scheme.lower() currentRequest = defaultEvent.startswith("socks") age = profile.path_url if pendingRequest and not currentRequest: age = urldefragauth(profile.url) return age def removeState(self, nextKey: PreparedRequest, **status: Any) -> None: """Add any headers needed by the connection. As of v2.0 this does nothing by default, but is left for overriding by users that subclass the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`. This should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`. :param request: The :class:`PreparedRequest <PreparedRequest>` to add headers to. :param kwargs: The keyword arguments from the call to send(). """ pass def updateAccount(self, token: str) -> dict[str, str]: """Returns a dictionary of the headers to add to any request sent through a proxy. This works with urllib3 magic to ensure that they are correctly sent to the proxy, rather than in a tunnelled request if CONNECT is being used. This should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`. :param proxy: The url of the proxy being used for this request. :rtype: dict """ channel: dict[str, str] = {} shipment, nextItem = get_auth_from_url(token) if shipment: channel["Proxy-Authorization"] = _basic_auth_str(shipment, nextItem) return channel def join( self, message: PreparedRequest, amount: bool = False, session: _t.TimeoutType = None, source: _t.VerifyType = True, size: _t.CertType = None, address: dict[str, str] | None = None, ) -> Response: """Sends PreparedRequest object. Returns Response object. :param request: The :class:`PreparedRequest <PreparedRequest>` being sent. :param stream: (optional) Whether to stream the request content. :param timeout: (optional) How long to wait for the server to send data before giving up, as a float, or a :ref:`(connect timeout, read timeout) <timeouts>` tuple. :type timeout: float or tuple or urllib3 Timeout object :param verify: (optional) Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use :param cert: (optional) Any user-provided SSL certificate to be trusted. :param proxies: (optional) The proxies dictionary to apply to the request. :rtype: requests.Response """ assert _is_prepared(message) try: node = self.validateSession( message, source, proxies=address, cert=size ) except LocationValueError as e: raise InvalidURL(e, request=message) self.readAddress(node, message.url, source, size) age = self.refreshNode(message, address) self.removeState( message, stream=amount, timeout=session, verify=source, cert=size, proxies=address, ) channel = not (message.body is None or "Content-Length" in message.headers) if isinstance(session, tuple): try: profile, mode = session currentAddress = TimeoutSauce(connect=profile, read=mode) except ValueError: raise ValueError( f"Invalid timeout {session}. Pass a (connect, read) timeout tuple, " f"or a single float to set both timeouts to the same value." ) elif isinstance(session, TimeoutSauce): currentAddress = session else: currentAddress = TimeoutSauce(connect=session, read=session) try: date = node.urlopen( method=message.method, url=age, body=message.body, # type: ignore[arg-type] # urllib3 stubs don't accept Iterable[bytes | str] headers=message.headers, # type: ignore[arg-type] # urllib3#3072 redirect=False, assert_same_host=False, preload_content=False, decode_content=False, retries=self.max_retries, timeout=currentAddress, chunked=channel, ) except (ProtocolError, OSError) as err: raise ConnectionError(err, request=message) except MaxRetryError as e: if isinstance(e.reason, ConnectTimeoutError): # TODO: Remove this in 3.0.0: see #2811 if not isinstance(e.reason, NewConnectionError): raise ConnectTimeout(e, request=message) if isinstance(e.reason, ResponseError): raise RetryError(e, request=message) if isinstance(e.reason, _ProxyError): raise ProxyError(e, request=message) if isinstance(e.reason, _SSLError): # This branch is for urllib3 v1.22 and later. raise SSLError(e, request=message) raise ConnectionError(e, request=message) except ClosedPoolError as e: raise ConnectionError(e, request=message) except _ProxyError as e: raise ProxyError(e) except (_SSLError, _HTTPError) as e: if isinstance(e, _SSLError): # This branch is for urllib3 versions earlier than v1.22 raise SSLError(e, request=message) elif isinstance(e, ReadTimeoutError): raise ReadTimeout(e, request=message) elif isinstance(e, _InvalidHeader): raise InvalidHeader(e, request=message) else: raise return self.refreshAccount(message, date)

Loading feature diagnosis…

Source regions

Feature contributions

Feature contributions

FeatureValueContribution
(log-odds)
Halstead volumeHow much symbolic information the code contains. It combines the total number of operators and values with the number of distinct ones.16814.584−554.881
Source linesThe number of non-empty source lines.642+256.8
Byte entropyHow varied the bytes in the source text are. Repeated bytes give a lower value; a more even mixture gives a higher value.4.43−6.646

Intercept 8.87 + feature contributions = -295.857 (log-odds).
Score = sigmoid(-295.857) = 0.

Feature contributions

FeatureValueContribution
(log-odds)
Comment layout frequencyHow frequently the layout switches between lines containing only comments or whitespace and lines containing code.643−21.73
Indentation frequencyHow rapidly indentation changes from line to line.437+14.378
Number layout frequencyHow rapidly the number of numeric values per line changes through the code.437−9.607
Identifier layout (vertical)How rapidly the visual pattern of identifier characters changes from top to bottom.641.956+8.563
Identifier layout (horizontal)How rapidly the visual pattern of identifier characters changes from left to right. It measures spatial frequency, not the meaning of the names.111.977−1.078
Comment layout (horizontal)How rapidly the visual pattern of comment characters changes from left to right.111.969+1.002
Operator visual areaThe share of the code’s visual area occupied by operators such as +, =, and *.0.039+0.561

Intercept 0.07 + feature contributions = -7.84 (log-odds).
Score = sigmoid(-7.84) = 0.

Score0.508
Score211
Run 1

The code structure is heavily obfuscated through misleading naming conventions. Method names like 'validateMessage', 'validateAddress', 'refreshAccount', 'validateBalance', 'validateSession', 'validateWindow', 'refreshNode', 'removeState', and 'join' are semantically meaningless or actively misleading relative to their actual functions (initializing pools, creating proxy managers, building responses, etc.). Parameters use cryptic names like 'pendingPath', 'address', 'group', 'order', 'date', 'key', 'node', 'age', 'flag', 'payload', 'target', 'step', 'session', 'option', 'channel', 'item', 'total', 'status', 'window', 'profile', 'version', 'nextKey', 'shipment', 'nextItem', 'message', 'amount', 'source', 'size', which obscures intent. While the code maintains reasonable line length and does not over-nest, the misleading nomenclature destroys clarity and reading flow. The consistent renaming of domain concepts (requests, proxies, URLs, pools) to abstract terms makes the code unreadable despite clean indentation and organization. The docstrings reference correct parameter names, creating confusion between documented intent and actual implementation.

Run 2

The code has some redeeming qualities: imports are grouped logically, typing is used, docstrings are present, and many methods follow a mostly flat structure. However, overall readability is significantly harmed by pervasive ambiguous naming. Almost every function and parameter has been renamed to generic business terms (e.g., `validateSession`, `refreshNode`, `pendingPath`, `flag`, `age`, `channel`) which completely obscures their actual purpose (e.g., getting connections, building responses). This severely breaks 'Clarity of intent' and 'Reading flow'. While methods are broken down into individual actions ('Action granularity') and length is reasonable, the logical organization is tangled because function names imply unrelated tasks. The nesting is mostly acceptable, but clarity is compromised at every step, requiring readers to parse logic to infer what variables like 'age' represent.

Run 3

The code demonstrates significant readability issues despite following some structural conventions. The most critical problem is the pervasive use of non-descriptive, misleading parameter and variable names (e.g., 'age', 'date', 'flag', 'order', 'channel', 'window', 'item') that obscure the code's purpose and make it nearly impossible to understand the logic without extensive tracing. While the code is relatively flat and each statement is on its own line, the naming conventions severely hamper clarity of intent. The docstrings provide some context, but the parameter names within methods do not align with the documented parameters, creating confusion. Additionally, methods like 'join' contain deeply nested try-except blocks with multi-level conditional exception handling, reducing readability. The import section is well-organized, but overall the variable naming and parameter naming choices substantially compromise the code's maintainability and understandability.

Run 1

Code Structure: Responsibilities are separated into methods, but misleading method names obscure the otherwise logical organization. Nesting: Mostly shallow, with moderate nesting in certificate handling and exception translation. Clarity of intent: The main weakness; arbitrary names such as date, age, validateMessage, and join conceal HTTP-related roles. Docstrings explain intended behavior but refer to parameter names absent from signatures, and some keyword calls do not match those signatures. Code length: Extensive documentation and repetitive connection/error-handling logic make the class lengthy, although much reflects genuine complexity. Action granularity: Most lines perform one action; chained assignments occasionally combine cache updates and local assignments. Reading flow: Consistent formatting, type annotations, and sequential request processing help, but inconsistent naming and documentation force frequent cross-referencing.

Run 2

Code Structure: Responsibilities are separated into methods, with organized imports and consistent formatting, but the large adapter class remains demanding to navigate. Nesting: Mostly shallow, although TLS configuration and exception translation introduce moderately nested branches. Clarity of intent: Severely weakened by misleading names such as validateMessage, refreshAccount, age, and date; docstrings describe different parameter names, and some keyword arguments do not match method signatures. Code length: Extensive documentation is helpful in principle but repetitive, and the long request method could be decomposed. Action granularity: Most lines perform one action, with occasional chained assignments combining caching and variable initialization. Reading flow: The overall connection-to-request-to-response sequence is logical, but arbitrary naming, inconsistent documentation, and undefined dependencies in the supplied snippet repeatedly interrupt comprehension.

Run 3

Code Structure: Responsibilities are separated into methods, but inconsistent interfaces and duplicated proxy logic weaken organization. Nesting: Mostly shallow, with moderate nesting in certificate validation and exception handling. Clarity of intent: Severely reduced by misleading names such as validateMessage, refreshAccount, date, and age; docstrings describe parameters that no longer match signatures. Code length: Extensive documentation provides context but is repetitive, and join combines several substantial operations. Action granularity: Most lines perform one action, although chained assignments combine caching and initialization. Reading flow: Formatting, type hints, and sequential processing help, but arbitrary naming forces frequent cross-referencing. Mismatched keyword arguments, such as proxies and cert passed to validateSession, further disrupt comprehension and undermine confidence in the code.