Skip to content

Commit

Permalink
Merge pull request #832 from vinzenz/bytearray-string-concat
Browse files Browse the repository at this point in the history
Allow string + bytearray => bytearray. Fixes #780
  • Loading branch information
kmod committed Aug 14, 2015
2 parents 60850a5 + a8e6299 commit 27df1a7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/runtime/str.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,16 @@ extern "C" Box* strAdd(BoxedString* lhs, Box* _rhs) {
}

if (!PyString_Check(_rhs)) {
// Note: this is deliberately not returning NotImplemented, even though
// that would be more usual. I assume this behavior of CPython's is
// for backwards compatibility.
raiseExcHelper(TypeError, "cannot concatenate 'str' and '%s' objects", getTypeName(_rhs));
if (PyByteArray_Check(_rhs)) {
Box* rtn = PyByteArray_Concat(lhs, _rhs);
checkAndThrowCAPIException();
return rtn;
} else {
// Note: this is deliberately not returning NotImplemented, even though
// that would be more usual. I assume this behavior of CPython's is
// for backwards compatibility.
raiseExcHelper(TypeError, "cannot concatenate 'str' and '%s' objects", getTypeName(_rhs));
}
}

BoxedString* rhs = static_cast<BoxedString*>(_rhs);
Expand Down
13 changes: 13 additions & 0 deletions test/tests/bytearray_str_concat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
assert(('abc' + bytearray('def')) == bytearray('abcdef'))
assert((bytearray('abc') + 'def') == bytearray('abcdef'))
try:
u'abc' + bytearray('def')
assert(False)
except TypeError:
pass

try:
bytearray('abc') + u'def'
assert(False)
except TypeError:
pass

0 comments on commit 27df1a7

Please sign in to comment.