Commits

Frank Wierzbicki committed e8b0f39

#2027 Fix handling of negatives for bin. Thanks Santoso Wijaya!

  • Participants
  • Parent commits 0d7595c

Comments (0)

Files changed (3)

File Lib/test/test_builtin.py

         class DerivedFromStr(str): pass
         self.assertEqual(format(0, DerivedFromStr('10')), '         0')
 
-    @unittest.skipIf(is_jython, "FIXME #1861: bin not implemented yet.")
     def test_bin(self):
         self.assertEqual(bin(0), '0b0')
         self.assertEqual(bin(1), '0b1')
         self.assertEqual(bin(-(2**65)), '-0b1' + '0' * 65)
         self.assertEqual(bin(-(2**65-1)), '-0b' + '1' * 65)
 
-    @unittest.skipIf(is_jython,
-                     "FIXME #1861: bytearray not implemented in Jython yet")
     def test_bytearray_translate(self):
         x = bytearray("abc")
         self.assertRaises(ValueError, x.translate, "1", 1)
 
 Jython 2.7b2
   Bugs Fixed
+    - [ 2027 ] Discrepancy in bin(-num) output
     - [ 2005 ] threading.Event object's wait([timeout]) function returns null instead of True/False.
     - [ 1926 ] Adjust MutableSet.pop test so we do not need to skip it
     - [ 2020 ] str.translate should delete characters in the second arg when table is None

File src/org/python/core/PyInteger.java

             }
 
             if (spec.alternate) {
-                if (radix == 2) {
-                    strValue = "0b" + strValue;
-                } else if (radix == 8) {
-                    strValue = "0o" + strValue;
-                } else if (radix == 16) {
-                    strValue = "0x" + strValue;
+                String strPrefix = "";
+                switch (radix) {
+                    case 2:
+                        strPrefix = "0b";
+                        break;
+                    case 8:
+                        strPrefix = "0o";
+                        break;
+                    case 16:
+                        strPrefix = "0x";
+                        break;
+                }
+
+                if (strValue.startsWith("-")) {
+                    //assert (sign < 0);
+                    if (!strPrefix.equals(""))
+                        strValue = "-" + strPrefix + strValue.substring(1, strValue.length());
+                } else {
+                    strValue = strPrefix + strValue;
                 }
             }
+
             if (spec.type == 'X') {
                 strValue = strValue.toUpperCase();
             }