Convert a URI to an IRI. All valid UTF-8 characters are unquoted, leaving all reserved and invalid characters quoted. If the URL has a domain, it is decoded from Punycode. >>> uri_to_iri("http://xn--n3h.net/p%C3%A5th?q=%C3%A8ry%DF") 'http://\\u2603.net/p\\xe5th?q=\\xe8ry%DF' :p
(uri: str)
| 59 | |
| 60 | |
| 61 | def uri_to_iri(uri: str) -> str: |
| 62 | """Convert a URI to an IRI. All valid UTF-8 characters are unquoted, |
| 63 | leaving all reserved and invalid characters quoted. If the URL has |
| 64 | a domain, it is decoded from Punycode. |
| 65 | |
| 66 | >>> uri_to_iri("http://xn--n3h.net/p%C3%A5th?q=%C3%A8ry%DF") |
| 67 | 'http://\\u2603.net/p\\xe5th?q=\\xe8ry%DF' |
| 68 | |
| 69 | :param uri: The URI to convert. |
| 70 | |
| 71 | .. versionchanged:: 3.0 |
| 72 | Passing a tuple or bytes, and the ``charset`` and ``errors`` parameters, |
| 73 | are removed. |
| 74 | |
| 75 | .. versionchanged:: 2.3 |
| 76 | Which characters remain quoted is specific to each part of the URL. |
| 77 | |
| 78 | .. versionchanged:: 0.15 |
| 79 | All reserved and invalid characters remain quoted. Previously, |
| 80 | only some reserved characters were preserved, and invalid bytes |
| 81 | were replaced instead of left quoted. |
| 82 | |
| 83 | .. versionadded:: 0.6 |
| 84 | """ |
| 85 | parts = urlsplit(uri) |
| 86 | path = _unquote_path(parts.path) |
| 87 | query = _unquote_query(parts.query) |
| 88 | fragment = _unquote_fragment(parts.fragment) |
| 89 | |
| 90 | if parts.hostname: |
| 91 | netloc = _decode_idna(parts.hostname) |
| 92 | else: |
| 93 | netloc = "" |
| 94 | |
| 95 | if ":" in netloc: |
| 96 | netloc = f"[{netloc}]" |
| 97 | |
| 98 | if parts.port: |
| 99 | netloc = f"{netloc}:{parts.port}" |
| 100 | |
| 101 | if parts.username: |
| 102 | auth = _unquote_user(parts.username) |
| 103 | |
| 104 | if parts.password: |
| 105 | password = _unquote_user(parts.password) |
| 106 | auth = f"{auth}:{password}" |
| 107 | |
| 108 | netloc = f"{auth}@{netloc}" |
| 109 | |
| 110 | return urlunsplit((parts.scheme, netloc, path, query, fragment)) |
| 111 | |
| 112 | |
| 113 | def iri_to_uri(iri: str) -> str: |
no test coverage detected