Commit 3b637401 authored by Krzysztof Klinikowski's avatar Krzysztof Klinikowski

Update chr to be compatible with CPython

parent 6acfb996
......@@ -202,11 +202,12 @@ Box* open(Box* arg1, Box* arg2) {
extern "C" Box* chr(Box* arg) {
if (arg->cls != int_cls) {
fprintf(stderr, "TypeError: coercing to Unicode: need string of buffer, %s found\n", getTypeName(arg)->c_str());
raiseExcHelper(TypeError, "");
raiseExcHelper(TypeError, "an integer is required");
}
i64 n = static_cast<BoxedInt*>(arg)->n;
RELEASE_ASSERT(n >= 0 && n < 256, "");
if (n < 0 || n >= 256) {
raiseExcHelper(ValueError, "chr() arg not in range(256)");
}
return boxString(std::string(1, (char)n));
}
......
assert chr(0) == '\x00'
assert chr(1) == '\x01'
assert chr(128) == '\x80'
assert chr(255) == '\xff'
try:
chr('a')
assert False
except TypeError as e:
assert e.message == 'an integer is required'
try:
chr(256)
assert 'chr(256) should throw chr() arg not in range(256)' == False
except ValueError as e:
assert e.message == 'chr() arg not in range(256)'
try:
chr(-1)
assert 'chr(-1) should throw chr() arg not in range(256)' == False
except ValueError as e:
assert e.message == 'chr() arg not in range(256)'
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