Skip to content

Commit e1d356f

Browse files
ported some more stuff to py 3.3
removed init_jinja_globals hack from app.py after consulting mitsuhiko (didn't work on py 3.3 "as is") removed with_statement future imports, not needed any more needs more work on 2.7 as well as on 3.3
1 parent a503520 commit e1d356f

24 files changed

+36
-87
lines changed

examples/flaskr/flaskr.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
:copyright: (c) 2010 by Armin Ronacher.
1010
:license: BSD, see LICENSE for more details.
1111
"""
12-
from __future__ import with_statement
12+
1313
from sqlite3 import dbapi2 as sqlite3
1414
from flask import Flask, request, session, g, redirect, url_for, abort, \
1515
render_template, flash, _app_ctx_stack

examples/minitwit/minitwit.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
:copyright: (c) 2010 by Armin Ronacher.
99
:license: BSD, see LICENSE for more details.
1010
"""
11-
from __future__ import with_statement
11+
1212
import time
1313
from sqlite3 import dbapi2 as sqlite3
1414
from hashlib import md5

flask/_compat.py

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
range_type = range
2323
text_type = str
2424
string_types = (str,)
25+
integer_types = (int, )
2526

2627
iterkeys = lambda d: iter(d.keys())
2728
itervalues = lambda d: iter(d.values())
@@ -46,11 +47,14 @@ def reraise(tp, value, tb=None):
4647
encode_filename = _identity
4748
get_next = lambda x: x.__next__
4849

50+
from urllib.parse import urlparse
51+
4952
else:
5053
unichr = unichr
5154
text_type = unicode
5255
range_type = xrange
5356
string_types = (str, unicode)
57+
integer_types = (int, long)
5458

5559
iterkeys = lambda d: d.iterkeys()
5660
itervalues = lambda d: d.itervalues()
@@ -82,6 +86,8 @@ def encode_filename(filename):
8286
return filename.encode('utf-8')
8387
return filename
8488

89+
from urlparse import urlparse
90+
8591

8692
def with_metaclass(meta, *bases):
8793
# This requires a bit of explanation: the basic idea is to make a

flask/app.py

+9-24
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
:license: BSD, see LICENSE for more details.
1010
"""
1111

12-
from __future__ import with_statement
13-
1412
import os
1513
import sys
1614
from threading import Lock
@@ -36,6 +34,7 @@
3634
_default_template_ctx_processor
3735
from .signals import request_started, request_finished, got_request_exception, \
3836
request_tearing_down, appcontext_tearing_down
37+
from flask._compat import reraise, string_types, integer_types
3938

4039
# a lock used for logger initialization
4140
_logger_lock = Lock()
@@ -585,21 +584,7 @@ def logger(self):
585584
@locked_cached_property
586585
def jinja_env(self):
587586
"""The Jinja2 environment used to load templates."""
588-
rv = self.create_jinja_environment()
589-
590-
# Hack to support the init_jinja_globals method which is supported
591-
# until 1.0 but has an API deficiency.
592-
if getattr(self.init_jinja_globals, 'im_func', None) is not \
593-
Flask.init_jinja_globals.__func__:
594-
from warnings import warn
595-
warn(DeprecationWarning('This flask class uses a customized '
596-
'init_jinja_globals() method which is deprecated. '
597-
'Move the code from that method into the '
598-
'create_jinja_environment() method instead.'))
599-
self.__dict__['jinja_env'] = rv
600-
self.init_jinja_globals()
601-
602-
return rv
587+
return self.create_jinja_environment()
603588

604589
@property
605590
def got_first_request(self):
@@ -1090,7 +1075,7 @@ def register_error_handler(self, code_or_exception, f):
10901075
def _register_error_handler(self, key, code_or_exception, f):
10911076
if isinstance(code_or_exception, HTTPException):
10921077
code_or_exception = code_or_exception.code
1093-
if isinstance(code_or_exception, (int, long)):
1078+
if isinstance(code_or_exception, integer_types):
10941079
assert code_or_exception != 500 or key is None, \
10951080
'It is currently not possible to register a 500 internal ' \
10961081
'server error on a per-blueprint level.'
@@ -1137,7 +1122,7 @@ def template_test(self, name=None):
11371122
def is_prime(n):
11381123
if n == 2:
11391124
return True
1140-
for i in xrange(2, int(math.ceil(math.sqrt(n))) + 1):
1125+
for i in range(2, int(math.ceil(math.sqrt(n))) + 1):
11411126
if n % i == 0:
11421127
return False
11431128
return True
@@ -1383,7 +1368,7 @@ def handle_user_exception(self, e):
13831368
if isinstance(e, typecheck):
13841369
return handler(e)
13851370

1386-
raise exc_type, exc_value, tb
1371+
reraise(exc_type, exc_value, tb)
13871372

13881373
def handle_exception(self, e):
13891374
"""Default exception handling that kicks in when an exception
@@ -1405,7 +1390,7 @@ def handle_exception(self, e):
14051390
# (the function was actually called from the except part)
14061391
# otherwise, we just raise the error again
14071392
if exc_value is e:
1408-
raise exc_type, exc_value, tb
1393+
reraise(exc_type, exc_value, tb)
14091394
else:
14101395
raise e
14111396

@@ -1565,14 +1550,14 @@ def make_response(self, rv):
15651550
# set the headers and status. We do this because there can be
15661551
# some extra logic involved when creating these objects with
15671552
# specific values (like defualt content type selection).
1568-
if isinstance(rv, basestring):
1553+
if isinstance(rv, string_types):
15691554
rv = self.response_class(rv, headers=headers, status=status)
15701555
headers = status = None
15711556
else:
15721557
rv = self.response_class.force_type(rv, request.environ)
15731558

15741559
if status is not None:
1575-
if isinstance(status, basestring):
1560+
if isinstance(status, string_types):
15761561
rv.status = status
15771562
else:
15781563
rv.status_code = status
@@ -1633,7 +1618,7 @@ def handle_url_build_error(self, error, endpoint, values):
16331618
# still the same one we can reraise it with the original traceback,
16341619
# otherwise we raise it from here.
16351620
if error is exc_value:
1636-
raise exc_type, exc_value, tb
1621+
reraise(exc_type, exc_value, tb)
16371622
raise error
16381623

16391624
def preprocess_request(self):

flask/config.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
:license: BSD, see LICENSE for more details.
1010
"""
1111

12-
from __future__ import with_statement
13-
1412
import imp
1513
import os
1614
import errno
1715

1816
from werkzeug.utils import import_string
17+
from flask._compat import string_types
1918

2019

2120
class ConfigAttribute(object):
@@ -158,7 +157,7 @@ def from_object(self, obj):
158157
159158
:param obj: an import name or object
160159
"""
161-
if isinstance(obj, basestring):
160+
if isinstance(obj, string_types):
162161
obj = import_string(obj)
163162
for key in dir(obj):
164163
if key.isupper():

flask/exthook.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"""
2222
import sys
2323
import os
24+
from flask._compat import reraise
2425

2526

2627
class ExtensionImporter(object):
@@ -77,7 +78,7 @@ def load_module(self, fullname):
7778
# we swallow it and try the next choice. The skipped frame
7879
# is the one from __import__ above which we don't care about
7980
if self.is_important_traceback(realname, tb):
80-
raise exc_type, exc_value, tb.tb_next
81+
reraise(exc_type, exc_value, tb.tb_next)
8182
continue
8283
module = sys.modules[fullname] = sys.modules[realname]
8384
if '.' not in modname:

flask/helpers.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
:license: BSD, see LICENSE for more details.
1010
"""
1111

12-
from __future__ import with_statement
13-
1412
import os
1513
import sys
1614
import pkgutil
@@ -27,6 +25,7 @@
2725
from werkzeug.datastructures import Headers
2826
from werkzeug.exceptions import NotFound
2927
import six
28+
from flask._compat import string_types, text_type
3029

3130
# this was moved in 0.7
3231
try:
@@ -467,7 +466,7 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
467466
:data:`~flask.current_app`.
468467
"""
469468
mtime = None
470-
if isinstance(filename_or_fp, basestring):
469+
if isinstance(filename_or_fp, string_types):
471470
filename = filename_or_fp
472471
file = None
473472
else:
@@ -478,7 +477,7 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
478477
# XXX: this behavior is now deprecated because it was unreliable.
479478
# removed in Flask 1.0
480479
if not attachment_filename and not mimetype \
481-
and isinstance(filename, basestring):
480+
and isinstance(filename, string_types):
482481
warn(DeprecationWarning('The filename support for file objects '
483482
'passed to send_file is now deprecated. Pass an '
484483
'attach_filename if you want mimetypes to be guessed.'),
@@ -540,7 +539,7 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
540539
os.path.getmtime(filename),
541540
os.path.getsize(filename),
542541
adler32(
543-
filename.encode('utf-8') if isinstance(filename, unicode)
542+
filename.encode('utf-8') if isinstance(filename, text_type)
544543
else filename
545544
) & 0xffffffff
546545
))

flask/testing.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010
:license: BSD, see LICENSE for more details.
1111
"""
1212

13-
from __future__ import with_statement
14-
1513
from contextlib import contextmanager
1614
from werkzeug.test import Client, EnvironBuilder
1715
from flask import _request_ctx_stack
18-
from urlparse import urlparse
16+
from flask._compat import urlparse
1917

2018

2119
def make_test_environ_builder(app, path='/', base_url=None, *args, **kwargs):

flask/testsuite/__init__.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,16 @@
1111
"""
1212

1313
from __future__ import print_function
14-
from __future__ import with_statement
1514

1615
import os
1716
import sys
1817
import flask
1918
import warnings
2019
import unittest
21-
from StringIO import StringIO
2220
from functools import update_wrapper
2321
from contextlib import contextmanager
2422
from werkzeug.utils import import_string, find_modules
23+
from flask._compat import reraise, StringIO
2524

2625

2726
def add_to_path(path):
@@ -159,7 +158,7 @@ def __exit__(self, exc_type, exc_value, tb):
159158
self.test_case.fail('Expected exception of type %r' %
160159
exception_name)
161160
elif not issubclass(exc_type, self.exc_type):
162-
raise exc_type, exc_value, tb
161+
reraise(exc_type, exc_value, tb)
163162
return True
164163

165164

flask/testsuite/appctx.py

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
:license: BSD, see LICENSE for more details.
1010
"""
1111

12-
from __future__ import with_statement
13-
1412
import flask
1513
import unittest
1614
from flask.testsuite import FlaskTestCase

flask/testsuite/basic.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
:license: BSD, see LICENSE for more details.
1010
"""
1111

12-
from __future__ import with_statement
13-
1412
import re
1513
import uuid
1614
import flask
@@ -1060,7 +1058,7 @@ def fail_func():
10601058
1/0
10611059

10621060
c = app.test_client()
1063-
for x in xrange(3):
1061+
for x in range(3):
10641062
with self.assert_raises(ZeroDivisionError):
10651063
c.get('/fail')
10661064

flask/testsuite/blueprints.py

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
:license: BSD, see LICENSE for more details.
1010
"""
1111

12-
from __future__ import with_statement
13-
1412
import flask
1513
import unittest
1614
import warnings

flask/testsuite/config.py

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
:copyright: (c) 2011 by Armin Ronacher.
99
:license: BSD, see LICENSE for more details.
1010
"""
11-
from __future__ import with_statement
1211

1312
import os
1413
import sys

flask/testsuite/deprecations.py

+1-18
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,13 @@
99
:license: BSD, see LICENSE for more details.
1010
"""
1111

12-
from __future__ import with_statement
13-
1412
import flask
1513
import unittest
1614
from flask.testsuite import FlaskTestCase, catch_warnings
1715

1816

1917
class DeprecationsTestCase(FlaskTestCase):
20-
21-
def test_init_jinja_globals(self):
22-
class MyFlask(flask.Flask):
23-
def init_jinja_globals(self):
24-
self.jinja_env.globals['foo'] = '42'
25-
26-
with catch_warnings() as log:
27-
app = MyFlask(__name__)
28-
@app.route('/')
29-
def foo():
30-
return app.jinja_env.globals['foo']
31-
32-
c = app.test_client()
33-
self.assert_equal(c.get('/').data, '42')
34-
self.assert_equal(len(log), 1)
35-
self.assert_('init_jinja_globals' in str(log[0]['message']))
18+
"""not used currently"""
3619

3720

3821
def suite():

flask/testsuite/ext.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
:copyright: (c) 2011 by Armin Ronacher.
99
:license: BSD, see LICENSE for more details.
1010
"""
11-
from __future__ import with_statement
1211

1312
import sys
1413
import unittest
@@ -22,7 +21,7 @@ def setup(self):
2221
# that a real flaskext could be in there which would disable our
2322
# fake package. Secondly we want to make sure that the flaskext
2423
# import hook does not break on reloading.
25-
for entry, value in sys.modules.items():
24+
for entry, value in list(sys.modules.items()):
2625
if (entry.startswith('flask.ext.') or
2726
entry.startswith('flask_') or
2827
entry.startswith('flaskext.') or
@@ -100,7 +99,7 @@ def test_flaskext_old_package_import_submodule_function(self):
10099
self.assert_equal(test_function(), 42)
101100

102101
def test_flaskext_broken_package_no_module_caching(self):
103-
for x in xrange(2):
102+
for x in range(2):
104103
with self.assert_raises(ImportError):
105104
import flask.ext.broken
106105

flask/testsuite/helpers.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@
99
:license: BSD, see LICENSE for more details.
1010
"""
1111

12-
from __future__ import with_statement
13-
1412
import os
1513
import flask
1614
import unittest
1715
from logging import StreamHandler
18-
from StringIO import StringIO
1916
from flask.testsuite import FlaskTestCase, catch_warnings, catch_stderr
2017
from werkzeug.http import parse_cache_control_header, parse_options_header
2118
import six
19+
from flask._compat import StringIO
2220

2321

2422
def has_encoding(name):

0 commit comments

Comments
 (0)