diff options
Diffstat (limited to 'Lib/urllib/parse.py')
-rw-r--r-- | Lib/urllib/parse.py | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index abe5d0d868..975c6ffb9c 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -182,10 +182,10 @@ class _NetlocResultMixinStr(_NetlocResultMixinBase, _ResultMixinStr): _, have_open_br, bracketed = hostinfo.partition('[') if have_open_br: hostname, _, port = bracketed.partition(']') - _, have_port, port = port.partition(':') + _, _, port = port.partition(':') else: - hostname, have_port, port = hostinfo.partition(':') - if not have_port: + hostname, _, port = hostinfo.partition(':') + if not port: port = None return hostname, port @@ -212,10 +212,10 @@ class _NetlocResultMixinBytes(_NetlocResultMixinBase, _ResultMixinBytes): _, have_open_br, bracketed = hostinfo.partition(b'[') if have_open_br: hostname, _, port = bracketed.partition(b']') - _, have_port, port = port.partition(b':') + _, _, port = port.partition(b':') else: - hostname, have_port, port = hostinfo.partition(b':') - if not have_port: + hostname, _, port = hostinfo.partition(b':') + if not port: port = None return hostname, port @@ -728,7 +728,7 @@ def quote_from_bytes(bs, safe='/'): return ''.join([quoter(char) for char in bs]) def urlencode(query, doseq=False, safe='', encoding=None, errors=None): - """Encode a sequence of two-element tuples or dictionary into a URL query string. + """Encode a dict or sequence of two-element tuples into a URL query string. If any values in the query arg are sequences and doseq is true, each sequence element is converted to a separate parameter. @@ -737,9 +737,9 @@ def urlencode(query, doseq=False, safe='', encoding=None, errors=None): parameters in the output will match the order of parameters in the input. - The query arg may be either a string or a bytes type. When query arg is a - string, the safe, encoding and error parameters are sent the quote_plus for - encoding. + The components of a query arg may each be either a string or a bytes type. + When a component is a string, the safe, encoding and error parameters are + sent to the quote_plus function for encoding. """ if hasattr(query, "items"): @@ -903,10 +903,13 @@ def splitport(host): global _portprog if _portprog is None: import re - _portprog = re.compile('^(.*):([0-9]+)$') + _portprog = re.compile('^(.*):([0-9]*)$') match = _portprog.match(host) - if match: return match.group(1, 2) + if match: + host, port = match.groups() + if port: + return host, port return host, None _nportprog = None @@ -923,12 +926,12 @@ def splitnport(host, defport=-1): match = _nportprog.match(host) if match: host, port = match.group(1, 2) - try: - if not port: raise ValueError("no digits") - nport = int(port) - except ValueError: - nport = None - return host, nport + if port: + try: + nport = int(port) + except ValueError: + nport = None + return host, nport return host, defport _queryprog = None |