From d713928c79fe8cd7406835ccf324ca557dc0c732 Mon Sep 17 00:00:00 2001 From: Juarez Rudsatz Date: Tue, 4 Aug 2020 14:58:32 -0300 Subject: [PATCH] avro: also improve TypeError handling --- petl/io/avro.py | 29 ++++++++++++++++++----------- petl/test/io/test_avro.py | 23 ++++++++++++++++++----- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/petl/io/avro.py b/petl/io/avro.py index 745c395b..b1331604 100644 --- a/petl/io/avro.py +++ b/petl/io/avro.py @@ -334,9 +334,12 @@ def _write_toavro(table, target, mode, schema, sample, try: writer.write(record) num = num + 1 - except ValueError as err: - details = _get_error_details(target, num, err, record, schema) - _raise_error(details) + except ValueError as verr: + vmsg = _get_error_details(target, num, verr, record, schema) + _raise_error(ValueError, vmsg) + except TypeError as terr: + tmsg = _get_error_details(target, num, terr, record, schema) + _raise_error(TypeError, tmsg) # finish writing writer.flush() @@ -541,14 +544,18 @@ def _get_schema_header_names(schema): return header -def _raise_error(details): - if PY3: - raise ValueError(details).with_traceback(sys.exc_info()[2]) - else: - import traceback - stacktrace = traceback.format_exc(sys.exc_info()) - err = "%s%s" % (stacktrace, details) - raise ValueError(err) +def _raise_error(ErrorType, new_message): + """Works like raise Excetion(msg) from prev_exp in python3.""" + exinf = sys.exc_info() + tracebk = exinf[2] + try: + if PY3: + raise ErrorType(new_message).with_traceback(tracebk) + # Python2 compatibility workaround + exec('raise ErrorType, new_message, tracebk') + finally: + exinf = None + tracebk = None # noqa: F841 def _ordered_dict_iterator(table): diff --git a/petl/test/io/test_avro.py b/petl/test/io/test_avro.py index d44f34dc..cf775cc7 100644 --- a/petl/test/io/test_avro.py +++ b/petl/test/io/test_avro.py @@ -101,14 +101,27 @@ def test_appendavro22(): def test_appendavro10(): _append_to_avro_file(table11, table12, schema1) - def test_toavro_troubleshooting(): - wrong_schema = dict(schema0) - schema_fields = wrong_schema['fields'] + def test_toavro_troubleshooting10(): + nullable_schema = dict(schema0) + schema_fields = nullable_schema['fields'] for field in schema_fields: field['type'] = ['null', 'string'] try: - _write_temp_avro_file(table1, wrong_schema) - except ValueError: + _write_temp_avro_file(table1, nullable_schema) + except ValueError as vex: + bob = "%s" % vex + assert 'Bob' in bob + return + assert False, 'Failed schema conversion' + + def test_toavro_troubleshooting11(): + table0 = list(table1) + table0[3][1] = None + try: + _write_temp_avro_file(table0, schema1) + except TypeError as tex: + joe = "%s" % tex + assert 'Joe' in joe return assert False, 'Failed schema conversion'