[Python-checkins] cpython (2.7): Issue #6500: Fixed infinite recursion in urllib2.Request.__getattr__().

serhiy.storchaka python-checkins at python.org
Mon Jan 18 03:40:47 EST 2016


https://hg.python.org/cpython/rev/d34fdd1736f2
changeset:   99957:d34fdd1736f2
branch:      2.7
parent:      99953:668827be66d6
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Mon Jan 18 10:35:40 2016 +0200
summary:
  Issue #6500: Fixed infinite recursion in urllib2.Request.__getattr__().

files:
  Lib/test/test_urllib2.py |  5 +++++
  Lib/urllib2.py           |  8 +++-----
  Misc/NEWS                |  2 ++
  3 files changed, 10 insertions(+), 5 deletions(-)


diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py
--- a/Lib/test/test_urllib2.py
+++ b/Lib/test/test_urllib2.py
@@ -1350,6 +1350,11 @@
         req = Request(url)
         self.assertEqual(req.get_full_url(), url)
 
+    def test_private_attributes(self):
+        self.assertFalse(hasattr(self.get, '_Request__r_xxx'))
+        # Issue #6500: infinite recursion
+        self.assertFalse(hasattr(self.get, '_Request__r_method'))
+
     def test_HTTPError_interface(self):
         """
         Issue 13211 reveals that HTTPError didn't implement the URLError
diff --git a/Lib/urllib2.py b/Lib/urllib2.py
--- a/Lib/urllib2.py
+++ b/Lib/urllib2.py
@@ -248,11 +248,9 @@
         # methods getting called in a non-standard order.  this may be
         # too complicated and/or unnecessary.
         # XXX should the __r_XXX attributes be public?
-        if attr[:12] == '_Request__r_':
-            name = attr[12:]
-            if hasattr(Request, 'get_' + name):
-                getattr(self, 'get_' + name)()
-                return getattr(self, attr)
+        if attr in ('_Request__r_type', '_Request__r_host'):
+            getattr(self, 'get_' + attr[12:])()
+            return self.__dict__[attr]
         raise AttributeError, attr
 
     def get_method(self):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -39,6 +39,8 @@
 Library
 -------
 
+- Issue #6500: Fixed infinite recursion in urllib2.Request.__getattr__().
+
 - Issue #26083: Workaround a subprocess bug that raises an incorrect
   "ValueError: insecure string pickle" exception instead of the actual
   exception on some platforms such as Mac OS X when an exception raised

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list