[Python-checkins] cpython (3.2): #17296: backport fix for issue 1692335, naive exception pickling.

r.david.murray python-checkins at python.org
Wed Feb 27 15:04:40 CET 2013


http://hg.python.org/cpython/rev/2c9f7ed28384
changeset:   82413:2c9f7ed28384
branch:      3.2
parent:      82404:ae25a38e6c17
user:        R David Murray <rdmurray at bitdance.com>
date:        Wed Feb 27 08:57:09 2013 -0500
summary:
  #17296: backport fix for issue 1692335, naive exception pickling.

files:
  Lib/test/test_exceptions.py |  16 +++++++++++++++-
  Misc/NEWS                   |   3 +++
  Objects/exceptions.c        |  11 ++++++++++-
  3 files changed, 28 insertions(+), 2 deletions(-)


diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -10,6 +10,15 @@
 from test.support import (TESTFN, captured_output, check_impl_detail,
                           cpython_only, gc_collect, run_unittest, unlink)
 
+class NaiveException(Exception):
+    def __init__(self, x):
+        self.x = x
+
+class SlottedNaiveException(Exception):
+    __slots__ = ('x',)
+    def __init__(self, x):
+        self.x = x
+
 # XXX This is not really enough, each *operation* should be tested!
 
 class ExceptionTests(unittest.TestCase):
@@ -272,6 +281,10 @@
                 {'args' : ('\u3042', 0, 1, 'ouch'),
                  'object' : '\u3042', 'reason' : 'ouch',
                  'start' : 0, 'end' : 1}),
+            (NaiveException, ('foo',),
+                {'args': ('foo',), 'x': 'foo'}),
+            (SlottedNaiveException, ('foo',),
+                {'args': ('foo',), 'x': 'foo'}),
         ]
         try:
             exceptionList.append(
@@ -291,7 +304,8 @@
                 raise
             else:
                 # Verify module name
-                self.assertEqual(type(e).__module__, 'builtins')
+                if not type(e).__name__.endswith('NaiveException'):
+                    self.assertEqual(type(e).__module__, 'builtins')
                 # Verify no ref leaks in Exc_str()
                 s = str(e)
                 for checkArgName in expected:
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #1692335: Move initial args assignment to
+  BaseException.__new__ to help pickling of naive subclasses.
+
 - Issue #17275: Corrected class name in init error messages of the C version of
   BufferedWriter and BufferedRandom.
 
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -29,6 +29,12 @@
     self->dict = NULL;
     self->traceback = self->cause = self->context = NULL;
 
+    if (args) {
+        self->args = args;
+        Py_INCREF(args);
+        return (PyObject *)self;
+    }
+
     self->args = PyTuple_New(0);
     if (!self->args) {
         Py_DECREF(self);
@@ -41,12 +47,15 @@
 static int
 BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
 {
+    PyObject *tmp;
+
     if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
         return -1;
 
-    Py_DECREF(self->args);
+    tmp = self->args;
     self->args = args;
     Py_INCREF(self->args);
+    Py_XDECREF(tmp);
 
     return 0;
 }

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list