New issue 2324: bytearray().replace('a', 'bc') raises MemoryError
https://bitbucket.org/pypy/pypy/issues/2324/bytearray-replace-a-bc-raises-memoryerror

Jason Madden:

Given an empty bytearry, trying to replace any string with a longer string 
raises a MemoryError in every version of PyPy from 2.5.0 onward (tested 2.5.0 
on OS X and Linux, 4.0.0 and 4.0.1, 5.0.0 and 5.3.0 on OS X):

```python
Python 2.7.10 (5f8302b8bf9f, Nov 18 2015, 10:38:03)
[PyPy 4.0.1 with GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>> bytearray().replace("\\", '\\\\')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError
>>>> bytearray().replace("a", 'b')
bytearray(b'')
>>>> bytearray().replace("a", '\\\\')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError
>>>> bytearray().replace("\\", 'b')
bytearray(b'')
>>>> bytearray().replace("\\", 'b')
bytearray(b'')
>>>> bytearray().replace('a', 'bc')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError
```

These cases all work in CPython. 

This came up in PyMySQL which is uses `value.replace('\\', '\\\\')` to do 
string escaping. See https://github.com/PyMySQL/PyMySQL/issues/474

Curiously, replacing an empty substring with a longer string does work:
```python
Python 2.7.10 (c09c19272c99, Jun 07 2016, 16:26:05)
[PyPy 5.3.0 with GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>> bytearray().replace('', 'bcd')
bytearray(b'bcd')
```


_______________________________________________
pypy-issue mailing list
pypy-issue@python.org
https://mail.python.org/mailman/listinfo/pypy-issue

Reply via email to