-
Notifications
You must be signed in to change notification settings - Fork 334
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
Allow unknown numerical types for indent parameter #29
Conversation
WTF? Sorry, that code doesn't even run. Lemme fix that... |
This was causing failures in software which passes objects which are neither of type str, int, or long, but rather a custom numerical class, as the value of the indent parameter. Since this custom numerical class understands multiplication by strings, it should be accepted by simplejson's encoder and treated as a numerical argument.
OK, should be fixed now. Sorry about the noise... |
How about you also write a test for this so that there are no future regressions, and I'll go ahead and take that. |
Great! Do you want a test that tests the situation I described, or just one which shows the regression? For example, instead of an actual numeric type, I could test that an object of the following type is accepted, which is a more minimal test case and easier to write, but I guess somehow less meaningful: class Foo(object):
def __mul__(self, other):
return '' |
I'd prefer to have something as close as possible to your use case, since the intent was always really just to support integers. Note that float and Decimal do not implement string multiplication, so we're not even going for generic number support. |
Hmm. Well, my actual use case is quite convoluted and involves a huge amount of code that is not even entirely written in Python - you can see the relevant segment here if you're interested - so I won't try to simplify my actual use case. I'll make something along the same general lines, though. |
I guess the real question is - why are you using this to specify the number of spaces to indent a JSON document? |
We have a Python-based user console in which integer literals are hooked in the parser and turned into our custom integer-like objects so that they can interact better with the array of mathematical objects we provide. This means that if, for example, you type I only happened to catch this because exactly such a line happened to appear in our test suite, and fails with the latest simplejson whereas it does not fail with the json module shipped with Python 2.7.2, making this technically a regression, IMO. Of course, you may consider this scenario not worth changing simplejson's code for, in which case we will need to modify our test suite, but it could still bite users who type in similar code in our console, and I think it's more elegant to patch simplejson in this way than to write code special-casing simplejson calls in our parser hooks... |
That sounds like a reasonable use case. It's worth a small change in the code, I just want to make sure that once it's fixed it will stay fixed for you. |
Great, I'm glad you think so. Here's a commit with a test and some documentation. |
Allow unknown numerical types for indent parameter
Thanks! |
Version 2.5.2 released 2012-05-10 * Fix for regression introduced in 2.5.1 simplejson/simplejson#35 Version 2.5.1 released 2012-05-10 * Support for use_decimal=True in environments that use Python sub-interpreters such as uWSGI simplejson/simplejson#34 Version 2.5.0 released 2012-03-29 * New item_sort_key option for encoder to allow fine grained control of sorted output Version 2.4.0 released 2012-03-06 * New bigint_as_string option for encoder to trade JavaScript number precision issues for type issues. simplejson/simplejson#31 Version 2.3.3 released 2012-02-27 * Allow unknown numerical types for indent parameter simplejson/simplejson#29 Version 2.3.2 released 2011-12-30 * Fix crashing regression in speedups introduced in 2.3.1 Version 2.3.1 released 2011-12-29 * namedtuple_as_object now checks _asdict to ensure that it is callable. simplejson/simplejson#26 Version 2.3.0 released 2011-12-05 * Any objects with _asdict() methods are now considered for namedtuple_as_object. simplejson/simplejson#22
Version 2.5.2 released 2012-05-10 * Fix for regression introduced in 2.5.1 simplejson/simplejson#35 Version 2.5.1 released 2012-05-10 * Support for use_decimal=True in environments that use Python sub-interpreters such as uWSGI simplejson/simplejson#34 Version 2.5.0 released 2012-03-29 * New item_sort_key option for encoder to allow fine grained control of sorted output Version 2.4.0 released 2012-03-06 * New bigint_as_string option for encoder to trade JavaScript number precision issues for type issues. simplejson/simplejson#31 Version 2.3.3 released 2012-02-27 * Allow unknown numerical types for indent parameter simplejson/simplejson#29 Version 2.3.2 released 2011-12-30 * Fix crashing regression in speedups introduced in 2.3.1 Version 2.3.1 released 2011-12-29 * namedtuple_as_object now checks _asdict to ensure that it is callable. simplejson/simplejson#26 Version 2.3.0 released 2011-12-05 * Any objects with _asdict() methods are now considered for namedtuple_as_object. simplejson/simplejson#22
This was causing failures in software which passes objects which are
neither of type str, int, or long, but rather a custom numerical class,
as the value of the indent parameter. Since this custom numerical class
understands multiplication by strings, it should be accepted by
simplejson's encoder and treated as a numerical argument.
... is what it says in the commit message. Of course, I make no such bold claim about what "should" happen, but am just submitting this pull request to see if you agree :) This is in some sense a regression because everything worked fine before string arguments for the indent parameter became supported, and the docstring suggests that backwards compatibility was aimed for.