From eb3208c87c3a3a5807b70dd9a9093cbbd061c293 Mon Sep 17 00:00:00 2001 From: Chris Seto Date: Mon, 21 Mar 2016 14:04:14 -0400 Subject: [PATCH] Use getattr for better performance __getattribute__ is called everytime any attribute is accessed on request. It catches AttributeErrors in order to proxy to the underlying request. getattr is called, by python, whenever an attribute is missing and has a much lower overhead. --- rest_framework/request.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/rest_framework/request.py b/rest_framework/request.py index aafafcb32..e49fee3f7 100644 --- a/rest_framework/request.py +++ b/rest_framework/request.py @@ -346,11 +346,24 @@ class Request(object): else: self.auth = None - def __getattribute__(self, attr): + def __getattr__(self, attr): """ If an attribute does not exist on this instance, then we also attempt to proxy it to the underlying HttpRequest object. """ + try: + return getattr(self._request, attr) + except AttributeError: + # Call the original implementation of getattribute + # So the correct attribute error will get raised + self.__getattr_trace__(attr) + + def __getattr_trace__(self, attr): + """ + The original implementation of __getattribute__ which + generates correct tracebacks + See #2108 and #2530 + """ try: return super(Request, self).__getattribute__(attr) except AttributeError: