Permalink
Browse files

Fixed #18013 -- Use the new 'as' syntax for exceptions.

Thanks Clueless for the initial patch.
Note that unittest has been purposely left out (external package only used by Python 2.6).
  • Loading branch information...
1 parent eefb00f commit 3904b74a3f2f92fefe1d39281ed683c52f2fef03 @claudep claudep committed Apr 28, 2012
Showing with 306 additions and 354 deletions.
  1. +1 −1 django/conf/__init__.py
  2. +1 −1 django/contrib/admin/tests.py
  3. +1 −1 django/contrib/admin/util.py
  4. +2 −2 django/contrib/admin/views/main.py
  5. +1 −1 django/contrib/admindocs/views.py
  6. +2 −2 django/contrib/auth/__init__.py
  7. +1 −1 django/contrib/comments/views/comments.py
  8. +1 −1 django/contrib/formtools/wizard/storage/__init__.py
  9. +2 −2 django/contrib/gis/db/backends/base.py
  10. +1 −1 django/contrib/gis/db/backends/oracle/introspection.py
  11. +1 −3 django/contrib/gis/db/backends/postgis/operations.py
  12. +1 −1 django/contrib/gis/db/backends/spatialite/base.py
  13. +1 −1 django/contrib/gis/db/backends/spatialite/operations.py
  14. +2 −2 django/contrib/gis/geometry/backend/__init__.py
  15. +1 −1 django/contrib/gis/management/commands/ogrinspect.py
  16. +15 −60 django/contrib/gis/tests/test_measure.py
  17. +3 −3 django/contrib/gis/utils/layermapping.py
  18. +1 −1 django/contrib/markup/tests.py
  19. +1 −1 django/contrib/messages/storage/__init__.py
  20. +1 −1 django/contrib/sessions/backends/file.py
  21. +1 −1 django/contrib/staticfiles/finders.py
  22. +1 −1 django/contrib/staticfiles/handlers.py
  23. +1 −1 django/core/cache/__init__.py
  24. +1 −1 django/core/files/move.py
  25. +4 −4 django/core/files/storage.py
  26. +1 −1 django/core/files/uploadedfile.py
  27. +4 −3 django/core/files/uploadhandler.py
  28. +3 −3 django/core/handlers/base.py
  29. +1 −1 django/core/mail/__init__.py
  30. +1 −1 django/core/mail/backends/filebased.py
  31. +1 −1 django/core/management/__init__.py
  32. +3 −3 django/core/management/base.py
  33. +1 −1 django/core/management/commands/createcachetable.py
  34. +1 −1 django/core/management/commands/dumpdata.py
  35. +1 −1 django/core/management/commands/flush.py
  36. +1 −1 django/core/management/commands/loaddata.py
  37. +1 −1 django/core/management/commands/runserver.py
  38. +3 −3 django/core/management/commands/syncdb.py
  39. +3 −3 django/core/management/templates.py
  40. +1 −1 django/core/serializers/json.py
  41. +1 −1 django/core/serializers/pyyaml.py
  42. +3 −3 django/core/servers/basehttp.py
  43. +1 −1 django/core/servers/fastcgi.py
  44. +2 −2 django/core/signing.py
  45. +3 −3 django/core/urlresolvers.py
  46. +2 −2 django/core/validators.py
  47. +2 −2 django/db/backends/creation.py
  48. +7 −7 django/db/backends/mysql/base.py
  49. +8 −8 django/db/backends/oracle/base.py
  50. +5 −5 django/db/backends/oracle/creation.py
  51. +6 −6 django/db/backends/postgresql_psycopg2/base.py
  52. +6 −6 django/db/backends/sqlite3/base.py
  53. +1 −1 django/db/backends/sqlite3/creation.py
  54. +4 −4 django/db/models/base.py
  55. +1 −1 django/db/models/fields/__init__.py
  56. +2 −2 django/db/models/query.py
  57. +1 −1 django/db/models/sql/query.py
  58. +2 −2 django/db/utils.py
  59. +1 −1 django/dispatch/dispatcher.py
  60. +2 −2 django/dispatch/saferef.py
  61. +2 −2 django/forms/fields.py
  62. +2 −2 django/forms/forms.py
  63. +1 −1 django/forms/formsets.py
  64. +3 −3 django/forms/models.py
  65. +1 −1 django/forms/util.py
  66. +2 −2 django/http/__init__.py
  67. +3 −3 django/http/multipartparser.py
  68. +3 −3 django/template/base.py
  69. +1 −1 django/template/context.py
  70. +2 −2 django/template/debug.py
  71. +1 −1 django/template/defaultfilters.py
  72. +5 −5 django/template/defaulttags.py
  73. +3 −3 django/template/loader.py
  74. +1 −1 django/template/loaders/app_directories.py
  75. +4 −4 django/test/_doctest.py
  76. +1 −1 django/test/client.py
  77. +3 −3 django/test/testcases.py
  78. +2 −2 django/utils/daemonize.py
  79. +1 −1 django/utils/decorators.py
  80. +14 −14 django/utils/dictconfig.py
  81. +1 −1 django/utils/encoding.py
  82. +1 −1 django/views/debug.py
  83. +2 −2 docs/ref/models/instances.txt
  84. +1 −1 extras/csrf_migration_helper.py
  85. +5 −5 tests/modeltests/basic/tests.py
  86. +1 −1 tests/modeltests/get_or_create/tests.py
  87. +1 −1 tests/modeltests/invalid_models/tests.py
  88. +2 −2 tests/modeltests/lookup/tests.py
  89. +2 −4 tests/modeltests/select_for_update/tests.py
  90. +1 −1 tests/modeltests/validation/models.py
  91. +16 −16 tests/modeltests/validation/test_error_messages.py
  92. +3 −3 tests/modeltests/validation/test_unique.py
  93. +1 −1 tests/regressiontests/app_loading/tests.py
  94. +3 −3 tests/regressiontests/backends/tests.py
  95. +6 −6 tests/regressiontests/file_uploads/tests.py
  96. +1 −1 tests/regressiontests/forms/tests/error_messages.py
  97. +1 −1 tests/regressiontests/forms/tests/validators.py
  98. +2 −2 tests/regressiontests/middleware/tests.py
  99. +2 −2 tests/regressiontests/middleware_exceptions/tests.py
  100. +1 −1 tests/regressiontests/model_fields/tests.py
  101. +1 −1 tests/regressiontests/model_forms_regress/tests.py
  102. +1 −1 tests/regressiontests/servers/tests.py
  103. +1 −1 tests/regressiontests/settings_tests/tests.py
  104. +1 −1 tests/regressiontests/templates/nodelist.py
  105. +8 −8 tests/regressiontests/templates/tests.py
  106. +50 −50 tests/regressiontests/test_client_regress/models.py
  107. +2 −2 tests/regressiontests/urlpatterns_reverse/tests.py
