Commits

Armin Rigo  committed 97d11d9

(arre, arigo)

Special-case hex() and oct() of sys.maxint-1. Avoids segfaults of pypy-c.

  • Participants
  • Parent commits 72bf7bc

Comments (0)

Files changed (3)

File pypy/rpython/lltypesystem/ll_str.py

     sign = 0
     if i < 0:
         sign = 1
-        i = -i
+        i = r_uint(-i)
+    else:
+        i = r_uint(i)
     if i == 0:
         len = 1
         temp[0] = '0'
     else:
         while i:
-            temp[len] = hex_chars[i%16]
-            i //= 16
+            temp[len] = hex_chars[i & 0xf]
+            i >>= 4
             len += 1
     len += sign
     if addPrefix:
     sign = 0
     if i < 0:
         sign = 1
-        i = -i
+        i = r_uint(-i)
+    else:
+        i = r_uint(i)
     while i:
-        temp[len] = hex_chars[i%8]
-        i //= 8
+        temp[len] = hex_chars[i & 0x7]
+        i >>= 3
         len += 1
     len += sign
     if addPrefix:

File pypy/rpython/ootypesystem/ll_str.py

+import sys
 from pypy.rpython.ootypesystem.ootype import new, oostring, StringBuilder
+from pypy.rpython.ootypesystem.ootype import make_string
 
 def ll_int_str(repr, i):
     return ll_int2dec(i)
 def ll_int2dec(i):
     return oostring(i, 10)
 
+SPECIAL_VALUE     = -sys.maxint-1
+SPECIAL_VALUE_HEX = make_string(
+    '-' + hex(sys.maxint+1).replace('L', '').replace('l', ''))
+SPECIAL_VALUE_OCT = make_string(
+    '-' + oct(sys.maxint+1).replace('L', '').replace('l', ''))
+
 def ll_int2hex(i, addPrefix):
     if not addPrefix:
         return oostring(i, 16)
 
     buf = new(StringBuilder)
     if i<0:
+        if i == SPECIAL_VALUE:
+            return SPECIAL_VALUE_HEX
         i = -i
         buf.ll_append_char('-')
 
 
     buf = new(StringBuilder)
     if i<0:
+        if i == SPECIAL_VALUE:
+            return SPECIAL_VALUE_OCT
         i = -i
         buf.ll_append_char('-')
 

File pypy/rpython/test/test_rint.py

         res = self.interpret(dummy, [-123])
         assert self.ll_to_string(res) == '-0x7b'
 
+        res = self.interpret(dummy, [-sys.maxint-1])
+        res = self.ll_to_string(res)
+        assert res == '-0x8' + '0' * (len(res)-4)
+
     def test_oct_of_int(self):
         def dummy(i):
             return oct(i)
         res = self.interpret(dummy, [-123])
         assert self.ll_to_string(res) == '-0173'
 
+        res = self.interpret(dummy, [-sys.maxint-1])
+        res = self.ll_to_string(res)
+        assert res == '-' + oct(sys.maxint+1).replace('L', '').replace('l', '')
+
     def test_unsigned(self):
         def dummy(i):
             i = r_uint(i)