-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
no longer trust custom repr for int/long/float subclasses #118
- Loading branch information
Showing
8 changed files
with
101 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from unittest import TestCase | ||
import simplejson as json | ||
|
||
from decimal import Decimal | ||
|
||
class AlternateInt(int): | ||
def __repr__(self): | ||
return 'invalid json' | ||
__str__ = __repr__ | ||
|
||
|
||
class AlternateFloat(float): | ||
def __repr__(self): | ||
return 'invalid json' | ||
__str__ = __repr__ | ||
|
||
|
||
# class AlternateDecimal(Decimal): | ||
# def __repr__(self): | ||
# return 'invalid json' | ||
|
||
|
||
class TestSubclass(TestCase): | ||
def test_int(self): | ||
self.assertEqual(json.dumps(AlternateInt(1)), '1') | ||
self.assertEqual(json.dumps(AlternateInt(-1)), '-1') | ||
self.assertEqual(json.loads(json.dumps({AlternateInt(1): 1})), {'1': 1}) | ||
|
||
def test_float(self): | ||
self.assertEqual(json.dumps(AlternateFloat(1.0)), '1.0') | ||
self.assertEqual(json.dumps(AlternateFloat(-1.0)), '-1.0') | ||
self.assertEqual(json.loads(json.dumps({AlternateFloat(1.0): 1})), {'1.0': 1}) | ||
|
||
# NOTE: Decimal subclasses are not supported as-is | ||
# def test_decimal(self): | ||
# self.assertEqual(json.dumps(AlternateDecimal('1.0')), '1.0') | ||
# self.assertEqual(json.dumps(AlternateDecimal('-1.0')), '-1.0') |
1 comment
on commit e18cc09
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be more efficient to use something like int.__repr__(o)
in Python code and PyLong_Type.tp_repr(key)
in C code.
Shouldn't this read
if type(key) not in integer_types
?