Skip to content

Commit e1ed598

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 e1ed598

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

rest_framework/request.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,19 +346,16 @@ 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
"""
354354
try:
355-
return super(Request, self).__getattribute__(attr)
355+
return getattr(self._request, attr)
356356
except AttributeError:
357357
info = sys.exc_info()
358-
try:
359-
return getattr(self._request, attr)
360-
except AttributeError:
361-
six.reraise(info[0], info[1], info[2].tb_next)
358+
six.reraise(info[0], info[1], info[2].tb_next)
362359

363360
@property
364361
def DATA(self):

0 commit comments

Comments
 (0)