Skip to content

Commit

Permalink
better enum support + fix for enum34 IntEnum quirk
Browse files Browse the repository at this point in the history
  • Loading branch information
ezarowny committed Aug 4, 2016
1 parent 0250e22 commit 89bbb94
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
13 changes: 10 additions & 3 deletions rollbar/lib/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,19 @@ def string_handler(s, key=None):
return do_transform('unicode', s, key=key)

def default_handler(o, key=None):
if isinstance(o, integer_types + (float,)):
return do_transform('number', o, key=key)

if isinstance(o, bool):
return do_transform('boolean', o, key=key)

# There is a quirk in the current version (1.1.6) of the enum
# backport enum34 which causes it to not have the same
# behavior as Python 3.4+. One way to identify IntEnums is that
# they are instances of numbers but not number types.
if isinstance(o, integer_types + (float,)):
if type(o) not in integer_types + (float, ):
return do_transform('custom', o, key=key)
else:
return do_transform('number', o, key=key)

return do_transform('custom', o, key=key)

handlers = {
Expand Down
38 changes: 37 additions & 1 deletion rollbar/test/test_serializable_transform.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import collections
import base64
import copy
import math
import enum

from rollbar.lib import transforms, python_major_version
from rollbar.lib.transforms.serializable import SerializableTransform
Expand Down Expand Up @@ -56,6 +56,42 @@ def test_simple_dict(self):
expected = copy.deepcopy(start)
self._assertSerialized(start, expected)

def test_enum(self):
class EnumTest(enum.Enum):
one = 1
two = 2

start = EnumTest.one
expected = "<enum 'EnumTest'>"
self._assertSerialized(start, expected)

def test_enum_no_safe_repr(self):
class EnumTest(enum.Enum):
one = 1
two = 2

start = EnumTest.one
expected = '<EnumTest.one: 1>'
self._assertSerialized(start, expected, safe_repr=False)

def test_int_enum(self):
class EnumTest(enum.IntEnum):
one = 1
two = 2

start = EnumTest.one
expected = "<enum 'EnumTest'>"
self._assertSerialized(start, expected)

def test_int_enum_no_safe_repr(self):
class EnumTest(enum.IntEnum):
one = 1
two = 2

start = EnumTest.one
expected = '<EnumTest.one: 1>'
self._assertSerialized(start, expected, safe_repr=False)

def test_encode_dict_with_invalid_utf8(self):
start = {
'invalid': invalid
Expand Down
20 changes: 13 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
import os.path
import sys
from setuptools import setup, find_packages

HERE = os.path.abspath(os.path.dirname(__file__))
Expand All @@ -16,6 +17,17 @@
INIT_DATA = fd.read()
VERSION = re.search(r"^__version__ = ['\"]([^'\"]+)['\"]", INIT_DATA, re.MULTILINE).group(1)

tests_require = [
'mock',
'webob',
'blinker',
'unittest2'
]

version = sys.version_info
if version[0] == 2 or (version[0] == 3 and version[1] < 4):
tests_require.append('enum34')

setup(
name='rollbar',
packages=find_packages(),
Expand Down Expand Up @@ -65,11 +77,5 @@
'requests',
'six',
],
tests_require=[
'mock',
'webob',
'blinker',
'unittest2'
],
tests_require=tests_require,
)

0 comments on commit 89bbb94

Please sign in to comment.