Skip to content

Commit

Permalink
Fixed #28837 -- Fixed test client crash if an exception with more tha…
Browse files Browse the repository at this point in the history
…n one arg is raised.

Also removed usage of the problematic pattern elsewhere.

Regression in 6e55e1d.
  • Loading branch information
ticosax authored and timgraham committed Nov 28, 2017
1 parent 7a6fbf3 commit 746caf3
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
6 changes: 2 additions & 4 deletions django/db/models/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import copy
import operator
import sys
import warnings
from collections import OrderedDict, namedtuple
from functools import lru_cache
Expand Down Expand Up @@ -521,13 +520,12 @@ def _create_object_from_params(self, lookup, params):
params = {k: v() if callable(v) else v for k, v in params.items()}
obj = self.create(**params)
return obj, True
except IntegrityError:
exc_info = sys.exc_info()
except IntegrityError as e:
try:
return self.get(**lookup), False
except self.model.DoesNotExist:
pass
raise exc_info[0](exc_info[1]).with_traceback(exc_info[2])
raise e

def _extract_model_params(self, defaults, **kwargs):
"""
Expand Down
4 changes: 2 additions & 2 deletions django/test/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,9 @@ def request(self, **request):
# Also make sure that the signalled exception is cleared from
# the local cache!
if self.exc_info:
exc_info = self.exc_info
_, exc_value, _ = self.exc_info
self.exc_info = None
raise exc_info[0](exc_info[1]).with_traceback(exc_info[2])
raise exc_value

# Save the client and request that stimulated the response.
response.client = self
Expand Down
2 changes: 1 addition & 1 deletion django/utils/autoreload.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def wrapper(*args, **kwargs):
def raise_last_exception():
global _exception
if _exception is not None:
raise _exception[0](_exception[1]).with_traceback(_exception[2])
raise _exception[1]


def ensure_echo_on():
Expand Down
7 changes: 6 additions & 1 deletion tests/test_client/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
)
from django.urls import reverse_lazy

from .views import get_view, post_view, trace_view
from .views import TwoArgException, get_view, post_view, trace_view


@override_settings(ROOT_URLCONF='test_client.urls')
Expand Down Expand Up @@ -713,6 +713,11 @@ def test_exception_following_nested_client_request(self):
with self.assertRaisesMessage(Exception, 'exception message'):
self.client.get('/nesting_exception_view/')

def test_response_raises_multi_arg_exception(self):
"""A request may raise an exception with more than one required arg."""
with self.assertRaises(TwoArgException):
self.client.get('/two_arg_exception/')

def test_uploading_temp_file(self):
with tempfile.TemporaryFile() as test_file:
response = self.client.post('/upload_view/', data={'temp_file': test_file})
Expand Down
1 change: 1 addition & 0 deletions tests/test_client/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
url(r'^mass_mail_sending_view/$', views.mass_mail_sending_view),
url(r'^nesting_exception_view/$', views.nesting_exception_view),
url(r'^django_project_redirect/$', views.django_project_redirect),
url(r'^two_arg_exception/$', views.two_arg_exception),

url(r'^accounts/$', RedirectView.as_view(url='login/')),
url(r'^accounts/no_trailing_slash$', RedirectView.as_view(url='login/')),
Expand Down
9 changes: 9 additions & 0 deletions tests/test_client/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,12 @@ def django_project_redirect(request):
def upload_view(request):
"""Prints keys of request.FILES to the response."""
return HttpResponse(', '.join(request.FILES))


class TwoArgException(Exception):
def __init__(self, one, two):
pass


def two_arg_exception(request):
raise TwoArgException('one', 'two')

0 comments on commit 746caf3

Please sign in to comment.