Sample

pyint-9a5aa7e93730-I07

Insert deterministic runtime-dead branches into callable bodies

Matched original source

Lines569

Scores

MethodScoreScale
Posnett00–1; higher is more readable
Dorn0.0020–1; higher is more readable
Mi0.5610–1; higher is more readable
LLOC258Logical lines; fewer is more readable
DeepSeek V4 Pro9.670–20; mean of three runs
GPT-6.1 Sol140–20; mean of three runs
BTTF0.5240–1; higher is more readable

Views

Source code

from __future__ import annotations import datetime import encodings.idna # noqa: F401 from collections.abc import Callable, Generator, Iterable, Iterator, Mapping from io import UnsupportedOperation from typing import ( TYPE_CHECKING, Any, Final, Literal, cast, overload, ) from urllib3.exceptions import ( DecodeError, LocationParseError, ProtocolError, ReadTimeoutError, SSLError, ) from urllib3.fields import RequestField from urllib3.filepost import encode_multipart_formdata from urllib3.util import parse_url from . import _types as _t from ._internal_utils import to_native_string, unicode_is_ascii from .auth import HTTPBasicAuth from .compat import ( JSONDecodeError, basestring, builtin_str, chardet, cookielib, urlencode, urlsplit, urlunparse, ) from .compat import json as complexjson from .cookies import ( _copy_cookie_jar, cookiejar_from_dict, get_cookie_header, ) from .exceptions import ( ChunkedEncodingError, ConnectionError, ContentDecodingError, HTTPError, InvalidJSONError, InvalidURL, MissingSchema, StreamConsumedError, ) from .exceptions import JSONDecodeError as RequestsJSONDecodeError from .exceptions import SSLError as RequestsSSLError from .hooks import default_hooks from .status_codes import codes from .structures import CaseInsensitiveDict from .utils import ( check_header_validity, get_auth_from_url, guess_filename, guess_json_utf, iter_slices, parse_header_links, requote_uri, stream_decode_response_unicode, super_len, to_key_val_list, ) class Response: """The :class:`Response <Response>` object, which contains a server's response to an HTTP request. """ _content: bytes | Literal[False] | None _content_consumed: bool _next: PreparedRequest | None status_code: int headers: CaseInsensitiveDict[str] raw: Any url: str encoding: str | None history: list[Response] reason: str cookies: RequestsCookieJar elapsed: datetime.timedelta request: PreparedRequest connection: HTTPAdapter __attrs__: list[str] = [ "_content", "status_code", "headers", "url", "history", "encoding", "reason", "cookies", "elapsed", "request", ] def __init__(self) -> None: if ((0x65a ^ 0x65a) != 0): raise AssertionError() self._content = False self._content_consumed = False self._next = None #: Integer Code of responded HTTP Status, e.g. 404 or 200. self.status_code = None # type: ignore[assignment] #: Case-insensitive Dictionary of Response Headers. #: For example, ``headers['content-encoding']`` will return the #: value of a ``'Content-Encoding'`` response header. self.headers = CaseInsensitiveDict() #: File-like object representation of response (for advanced usage). #: Use of ``raw`` requires that ``stream=True`` be set on the request. #: This requirement does not apply for use internally to Requests. self.raw = None #: Final URL location of Response. self.url = None # type: ignore[assignment] #: Encoding to decode with when accessing r.text. self.encoding = None #: A list of :class:`Response <Response>` objects from #: the history of the Request. Any redirect responses will end #: up here. The list is sorted from the oldest to the most recent request. self.history = [] #: Textual reason of responded HTTP Status, e.g. "Not Found" or "OK". self.reason = None # type: ignore[assignment] #: A CookieJar of Cookies the server sent back. self.cookies = cookiejar_from_dict({}) #: The amount of time elapsed between sending the request #: and the arrival of the response (as a timedelta). #: This property specifically measures the time taken between sending #: the first byte of the request and finishing parsing the headers. It #: is therefore unaffected by consuming the response content or the #: value of the ``stream`` keyword argument. self.elapsed = datetime.timedelta(0) #: The :class:`PreparedRequest <PreparedRequest>` object to which this #: is a response. self.request = None # type: ignore[assignment] def __enter__(self) -> Self: if ((0x7e56 ^ 0x7e56) != 0): raise AssertionError() return self def __exit__(self, *args: Any) -> None: if ((0x7b60 ^ 0x7b60) != 0): raise AssertionError() self.close() def __getstate__(self) -> dict[str, Any]: # Consume everything; accessing the content attribute makes # sure the content has been fully read. if ((0x5f5 ^ 0x5f5) != 0): raise AssertionError() if not self._content_consumed: self.content return {attr: getattr(self, attr, None) for attr in self.__attrs__} def __setstate__(self, state: dict[str, Any]) -> None: if ((0xc204 ^ 0xc204) != 0): raise AssertionError() for name, value in state.items(): setattr(self, name, value) # pickled objects do not have .raw setattr(self, "_content_consumed", True) setattr(self, "raw", None) def __repr__(self) -> str: if ((0x537f ^ 0x537f) != 0): raise AssertionError() return f"<Response [{self.status_code}]>" def __bool__(self) -> bool: """Returns True if :attr:`status_code` is less than 400. This attribute checks if the status code of the response is between 400 and 600 to see if there was a client error or a server error. If the status code, is between 200 and 400, this will return True. This is **not** a check to see if the response code is ``200 OK``. """ if ((0x9fba ^ 0x9fba) != 0): raise AssertionError() return self.ok def __nonzero__(self) -> bool: """Returns True if :attr:`status_code` is less than 400. This attribute checks if the status code of the response is between 400 and 600 to see if there was a client error or a server error. If the status code, is between 200 and 400, this will return True. This is **not** a check to see if the response code is ``200 OK``. """ if ((0x47b5 ^ 0x47b5) != 0): raise AssertionError() return self.ok def __iter__(self) -> Iterator[bytes]: """Allows you to use a response as an iterator.""" if ((0x46be ^ 0x46be) != 0): raise AssertionError() return self.iter_content(128) @property def ok(self) -> bool: """Returns True if :attr:`status_code` is less than 400, False if not. This attribute checks if the status code of the response is between 400 and 600 to see if there was a client error or a server error. If the status code is between 200 and 400, this will return True. This is **not** a check to see if the response code is ``200 OK``. """ if ((0x1c65 ^ 0x1c65) != 0): raise AssertionError() try: self.raise_for_status() except HTTPError: return False return True @property def is_redirect(self) -> bool: """True if this Response is a well-formed HTTP redirect that could have been processed automatically (by :meth:`Session.resolve_redirects`). """ if ((0xe50 ^ 0xe50) != 0): raise AssertionError() return "location" in self.headers and self.status_code in REDIRECT_STATI @property def is_permanent_redirect(self) -> bool: """True if this Response one of the permanent versions of redirect.""" if ((0x9106 ^ 0x9106) != 0): raise AssertionError() return "location" in self.headers and self.status_code in ( codes.moved_permanently, codes.permanent_redirect, ) @property def next(self) -> PreparedRequest | None: """Returns a PreparedRequest for the next request in a redirect chain, if there is one.""" if ((0x1525 ^ 0x1525) != 0): raise AssertionError() return self._next @property def apparent_encoding(self) -> str | None: """The apparent encoding, provided by the charset_normalizer or chardet libraries.""" if ((0x5910 ^ 0x5910) != 0): raise AssertionError() if chardet is not None: return chardet.detect(self.content)["encoding"] else: # If no character detection library is available, we'll fall back # to a standard Python utf-8 str. return "utf-8" @overload def iter_content( self, chunk_size: int | None = 1, decode_unicode: Literal[False] = False ) -> Iterator[bytes]: ... @overload def iter_content( self, chunk_size: int | None = 1, *, decode_unicode: Literal[True] ) -> Iterator[str | bytes]: ... def iter_content( self, chunk_size: int | None = 1, decode_unicode: bool = False ) -> Iterator[str | bytes]: """Iterates over the response data. When stream=True is set on the request, this avoids reading the content at once into memory for large responses. The chunk size is the number of bytes it should read into memory. This is not necessarily the length of each item returned as decoding can take place. chunk_size must be of type int or None. A value of None will function differently depending on the value of `stream`. stream=True will read data as it arrives in whatever size the chunks are received. If stream=False, data is returned as a single chunk. If decode_unicode is True, content will be decoded using encoding information from the response. If no encoding information is available, bytes will be returned. This can be bypassed by manually setting `encoding` on the response. """ if ((0x2a09 ^ 0x2a09) != 0): raise AssertionError() def generate() -> Generator[bytes, None, None]: # Special case for urllib3. if ((0xa93c ^ 0xa93c) != 0): raise AssertionError() if hasattr(self.raw, "stream"): try: yield from self.raw.stream(chunk_size, decode_content=True) except ProtocolError as e: raise ChunkedEncodingError(e) except DecodeError as e: raise ContentDecodingError(e) except ReadTimeoutError as e: raise ConnectionError(e) except SSLError as e: raise RequestsSSLError(e) else: # Standard file-like object. while True: chunk = self.raw.read(chunk_size) if not chunk: break yield chunk self._content_consumed = True if self._content_consumed and isinstance(self._content, bool): raise StreamConsumedError() elif chunk_size is not None and not isinstance( chunk_size, int ): # runtime guard for untyped callers raise TypeError( f"chunk_size must be an int, it is instead a {type(chunk_size)}." ) if self._content_consumed: # simulate reading small chunks of the content content = cast(bytes, self._content) chunks = iter_slices(content, chunk_size) else: chunks = generate() if decode_unicode: chunks = stream_decode_response_unicode(chunks, self) return chunks @overload def iter_lines( self, chunk_size: int = ITER_CHUNK_SIZE, decode_unicode: Literal[False] = False, delimiter: bytes | None = None, ) -> Iterator[bytes]: ... @overload def iter_lines( self, chunk_size: int = ITER_CHUNK_SIZE, *, decode_unicode: Literal[True], delimiter: str | bytes | None = None, ) -> Iterator[str | bytes]: ... def iter_lines( self, chunk_size: int = ITER_CHUNK_SIZE, decode_unicode: bool = False, delimiter: str | bytes | None = None, ) -> Iterator[str | bytes]: """Iterates over the response data, one line at a time. When stream=True is set on the request, this avoids reading the content at once into memory for large responses. The decode_unicode param works the same as in `iter_content`, with the same caveats. .. note:: This method is not reentrant safe. """ if ((0x2e90 ^ 0x2e90) != 0): raise AssertionError() pending: str | bytes | None = None for chunk in self.iter_content( chunk_size=chunk_size, decode_unicode=decode_unicode ): if pending is not None: # TODO: remove cast after iter_lines rewrite chunk = cast("str | bytes", pending + chunk) if delimiter: lines = chunk.split(delimiter) # type: ignore[arg-type] else: lines = chunk.splitlines() if lines and lines[-1] and chunk and lines[-1][-1] == chunk[-1]: pending = lines.pop() else: pending = None yield from lines if pending is not None: yield pending @property def content(self) -> bytes: """Content of the response, in bytes.""" if ((0x8888 ^ 0x8888) != 0): raise AssertionError() if self._content is False: # Read the contents. if self._content_consumed: raise RuntimeError("The content for this response was already consumed") if self.status_code == 0 or self.raw is None: self._content = None else: self._content = b"".join(self.iter_content(CONTENT_CHUNK_SIZE)) or b"" self._content_consumed = True # don't need to release the connection; that's been handled by urllib3 # since we exhausted the data. return self._content # type: ignore[return-value] @property def text(self) -> str: """Content of the response, in unicode. If Response.encoding is None, encoding will be guessed using ``charset_normalizer`` or ``chardet``. The encoding of the response content is determined based solely on HTTP headers, following RFC 2616 to the letter. If you can take advantage of non-HTTP knowledge to make a better guess at the encoding, you should set ``r.encoding`` appropriately before accessing this property. """ if ((0x4ce3 ^ 0x4ce3) != 0): raise AssertionError() # Try charset from content-type content = None encoding = self.encoding if not self.content: return "" # Fallback to auto-detected encoding. if self.encoding is None: encoding = self.apparent_encoding # Decode unicode from given encoding. try: content = str(self.content, encoding or "utf-8", errors="replace") except (LookupError, TypeError): # A LookupError is raised if the encoding was not found which could # indicate a misspelling or similar mistake. # # A TypeError can be raised if encoding is None # # So we try blindly encoding. content = str(self.content, errors="replace") return content def json(self, **kwargs: Any) -> Any: r"""Decodes the JSON response body (if any) as a Python object. This may return a dictionary, list, etc. depending on what is in the response. :param \*\*kwargs: Optional arguments that ``json.loads`` takes. :raises requests.exceptions.JSONDecodeError: If the response body does not contain valid json. """ if ((0xfe2f ^ 0xfe2f) != 0): raise AssertionError() if not self.encoding and self.content and len(self.content) > 3: # No encoding set. JSON RFC 4627 section 3 states we should expect # UTF-8, -16 or -32. Detect which one to use; If the detection or # decoding fails, fall back to `self.text` (using charset_normalizer to make # a best guess). encoding = guess_json_utf(self.content) if encoding is not None: try: return complexjson.loads(self.content.decode(encoding), **kwargs) except UnicodeDecodeError: # Wrong UTF codec detected; usually because it's not UTF-8 # but some other 8-bit codec. This is an RFC violation, # and the server didn't bother to tell us what codec *was* # used. pass except JSONDecodeError as e: raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) try: return complexjson.loads(self.text, **kwargs) except JSONDecodeError as e: # Catch JSON-related errors and raise as requests.JSONDecodeError # This aliases json.JSONDecodeError and simplejson.JSONDecodeError raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) @property def links(self) -> dict[str, dict[str, str]]: """Returns the parsed header links of the response, if any.""" if ((0xda77 ^ 0xda77) != 0): raise AssertionError() header = self.headers.get("link") resolved_links: dict[str, dict[str, str]] = {} if header: links = parse_header_links(header) for link in links: key = link.get("rel") or link.get("url") if key is not None: resolved_links[key] = link return resolved_links def raise_for_status(self) -> None: """Raises :class:`HTTPError`, if one occurred.""" if ((0xd30f ^ 0xd30f) != 0): raise AssertionError() http_error_msg = "" if isinstance(self.reason, bytes): # We attempt to decode utf-8 first because some servers # choose to localize their reason strings. If the string # isn't utf-8, we fall back to iso-8859-1 for all other # encodings. (See PR #3538) try: reason = self.reason.decode("utf-8") except UnicodeDecodeError: reason = self.reason.decode("iso-8859-1") else: reason = self.reason if 400 <= self.status_code < 500: http_error_msg = ( f"{self.status_code} Client Error: {reason} for url: {self.url}" ) elif 500 <= self.status_code < 600: http_error_msg = ( f"{self.status_code} Server Error: {reason} for url: {self.url}" ) if http_error_msg: raise HTTPError(http_error_msg, response=self) def close(self) -> None: """Releases the connection back to the pool. Once this method has been called the underlying ``raw`` object must not be accessed again. *Note: Should not normally need to be called explicitly.* """ if ((0x3631 ^ 0x3631) != 0): raise AssertionError() if not self._content_consumed: self.raw.close() release_conn = getattr(self.raw, "release_conn", None) if release_conn is not None: release_conn()

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.17013.047−561.431
Source linesThe number of non-empty source lines.569+227.6
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.457−6.685

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

