Skip to content

Commit 132a9f5

Browse files
committed
py: int - fix base conversion with base and sigil
1 parent c61c054 commit 132a9f5

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

py/int.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func IntFromString(str string, base int) (Object, error) {
101101
}
102102
}
103103

104-
// Get rid of leading sigils and set base
104+
// Get rid of leading sigils and set convertBase
105105
if len(s) > 1 && s[0] == '0' {
106106
switch s[1] {
107107
case 'x', 'X':
@@ -115,7 +115,9 @@ func IntFromString(str string, base int) (Object, error) {
115115
}
116116
if base != 0 && base != convertBase {
117117
// int("0xFF", 10)
118-
goto error
118+
// int("0b", 16)
119+
convertBase = base // ignore sigil
120+
goto nosigil
119121
}
120122
s = s[2:]
121123
if len(s) == 0 {
@@ -161,7 +163,7 @@ func IntFromString(str string, base int) (Object, error) {
161163
}
162164
return (*BigInt)(x).MaybeInt(), nil
163165
error:
164-
return nil, ExceptionNewf(ValueError, "invalid literal for int() with base %d: '%s'", base, str)
166+
return nil, ExceptionNewf(ValueError, "invalid literal for int() with base %d: '%s'", convertBase, str)
165167
}
166168

167169
// Truncates to go int

py/tests/int.py

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
assert int("F", 16) == 15
105105
assert int("0xF", 16) == 15
106106
assert int("0XF", 0) == 15
107+
assert int("0b", 16) == 11
107108
assertRaises(ValueError, int, "0xF", 10)
108109

109110
assert int("77", 8) == 63

0 commit comments

Comments
 (0)