|
From: Robert W. B. <rb...@di...> - 2001-04-30 13:07:34
|
Hello all,
The current atol in PyString doesn't handle hexstring's.
Use of hex string in long() currently looks like:
>>> long("0x12", 16)
Traceback (innermost last):
File "<console>", line 1, in ?
ValueError: invalid literal for __int__: 0x12
It also seems the ValueError argument should be '__long__'
instead of '__int__', is that right?
Proposed change would allows hex strings when base=16 is
specified, and changes '__int__' to '__long__'. This means:
>>> long("0x12")
Traceback (innermost last):
File "<console>", line 1, in ?
ValueError: invalid literal for __long__: 0x12
>>>
>>> long("0x12", 16)
18L
>>>
=========begin diff -u===========
--- local/org/python/core/PyString.java Sun Apr 29 21:15:08 2001
+++ org/python/core/PyString.java Sun Apr 29 21:45:46 2001
@@ -1326,6 +1326,10 @@
} else
base = 8;
}
+ if (base == 16) {
+ if (str.charAt(b+1) == 'x' || str.charAt(b+1) == 'X')
+ b += 2;
+ }
if (base < 2 || base > 36)
throw Py.ValueError("invalid base for long literal:" + base);
@@ -1340,9 +1344,9 @@
bi = new java.math.BigInteger(str, base);
return new PyLong(bi);
} catch (NumberFormatException exc) {
- throw Py.ValueError("invalid literal for __int__: "+str);
+ throw Py.ValueError("invalid literal for __long__: "+str);
} catch (StringIndexOutOfBoundsException exc) {
- throw Py.ValueError("invalid literal for __int__: "+str);
+ throw Py.ValueError("invalid literal for __long__: "+str);
}
}
=========end diff=========
Cheers,
Robert
|