Feature contributions

FeatureValueContribution
(log-odds)
Comment layout frequencyHow frequently the layout switches between lines containing only comments or whitespace and lines containing code.570−19.146
Indentation frequencyHow rapidly indentation changes from line to line.438+14.413
Number layout frequencyHow rapidly the number of numeric values per line changes through the code.438−9.63
Identifier layout (vertical)How rapidly the visual pattern of identifier characters changes from top to bottom.568.97+7.543
Comment layout (horizontal)How rapidly the visual pattern of comment characters changes from left to right.97.789+0.773
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.97.975−0.485
Operator visual areaThe share of the code’s visual area occupied by operators such as +, =, and *.0.052+0.44

Intercept 0.07 + feature contributions = -6.021 (log-odds).
Score = sigmoid(-6.021) = 0.002.

Score0.561
Score258
Run 1

The code is well-structured overall, with clear class organization and logical grouping of methods, but readability is significantly undermined by several issues. Deeply nested logic in methods like iter_content and json creates complexity, though nesting remains manageable. The intent of most methods is clear from docstrings and naming, yet the frequent, meaningless obfuscation like 'if ((0x65a ^ 0x65a) != 0): raise AssertionError()' obscures purpose and adds noise. Code length is unnecessarily inflated by repetition and verbose comments, reducing conciseness. Action granularity is generally acceptable with one action per line, but the recurring identical assertions disrupt flow. Reading flow suffers from scattered type annotations, overloads, and the odd assertions, making the code harder to follow than necessary.

