Skip to content

Commit

Permalink
Fix config.from_pyfile on Python 3 (pallets#2123)
Browse files Browse the repository at this point in the history
* Fix config.from_pyfile on Python 3

Fix pallets#2118

* Support Python 2.6

* Fix tests on Python 2
  • Loading branch information
untitaker authored Dec 26, 2016
1 parent 8cd0b03 commit 789715a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Version 0.12.1

Bugfix release, unreleased

- Fix encoding behavior of ``app.config.from_pyfile`` for Python 3. Fix
``#2118``.

Version 0.12
------------

Expand Down
2 changes: 1 addition & 1 deletion flask/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def from_pyfile(self, filename, silent=False):
d = types.ModuleType('config')
d.__file__ = filename
try:
with open(filename) as config_file:
with open(filename, mode='rb') as config_file:
exec(compile(config_file.read(), filename, 'exec'), d.__dict__)
except IOError as e:
if silent and e.errno in (errno.ENOENT, errno.EISDIR):
Expand Down
22 changes: 20 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
:license: BSD, see LICENSE for more details.
"""

import pytest

import os
from datetime import timedelta
import os
import textwrap

import flask
from flask._compat import PY2
import pytest


# config keys used for the TestConfig
Expand Down Expand Up @@ -187,3 +190,18 @@ def test_get_namespace():
assert 2 == len(bar_options)
assert 'bar stuff 1' == bar_options['BAR_STUFF_1']
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öö'

0 comments on commit 789715a

Please sign in to comment.