Discussion:
[Python-3000-checkins] r65341 - in python/branches/py3k: Lib/test/test_exceptions.py Python/errors.c
amaury.forgeotdarc
2008-07-31 22:56:03 UTC
Permalink
Author: amaury.forgeotdarc
Date: Fri Aug 1 00:56:02 2008
New Revision: 65341

Log:
Correct one of the "MemoryError oddities":
the traceback would grow each time a MemoryError is raised.


Modified:
python/branches/py3k/Lib/test/test_exceptions.py
python/branches/py3k/Python/errors.c

Modified: python/branches/py3k/Lib/test/test_exceptions.py
==============================================================================
--- python/branches/py3k/Lib/test/test_exceptions.py (original)
+++ python/branches/py3k/Lib/test/test_exceptions.py Fri Aug 1 00:56:02 2008
@@ -596,6 +596,24 @@
"Exception ValueError: ValueError() "
"in <class 'KeyError'> ignored\n")

+
+ def test_MemoryError(self):
+ # PyErr_NoMemory always raises the same exception instance.
+ # Check that the traceback is not doubled.
+ import traceback
+ def raiseMemError():
+ try:
+ "a" * (sys.maxsize // 2)
+ except MemoryError as e:
+ tb = e.__traceback__
+ else:
+ self.fail("Should have raises a MemoryError")
+ return traceback.format_tb(tb)
+
+ tb1 = raiseMemError()
+ tb2 = raiseMemError()
+ self.assertEqual(tb1, tb2)
+
def test_main():
run_unittest(ExceptionTests)


Modified: python/branches/py3k/Python/errors.c
==============================================================================
--- python/branches/py3k/Python/errors.c (original)
+++ python/branches/py3k/Python/errors.c Fri Aug 1 00:56:02 2008
@@ -321,7 +321,17 @@

/* raise the pre-allocated instance if it still exists */
if (PyExc_MemoryErrorInst)
+ {
+ /* Clear the previous traceback, otherwise it will be appended
+ * to the current one.
+ *
+ * The following statement is not likely to raise any error;
+ * if it does, we simply discard it.
+ */
+ PyException_SetTraceback(PyExc_MemoryErrorInst, Py_None);
+
PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
+ }
else
/* this will probably fail since there's no memory and hee,
hee, we have to instantiate this class

Loading...