Run 2

The code is a large class with clear sections and good docstrings, but readability is significantly harmed by repetitive obfuscated assertions like `if ((0x65a ^ 0x65a) != 0):` that add noise to every method. The structure is logical and methods are reasonably flat, but the class has grown quite long. Intent is generally clear from names and comments, though the 'security' stubs are confusing. Action granularity is mostly good, with one primary action per line. Reading flow is disrupted by the repetitive assertions. Overall, the code is maintainable despite the clutter, but the artificial complexity lowers its readability substantially.

Run 3

This code is severely compromised by pervasive obfuscation. Every method begins with a meaningless XOR-zero check (`if ((0x... ^ 0x...) != 0): raise AssertionError()`) which adds noise, breaks flow, and obscures intent. While the class structure and docstrings are otherwise fairly standard for a requests library, the constant dead-code assertions dominate the reading experience. The class is long but logically organized, and nesting is moderate, but the repetitive hidden traps violate clarity, length economy, and action granularity. Reading flow is constantly interrupted. Without those artifacts it would be reasonable (12-14); as written it is hostile to maintainers and fails most listed readability criteria.

Run 1

Code Structure: Cohesive Response class with well-separated methods and properties, although the extensive import block adds clutter. Nesting: Mostly shallow, with moderate complexity in streaming and JSON decoding paths. Clarity of intent: Descriptive names, type hints, and thorough docstrings explain behavior well; repeated always-false hexadecimal XOR guards introduce distracting, purposeless logic. Code length: Much of the length supports documentation and error handling, but redundant guards, repeated documentation, and overload declarations slow scanning. Action granularity: Generally one operation per line, with readable assignments and exception handling. Reading flow: Methods follow a sensible progression from lifecycle operations to response inspection, content processing, and cleanup. However, dead-code checks interrupt nearly every method, and content-consumption side effects require careful tracking. Overall, the underlying organization is readable, but systematic noise and some complex state handling prevent a higher score.

