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

Use scrub mechanism to clean up odd int/long/floats #62

Merged
merged 2 commits into from
May 19, 2015
Merged
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
8 changes: 8 additions & 0 deletions rollbar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import inspect
import json
import logging
import math
import numbers
import os
import socket
import sys
Expand Down Expand Up @@ -871,6 +873,12 @@ def _scrub(obj, k=None):
return dict((_k, _scrub(v, _k)) for _k, v in obj.items())
elif isinstance(obj, list):
return [_scrub(x, k) for x in obj]
elif isinstance(obj, float) and math.isnan(obj):
return 'NaN'
elif isinstance(obj, float) and math.isinf(obj):
return 'Inf'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, should this be 'Infinity'?

>>> simplejson.dumps(simplejson.loads('Inf'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/brian/env-mox/local/lib/python2.7/site-packages/simplejson/__init__.py", line 451, in loads
    return _default_decoder.decode(s)
  File "/home/brian/env-mox/local/lib/python2.7/site-packages/simplejson/decoder.py", line 406, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/home/brian/env-mox/local/lib/python2.7/site-packages/simplejson/decoder.py", line 424, in raw_decode
    raise JSONDecodeError("No JSON object could be decoded", s, idx)
simplejson.decoder.JSONDecodeError: No JSON object could be decoded: line 1 column 0 (char 0)
>>> simplejson.dumps(simplejson.loads('Infinity'))
'Infinity'

elif isinstance(obj, (numbers.Integral, float)) and str(obj + 0) != str(obj):
return str(obj)
else:
return obj

Expand Down
19 changes: 19 additions & 0 deletions rollbar/test/test_rollbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,25 @@ def _raise():

self.assertEqual('*********', payload['data']['body']['trace']['frames'][-1]['locals']['password'])

@mock.patch('rollbar.send_payload')
def test_scrub_nans(self, send_payload):
def _raise():
infinity = float('Inf')
not_a_number = float('NaN')
raise Exception()

try:
_raise()
except:
rollbar.report_exc_info()

self.assertEqual(send_payload.called, True)

payload = send_payload.call_args[0][0]

self.assertEqual('Inf', payload['data']['body']['trace']['frames'][-1]['locals']['infinity'])
self.assertEqual('NaN', payload['data']['body']['trace']['frames'][-1]['locals']['not_a_number'])

@mock.patch('rollbar.send_payload')
def test_cannot_scrub_local_ref(self, send_payload):
"""
Expand Down