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.

classification
Title: Same MemoryError object gets thrown from different places.
Type: Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: SylvainDe, r.david.murray, vstinner
Priority: normal Keywords:

Created on 2015-06-29 14:42 by SylvainDe, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg245940 - (view) Author: SylvainDe (SylvainDe) * Date: 2015-06-29 14:42
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 ).
msg245948 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-06-29 16:11
If the same memory exception object were not used, we'd have to allocate memory for the new one...
History
Date User Action Args
2022-04-11 14:58:18adminsetgithub: 68717
2015-06-29 16:12:18vstinnersetnosy: + vstinner
2015-06-29 16:11:11r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg245948

resolution: not a bug
stage: resolved
2015-06-29 14:42:55SylvainDecreate