File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -410,6 +410,8 @@ def test_concat(self):
410410 self .assertRaises (TypeError , lambda : b1 + "def" )
411411 self .assertRaises (TypeError , lambda : "abc" + b2 )
412412
413+ self .assertEqual (self .type2test () + memoryview (b'' ), b'' )
414+
413415 def test_repeat (self ):
414416 for b in b"abc" , self .type2test (b"abc" ):
415417 self .assertEqual (b * 3 , b"abcabcabc" )
@@ -1439,7 +1441,19 @@ class SubBytes(bytes):
14391441 self .assertNotEqual (id (s ), id (s * 2 ))
14401442
14411443 @support .cpython_only
1442- def test_repeat_singleton (self ):
1444+ def test_concat_cpython (self ):
1445+ # Test optimizations
1446+ empty = b''
1447+ s = b"abc"
1448+ self .assertIs (s + empty , s )
1449+ self .assertIs (empty + s , s )
1450+
1451+ self .assertIs (empty + bytearray (b'' ), empty )
1452+ self .assertIs (empty + memoryview (b'' ), empty )
1453+
1454+ @support .cpython_only
1455+ def test_repeat_cpython (self ):
1456+ # Test optimizations
14431457 for b in b'' , b'x' , b"abc" :
14441458 self .assertIs (b * 1 , b )
14451459 self .assertIs (b * 0 , b'' )
Original file line number Diff line number Diff line change @@ -1614,10 +1614,15 @@ _PyBytes_Concat(PyObject *a, PyObject *b)
16141614 goto done ;
16151615 }
16161616
1617- result = bytes_alloc (va .len + vb .len );
1618- if (result != NULL ) {
1619- memcpy (PyBytes_AS_STRING (result ), va .buf , va .len );
1620- memcpy (PyBytes_AS_STRING (result ) + va .len , vb .buf , vb .len );
1617+ if ((va .len + vb .len ) != 0 ) {
1618+ result = bytes_alloc (va .len + vb .len );
1619+ if (result != NULL ) {
1620+ memcpy (PyBytes_AS_STRING (result ), va .buf , va .len );
1621+ memcpy (PyBytes_AS_STRING (result ) + va .len , vb .buf , vb .len );
1622+ }
1623+ }
1624+ else {
1625+ result = bytes_get_empty ();
16211626 }
16221627
16231628 done :
You can’t perform that action at this time.
0 commit comments