Code

Ticket #3349: patch_3349.diff

File patch_3349.diff, 10.4 KB (added by durdinator, 6 years ago)

Updated patch against r7574 of trunk

Line 
1Index: django/conf/__init__.py
2===================================================================
3--- django/conf/__init__.py     (revision 7574)
4+++ django/conf/__init__.py     (working copy)
5@@ -9,6 +9,7 @@
6 import os
7 import time     # Needed for Windows
8 from django.conf import global_settings
9+from django.core.exceptions import wrap_and_raise
10 
11 ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE"
12 
13@@ -84,7 +85,7 @@
14         try:
15             mod = __import__(self.SETTINGS_MODULE, {}, {}, [''])
16         except ImportError, e:
17-            raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
18+            wrap_and_raise(ImportError("Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)))
19 
20         # Settings that should be converted into tuples if they're mistakenly entered
21         # as strings.
22Index: django/core/urlresolvers.py
23===================================================================
24--- django/core/urlresolvers.py (revision 7574)
25+++ django/core/urlresolvers.py (working copy)
26@@ -8,7 +8,7 @@
27 """
28 
29 from django.http import Http404
30-from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
31+from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist, wrap_and_raise
32 from django.utils.encoding import iri_to_uri, force_unicode, smart_str
33 from django.utils.functional import memoize
34 import re
35@@ -178,7 +178,7 @@
36             self._callback = get_callable(self._callback_str)
37         except ImportError, e:
38             mod_name, _ = get_mod_func(self._callback_str)
39-            raise ViewDoesNotExist, "Could not import %s. Error was: %s" % (mod_name, str(e))
40+            wrap_and_raise(ViewDoesNotExist("Could not import %s. Error was: %s" % (mod_name, str(e))))
41         except AttributeError, e:
42             mod_name, func_name = get_mod_func(self._callback_str)
43             raise ViewDoesNotExist, "Tried %s in module %s. Error was: %s" % (func_name, mod_name, str(e))
44@@ -252,7 +252,7 @@
45             except Exception, e:
46                 # Either an invalid urlconf_name, such as "foo.bar.", or some
47                 # kind of problem during the actual import.
48-                raise ImproperlyConfigured, "Error while importing URLconf %r: %s" % (self.urlconf_name, e)
49+                wrap_and_raise(ImproperlyConfigured("Error while importing URLconf %r: %s" % (self.urlconf_name, e)))
50             return self._urlconf_module
51     urlconf_module = property(_get_urlconf_module)
52 
53@@ -266,7 +266,7 @@
54         try:
55             return getattr(__import__(mod_name, {}, {}, ['']), func_name), {}
56         except (ImportError, AttributeError), e:
57-            raise ViewDoesNotExist, "Tried %s. Error was: %s" % (callback, str(e))
58+            wrap_and_raise(ViewDoesNotExist("Tried %s. Error was: %s" % (callback, str(e))))
59 
60     def resolve404(self):
61         return self._resolve_special('404')
62Index: django/core/handlers/base.py
63===================================================================
64--- django/core/handlers/base.py        (revision 7574)
65+++ django/core/handlers/base.py        (working copy)
66@@ -2,6 +2,7 @@
67 
68 from django import http
69 from django.core import signals
70+from django.core.exceptions import wrap_and_raise
71 from django.dispatch import dispatcher
72 
73 class BaseHandler(object):
74@@ -33,7 +34,7 @@
75             try:
76                 mod = __import__(mw_module, {}, {}, [''])
77             except ImportError, e:
78-                raise exceptions.ImproperlyConfigured, 'Error importing middleware %s: "%s"' % (mw_module, e)
79+                wrap_and_raise(exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e)))
80             try:
81                 mw_class = getattr(mod, mw_classname)
82             except AttributeError:
83Index: django/core/exceptions.py
84===================================================================
85--- django/core/exceptions.py   (revision 7574)
86+++ django/core/exceptions.py   (working copy)
87@@ -32,3 +32,18 @@
88     """Some kind of problem with a model field."""
89     pass
90 
91+
92+def wrap_and_raise(new_exception):
93+    """
94+    Call from an except: block to raise a new exception, while keeping
95+    the traceback info from the source exception.
96+   
97+    new_exception is be the Exception (or subclass) instance to raise.
98+    """
99+    import sys
100+    exc_class, exc, tb = sys.exc_info()
101+
102+    if issubclass(exc_class, Exception):
103+        raise new_exception.__class__, new_exception, tb
104+    else:
105+        raise new_exception
106Index: django/contrib/auth/__init__.py
107===================================================================
108--- django/contrib/auth/__init__.py     (revision 7574)
109+++ django/contrib/auth/__init__.py     (working copy)
110@@ -1,5 +1,5 @@
111 import datetime
112-from django.core.exceptions import ImproperlyConfigured
113+from django.core.exceptions import ImproperlyConfigured, wrap_and_raise
114 
115 SESSION_KEY = '_auth_user_id'
116 BACKEND_SESSION_KEY = '_auth_user_backend'
117@@ -11,7 +11,7 @@
118     try:
119         mod = __import__(module, {}, {}, [attr])
120     except ImportError, e:
121-        raise ImproperlyConfigured, 'Error importing authentication backend %s: "%s"' % (module, e)
122+        wrap_and_raise(ImproperlyConfigured('Error importing authentication backend %s: "%s"' % (module, e)))
123     except ValueError, e:
124         raise ImproperlyConfigured, 'Error importing authentication backends. Is AUTHENTICATION_BACKENDS a correctly defined list or tuple?'
125     try:
126Index: django/template/__init__.py
127===================================================================
128--- django/template/__init__.py (revision 7574)
129+++ django/template/__init__.py (working copy)
130@@ -51,6 +51,7 @@
131 import re
132 from inspect import getargspec
133 from django.conf import settings
134+from django.core.exceptions import wrap_and_raise
135 from django.template.context import Context, RequestContext, ContextPopException
136 from django.utils.itercompat import is_iterable
137 from django.utils.functional import curry, Promise
138@@ -919,7 +920,7 @@
139         try:
140             mod = __import__(module_name, {}, {}, [''])
141         except ImportError, e:
142-            raise InvalidTemplateLibrary("Could not load template library from %s, %s" % (module_name, e))
143+            wrap_and_raise(InvalidTemplateLibrary("Could not load template library from %s, %s" % (module_name, e)))
144         try:
145             lib = mod.register
146             libraries[module_name] = lib
147Index: django/template/loaders/app_directories.py
148===================================================================
149--- django/template/loaders/app_directories.py  (revision 7574)
150+++ django/template/loaders/app_directories.py  (working copy)
151@@ -6,7 +6,7 @@
152 import os
153 
154 from django.conf import settings
155-from django.core.exceptions import ImproperlyConfigured
156+from django.core.exceptions import ImproperlyConfigured, wrap_and_raise
157 from django.template import TemplateDoesNotExist
158 from django.utils._os import safe_join
159 
160@@ -24,7 +24,7 @@
161         else:
162             mod = getattr(__import__(m, {}, {}, [a]), a)
163     except ImportError, e:
164-        raise ImproperlyConfigured, 'ImportError %s: %s' % (app, e.args[0])
165+        wrap_and_raise(ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0])))
166     template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates')
167     if os.path.isdir(template_dir):
168         app_template_dirs.append(template_dir)
169Index: django/template/defaulttags.py
170===================================================================
171--- django/template/defaulttags.py      (revision 7574)
172+++ django/template/defaulttags.py      (working copy)
173@@ -12,6 +12,7 @@
174 from django.template import TemplateSyntaxError, VariableDoesNotExist, BLOCK_TAG_START, BLOCK_TAG_END, VARIABLE_TAG_START, VARIABLE_TAG_END, SINGLE_BRACE_START, SINGLE_BRACE_END, COMMENT_TAG_START, COMMENT_TAG_END
175 from django.template import get_library, Library, InvalidTemplateLibrary
176 from django.conf import settings
177+from django.core.exceptions import wrap_and_raise
178 from django.utils.encoding import smart_str, smart_unicode
179 from django.utils.itercompat import groupby
180 from django.utils.safestring import mark_safe
181@@ -853,8 +854,8 @@
182             lib = get_library("django.templatetags.%s" % taglib)
183             parser.add_library(lib)
184         except InvalidTemplateLibrary, e:
185-            raise TemplateSyntaxError("'%s' is not a valid tag library: %s" %
186-                                      (taglib, e))
187+            wrap_and_raise(TemplateSyntaxError("'%s' is not a valid tag library: %s" %
188+                                      (taglib, e)))
189     return LoadNode()
190 load = register.tag(load)
191 
192Index: django/template/context.py
193===================================================================
194--- django/template/context.py  (revision 7574)
195+++ django/template/context.py  (working copy)
196@@ -1,5 +1,5 @@
197 from django.conf import settings
198-from django.core.exceptions import ImproperlyConfigured
199+from django.core.exceptions import ImproperlyConfigured, wrap_and_raise
200 
201 _standard_context_processors = None
202 
203@@ -77,7 +77,7 @@
204             try:
205                 mod = __import__(module, {}, {}, [attr])
206             except ImportError, e:
207-                raise ImproperlyConfigured('Error importing request processor module %s: "%s"' % (module, e))
208+                wrap_and_raise(ImproperlyConfigured('Error importing request processor module %s: "%s"' % (module, e)))
209             try:
210                 func = getattr(mod, attr)
211             except AttributeError:
212Index: django/template/loader.py
213===================================================================
214--- django/template/loader.py   (revision 7574)
215+++ django/template/loader.py   (working copy)
216@@ -20,7 +20,7 @@
217 # Python eggs) sets is_usable to False if the "pkg_resources" module isn't
218 # installed, because pkg_resources is necessary to read eggs.
219 
220-from django.core.exceptions import ImproperlyConfigured
221+from django.core.exceptions import ImproperlyConfigured, wrap_and_raise
222 from django.template import Origin, Template, Context, TemplateDoesNotExist, add_to_builtins
223 from django.conf import settings
224 
225@@ -53,7 +53,7 @@
226             try:
227                 mod = __import__(module, globals(), locals(), [attr])
228             except ImportError, e:
229-                raise ImproperlyConfigured, 'Error importing template source loader %s: "%s"' % (module, e)
230+                wrap_and_raise(ImproperlyConfigured('Error importing template source loader %s: "%s"' % (module, e)))
231             try:
232                 func = getattr(mod, attr)
233             except AttributeError: