Commit 06339b3b authored by Rudi Chen's avatar Rudi Chen

Handle checks of string padding functions better.

Allow widths represented by subclasses of int and throw an exception
instead of aborting if the wrong types are given.
parent df8cac1c
......@@ -1226,9 +1226,13 @@ Box* str_richcompare(Box* lhs, Box* rhs, int op) {
#define JUST_RIGHT 1
#define JUST_CENTER 2
static Box* pad(BoxedString* self, Box* width, Box* fillchar, int justType) {
assert(width->cls == int_cls);
assert(PyString_Check(fillchar));
assert(static_cast<BoxedString*>(fillchar)->size() == 1);
if (!PyInt_Check(width)) {
raiseExcHelper(TypeError, "an integer is required");
}
if (!PyString_Check(fillchar) || static_cast<BoxedString*>(fillchar)->size() != 1) {
raiseExcHelper(TypeError, "must be char, not %s", fillchar->cls->tp_name);
}
int64_t curWidth = self->size();
int64_t targetWidth = static_cast<BoxedInt*>(width)->n;
......
......@@ -112,6 +112,31 @@ def test_just_funcs(s, w):
t5 = s.rjust(w)
t6 = s.center(w)
try:
print s.ljust("a string")
except TypeError as e:
print e
try:
print s.rjust("a string")
except TypeError:
print e
try:
print s.center("a string")
except TypeError:
print e
try:
print s.ljust(10, 12345)
except TypeError:
print e
try:
print s.rjust(10, 12345)
except TypeError:
print e
try:
print s.center(10, 12345)
except TypeError:
print e
print t1, t1 == s, t1 is s, type(t1)
print t2, t2 == s, t2 is s, type(t2)
print t3, t3 == s, t3 is s, type(t3)
......
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