Repository navigation
Conversation
orsenthil
force-pushed
the
gh-88037-port-validation
branch
from
October 10, 2026 15:24
802a54b to
24fb8bf
Compare
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.
Currently
urlsplit()/urlparse()accept a netloc with an invalid port without complaint, the error only surfaces later, and only if something actually reads.port:This moves that validation to parse time, so
urlsplit()/urlparse()raise immediately on a malformed port instead of returning a result that looks valid until.porthappens to be accessed.Supersedes #25774 - Rebase of the old branch wasn't suitable:
Lib/urllib/parse.pyhas seen significant changes since 2021 (WHATWG-compliance work, bracketed IPv6 host validation) that the original patch's diff didn't account for. The original commit is kept in-tact.I was curious on performance of moving this lazy validation to eager, I let the claude run in tiny benchmark.
Benchmark
Measured with
timeit(best of 5 runs) on a local./pythonbuildof this branch (
3.16.0a0), comparing the real, unmodifiedLib/urllib/parse.pyfrommain(lazy validation) against thisbranch's version (eager, parse-time validation).
_checknetlocin isolation (500k calls each):Public
urlsplit(), which is@functools.lru_cache-wrapped(128 entries):
Takeaways:
difference entirely — effectively zero added cost.
in absolute terms and negligible next to any I/O the parsed result is
normally used for (DNS/connect/TLS are milliseconds; this is
nanoseconds).