GH-125866: RFC8089 file URIs in urllib.request
#126148
Closed
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.
Adjust
urllib.request.pathname2url()andurl2pathname()to generate and accept file URIs as described in RFC8089.pathname2url()gains a new include_scheme argument, which defaults to false. When set to true, the returned URL includes afile:prefix.url2pathname()now automatically removes afile:prefix if present.On Windows,
pathname2url()now generates URIs that begin with two slashes rather than four when given a UNC path.On other platforms,
pathname2url()now generates URIs that begin with three slashes rather than one when given an absolute path.url2pathname()now performs the opposite transformation, sofile:///etc/hostsbecomes/etc/hosts. Furthermore,url2pathname()now ignores local hosts (like "localhost" or any alias) and raisesURLErrorfor non-local hosts.pathname2url()examples (star marks differences):pathname2url('foo/bar')foo/barfoo/barpathname2url('/foo/bar')/foo/bar///foo/bar*pathname2url('//foo/bar')(POSIX)//foo/bar////foo/bar*pathname2url('//foo/bar')(Windows)////foo/bar//foo/bar*url2pathname()examples (star marks differences):url2pathname('foo/bar')foo/barfoo/barurl2pathname('/foo/bar')/foo/bar/foo/barurl2pathname('//foo/bar')(POSIX)//foo/barraise URLError*url2pathname('//foo/bar')(Windows)//foo/bar//foo/barurl2pathname('///foo/bar')///foo/bar/foo/bar*url2pathname('////foo/bar')(POSIX)////foo/bar//foo/bar*url2pathname('////foo/bar')(Windows)//foo/bar//foo/barurl2pathname('/////foo/bar')(Windows)///foo/bar//foo/bar*urllib.request#125866📚 Documentation preview 📚: https://cpython-previews--126148.org.readthedocs.build/