Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #456 from jarondl/exceptions
Browse files Browse the repository at this point in the history
Fix excpetion style to be Python3 compatible
  • Loading branch information
stotala authored Dec 19, 2018
2 parents 54a4081 + ffc8728 commit 4c277fa
Show file tree
Hide file tree
Showing 17 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion examples/shuttle_from_xmlfeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
4 changes: 2 additions & 2 deletions feedvalidator.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def FormatException(self, e, output):
transitfeed.EncodeUnicode(table_header))
output.append('<tr>%s</tr></table>\n' %
transitfeed.EncodeUnicode(table_data))
except AttributeError, e:
except AttributeError as e:
pass # Hope this was getting an attribute from e ;-)
output.append('<br></li>\n')

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions kmlwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" %
Expand Down
6 changes: 3 additions & 3 deletions merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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" %
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion misc/find_pytz_transition_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
2 changes: 1 addition & 1 deletion misc/sql_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion schedule_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/testkmlwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
4 changes: 2 additions & 2 deletions tests/testmerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
12 changes: 6 additions & 6 deletions tests/transitfeed/testloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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)


Expand All @@ -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)

Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions tests/transitfeed/testschedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion tests/transitfeed/teststop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions tests/transitfeed/testtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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()))

Expand Down
2 changes: 1 addition & 1 deletion tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion transitfeed/trip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
4 changes: 2 additions & 2 deletions transitfeed/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 4c277fa

Please sign in to comment.