Fixed pickling of exceptions with no pgerror/pgcode set

Fixes ticket #170.
This commit is contained in:
Daniele Varrazzo 2013-07-19 15:57:27 +01:00
parent 81e8b869d0
commit 345077d5f7
3 changed files with 32 additions and 2 deletions

7
NEWS
View File

@ -1,6 +1,13 @@
Current release Current release
--------------- ---------------
What's new in psycopg 2.5.2
^^^^^^^^^^^^^^^^^^^^^^^^^^^
- Fixed segfault pickling the exception raised on connection error
(:ticket:`#170`).
What's new in psycopg 2.5.1 What's new in psycopg 2.5.1
^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -163,8 +163,16 @@ psyco_error_reduce(errorObject *self)
if (2 != PyTuple_GET_SIZE(tuple)) { goto exit; } if (2 != PyTuple_GET_SIZE(tuple)) { goto exit; }
if (!(dict = PyDict_New())) { goto error; } if (!(dict = PyDict_New())) { goto error; }
if (0 != PyDict_SetItemString(dict, "pgerror", self->pgerror)) { goto error; } if (self->pgerror) {
if (0 != PyDict_SetItemString(dict, "pgcode", self->pgcode)) { goto error; } if (0 != PyDict_SetItemString(dict, "pgerror", self->pgerror)) {
goto error;
}
}
if (self->pgcode) {
if (0 != PyDict_SetItemString(dict, "pgcode", self->pgcode)) {
goto error;
}
}
{ {
PyObject *newtuple; PyObject *newtuple;

View File

@ -279,6 +279,21 @@ class ExceptionsTestCase(ConnectingTestCase):
self.assertEqual(e.pgcode, e1.pgcode) self.assertEqual(e.pgcode, e1.pgcode)
self.assert_(e1.cursor is None) self.assert_(e1.cursor is None)
@skip_before_python(2, 5)
def test_pickle_connection_error(self):
# segfaults on psycopg 2.5.1 - see ticket #170
import pickle
try:
psycopg2.connect('dbname=nosuchdatabasemate')
except psycopg2.Error, exc:
e = exc
e1 = pickle.loads(pickle.dumps(e))
self.assertEqual(e.pgerror, e1.pgerror)
self.assertEqual(e.pgcode, e1.pgcode)
self.assert_(e1.cursor is None)
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)