[New-bugs-announce] [issue24529] Same MemoryError object gets thrown from different places.

SylvainDe report at bugs.python.org
Mon Jun 29 16:42:55 CEST 2015


New submission from SylvainDe:

Disclaimer: This is very minor, impacts only Python 2 and is maybe not even a bug.

Context: I was "randomly" changing attribute of Exception objects when I discovered something I did not expect : sometimes when an exception occurs, a new object is raised (which is what I expected), sometimes a previously thrown exception is re-thrown (which surprised me).

More specifically, MemoryError objects seem to be reused while most of the other exceptions I've played with where newly created.

One can easily see this happening in the following piece of code :
 - first a NameError is caught and updated
 - then, we run the same code and print the re-caught exception : it is a new one
 - then, a MemoryError is caught and updated
 - then, we run the same code and print the re-caught exception : it is the same as previously.


<pre>
from __future__ import print_function

old_e = None
try:
    foobar
except NameError as old_e:
    print("old_e:", old_e)
    old_e.args = tuple(['NEW VALUE'])
    print("old_e:", old_e)
try:
    foobar
except NameError as new_e:
    print("new_e:", new_e)
    print(old_e is new_e)

old_e = None
try:
    l = [0] * 999999999999999999
except MemoryError as old_e:
    print("old_e:", old_e)
    old_e.args = tuple(['NEW VALUE'])
    print("old_e:", old_e)
try:
    l = [0] * 999999999999999999
except MemoryError as new_e:
    print("new_e:", new_e)
    print(old_e is new_e)
</pre>



For the record, I am quite aware that this shouldn't impact anyone using Python normally. I was just messing around as part of a toy project where I try to enhance exception messages for a better user experience (  https://github.com/SylvainDe/DidYouMean-Python ).

----------
components: Interpreter Core
messages: 245940
nosy: SylvainDe
priority: normal
severity: normal
status: open
title: Same MemoryError object gets thrown from different places.
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24529>
_______________________________________


More information about the New-bugs-announce mailing list