From ffc87281982cf8125786a3fa34c6b9e81122d97e Mon Sep 17 00:00:00 2001 From: Yaron de Leeuw Date: Wed, 14 Nov 2018 21:10:08 -0500 Subject: [PATCH] Fix excpetion style to be Python3 compatible by running 2to3 -nw -f except . Part of #412 --- examples/shuttle_from_xmlfeed.py | 2 +- feedvalidator.py | 4 ++-- kmlwriter.py | 4 ++-- merge.py | 6 +++--- misc/find_pytz_transition_times.py | 2 +- misc/sql_loop.py | 2 +- schedule_viewer.py | 2 +- setup.py | 2 +- tests/testkmlwriter.py | 2 +- tests/testmerge.py | 4 ++-- tests/transitfeed/testloader.py | 12 ++++++------ tests/transitfeed/testschedule.py | 6 +++--- tests/transitfeed/teststop.py | 2 +- tests/transitfeed/testtrip.py | 4 ++-- tests/util.py | 2 +- transitfeed/trip.py | 2 +- transitfeed/util.py | 4 ++-- 17 files changed, 31 insertions(+), 31 deletions(-) diff --git a/examples/shuttle_from_xmlfeed.py b/examples/shuttle_from_xmlfeed.py index fc766e26..da470a66 100755 --- a/examples/shuttle_from_xmlfeed.py +++ b/examples/shuttle_from_xmlfeed.py @@ -28,7 +28,7 @@ try: import xml.etree.ElementTree as ET # python 2.5 -except ImportError, e: +except ImportError as e: import elementtree.ElementTree as ET # older pythons diff --git a/feedvalidator.py b/feedvalidator.py index f2c0fd00..1d265859 100755 --- a/feedvalidator.py +++ b/feedvalidator.py @@ -333,7 +333,7 @@ def FormatException(self, e, output): transitfeed.EncodeUnicode(table_header)) output.append('%s\n' % transitfeed.EncodeUnicode(table_data)) - except AttributeError, e: + except AttributeError as e: pass # Hope this was getting an attribute from e ;-) output.append('
\n') @@ -513,7 +513,7 @@ def RunValidationOutputToFilename(feed, options, output_filename): output_file = open(output_filename, 'w') exit_code = RunValidationOutputToFile(feed, options, output_file) output_file.close() - except IOError, e: + except IOError as e: print 'Error while writing %s: %s' % (output_filename, e) output_filename = None exit_code = 2 diff --git a/kmlwriter.py b/kmlwriter.py index 5b011d1e..4c413abe 100755 --- a/kmlwriter.py +++ b/kmlwriter.py @@ -71,7 +71,7 @@ try: import xml.etree.ElementTree as ET # python 2.5 -except ImportError, e: +except ImportError as e: import elementtree.ElementTree as ET # older pythons import extensions.googletransit as googletransit import optparse @@ -790,7 +790,7 @@ def main(): try: loader = transitfeed.Loader(input_path) feed = loader.Load() - except transitfeed.ExceptionWithContext, e: + except transitfeed.ExceptionWithContext as e: print >>sys.stderr, ( "\n\nGTFS feed must load without any errors.\n" "While loading %s the following error was found:\n%s\n%s\n" % diff --git a/merge.py b/merge.py index 0bb759f7..d900676d 100755 --- a/merge.py +++ b/merge.py @@ -358,7 +358,7 @@ def LoadWithoutErrors(path, memory_db): memory_db=memory_db, problems=loading_problem_handler, extra_validation=True).Load() - except transitfeed.ExceptionWithContext, e: + except transitfeed.ExceptionWithContext as e: print >>sys.stderr, ( "\n\nFeeds to merge must load without any errors.\n" "While loading %s the following error was found:\n%s\n%s\n" % @@ -509,7 +509,7 @@ def _SchemedMerge(self, scheme, a, b): b_attr = getattr(b, attr, None) try: merged_attr = merger(a_attr, b_attr) - except MergeError, merge_error: + except MergeError as merge_error: raise MergeError("Attribute '%s' could not be merged: %s." % ( attr, merge_error)) setattr(migrated, attr, merged_attr) @@ -549,7 +549,7 @@ def _MergeSameId(self): try: self._Add(a, b, self._MergeEntities(a, b)) self._num_merged += 1 - except MergeError, merge_error: + except MergeError as merge_error: a_not_merged.append(a) b_not_merged.append(b) self._ReportSameIdButNotMerged(self._GetId(a), merge_error) diff --git a/misc/find_pytz_transition_times.py b/misc/find_pytz_transition_times.py index 527d80ab..13c90be5 100644 --- a/misc/find_pytz_transition_times.py +++ b/misc/find_pytz_transition_times.py @@ -82,7 +82,7 @@ def show_tran(dist, tz_name, tran, inf): from_noon.append((distance_from_noon, tz_name, tran, inf)) distance_from_midnight = hour_dist(average, 0) from_midnight.append((distance_from_midnight, tz_name, tran, inf)) - except Exception, e: + except Exception as e: print "Trouble with %s %s %s: %s" % (tz_name, tran, inf, e) diff --git a/misc/sql_loop.py b/misc/sql_loop.py index 69996ac4..4f1cec0e 100755 --- a/misc/sql_loop.py +++ b/misc/sql_loop.py @@ -65,7 +65,7 @@ def default(self, line): print s[0:2000] else: print s - except sqlite.DatabaseError, e: + except sqlite.DatabaseError as e: print "error %s" % e diff --git a/schedule_viewer.py b/schedule_viewer.py index c1f4c289..63ad6208 100755 --- a/schedule_viewer.py +++ b/schedule_viewer.py @@ -119,7 +119,7 @@ def do_GET(self): try: f, mime_type = self.OpenFile(m.group(1)) return self.handle_static_file_GET(f, mime_type) - except IOError, e: + except IOError as e: print "Error: unable to open %s" % m.group(1) # Ignore and treat as 404 diff --git a/setup.py b/setup.py index 676f0385..d131e4d1 100755 --- a/setup.py +++ b/setup.py @@ -31,7 +31,7 @@ try: import py2exe has_py2exe = True -except ImportError, e: +except ImportError as e: # Won't be able to generate win32 exe has_py2exe = False diff --git a/tests/testkmlwriter.py b/tests/testkmlwriter.py index 0f12215a..584b9d8a 100644 --- a/tests/testkmlwriter.py +++ b/tests/testkmlwriter.py @@ -28,7 +28,7 @@ try: import xml.etree.ElementTree as ET # python 2.5 -except ImportError, e: +except ImportError as e: import elementtree.ElementTree as ET # older pythons diff --git a/tests/testmerge.py b/tests/testmerge.py index d7ec7b35..9068b895 100644 --- a/tests/testmerge.py +++ b/tests/testmerge.py @@ -290,7 +290,7 @@ def BadMerger(a, b): try: self.ds._SchemedMerge(scheme, a, b) - except merge.MergeError, merge_error: + except merge.MergeError as merge_error: error_text = str(merge_error) self.assert_(reason in error_text) self.assert_(attribute_name in error_text) @@ -779,7 +779,7 @@ def testMergeDifferentTypes(self): try: self.fm.MergeSchedules() self.fail("Expecting MergeError") - except merge.SameIdButNotMerged, merge_error: + except merge.SameIdButNotMerged as merge_error: self.assertTrue(("%s" % merge_error).find("location_type") != -1) def AssertS1ParentIsS2(self): diff --git a/tests/transitfeed/testloader.py b/tests/transitfeed/testloader.py index e1572a34..2250121d 100644 --- a/tests/transitfeed/testloader.py +++ b/tests/transitfeed/testloader.py @@ -263,7 +263,7 @@ def runTest(self): try: loader.Load() self.fail('FeedNotFound exception expected') - except transitfeed.FeedNotFound, e: + except transitfeed.FeedNotFound as e: self.assertEqual(feed_name, e.feed_name) class LoadUnknownFormatTestCase(util.TestCase): @@ -276,7 +276,7 @@ def runTest(self): try: loader.Load() self.fail('UnknownFormat exception expected') - except transitfeed.UnknownFormat, e: + except transitfeed.UnknownFormat as e: self.assertEqual(feed_name, e.feed_name) class LoadUnrecognizedColumnsTestCase(util.TestCase): @@ -382,7 +382,7 @@ def runTest(self): try: loader.Load() self.fail('EmptyFile exception expected') - except transitfeed.EmptyFile, e: + except transitfeed.EmptyFile as e: self.assertEqual('agency.txt', e.file_name) @@ -395,7 +395,7 @@ def runTest(self): try: loader.Load() self.fail('MissingColumn exception expected') - except transitfeed.MissingColumn, e: + except transitfeed.MissingColumn as e: self.assertEqual('agency.txt', e.file_name) self.assertEqual('agency_name', e.column_name) @@ -422,7 +422,7 @@ def runTest(self): loader.Load() # TODO: make sure processing proceeds beyond the problem self.fail('FileFormat exception expected') - except transitfeed.FileFormat, e: + except transitfeed.FileFormat as e: # make sure these don't raise an exception self.assertTrue(re.search(r'encoded in utf-16', e.FormatProblem())) e.FormatContext() @@ -452,7 +452,7 @@ def runTest(self): try: loader.Load() self.fail('FileFormat exception expected') - except transitfeed.FileFormat, e: + except transitfeed.FileFormat as e: self.assertTrue(re.search(r'contains a null', e.FormatProblem())) # make sure these don't raise an exception e.FormatContext() diff --git a/tests/transitfeed/testschedule.py b/tests/transitfeed/testschedule.py index 5f046c82..0b9fb90a 100644 --- a/tests/transitfeed/testschedule.py +++ b/tests/transitfeed/testschedule.py @@ -739,7 +739,7 @@ def runTest(self): try: schedule.AddTripObject(trip2) self.fail("Expected Duplicate ID validation failure") - except transitfeed.DuplicateID, e: + except transitfeed.DuplicateID as e: self.assertEqual("trip_id", e.column_name) self.assertEqual("SAMPLE_TRIP", e.value) @@ -756,7 +756,7 @@ def runTest(self): try: schedule.AddRouteObject(route) self.fail("Expected validation error") - except transitfeed.InvalidValue, e: + except transitfeed.InvalidValue as e: self.assertEqual('agency_id', e.column_name) self.assertEqual(None, e.value) @@ -775,7 +775,7 @@ def runTest(self): try: schedule.AddRouteObject(route) self.fail("Expected validation error") - except transitfeed.InvalidValue, e: + except transitfeed.InvalidValue as e: self.assertEqual('agency_id', e.column_name) self.assertEqual(None, e.value) diff --git a/tests/transitfeed/teststop.py b/tests/transitfeed/teststop.py index 7c83291c..88eb7b39 100644 --- a/tests/transitfeed/teststop.py +++ b/tests/transitfeed/teststop.py @@ -453,7 +453,7 @@ def testWithoutSchedule(self): try: t = stop.new_column self.fail('Expecting AttributeError') - except AttributeError, e: + except AttributeError as e: pass # Expected stop.stop_id = 'a' stop.stop_name = 'my stop' diff --git a/tests/transitfeed/testtrip.py b/tests/transitfeed/testtrip.py index 81e14bd3..57c5a953 100644 --- a/tests/transitfeed/testtrip.py +++ b/tests/transitfeed/testtrip.py @@ -643,7 +643,7 @@ def ExpectInvalidValue(self, start_time, end_time, headway, trip = transitfeed.Trip() trip.AddFrequency(start_time, end_time, headway) self.fail("Expected InvalidValue error on %s" % column_name) - except transitfeed.InvalidValue, e: + except transitfeed.InvalidValue as e: self.assertEqual(column_name, e.column_name) self.assertEqual(value, e.value) self.assertEqual(0, len(trip.GetFrequencyTuples())) @@ -653,7 +653,7 @@ def ExpectMissingValue(self, start_time, end_time, headway, column_name): trip = transitfeed.Trip() trip.AddFrequency(start_time, end_time, headway) self.fail("Expected MissingValue error on %s" % column_name) - except transitfeed.MissingValue, e: + except transitfeed.MissingValue as e: self.assertEqual(column_name, e.column_name) self.assertEqual(0, len(trip.GetFrequencyTuples())) diff --git a/tests/util.py b/tests/util.py index 17a802aa..452d2007 100644 --- a/tests/util.py +++ b/tests/util.py @@ -56,7 +56,7 @@ def check_call(cmd, expected_retcode=0, stdin_str="", **kwargs): **kwargs) (out, err) = p.communicate(stdin_str) retcode = p.returncode - except Exception, e: + except Exception as e: raise Exception("When running %s: %s" % (cmd, e)) if retcode < 0: raise Exception( diff --git a/transitfeed/trip.py b/transitfeed/trip.py index 9a298ead..51300dc4 100755 --- a/transitfeed/trip.py +++ b/transitfeed/trip.py @@ -604,7 +604,7 @@ def ValidateStopTimesSequenceHasIncreasingTimeAndDistance(self, try: route_type = self._schedule.GetRoute(self.route_id).route_type max_speed = route_class._ROUTE_TYPES[route_type]['max_speed'] - except KeyError, e: + except KeyError as e: # If route_type cannot be found, assume it is 0 (Tram) for checking # speeds between stops. max_speed = route_class._ROUTE_TYPES[0]['max_speed'] diff --git a/transitfeed/util.py b/transitfeed/util.py index 320ebf5d..f96cae09 100644 --- a/transitfeed/util.py +++ b/transitfeed/util.py @@ -91,7 +91,7 @@ def RunWithCrashHandler(f): for local_name, local_val in frame_obj.f_locals.items(): try: truncated_val = str(local_val)[0:500] - except Exception, e: + except Exception as e: dump.append(' Exception in str(%s): %s' % (local_name, e)) else: if len(truncated_val) >= 500: @@ -556,7 +556,7 @@ def writerow(self, row): encoded_row.append(s) try: self.writer.writerow(encoded_row) - except Exception, e: + except Exception as e: print 'error writing %s as %s' % (row, encoded_row) raise e