]> granicus.if.org Git - python/commitdiff
Issue #1692335: Move initial args assignment to BaseException.__new__
authorRichard Oudkerk <shibturn@gmail.com>
Sat, 28 Jul 2012 16:45:28 +0000 (17:45 +0100)
committerRichard Oudkerk <shibturn@gmail.com>
Sat, 28 Jul 2012 16:45:28 +0000 (17:45 +0100)
to help pickling of naive subclasses.

Lib/test/test_exceptions.py
Misc/NEWS
Objects/exceptions.c

index 97762f98305027cb8e45dbc01614672bd3eaa2c7..0b1fd1b8b9bc214df2fe75ceb3dc9c4129a44640 100644 (file)
@@ -10,6 +10,15 @@ import errno
 from test.support import (TESTFN, unlink, run_unittest, captured_output,
                           gc_collect, cpython_only, no_tracing)
 
+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):
@@ -296,6 +305,10 @@ class ExceptionTests(unittest.TestCase):
                 {'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:
             # More tests are in test_WindowsError
@@ -316,7 +329,8 @@ class ExceptionTests(unittest.TestCase):
                 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:
index cabea97150d2a4bc4587d36d668e4730cb2cbf89..5201d4930ff757b157bee8094dcd8de6bf24773c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Beta 2?
 Core and Builtins
 -----------------
 
+- Issue #1692335: Move initial args assignment to
+  BaseException.__new__ to help pickling of naive subclasses.
+
 - Issue #12834: Fix PyBuffer_ToContiguous() for non-contiguous arrays.
 
 - Issue #15456: Fix code __sizeof__ after #12399 change.
index 5c85f10444a00dea2650d99120060882b7a6be4a..b7e11f85bfebe2297631c58302757988031685e3 100644 (file)
@@ -44,6 +44,12 @@ BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
     self->traceback = self->cause = self->context = NULL;
     self->suppress_context = 0;
 
+    if (args) {
+        self->args = args;
+        Py_INCREF(args);
+        return (PyObject *)self;
+    }
+
     self->args = PyTuple_New(0);
     if (!self->args) {
         Py_DECREF(self);
@@ -56,12 +62,15 @@ BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 static int
 BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
 {
+    PyObject *tmp;
+
     if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
         return -1;
 
-    Py_XDECREF(self->args);
+    tmp = self->args;
     self->args = args;
     Py_INCREF(self->args);
+    Py_XDECREF(tmp);
 
     return 0;
 }