Skip to content

Commit d2a66ef

Browse files
committed
result of string.__add__ has to be str
1 parent 55b5ccb commit d2a66ef

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_string.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,3 +1155,21 @@ def test_replace_count():
11551155
s = "1 2 3 1 2 3 1 2 3 1 2 3"
11561156
s = s.replace("1", "1 _", s.count("1"))
11571157
assert s == "1 _ 2 3 1 _ 2 3 1 _ 2 3 1 _ 2 3"
1158+
1159+
def test_str_add_result_type():
1160+
class S(str): pass
1161+
1162+
assert (S('') + S('a')).__class__ == str
1163+
assert (S('a') + S('')).__class__ == str
1164+
assert (S('') + S('')).__class__ == str
1165+
assert (S('a') + S('a')).__class__ == str
1166+
1167+
assert ('' + S('')).__class__ == str
1168+
assert ('a' + S('')).__class__ == str
1169+
assert (S('') + '').__class__ == str
1170+
assert (S('') + 'a').__class__ == str
1171+
1172+
assert ('' + '').__class__ == str
1173+
assert ('' + 'a').__class__ == str
1174+
assert ('a' + '').__class__ == str
1175+
assert ('a' + 'a').__class__ == str

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/StringBuiltins.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,27 +405,29 @@ String doSSSimple(String self, String other) {
405405
}
406406

407407
@Specialization(guards = "!concatGuard(self, other.getCharSequence())")
408-
Object doSSSimple(String self, PString other) {
408+
String doSSSimple(String self, PString other) {
409409
if (LazyString.length(self, leftProfile1, leftProfile2) == 0) {
410-
return other;
410+
// result type has to be str
411+
return toString(other.getCharSequence());
411412
}
412413
return self;
413414
}
414415

415416
@Specialization(guards = "!concatGuard(self.getCharSequence(), other)")
416-
Object doSSSimple(PString self, String other) {
417+
String doSSSimple(PString self, String other) {
417418
if (LazyString.length(self.getCharSequence(), leftProfile1, leftProfile2) == 0) {
418419
return other;
419420
}
420-
return self;
421+
// result type has to be str
422+
return toString(self.getCharSequence());
421423
}
422424

423425
@Specialization(guards = "!concatGuard(self.getCharSequence(), other.getCharSequence())")
424-
PString doSSSimple(PString self, PString other) {
426+
String doSSSimple(PString self, PString other) {
425427
if (LazyString.length(self.getCharSequence(), leftProfile1, leftProfile2) == 0) {
426-
return other;
428+
return toString(other.getCharSequence());
427429
}
428-
return self;
430+
return toString(self.getCharSequence());
429431
}
430432

431433
@Specialization(guards = "concatGuard(self.getCharSequence(), other)")
@@ -474,6 +476,11 @@ private static String stringConcat(CharSequence left, CharSequence right) {
474476
return left.toString() + right.toString();
475477
}
476478

479+
@TruffleBoundary
480+
private static String toString(CharSequence cs) {
481+
return cs.toString();
482+
}
483+
477484
@Specialization(guards = "isString(self)")
478485
Object doSNative(VirtualFrame frame, Object self, PythonAbstractNativeObject other,
479486
@Cached CastToJavaStringNode cast,

0 commit comments

Comments
 (0)