diff options
author | Marcel Hellkamp <marc@gsites.de> | 2020-01-01 17:08:00 +0100 |
---|---|---|
committer | Marcel Hellkamp <marc@gsites.de> | 2020-01-01 17:11:37 +0100 |
commit | 337384fd7345e4a801c5c6b852db1800a756f211 (patch) | |
tree | f4fd6eb02debd59bec2e436766482a95b6850bb1 | |
parent | 556ec241464397117af67394a367bf6c0b994547 (diff) | |
download | bottle-337384fd7345e4a801c5c6b852db1800a756f211.tar.gz |
Removed most uses of tob() or touni()
-rwxr-xr-x | bottle.py | 10 | ||||
-rwxr-xr-x | test/test_environ.py | 42 | ||||
-rw-r--r-- | test/test_fileupload.py | 4 | ||||
-rw-r--r-- | test/test_formsdict.py | 15 | ||||
-rw-r--r-- | test/test_jinja2.py | 6 | ||||
-rw-r--r-- | test/test_mako.py | 4 | ||||
-rwxr-xr-x | test/test_outputfilter.py | 26 | ||||
-rw-r--r-- | test/test_securecookies.py | 6 | ||||
-rwxr-xr-x | test/test_sendfile.py | 2 | ||||
-rw-r--r-- | test/test_server.py | 6 | ||||
-rwxr-xr-x | test/test_stpl.py | 16 | ||||
-rwxr-xr-x | test/test_wsgi.py | 5 | ||||
-rwxr-xr-x | test/tools.py | 2 |
13 files changed, 72 insertions, 72 deletions
@@ -1289,7 +1289,7 @@ class BaseRequest(object): @staticmethod def _iter_chunked(read, bufsize): err = HTTPError(400, 'Error while parsing chunked transfer body.') - rn, sem, bs = tob('\r\n'), tob(';'), tob('') + rn, sem, bs = b'\r\n', b';', b'' while True: header = read(1) while header[-2:] != rn: @@ -1839,7 +1839,7 @@ class BaseResponse(object): encoded = base64.b64encode(pickle.dumps([name, value], -1)) sig = base64.b64encode(hmac.new(tob(secret), encoded, digestmod=digestmod).digest()) - value = touni(tob('!') + sig + tob('?') + encoded) + value = touni(b'!' + sig + b'?' + encoded) elif not isinstance(value, basestring): raise TypeError('Secret key required for non-string cookies.') @@ -3017,7 +3017,7 @@ def cookie_encode(data, key, digestmod=None): digestmod = digestmod or hashlib.sha256 msg = base64.b64encode(pickle.dumps(data, -1)) sig = base64.b64encode(hmac.new(tob(key), msg, digestmod=digestmod).digest()) - return tob('!') + sig + tob('?') + msg + return b'!' + sig + b'?' + msg def cookie_decode(data, key, digestmod=None): @@ -3026,7 +3026,7 @@ def cookie_decode(data, key, digestmod=None): "Do not use this API directly.") data = tob(data) if cookie_is_encoded(data): - sig, msg = data.split(tob('?'), 1) + sig, msg = data.split(b'?', 1) digestmod = digestmod or hashlib.sha256 hashed = hmac.new(tob(key), msg, digestmod=digestmod).digest() if _lscmp(sig[1:], base64.b64encode(hashed)): @@ -3038,7 +3038,7 @@ def cookie_is_encoded(data): """ Return True if the argument looks like a encoded cookie.""" depr(0, 13, "cookie_is_encoded() will be removed soon.", "Do not use this API directly.") - return bool(data.startswith(tob('!')) and tob('?') in data) + return bool(data.startswith(b'!') and b'?' in data) def html_escape(string): diff --git a/test/test_environ.py b/test/test_environ.py index b51eccf..e8bb9d6 100755 --- a/test/test_environ.py +++ b/test/test_environ.py @@ -160,7 +160,7 @@ class TestRequest(unittest.TestCase): def test_get(self): """ Environ: GET data """ - qs = touni(tob('a=a&a=1&b=b&c=c&cn=%e7%93%b6'), 'latin1') + qs = touni(b'a=a&a=1&b=b&c=c&cn=%e7%93%b6', 'latin1') request = BaseRequest({'QUERY_STRING':qs}) self.assertTrue('a' in request.query) self.assertTrue('b' in request.query) @@ -169,11 +169,11 @@ class TestRequest(unittest.TestCase): self.assertEqual('1', request.query['a']) self.assertEqual('b', request.query['b']) self.assertEqual(touni(tob('瓶'), 'latin1'), request.query['cn']) - self.assertEqual(touni('瓶'), request.query.cn) + self.assertEqual('瓶', request.query.cn) def test_post(self): """ Environ: POST data """ - sq = tob('a=a&a=1&b=b&c=&d&cn=%e7%93%b6') + sq = b'a=a&a=1&b=b&c=&d&cn=%e7%93%b6' e = {} wsgiref.util.setup_testing_defaults(e) e['wsgi.input'].write(sq) @@ -190,10 +190,10 @@ class TestRequest(unittest.TestCase): self.assertEqual('', request.POST['c']) self.assertEqual('', request.POST['d']) self.assertEqual(touni(tob('瓶'), 'latin1'), request.POST['cn']) - self.assertEqual(touni('瓶'), request.POST.cn) + self.assertEqual('瓶', request.POST.cn) def test_bodypost(self): - sq = tob('foobar') + sq = b'foobar' e = {} wsgiref.util.setup_testing_defaults(e) e['wsgi.input'].write(sq) @@ -205,7 +205,7 @@ class TestRequest(unittest.TestCase): def test_body_noclose(self): """ Test that the body file handler is not closed after request.POST """ - sq = tob('a=a&a=1&b=b&c=&d') + sq = b'a=a&a=1&b=b&c=&d' e = {} wsgiref.util.setup_testing_defaults(e) e['wsgi.input'].write(sq) @@ -221,7 +221,7 @@ class TestRequest(unittest.TestCase): """ Environ: GET and POST are combined in request.param """ e = {} wsgiref.util.setup_testing_defaults(e) - e['wsgi.input'].write(tob('b=b&c=p')) + e['wsgi.input'].write(b'b=b&c=p') e['wsgi.input'].seek(0) e['CONTENT_LENGTH'] = '7' e['QUERY_STRING'] = 'a=a&c=g' @@ -234,7 +234,7 @@ class TestRequest(unittest.TestCase): """ Environ: GET and POST should not leak into each other """ e = {} wsgiref.util.setup_testing_defaults(e) - e['wsgi.input'].write(tob('b=b')) + e['wsgi.input'].write(b'b=b') e['wsgi.input'].seek(0) e['CONTENT_LENGTH'] = '3' e['QUERY_STRING'] = 'a=a' @@ -247,20 +247,20 @@ class TestRequest(unittest.TestCase): """ Environ: Request.body should behave like a file object factory """ e = {} wsgiref.util.setup_testing_defaults(e) - e['wsgi.input'].write(tob('abc')) + e['wsgi.input'].write(b'abc') e['wsgi.input'].seek(0) e['CONTENT_LENGTH'] = str(3) request = BaseRequest(e) - self.assertEqual(tob('abc'), request.body.read()) - self.assertEqual(tob('abc'), request.body.read(3)) - self.assertEqual(tob('abc'), request.body.readline()) - self.assertEqual(tob('abc'), request.body.readline(3)) + self.assertEqual(b'abc', request.body.read()) + self.assertEqual(b'abc', request.body.read(3)) + self.assertEqual(b'abc', request.body.readline()) + self.assertEqual(b'abc', request.body.readline(3)) def test_bigbody(self): """ Environ: Request.body should handle big uploads using files """ e = {} wsgiref.util.setup_testing_defaults(e) - e['wsgi.input'].write(tob('x')*1024*1000) + e['wsgi.input'].write(b'x'*1024*1000) e['wsgi.input'].seek(0) e['CONTENT_LENGTH'] = str(1024*1000) request = BaseRequest(e) @@ -274,7 +274,7 @@ class TestRequest(unittest.TestCase): """ Environ: Request.body should truncate to Content-Length bytes """ e = {} wsgiref.util.setup_testing_defaults(e) - e['wsgi.input'].write(tob('x')*1024) + e['wsgi.input'].write(b'x'*1024) e['wsgi.input'].seek(0) e['CONTENT_LENGTH'] = '42' request = BaseRequest(e) @@ -327,7 +327,7 @@ class TestRequest(unittest.TestCase): self.assertTrue('file1' in request.POST) self.assertTrue('file1' in request.files) self.assertTrue('file1' not in request.forms) - cmp = tob('content1') if sys.version_info >= (3,2,0) else 'content1' + cmp = b'content1' if sys.version_info >= (3,2,0) else 'content1' self.assertEqual(cmp, request.POST['file1'].file.read()) # File name and meta data self.assertTrue('万难' in request.POST) @@ -351,7 +351,7 @@ class TestRequest(unittest.TestCase): self.assertEqual('value1', request.forms['field1']) print(request.forms.dict, request.forms.recode_unicode) self.assertEqual('万难', request.forms['field2']) - self.assertEqual(touni('万难'), request.forms.field2) + self.assertEqual('万难', request.forms.field2) # Field (multi) self.assertEqual(2, len(request.POST.getall('field2'))) self.assertEqual(['value2', '万难'], request.POST.getall('field2')) @@ -724,8 +724,8 @@ class TestResponse(unittest.TestCase): self.assertEqual('5', response['x-test']) response['x-test'] = None self.assertEqual('', response['x-test']) - response['x-test'] = touni('瓶') - self.assertEqual(touni('瓶'), response['x-test']) + response['x-test'] = '瓶' + self.assertEqual('瓶', response['x-test']) def test_prevent_control_characters_in_headers(self): masks = '{}test', 'test{}', 'te{}st' @@ -885,11 +885,11 @@ class TestWSGIHeaderDict(unittest.TestCase): self.assertEqual(self.headers['Test-header'], 'foobar') def test_bytes(self): - self.env['HTTP_TEST_HEADER'] = tob('foobar') + self.env['HTTP_TEST_HEADER'] = b'foobar' self.assertEqual(self.headers['Test-Header'], 'foobar') def test_unicode(self): - self.env['HTTP_TEST_HEADER'] = touni('foobar') + self.env['HTTP_TEST_HEADER'] = 'foobar' self.assertEqual(self.headers['Test-Header'], 'foobar') def test_dict(self): diff --git a/test/test_fileupload.py b/test/test_fileupload.py index f5e2c07..0dbf351 100644 --- a/test/test_fileupload.py +++ b/test/test_fileupload.py @@ -21,7 +21,7 @@ class TestFileUpload(unittest.TestCase): def assertFilename(self, bad, good): fu = FileUpload(None, None, bad) self.assertEqual(fu.filename, good) - + def test_filename(self): self.assertFilename('with space', 'with-space') self.assertFilename('with more \t\n\r space', 'with-more-space') @@ -34,7 +34,7 @@ class TestFileUpload(unittest.TestCase): self.assertFilename(' . na me . ', 'na-me') self.assertFilename('path/', 'empty') self.assertFilename(bottle.tob('ümläüts$'), 'umlauts') - self.assertFilename(bottle.touni('ümläüts$'), 'umlauts') + self.assertFilename('ümläüts$', 'umlauts') self.assertFilename('', 'empty') self.assertFilename('a'+'b'*1337+'c', 'a'+'b'*254) diff --git a/test/test_formsdict.py b/test/test_formsdict.py index 188e1d5..1ee1f13 100644 --- a/test/test_formsdict.py +++ b/test/test_formsdict.py @@ -7,24 +7,23 @@ from bottle import FormsDict, touni, tob class TestFormsDict(unittest.TestCase): def test_attr_access(self): """ FomsDict.attribute returs string values as unicode. """ - d = FormsDict(py2=tob('瓶'), py3=tob('瓶').decode('latin1')) - self.assertEqual(touni('瓶'), d.py2) - self.assertEqual(touni('瓶'), d.py3) + d = FormsDict(py3=tob('瓶').decode('latin1')) + self.assertEqual('瓶', d.py3) def test_attr_missing(self): """ FomsDict.attribute returs u'' on missing keys. """ d = FormsDict() - self.assertEqual(touni(''), d.missing) + self.assertEqual('', d.missing) def test_attr_unicode_error(self): """ FomsDict.attribute returs u'' on UnicodeError. """ - d = FormsDict(latin=touni('öäüß').encode('latin1')) - self.assertEqual(touni(''), d.latin) + d = FormsDict(latin='öäüß'.encode('latin1')) + self.assertEqual('', d.latin) d.input_encoding = 'latin1' - self.assertEqual(touni('öäüß'), d.latin) + self.assertEqual('öäüß', d.latin) def test_decode_method(self): - d = FormsDict(py2=tob('瓶'), py3=tob('瓶').decode('latin1')) + d = FormsDict(py3=tob('瓶').decode('latin1')) d = d.decode() self.assertFalse(d.recode_unicode) self.assertTrue(hasattr(list(d.keys())[0], 'encode')) diff --git a/test/test_jinja2.py b/test/test_jinja2.py index 751b9e8..ab8da22 100644 --- a/test/test_jinja2.py +++ b/test/test_jinja2.py @@ -41,7 +41,7 @@ class TestJinja2Template(unittest.TestCase): def test_custom_filters(self): """Templates: jinja2 custom filters """ from bottle import jinja2_template as template - settings = dict(filters = {"star": lambda var: touni("").join((touni('*'), var, touni('*')))}) + settings = dict(filters = {"star": lambda var: touni("").join(('*', var, '*'))}) t = Jinja2Template("start {{var|star}} end", **settings) self.assertEqual("start *var* end", t.render(var="var")) @@ -56,13 +56,13 @@ class TestJinja2Template(unittest.TestCase): def test_template_shortcut(self): result = jinja2_template('start {{var}} end', var='middle') - self.assertEqual(touni('start middle end'), result) + self.assertEqual('start middle end', result) def test_view_decorator(self): @jinja2_view('start {{var}} end') def test(): return dict(var='middle') - self.assertEqual(touni('start middle end'), test()) + self.assertEqual('start middle end', test()) try: diff --git a/test/test_mako.py b/test/test_mako.py index 66de493..95de646 100644 --- a/test/test_mako.py +++ b/test/test_mako.py @@ -41,13 +41,13 @@ class TestMakoTemplate(unittest.TestCase): def test_template_shortcut(self): result = mako_template('start ${var} end', var='middle') - self.assertEqual(touni('start middle end'), result) + self.assertEqual('start middle end', result) def test_view_decorator(self): @mako_view('start ${var} end') def test(): return dict(var='middle') - self.assertEqual(touni('start middle end'), test()) + self.assertEqual('start middle end', test()) try: diff --git a/test/test_outputfilter.py b/test/test_outputfilter.py index 219bd15..7f776cd 100755 --- a/test/test_outputfilter.py +++ b/test/test_outputfilter.py @@ -17,7 +17,7 @@ class TestOutputFilter(ServerTestBase): ''' Tests for WSGI functionality, routing and output casting (decorators) ''' def test_bytes(self): - self.app.route('/')(lambda: tob('test')) + self.app.route('/')(lambda: b'test') self.assertBody('test') def test_bytearray(self): @@ -57,23 +57,23 @@ class TestOutputFilter(ServerTestBase): self.assertBody('test') def test_unicode(self): - self.app.route('/')(lambda: touni('äöüß')) - self.assertBody(touni('äöüß').encode('utf8')) + self.app.route('/')(lambda: 'äöüß') + self.assertBody('äöüß'.encode('utf8')) - self.app.route('/')(lambda: [touni('äö'), touni('üß')]) - self.assertBody(touni('äöüß').encode('utf8')) + self.app.route('/')(lambda: ['äö', 'üß']) + self.assertBody('äöüß'.encode('utf8')) @self.app.route('/') def test5(): bottle.response.content_type='text/html; charset=iso-8859-15' - return touni('äöüß') - self.assertBody(touni('äöüß').encode('iso-8859-15')) + return 'äöüß' + self.assertBody('äöüß'.encode('iso-8859-15')) @self.app.route('/') def test5(): bottle.response.content_type='text/html' - return touni('äöüß') - self.assertBody(touni('äöüß').encode('utf8')) + return 'äöüß' + self.assertBody('äöüß'.encode('utf8')) def test_json(self): self.app.route('/')(lambda: {'a': 1}) @@ -154,8 +154,8 @@ class TestOutputFilter(ServerTestBase): def test_unicode_generator_callback(self): @self.app.route('/') def test(): - yield touni('äöüß') - self.assertBody(touni('äöüß').encode('utf8')) + yield 'äöüß' + self.assertBody('äöüß'.encode('utf8')) def test_invalid_generator_callback(self): @self.app.route('/') @@ -172,8 +172,8 @@ class TestOutputFilter(ServerTestBase): def close(self): self.closed = True def __iter__(self): return iter(self.data) - byte_iter = MyIter([tob('abc'), tob('def')]) - unicode_iter = MyIter([touni('abc'), touni('def')]) + byte_iter = MyIter([b'abc', b'def']) + unicode_iter = MyIter(['abc', 'def']) for test_iter in (byte_iter, unicode_iter): @self.app.route('/') diff --git a/test/test_securecookies.py b/test/test_securecookies.py index 1ade52c..c9b0f4b 100644 --- a/test/test_securecookies.py +++ b/test/test_securecookies.py @@ -7,8 +7,8 @@ from bottle import tob, touni class TestSignedCookies(unittest.TestCase): def setUp(self): - self.data = touni('υηι¢σ∂є') - self.secret = tob('secret') + self.data = 'υηι¢σ∂є' + self.secret = b'secret' bottle.app.push() bottle.response.bind() @@ -43,4 +43,4 @@ class TestSignedCookies(unittest.TestCase): class TestSignedCookiesWithPickle(TestSignedCookies): def setUp(self): super(TestSignedCookiesWithPickle, self).setUp() - self.data = dict(a=5, b=touni('υηι¢σ∂є'), c=[1,2,3,4,tob('bytestring')]) + self.data = dict(a=5, b='υηι¢σ∂є', c=[1,2,3,4,b'bytestring']) diff --git a/test/test_sendfile.py b/test/test_sendfile.py index 622d992..5dad9ba 100755 --- a/test/test_sendfile.py +++ b/test/test_sendfile.py @@ -131,7 +131,7 @@ class TestSendFile(unittest.TestCase): request.environ['HTTP_RANGE'] = 'bytes=10-25,-80' f = static_file(basename, root=root) c = open(__file__, 'rb'); c.seek(10) - self.assertEqual(c.read(16), tob('').join(f.body)) + self.assertEqual(c.read(16), b''.join(f.body)) self.assertEqual('bytes 10-25/%d' % len(open(__file__, 'rb').read()), f.headers['Content-Range']) self.assertEqual('bytes', f.headers['Accept-Ranges']) diff --git a/test/test_server.py b/test/test_server.py index 0a48e41..fbca032 100644 --- a/test/test_server.py +++ b/test/test_server.py @@ -81,9 +81,9 @@ class TestServer(unittest.TestCase): lines = [line for stream in (self.p.stdout, self.p.stderr) for line in stream] for line in lines: - if tob('warning') in line.lower(): + if b'warning' in line.lower(): tools.warn(line.strip().decode('utf8')) - elif tob('error') in line.lower(): + elif b'error' in line.lower(): raise AssertionError(line.strip().decode('utf8')) def fetch(self, url): @@ -95,7 +95,7 @@ class TestServer(unittest.TestCase): def test_simple(self): ''' Test a simple static page with this server adapter. ''' if self.skip: return - self.assertEqual(tob('OK'), self.fetch('test')) + self.assertEqual(b'OK', self.fetch('test')) blacklist = ['cgi', 'flup', 'gae', 'wsgiref'] diff --git a/test/test_stpl.py b/test/test_stpl.py index 73f37d3..246f720 100755 --- a/test/test_stpl.py +++ b/test/test_stpl.py @@ -31,14 +31,14 @@ class TestSimpleTemplate(unittest.TestCase): self.assertRenders(t, 'start var end\n', var='var') def test_unicode(self): - self.assertRenders('start {{var}} end', 'start äöü end', var=touni('äöü')) + self.assertRenders('start {{var}} end', 'start äöü end', var='äöü') self.assertRenders('start {{var}} end', 'start äöü end', var=tob('äöü')) def test_unicode_code(self): """ Templates: utf8 code in file""" with chdir(__file__): t = SimpleTemplate(name='./views/stpl_unicode.tpl', lookup=['.']) - self.assertRenders(t, 'start ñç äöü end\n', var=touni('äöü')) + self.assertRenders(t, 'start ñç äöü end\n', var='äöü') def test_import(self): """ Templates: import statement""" @@ -53,7 +53,7 @@ class TestSimpleTemplate(unittest.TestCase): self.assertRenders('<{{var}}>', '<>', var=None) self.assertRenders('<{{var}}>', '<0>', var=0) self.assertRenders('<{{var}}>', '<5>', var=5) - self.assertRenders('<{{var}}>', '<b>', var=tob('b')) + self.assertRenders('<{{var}}>', '<b>', var=b'b') self.assertRenders('<{{var}}>', '<1.0>', var=1.0) self.assertRenders('<{{var}}>', '<[1, 2]>', var=[1,2]) @@ -204,29 +204,29 @@ class TestSimpleTemplate(unittest.TestCase): def test_template_shortcut(self): result = template('start {{var}} end', var='middle') - self.assertEqual(touni('start middle end'), result) + self.assertEqual('start middle end', result) def test_view_decorator(self): @view('start {{var}} end') def test(): return dict(var='middle') - self.assertEqual(touni('start middle end'), test()) + self.assertEqual('start middle end', test()) def test_view_decorator_issue_407(self): with chdir(__file__): @view('stpl_no_vars') def test(): pass - self.assertEqual(touni('hihi'), test()) + self.assertEqual('hihi', test()) @view('aaa {{x}}', x='bbb') def test2(): pass - self.assertEqual(touni('aaa bbb'), test2()) + self.assertEqual('aaa bbb', test2()) def test_global_config(self): SimpleTemplate.global_config('meh', 1) t = SimpleTemplate('anything') - self.assertEqual(touni('anything'), t.render()) + self.assertEqual('anything', t.render()) def test_bug_no_whitespace_before_stmt(self): self.assertRenders('\n{{var}}', '\nx', var='x') diff --git a/test/test_wsgi.py b/test/test_wsgi.py index 9f46fa7..92ef68d 100755 --- a/test/test_wsgi.py +++ b/test/test_wsgi.py @@ -2,7 +2,8 @@ from __future__ import with_statement import bottle from .tools import ServerTestBase, chdir -from bottle import tob, touni, HTTPResponse +from bottle import tob, HTTPResponse + class TestWsgi(ServerTestBase): ''' Tests for WSGI functionality, routing and output casting (decorators) ''' @@ -93,7 +94,7 @@ class TestWsgi(ServerTestBase): def test_500_unicode(self): @bottle.route('/') - def test(): raise Exception(touni('Unicode äöüß message.')) + def test(): raise Exception('Unicode äöüß message.') self.assertStatus(500, '/') def test_utf8_url(self): diff --git a/test/tools.py b/test/tools.py index d590cf0..646ec37 100755 --- a/test/tools.py +++ b/test/tools.py @@ -95,7 +95,7 @@ class ServerTestBase(unittest.TestCase): self.wsgiapp = wsgiref.validate.validator(self.app) def urlopen(self, path, method='GET', post='', env=None): - result = {'code':0, 'status':'error', 'header':{}, 'body':tob('')} + result = {'code':0, 'status':'error', 'header':{}, 'body':b''} def start_response(status, header, exc_info=None): result['code'] = int(status.split()[0]) result['status'] = status.split(None, 1)[-1] |