Commit 9f8f77c1 authored by Kevin Modzelewski's avatar Kevin Modzelewski

__str__ and __repr__ can return ascii-compatible unicode

parent fa8eb0de
...@@ -1862,8 +1862,13 @@ extern "C" BoxedString* str(Box* obj) { ...@@ -1862,8 +1862,13 @@ extern "C" BoxedString* str(Box* obj) {
obj = callattrInternal(obj, &str_str, CLASS_ONLY, NULL, ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL); obj = callattrInternal(obj, &str_str, CLASS_ONLY, NULL, ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL);
} }
if (obj->cls != str_cls) { if (isSubclass(obj->cls, unicode_cls)) {
raiseExcHelper(TypeError, "__str__ did not return a string!"); obj = PyUnicode_AsASCIIString(obj);
checkAndThrowCAPIException();
}
if (!isSubclass(obj->cls, str_cls)) {
raiseExcHelper(TypeError, "__str__ returned non-string (type %s)", obj->cls->tp_name);
} }
return static_cast<BoxedString*>(obj); return static_cast<BoxedString*>(obj);
} }
...@@ -1874,8 +1879,13 @@ extern "C" BoxedString* repr(Box* obj) { ...@@ -1874,8 +1879,13 @@ extern "C" BoxedString* repr(Box* obj) {
obj = callattrInternal(obj, &repr_str, CLASS_ONLY, NULL, ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL); obj = callattrInternal(obj, &repr_str, CLASS_ONLY, NULL, ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL);
if (obj->cls != str_cls) { if (isSubclass(obj->cls, unicode_cls)) {
raiseExcHelper(TypeError, "__repr__ did not return a string!"); obj = PyUnicode_AsASCIIString(obj);
checkAndThrowCAPIException();
}
if (!isSubclass(obj->cls, str_cls)) {
raiseExcHelper(TypeError, "__repr__ returned non-string (type %s)", obj->cls->tp_name);
} }
return static_cast<BoxedString*>(obj); return static_cast<BoxedString*>(obj);
} }
......
...@@ -1214,7 +1214,10 @@ extern "C" Box* strLen(BoxedString* self) { ...@@ -1214,7 +1214,10 @@ extern "C" Box* strLen(BoxedString* self) {
extern "C" Box* strStr(BoxedString* self) { extern "C" Box* strStr(BoxedString* self) {
assert(isSubclass(self->cls, str_cls)); assert(isSubclass(self->cls, str_cls));
return self; if (self->cls == str_cls)
return self;
return new BoxedString(self->s);
} }
static bool _needs_escaping[256] static bool _needs_escaping[256]
...@@ -1488,7 +1491,7 @@ extern "C" Box* strNew(BoxedClass* cls, Box* obj) { ...@@ -1488,7 +1491,7 @@ extern "C" Box* strNew(BoxedClass* cls, Box* obj) {
assert(isSubclass(cls, str_cls)); assert(isSubclass(cls, str_cls));
Box* rtn = str(obj); Box* rtn = str(obj);
assert(rtn->cls == str_cls); assert(isSubclass(rtn->cls, str_cls));
if (cls == str_cls) if (cls == str_cls)
return rtn; return rtn;
......
...@@ -99,3 +99,41 @@ print "hello world".rsplit(u'l') ...@@ -99,3 +99,41 @@ print "hello world".rsplit(u'l')
with open(u"/dev/null", u"r") as f: with open(u"/dev/null", u"r") as f:
print f.read() print f.read()
class CustomRepr(object):
def __init__(self, x):
self.x = x
def __str__(self):
return self.x
def __repr__(self):
return self.x
print repr(str(CustomRepr(u'')))
print repr(repr(CustomRepr(u'')))
try:
str(CustomRepr(u'\u0180'))
except Exception as e:
print type(e), e
try:
repr(CustomRepr(u'\u0180'))
except Exception as e:
print type(e), e
try:
str(CustomRepr(1))
except Exception as e:
print type(e), e
try:
repr(CustomRepr(1))
except Exception as e:
print type(e), e
class MyStr(str):
pass
print type(str(CustomRepr(MyStr("hi"))))
print type(MyStr("hi").__str__())
print type(str(MyStr("hi")))
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