Skip to content

Commit eb3208c

Browse files
committed
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.
1 parent 85e57af commit eb3208c

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

rest_framework/request.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,24 @@ def _not_authenticated(self):
346346
else:
347347
self.auth = None
348348

349-
def __getattribute__(self, attr):
349+
def __getattr__(self, attr):
350350
"""
351351
If an attribute does not exist on this instance, then we also attempt
352352
to proxy it to the underlying HttpRequest object.
353353
"""
354+
try:
355+
return getattr(self._request, attr)
356+
except AttributeError:
357+
# Call the original implementation of getattribute
358+
# So the correct attribute error will get raised
359+
self.__getattr_trace__(attr)
360+
361+
def __getattr_trace__(self, attr):
362+
"""
363+
The original implementation of __getattribute__ which
364+
generates correct tracebacks
365+
See #2108 and #2530
366+
"""
354367
try:
355368
return super(Request, self).__getattribute__(attr)
356369
except AttributeError:

0 commit comments

Comments
 (0)