# HG changeset patch
# User Bob Ippolito <bob@redivi.com>
# Date 1497896293 25200
#      Mon Jun 19 11:18:13 2017 -0700
# Node ID fcac8650fadb43830cc4d110240f5f655d6bba4b
# Parent  789b73ea6b35598e9f74e4779657bfd1663649de
Fix #173 with item_sort_key and add auto-discovery to test suite

diff --git a/CHANGES.txt b/CHANGES.txt
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,3 +1,9 @@
+Version 3.11.1 released 2017-06-19
+
+* Fix issue with item_sort_key when speedups are available, and add
+  auto-discovery to test suites to prevent similar regressions
+  https://github.com/simplejson/simplejson/issues/173
+
 Version 3.11.0 released 2017-06-18
 
 * docstring fix in JSONEncoder
diff --git a/simplejson/_speedups.c b/simplejson/_speedups.c
--- a/simplejson/_speedups.c
+++ b/simplejson/_speedups.c
@@ -2678,17 +2678,17 @@
             if (!item_sort_key)
                 goto bail;
         }
-        if (item_sort_key == Py_None) {
-            Py_INCREF(Py_None);
-            s->item_sort_kw = Py_None;
-        }
-        else {
-            s->item_sort_kw = PyDict_New();
-            if (s->item_sort_kw == NULL)
-                goto bail;
-            if (PyDict_SetItemString(s->item_sort_kw, "key", item_sort_key))
-                goto bail;
-        }
+    }
+    if (item_sort_key == Py_None) {
+        Py_INCREF(Py_None);
+        s->item_sort_kw = Py_None;
+    }
+    else {
+        s->item_sort_kw = PyDict_New();
+        if (s->item_sort_kw == NULL)
+            goto bail;
+        if (PyDict_SetItemString(s->item_sort_kw, "key", item_sort_key))
+            goto bail;
     }
     Py_INCREF(sort_keys);
     s->sort_keys = sort_keys;
diff --git a/simplejson/tests/__init__.py b/simplejson/tests/__init__.py
--- a/simplejson/tests/__init__.py
+++ b/simplejson/tests/__init__.py
@@ -2,6 +2,7 @@
 import unittest
 import doctest
 import sys
+import os
 
 
 class NoExtensionTestSuite(unittest.TestSuite):
@@ -35,38 +36,13 @@
 
 def all_tests_suite():
     def get_suite():
+        suite_names = [
+            'simplejson.tests.%s' % (os.path.splitext(f)[0],)
+            for f in os.listdir(os.path.dirname(__file__))
+            if f.startswith('test_') and f.endswith('.py')
+        ]
         return additional_tests(
-            unittest.TestLoader().loadTestsFromNames([
-                'simplejson.tests.test_bitsize_int_as_string',
-                'simplejson.tests.test_bigint_as_string',
-                'simplejson.tests.test_check_circular',
-                'simplejson.tests.test_decode',
-                'simplejson.tests.test_default',
-                'simplejson.tests.test_dump',
-                'simplejson.tests.test_encode_basestring_ascii',
-                'simplejson.tests.test_encode_for_html',
-                'simplejson.tests.test_errors',
-                'simplejson.tests.test_fail',
-                'simplejson.tests.test_float',
-                'simplejson.tests.test_indent',
-                'simplejson.tests.test_iterable',
-                'simplejson.tests.test_pass1',
-                'simplejson.tests.test_pass2',
-                'simplejson.tests.test_pass3',
-                'simplejson.tests.test_recursion',
-                'simplejson.tests.test_scanstring',
-                'simplejson.tests.test_separators',
-                'simplejson.tests.test_speedups',
-                'simplejson.tests.test_str_subclass',
-                'simplejson.tests.test_unicode',
-                'simplejson.tests.test_decimal',
-                'simplejson.tests.test_tuple',
-                'simplejson.tests.test_namedtuple',
-                'simplejson.tests.test_tool',
-                'simplejson.tests.test_for_json',
-                'simplejson.tests.test_subclass',
-                'simplejson.tests.test_raw_json',
-            ]))
+            unittest.TestLoader().loadTestsFromNames(suite_names))
     suite = get_suite()
     import simplejson
     if simplejson._import_c_make_encoder() is None:
diff --git a/simplejson/tests/test_item_sort_key.py b/simplejson/tests/test_item_sort_key.py
--- a/simplejson/tests/test_item_sort_key.py
+++ b/simplejson/tests/test_item_sort_key.py
@@ -18,3 +18,10 @@
         self.assertEqual(
             '{"a": 1, "Array": [1, 5, 6, 9], "c": 5, "crate": "dog", "Jack": "jill", "pick": "axe", "tuple": [83, 12, 3], "zeak": "oh"}',
             json.dumps(a, item_sort_key=lambda kv: kv[0].lower()))
+
+    def test_item_sort_key_value(self):
+        # https://github.com/simplejson/simplejson/issues/173
+        a = {'a': 1, 'b': 0}
+        self.assertEqual(
+            '{"b": 0, "a": 1}',
+            json.dumps(a, item_sort_key=lambda kv: kv[1]))