Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent attribute error when trying to decode none value #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Changelog
=========

Upcoming release
----------------

- Prevent ``AttributeError`` when trying to humanize ``None`` value (`#6`_),
thanks to `@earlbread`_ for the report.

.. _#6: https://github.com/vitalk/flask-humanize/pull/6
.. _@earlbread: https://github.com/earlbread

0.3.0 (2016-03-26)
------------------

Expand Down
9 changes: 6 additions & 3 deletions flask_humanize/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
from flask import current_app
from werkzeug.datastructures import ImmutableDict

from . import compat
from .compat import (
text_type, string_types
)


__version__ = '0.3.0'


def force_unicode(value):
if not isinstance(value, compat.text_type):
return value.decode('utf-8')
if isinstance(value, string_types):
if not isinstance(value, text_type):
return value.decode('utf-8')
return value


Expand Down
2 changes: 2 additions & 0 deletions flask_humanize/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@

if PY2:
text_type = unicode
string_types = basestring,
else:
text_type = str
string_types = str,
3 changes: 3 additions & 0 deletions tests/test_humanize.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def test_defaults(self, config, h):
@pytest.mark.usefixtures('app')
class TestHumanize:

def test_none(self, h):
assert h._humanize(None) is None

def test_naturalday(self, h):
assert h._humanize(today, 'naturalday') == 'today'
assert h._humanize(today + one_day, 'naturalday') == 'tomorrow'
Expand Down