This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients pitrou, serhiy.storchaka, skrah, vstinner
Date 2013-11-19.23:51:16
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1384905077.13.0.251325220327.issue19568@psf.upfronthosting.co.za>
In-reply-to
Content
> Can you suppress the MemoryError if deletion succeeds? That would be ok IMO.

In case (1) and (3) (see below), the MemoryError might be suppressed, but I prefer to keep the exception to warn the user that something bad happened and its program will slowly leak memory.

Reminder that I found the error using failmalloc which is stupid tool: it injects random MemoryError errors. In practice, realloc() should never fail if the buffer is shrinked (becomes smaller). So I don't think that you should worry too much on the exact behaviour of this case :-)

Only the case (3) (bytearray grows) may appear in practice, and this case is handleded correctly (do nothing if realloc() failed, leave the bytearray object unchanged).


> Yes, but if a MemoryError occurred during slice assignment b[3:6] = b'ab', the bytearray will be not consistent. For consistency we should copy replacement bytes.

Correct, fixed in new patch.


New patch copying bytes if the memory allocation failed but we cannot restore removed bytes (case 2). I also added a long comment explaining the issue.

Behaviour with the patch 3:

Case 1: growth<0, lo == 0

>>> b=bytearray(b'asciiUNICODE'); b[:5]=b'#'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError
>>> b
bytearray(b'asciiUNICODE')

=> b leaved unchanged (ok)


Case 2: growth<0, lo != 0

>>> b=bytearray(b'asciiUNICODE'); b[1:5]=b'#'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError
>>> b
bytearray(b'a#UNICODE')

=> function succeed, but exception raised (and memory block not resized)


Case 3: growth>0

>>> b=bytearray(b'asciiUNICODE'); b[5:5]=b'###'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError
>>> b
bytearray(b'asciiUNICODE')

=> b leaved unchanged (ok)
History
Date User Action Args
2013-11-19 23:51:17vstinnersetrecipients: + vstinner, pitrou, skrah, serhiy.storchaka
2013-11-19 23:51:17vstinnersetmessageid: <1384905077.13.0.251325220327.issue19568@psf.upfronthosting.co.za>
2013-11-19 23:51:17vstinnerlinkissue19568 messages
2013-11-19 23:51:16vstinnercreate