Mercurial > hg
annotate mercurial/urllibcompat.py @ 52222:2cdd3e85abf7 stable
tests: drop PYTHONPATH manipulation in test-pushvars.t
It does not seems to serve any purpose and confuses Windows.
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Mon, 11 Nov 2024 12:52:53 +0100 |
parents | f4733654f144 |
children |
rev | line source |
---|---|
34465
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
1 # urllibcompat.py - adapters to ease using urllib2 on Py2 and urllib on Py3 |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
2 # |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
3 # Copyright 2017 Google, Inc. |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
4 # |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
6 # GNU General Public License version 2 or any later version. |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
7 |
51863
f4733654f144
typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents:
51830
diff
changeset
|
8 from __future__ import annotations |
f4733654f144
typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents:
51830
diff
changeset
|
9 |
48896
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
10 import http.server |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
11 import urllib.error |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
12 import urllib.parse |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
13 import urllib.request |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
14 import urllib.response |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
15 |
34465
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
16 from . import pycompat |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
17 |
34467
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
18 _sysstr = pycompat.sysstr |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
19 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40159
diff
changeset
|
20 |
48946
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48896
diff
changeset
|
21 class _pycompatstub: |
34467
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
22 def __init__(self): |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
23 self._aliases = {} |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
24 |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
25 def _registeraliases(self, origin, items): |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
26 """Add items that will be populated at the first access""" |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
27 items = map(_sysstr, items) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
28 self._aliases.update( |
43506
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43089
diff
changeset
|
29 (item.replace('_', '').lower(), (origin, item)) for item in items |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40159
diff
changeset
|
30 ) |
34467
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
31 |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
32 def _registeralias(self, origin, attr, name): |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
33 """Alias ``origin``.``attr`` as ``name``""" |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
34 self._aliases[_sysstr(name)] = (origin, _sysstr(attr)) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
35 |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
36 def __getattr__(self, name): |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
37 try: |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
38 origin, item = self._aliases[name] |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
39 except KeyError: |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
40 raise AttributeError(name) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
41 self.__dict__[name] = obj = getattr(origin, item) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
42 return obj |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
43 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40159
diff
changeset
|
44 |
34467
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
45 httpserver = _pycompatstub() |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
46 urlreq = _pycompatstub() |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
47 urlerr = _pycompatstub() |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
48 |
48896
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
49 urlreq._registeraliases( |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
50 urllib.parse, |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
51 ( |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
52 b"splitattr", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
53 b"splitpasswd", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
54 b"splitport", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
55 b"splituser", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
56 b"urlparse", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
57 b"urlunparse", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
58 ), |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
59 ) |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
60 urlreq._registeralias(urllib.parse, b"parse_qs", b"parseqs") |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
61 urlreq._registeralias(urllib.parse, b"parse_qsl", b"parseqsl") |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
62 urlreq._registeralias(urllib.parse, b"unquote_to_bytes", b"unquote") |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40159
diff
changeset
|
63 |
48896
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
64 urlreq._registeraliases( |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
65 urllib.request, |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
66 ( |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
67 b"AbstractHTTPHandler", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
68 b"BaseHandler", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
69 b"build_opener", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
70 b"FileHandler", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
71 b"FTPHandler", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
72 b"ftpwrapper", |
51830
208698117124
http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents:
51703
diff
changeset
|
73 b"HTTPCookieProcessor", |
48896
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
74 b"HTTPHandler", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
75 b"HTTPSHandler", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
76 b"install_opener", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
77 b"pathname2url", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
78 b"HTTPBasicAuthHandler", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
79 b"HTTPDigestAuthHandler", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
80 b"HTTPPasswordMgrWithDefaultRealm", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
81 b"ProxyHandler", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
82 b"Request", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
83 b"url2pathname", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
84 b"urlopen", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
85 ), |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
86 ) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40159
diff
changeset
|
87 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40159
diff
changeset
|
88 |
48896
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
89 urlreq._registeraliases( |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
90 urllib.response, |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
91 ( |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
92 b"addclosehook", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
93 b"addinfourl", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
94 ), |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
95 ) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40159
diff
changeset
|
96 |
48896
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
97 urlerr._registeraliases( |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
98 urllib.error, |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
99 ( |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
100 b"HTTPError", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
101 b"URLError", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
102 ), |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
103 ) |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
104 |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
105 httpserver._registeraliases( |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
106 http.server, |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
107 ( |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
108 b"HTTPServer", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
109 b"BaseHTTPRequestHandler", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
110 b"SimpleHTTPRequestHandler", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
111 b"CGIHTTPRequestHandler", |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
112 ), |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
113 ) |
46785
521ac0d7047f
typing: disable import error warnings that are already handled
Matt Harbison <matt_harbison@yahoo.com>
parents:
45942
diff
changeset
|
114 |
51703
ca7bde5dbafb
black: format the codebase with 23.3.0
Raphaël Gomès <rgomes@octobus.net>
parents:
51700
diff
changeset
|
115 |
48896
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
116 # urllib.parse.quote() accepts both str and bytes, decodes bytes |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
117 # (if necessary), and returns str. This is wonky. We provide a custom |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
118 # implementation that only accepts bytes and emits bytes. |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
119 def quote(s, safe='/'): |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
120 # bytestr has an __iter__ that emits characters. quote_from_bytes() |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
121 # does an iteration and expects ints. We coerce to bytes to appease it. |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
122 if isinstance(s, pycompat.bytestr): |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
123 s = bytes(s) |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
124 s = urllib.parse.quote_from_bytes(s, safe=safe) |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
125 return s.encode('ascii', 'strict') |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
126 |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
127 |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
128 # urllib.parse.urlencode() returns str. We use this function to make |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
129 # sure we return bytes. |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
130 def urlencode(query, doseq=False): |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
131 s = urllib.parse.urlencode(query, doseq=doseq) |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
132 return s.encode('ascii') |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
133 |
34465
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
134 |
48896
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
135 urlreq.quote = quote |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
136 urlreq.urlencode = urlencode |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
137 |
34465
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
138 |
48896
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
139 def getfullurl(req): |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
140 return req.full_url |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
141 |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
142 |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
143 def gethost(req): |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
144 return req.host |
34465
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
145 |
48896
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
146 |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
147 def getselector(req): |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
148 return req.selector |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
149 |
34465
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
150 |
48896
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
151 def getdata(req): |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
152 return req.data |
34465
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
153 |
48896
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
154 |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
155 def hasdata(req): |
4286ec1d9842
urllibcompat: remove Python 2 support code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
156 return req.data is not None |