Browse Source

Fix config.from_pyfile on Python 3 (#2123)

* Fix config.from_pyfile on Python 3

Fix #2118

* Support Python 2.6

* Fix tests on Python 2
pull/2247/head
Markus Unterwaditzer 8 years ago committed by GitHub
parent
commit
789715adb9
  1. 3
      CHANGES
  2. 2
      flask/config.py
  3. 22
      tests/test_config.py

3
CHANGES

@ -8,6 +8,9 @@ Version 0.12.1
Bugfix release, unreleased Bugfix release, unreleased
- Fix encoding behavior of ``app.config.from_pyfile`` for Python 3. Fix
``#2118``.
Version 0.12 Version 0.12
------------ ------------

2
flask/config.py

@ -126,7 +126,7 @@ class Config(dict):
d = types.ModuleType('config') d = types.ModuleType('config')
d.__file__ = filename d.__file__ = filename
try: try:
with open(filename) as config_file: with open(filename, mode='rb') as config_file:
exec(compile(config_file.read(), filename, 'exec'), d.__dict__) exec(compile(config_file.read(), filename, 'exec'), d.__dict__)
except IOError as e: except IOError as e:
if silent and e.errno in (errno.ENOENT, errno.EISDIR): if silent and e.errno in (errno.ENOENT, errno.EISDIR):

22
tests/test_config.py

@ -7,11 +7,14 @@
:license: BSD, see LICENSE for more details. :license: BSD, see LICENSE for more details.
""" """
import pytest
import os
from datetime import timedelta from datetime import timedelta
import os
import textwrap
import flask import flask
from flask._compat import PY2
import pytest
# config keys used for the TestConfig # config keys used for the TestConfig
@ -187,3 +190,18 @@ def test_get_namespace():
assert 2 == len(bar_options) assert 2 == len(bar_options)
assert 'bar stuff 1' == bar_options['BAR_STUFF_1'] assert 'bar stuff 1' == bar_options['BAR_STUFF_1']
assert 'bar stuff 2' == bar_options['BAR_STUFF_2'] assert 'bar stuff 2' == bar_options['BAR_STUFF_2']
@pytest.mark.parametrize('encoding', ['utf-8', 'iso-8859-15', 'latin-1'])
def test_from_pyfile_weird_encoding(tmpdir, encoding):
f = tmpdir.join('my_config.py')
f.write_binary(textwrap.dedent(u'''
# -*- coding: {0} -*-
TEST_VALUE = "föö"
'''.format(encoding)).encode(encoding))
app = flask.Flask(__name__)
app.config.from_pyfile(str(f))
value = app.config['TEST_VALUE']
if PY2:
value = value.decode(encoding)
assert value == u'föö'

Loading…
Cancel
Save