Repository navigation
gh-88037 - Move port validation logic to parsing time - #159122
Conversation
802a54b to
24fb8bf
Compare
d515b7f to
315747a
Compare
function where ":" should not be treated as port separator.
|
Thanks @orsenthil for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15. |
|
Sorry, @orsenthil, I could not cleanly backport this to Please backport manually with cherry_picker, see the devguide for more information. |
|
GH-161619 is a backport of this pull request to the 3.15 branch. |
|
Thanks @orsenthil for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14. |
|
Sorry, @orsenthil, I could not cleanly backport this to Please backport manually with cherry_picker, see the devguide for more information. |
|
3.14 Backport #161621 |
|
GH-161621 is a backport of this pull request to the 3.14 branch. |
…) (#161619) gh-88037 - Move port validation logic to parsing time (GH-159122) * Move port validation logic to parsing time * Fix CI. Handle the special case of file://C:\\ parsed by url2pathname function where ":" should not be treated as port separator. --------- (cherry picked from commit 403f6dd) Co-authored-by: Senthil Kumaran <senthil@python.org> Co-authored-by: Miguel Mendes <miguel.mdebrito@gmail.com>
#161621) gh-88037 - Move port validation logic to parsing time (#159122) * Move port validation logic to parsing time * Fix CI. Handle the special case of file://C:\\ parsed by url2pathname function where ":" should not be treated as port separator. --------- (cherry picked from commit 403f6dd) Co-authored-by: Miguel Mendes <miguel.mdebrito@gmail.com>
|
This change was made only to 3.16 - Even as this is a bug report, since there is a behavior difference in parsing, not porting it to older versions to prevent surprises. I have raised a pip issue to warn about this upcoming change pypa/pip#14438 in Python3.16 |
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. This in conformance with the RFC expectations.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).