Commit 020ce1ba authored by Marius Wachtler's avatar Marius Wachtler

Fix int.__oct__() and int.__hex__() for negative numbers

parent f4f23657
......@@ -882,7 +882,12 @@ extern "C" Box* intHex(BoxedInt* self) {
getTypeName(self));
char buf[80];
int len = snprintf(buf, sizeof(buf), "0x%lx", self->n);
int len = 0;
bool is_negative = self->n < 0;
if (is_negative)
len = snprintf(buf, sizeof(buf), "-0x%lx", std::abs(self->n));
else
len = snprintf(buf, sizeof(buf), "0x%lx", self->n);
return new BoxedString(std::string(buf, len));
}
......@@ -892,7 +897,12 @@ extern "C" Box* intOct(BoxedInt* self) {
getTypeName(self));
char buf[80];
int len = snprintf(buf, sizeof(buf), "%#lo", self->n);
int len = 0;
bool is_negative = self->n < 0;
if (is_negative)
len = snprintf(buf, sizeof(buf), "-%#lo", std::abs(self->n));
else
len = snprintf(buf, sizeof(buf), "%#lo", self->n);
return new BoxedString(std::string(buf, len));
}
......
......@@ -14,7 +14,8 @@ print 1 ** 0
print 0 ** 0
print -1 ** 0
for i in (-10, 10, 0, -15):
print i, i.__hex__(), i.__oct__()
# Testing int.__new__:
class C(int):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment