Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Pass original error to process_tuple exception handler consistently

Instead of relying on exception context, pass the original error to
process_tuple exception handler explicitly.  This makes exception chain
semantically correct.
  • Loading branch information...
commit a845949dbe7affe95383146f616fdc8a46b3dc81 1 parent ab8471e
@elprans authored
View
20 postgresql/driver/pq3.py
@@ -417,7 +417,7 @@ def record_io_factory(self,
funpack = tuple(map(get1, column_io))
row_constructor = self.RowTypeFactory(attribute_map = attmap, composite_relid = composite_relid)
- def raise_pack_tuple_error(procs, tup, itemnum):
+ def raise_pack_tuple_error(procs, tup, itemnum, cause = None):
data = repr(tup[itemnum])
if len(data) > 80:
# Be sure not to fill screen with noise.
@@ -428,9 +428,9 @@ def raise_pack_tuple_error(procs, tup, itemnum):
(b'M', fmt_errmsg('pack', itemnum, attnames[itemnum], typnames[itemnum], composite_name),),
(b'W', data,),
(b'P', str(itemnum),)
- )))
+ )), cause = cause)
- def raise_unpack_tuple_error(procs, tup, itemnum):
+ def raise_unpack_tuple_error(procs, tup, itemnum, cause = None):
data = repr(tup[itemnum])
if len(data) > 80:
# Be sure not to fill screen with noise.
@@ -441,7 +441,7 @@ def raise_unpack_tuple_error(procs, tup, itemnum):
(b'M', fmt_errmsg('unpack', itemnum, attnames[itemnum], typnames[itemnum], composite_name),),
(b'W', data,),
(b'P', str(itemnum),),
- )))
+ )), cause = cause)
def unpack_a_record(data,
unpack = io_lib.record_unpack,
@@ -752,7 +752,7 @@ def _process_tuple_chunk_Row(self, x,
def _process_tuple_chunk(self, x, proc = process_chunk):
return proc(self._output_io, x, self._raise_column_tuple_error)
- def _raise_column_tuple_error(self, procs, tup, itemnum):
+ def _raise_column_tuple_error(self, procs, tup, itemnum, cause = None):
'for column processing'
# The element traceback will include the full list of parameters.
data = repr(tup[itemnum])
@@ -775,7 +775,7 @@ def _raise_column_tuple_error(self, procs, tup, itemnum):
(b'H', "Try casting the column to 'text'."),
(b'P', str(itemnum)),
))
- self.database.typio.raise_client_error(em, creator = self)
+ self.database.typio.raise_client_error(em, creator = self, cause = cause)
@property
def state(self):
@@ -1302,7 +1302,7 @@ def _pq_parameters(self, parameters, proc = process_tuple):
# process_tuple failed(exception). The parameters could not be packed.
# This function is called with the given information in the context
# of the original exception(to allow chaining).
- def _raise_parameter_tuple_error(self, procs, tup, itemnum):
+ def _raise_parameter_tuple_error(self, procs, tup, itemnum, cause = None):
# Find the SQL type name. This should *not* hit the server.
typ = self.database.typio.sql_type_from_oid(
self.pg_parameter_types[itemnum]
@@ -1325,11 +1325,11 @@ def _raise_parameter_tuple_error(self, procs, tup, itemnum):
(b'H', "Try casting the parameter to 'text', then to the target type."),
(b'P', str(itemnum))
))
- self.database.typio.raise_client_error(em, creator = self)
+ self.database.typio.raise_client_error(em, creator = self, cause = cause)
##
# Similar to the parameter variant.
- def _raise_column_tuple_error(self, procs, tup, itemnum):
+ def _raise_column_tuple_error(self, procs, tup, itemnum, cause = None):
# Find the SQL type name. This should *not* hit the server.
typ = self.database.typio.sql_type_from_oid(
self.pg_column_types[itemnum]
@@ -1352,7 +1352,7 @@ def _raise_column_tuple_error(self, procs, tup, itemnum):
(b'H', "Try casting the column to 'text'."),
(b'P', str(itemnum)),
))
- self.database.typio.raise_client_error(em, creator = self)
+ self.database.typio.raise_client_error(em, creator = self, cause = cause)
@property
def state(self) -> str:
View
5 postgresql/python/functools.py
@@ -56,9 +56,8 @@ def process_tuple(procs, tup, exception_handler, len = len, tuple = tuple):
if ob is None:
continue
r[i] = procs[i](ob)
- except Exception:
- # relying on __context__
- exception_handler(procs, tup, i)
+ except Exception as e:
+ exception_handler(procs, tup, i, cause = e)
raise RuntimeError("process_tuple exception handler failed to raise")
return tuple(r)
View
4 postgresql/test/test_types.py
@@ -341,7 +341,7 @@ def testExpectIO(self, samples):
class test_io(unittest.TestCase):
def test_process_tuple(self, pt = process_tuple):
- def funpass(procs, tup, col):
+ def funpass(procs, tup, col, cause):
pass
self.assertEqual(tuple(pt((),(), funpass)), ())
self.assertEqual(tuple(pt((int,),("100",), funpass)), (100,))
@@ -354,7 +354,7 @@ def funpass(procs, tup, col):
class ThisError(Exception):
pass
data = []
- def funraise(procs, tup, col):
+ def funraise(procs, tup, col, cause):
data.append((procs, tup, col))
raise ThisError
self.assertRaises(ThisError, pt, (int,), ("foo",), funraise)
Please sign in to comment.
Something went wrong with that request. Please try again.