@@ -91,7 +91,7 @@ def __init__(self, settings_module):
try:
mod = importlib.import_module(self.SETTINGS_MODULE)
- except ImportError, e:
+ except ImportError as e:
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
# Settings that should be converted into tuples if they're mistakenly entered
@@ -14,7 +14,7 @@ def setUpClass(cls):
mod = import_module(module)
WebDriver = getattr(mod, attr)
cls.selenium = WebDriver()
- except Exception, e:
+ except Exception as e:
raise SkipTest('Selenium webdriver "%s" not installed or not '
'operational: %s' % (cls.webdriver_class, str(e)))
super(AdminSeleniumWebDriverTestCase, cls).setUpClass()
@@ -156,7 +156,7 @@ def collect(self, objs, source_attr=None, **kwargs):
self.add_edge(None, obj)
try:
return super(NestedObjects, self).collect(objs, source_attr=source_attr, **kwargs)
- except models.ProtectedError, e:
+ except models.ProtectedError as e:
self.protected.update(e.protected_objects)
def related_objects(self, related, objs):
@@ -139,7 +139,7 @@ def get_filters(self, request):
use_distinct = (use_distinct or
lookup_needs_distinct(self.lookup_opts, key))
return filter_specs, bool(filter_specs), lookup_params, use_distinct
- except FieldDoesNotExist, e:
+ except FieldDoesNotExist as e:
raise IncorrectLookupParameters(e)
def get_query_string(self, new_params=None, remove=None):
@@ -316,7 +316,7 @@ def get_query_set(self, request):
# Allow certain types of errors to be re-raised as-is so that the
# caller can treat them in a special way.
raise
- except Exception, e:
+ except Exception as e:
# Every other error is caught with a naked except, because we don't
# have any other way of validating lookup parameters. They might be
# invalid if the keyword arguments are incorrect, or if the values
@@ -318,7 +318,7 @@ def load_all_installed_template_libraries():
for library_name in libraries:
try:
lib = template.get_library(library_name)
- except template.InvalidTemplateLibrary, e:
+ except template.InvalidTemplateLibrary:
pass
def get_return_data_type(func_name):
@@ -11,9 +11,9 @@ def load_backend(path):
module, attr = path[:i], path[i+1:]
try:
mod = import_module(module)
- except ImportError, e:
+ except ImportError as e:
raise ImproperlyConfigured('Error importing authentication backend %s: "%s"' % (path, e))
- except ValueError, e:
+ except ValueError:
raise ImproperlyConfigured('Error importing authentication backends. Is AUTHENTICATION_BACKENDS a correctly defined list or tuple?')
try:
cls = getattr(mod, attr)
@@ -66,7 +66,7 @@ def post_comment(request, next=None, using=None):
return CommentPostBadRequest(
"No object matching content-type %r and object PK %r exists." % \
(escape(ctype), escape(object_pk)))
- except (ValueError, ValidationError), e:
+ except (ValueError, ValidationError) as e:
return CommentPostBadRequest(
"Attempting go get content-type %r and object PK %r exists raised %s" % \
(escape(ctype), escape(object_pk), e.__class__.__name__))
@@ -10,7 +10,7 @@ def get_storage(path, *args, **kwargs):
module, attr = path[:i], path[i+1:]
try:
mod = import_module(module)
- except ImportError, e:
+ except ImportError as e:
raise MissingStorageModule(
'Error loading storage %s: "%s"' % (module, e))
try:
@@ -164,13 +164,13 @@ def srs(self):
try:
self._srs = gdal.SpatialReference(self.wkt)
return self.srs
- except Exception, msg:
+ except Exception as msg:
pass
try:
self._srs = gdal.SpatialReference(self.proj4text)
return self.srs
- except Exception, msg:
+ except Exception as msg:
pass
raise Exception('Could not get OSR SpatialReference from WKT: %s\nError:\n%s' % (self.wkt, msg))
@@ -16,7 +16,7 @@ def get_geometry_type(self, table_name, geo_col):
cursor.execute('SELECT "DIMINFO", "SRID" FROM "USER_SDO_GEOM_METADATA" WHERE "TABLE_NAME"=%s AND "COLUMN_NAME"=%s',
(table_name.upper(), geo_col.upper()))
row = cursor.fetchone()
- except Exception, msg:
+ except Exception as msg:
raise Exception('Could not find entry in USER_SDO_GEOM_METADATA corresponding to "%s"."%s"\n'
'Error message: %s.' % (table_name, geo_col, msg))
@@ -107,9 +107,7 @@ def __init__(self, connection):
'Was the database created from a spatial database '
'template?' % self.connection.settings_dict['NAME']
)
- except Exception, e:
- # TODO: Raise helpful exceptions as they become known.
- raise
+ # TODO: Raise helpful exceptions as they become known.
# PostGIS-specific operators. The commented descriptions of these
# operators come from Section 7.6 of the PostGIS 1.4 documentation.
@@ -56,7 +56,7 @@ def _cursor(self):
cur = self.connection.cursor(factory=SQLiteCursorWrapper)
try:
cur.execute("SELECT load_extension(%s)", (self.spatialite_lib,))
- except Exception, msg:
+ except Exception as msg:
raise ImproperlyConfigured('Unable to load the SpatiaLite library extension '
'"%s" because: %s' % (self.spatialite_lib, msg))
return cur
@@ -122,7 +122,7 @@ def __init__(self, connection):
self.spatial_version = version
except ImproperlyConfigured:
raise
- except Exception, msg:
+ except Exception as msg:
raise ImproperlyConfigured('Cannot determine the SpatiaLite version for the "%s" '
'database (error was "%s"). Was the SpatiaLite initialization '
'SQL loaded on this database?' %
@@ -6,10 +6,10 @@
try:
module = import_module('.%s' % geom_backend, 'django.contrib.gis.geometry.backend')
-except ImportError, e:
+except ImportError:
try:
module = import_module(geom_backend)
- except ImportError, e_user:
+ except ImportError:
raise ImproperlyConfigured('Could not import user-defined GEOMETRY_BACKEND '
'"%s".' % geom_backend)
@@ -87,7 +87,7 @@ def handle(self, *args, **options):
# Getting the OGR DataSource from the string parameter.
try:
ds = gdal.DataSource(data_source)
- except gdal.OGRException, msg:
+ except gdal.OGRException as msg:
raise CommandError(msg)
# Whether the user wants to generate the LayerMapping dictionary as well.
@@ -62,32 +62,20 @@ def testAddition(self):
d4 -= d1
self.assertEqual(d4.m, -200)
- try:
+ with self.assertRaises(TypeError):
d5 = d1 + 1
- except TypeError, e:
- pass
- else:
self.fail('Distance + number should raise TypeError')
- try:
+ with self.assertRaises(TypeError):
d5 = d1 - 1
- except TypeError, e:
- pass
- else:
self.fail('Distance - number should raise TypeError')
- try:
+ with self.assertRaises(TypeError):
d1 += 1
- except TypeError, e:
- pass
- else:
self.fail('Distance += number should raise TypeError')
- try:
+ with self.assertRaises(TypeError):
d1 -= 1
- except TypeError, e:
- pass
- else:
self.fail('Distance -= number should raise TypeError')
def testMultiplication(self):
@@ -110,25 +98,16 @@ def testMultiplication(self):
self.assertTrue(isinstance(a5, Area))
self.assertEqual(a5.sq_m, 100*10)
- try:
+ with self.assertRaises(TypeError):
d1 *= D(m=1)
- except TypeError, e:
- pass
- else:
self.fail('Distance *= Distance should raise TypeError')
- try:
+ with self.assertRaises(TypeError):
d5 = d1 / D(m=1)
- except TypeError, e:
- pass
- else:
self.fail('Distance / Distance should raise TypeError')
- try:
+ with self.assertRaises(TypeError):
d1 /= D(m=1)
- except TypeError, e:
- pass
- else:
self.fail('Distance /= Distance should raise TypeError')
def testUnitConversions(self):
@@ -217,32 +196,20 @@ def testAddition(self):
a4 -= a1
self.assertEqual(a4.sq_m, -200)
- try:
+ with self.assertRaises(TypeError):
a5 = a1 + 1
- except TypeError, e:
- pass
- else:
self.fail('Area + number should raise TypeError')
- try:
+ with self.assertRaises(TypeError):
a5 = a1 - 1
- except TypeError, e:
- pass
- else:
self.fail('Area - number should raise TypeError')
- try:
+ with self.assertRaises(TypeError):
a1 += 1
- except TypeError, e:
- pass
- else:
self.fail('Area += number should raise TypeError')
- try:
+ with self.assertRaises(TypeError):
a1 -= 1
- except TypeError, e:
- pass
- else:
self.fail('Area -= number should raise TypeError')
def testMultiplication(self):
@@ -261,32 +228,20 @@ def testMultiplication(self):
a4 /= 5
self.assertEqual(a4.sq_m, 10)
- try:
+ with self.assertRaises(TypeError):
a5 = a1 * A(sq_m=1)
- except TypeError, e:
- pass
- else:
self.fail('Area * Area should raise TypeError')
- try:
+ with self.assertRaises(TypeError):
a1 *= A(sq_m=1)
- except TypeError, e:
- pass
- else:
self.fail('Area *= Area should raise TypeError')
- try:
+ with self.assertRaises(TypeError):
a5 = a1 / A(sq_m=1)
- except TypeError, e:
- pass
- else:
self.fail('Area / Area should raise TypeError')
- try:
+ with self.assertRaises(TypeError):
a1 /= A(sq_m=1)
- except TypeError, e:
- pass
- else:
self.fail('Area /= Area should raise TypeError')
def testUnitConversions(self):
@@ -430,7 +430,7 @@ def coord_transform(self):
# Creating the CoordTransform object
return CoordTransform(self.source_srs, target_srs)
- except Exception, msg:
+ except Exception as msg:
raise LayerMapError('Could not translate between the data source and model geometry: %s' % msg)
def geometry_field(self):
@@ -514,7 +514,7 @@ def _save(feat_range=default_range, num_feat=0, num_saved=0):
# Getting the keyword arguments
try:
kwargs = self.feature_kwargs(feat)
- except LayerMapError, msg:
+ except LayerMapError as msg:
# Something borked the validation
if strict: raise
elif not silent:
@@ -553,7 +553,7 @@ def _save(feat_range=default_range, num_feat=0, num_saved=0):
if verbose: stream.write('%s: %s\n' % (is_update and 'Updated' or 'Saved', m))
except SystemExit:
raise
- except Exception, msg:
+ except Exception as msg:
if self.transaction_mode == 'autocommit':
# Rolling back the transaction so that other model saves
# will work.
@@ -87,7 +87,7 @@ def test_docutils(self):
# Docutils v0.4 and earlier
self.assertEqual(rendered, """<p>Paragraph 1</p>
<p>Paragraph 2 with a <a class="reference" href="http://www.example.com/">link</a></p>""")
- except AssertionError, e:
+ except AssertionError:
# Docutils from SVN (which will become 0.5)
self.assertEqual(rendered, """<p>Paragraph 1</p>
<p>Paragraph 2 with a <a class="reference external" href="http://www.example.com/">link</a></p>""")
@@ -15,7 +15,7 @@ def get_storage(import_path):
module, classname = import_path[:dot], import_path[dot + 1:]
try:
mod = import_module(module)
- except ImportError, e:
+ except ImportError as e:
raise ImproperlyConfigured('Error importing module %s: "%s"' %
(module, e))
try:
@@ -90,7 +90,7 @@ def save(self, must_create=False):
fd = os.open(session_file_name, flags)
os.close(fd)
- except OSError, e:
+ except OSError as e:
if must_create and e.errno == errno.EEXIST:
raise CreateError
raise
@@ -260,7 +260,7 @@ def _get_finder(import_path):
module, attr = import_path.rsplit('.', 1)
try:
mod = import_module(module)
- except ImportError, e:
+ except ImportError as e:
raise ImproperlyConfigured('Error importing module %s: "%s"' %
(module, e))
try:
@@ -56,7 +56,7 @@ def get_response(self, request):
if self._should_handle(request.path):
try:
return self.serve(request)
- except Http404, e:
+ except Http404 as e:
if settings.DEBUG:
from django.views import debug
return debug.technical_404_response(request, e)
@@ -128,7 +128,7 @@ def get_cache(backend, **kwargs):
mod_path, cls_name = backend.rsplit('.', 1)
mod = importlib.import_module(mod_path)
backend_cls = getattr(mod, cls_name)
- except (AttributeError, ImportError), e:
+ except (AttributeError, ImportError) as e:
raise InvalidCacheBackendError(
"Could not find backend '%s': %s" % (backend, e))
cache = backend_cls(location, params)
@@ -79,7 +79,7 @@ def file_move_safe(old_file_name, new_file_name, chunk_size = 1024*64, allow_ove
try:
os.remove(old_file_name)
- except OSError, e:
+ except OSError as e:
# Certain operating systems (Cygwin and Windows)
# fail when deleting opened files, ignore it. (For the
# systems where this happens, temporary files will be auto-deleted
Oops, something went wrong. Retry.

0 comments on commit 3904b74

Please sign in to comment.