fix: normalize transport header extraction and status handling#11
Open
kvdhanush06 wants to merge 1 commit into
Open
fix: normalize transport header extraction and status handling#11kvdhanush06 wants to merge 1 commit into
kvdhanush06 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
Improves the Python SDK’s default urllib transport robustness by supporting more HTTP response shapes and normalizing headers for consistent downstream access.
Changes:
- Add fallback HTTP status resolution via
response.status,response.getcode(), andresponse.code. - Normalize response headers into a lower-cased dictionary using
.headers/.getheaders()extraction. - Remove reliance on response context-manager support and close responses when possible.
Comments suppressed due to low confidence (1)
python/runinfra/init.py:465
- The
urllib.error.HTTPErrorhandling path reads the body but never closes the underlying file/response object.HTTPErrortypically supports.close(), so skipping it can leak sockets/file descriptors on repeated errors. Mirror the success-path cleanup by callingexc.close()(if available) in afinallyafterexc.read()(including the read-error branch).
response_headers = _extract_headers(exc)
try:
body = exc.read()
except Exception as read_exc:
raise _TransportBodyReadError(
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
419
to
423
| while True: | ||
| chunk = response.readline() | ||
| chunk = getattr(response, "readline", lambda: b"")() | ||
| if not chunk: | ||
| break | ||
| yield chunk |
Comment on lines
+355
to
+359
| def _extract_status(obj: Any) -> int: | ||
| # Accept common shapes: .status, .getcode(), or .code | ||
| if hasattr(obj, "status"): | ||
| try: | ||
| return int(getattr(obj, "status")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes robustness issues in the Python SDK default transport when handling different HTTP response implementations.
This change:
.getcode()and.codeThe fix improves compatibility with
urllib-style responses, mocked transports, and mixed-case HTTP headers while preserving existing SDK behavior.Existing SDK response interfaces and downstream behavior remain backward compatible.
Type of change
Changes made
Transport status handling
Updated
_default_transport()to resolve HTTP status codes using:response.statusresponse.getcode()response.codeThis improves compatibility with:
urllibHeader normalization
Normalized all transport response headers into a lower-case dictionary:
This removes duplicated casing checks such as:
X-Request-Idx-request-idRetry-Afterretry-afterand ensures consistent downstream header access.
Response lifecycle handling
Removed direct reliance on:
The transport now:
.close()existsThe same compatibility and normalization logic was applied to
urllib.error.HTTPErrorhandling paths.Verification
Validated using:
getcode()getheaders().statusLocal verification commands:
All tests passed locally.
Checklist
anyin public surface; Python type hints + docstrings)pnpm testfor TS,python -m pytestfor Python)CHANGELOG.mdfiles (typescript/ and/or python/)Related issues
Closes #10