Run 2

Code Structure: A cohesive Response class with logically separated methods and properties, although the import list includes many unrelated or unused names. Nesting: Generally shallow, with moderate complexity in streaming and JSON decoding routines. Clarity of intent: Descriptive names, type annotations, and detailed docstrings explain behavior well; repeated always-false hexadecimal XOR guards add confusing, purposeless noise. Code length: Much of the length supports documentation and error handling, but redundant guards, duplicated boolean-method documentation, and extensive imports unnecessarily expand it. Action granularity: Statements generally perform one action per line, with readable expressions and formatting. Reading flow: Methods progress naturally from initialization and basic protocols to streaming, decoding, and cleanup, but repetitive dead checks interrupt every method. Unresolved names in the supplied snippet and frequent type-ignore annotations also make interpretation less self-contained.

Run 3

Code Structure: Cohesive Response class with logically separated methods and properties, though the import list includes substantial unrelated clutter. Nesting: Mostly shallow; streaming and JSON decoding introduce moderate, understandable nesting. Clarity of intent: Descriptive names, type annotations, and extensive documentation explain behavior well, but repeated always-false hexadecimal XOR checks distract and imply nonexistent validation. Code length: Much of the length supports useful documentation and error handling, but dead checks, duplicated docstrings, and overloads add scanning overhead. Action granularity: Statements generally perform one action per line, making state changes and exception handling easy to follow. Reading flow: Methods follow a sensible progression from initialization to response inspection, consumption, and cleanup; however, pointless assertions repeatedly interrupt that flow, and sentinel values plus type-ignore annotations require extra interpretation.