[Python-checkins] cpython (2.7): #14072: Fix parsing of tel URIs in urlparse by making the check for ports

ezio.melotti python-checkins at python.org
Sat May 19 16:16:31 CEST 2012


http://hg.python.org/cpython/rev/ff0fd7b26219
changeset:   77051:ff0fd7b26219
branch:      2.7
parent:      77047:57f1d13c2cd4
user:        Ezio Melotti <ezio.melotti at gmail.com>
date:        Sat May 19 17:12:17 2012 +0300
summary:
  #14072: Fix parsing of tel URIs in urlparse by making the check for ports stricter.

files:
  Lib/test/test_urlparse.py |   7 +++++++
  Lib/urlparse.py           |  12 ++++++------
  Misc/NEWS                 |   3 +++
  3 files changed, 16 insertions(+), 6 deletions(-)


diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py
--- a/Lib/test/test_urlparse.py
+++ b/Lib/test/test_urlparse.py
@@ -437,6 +437,13 @@
         self.assertEqual(p.port, 80)
         self.assertEqual(p.geturl(), url)
 
+    def test_issue14072(self):
+        p1 = urlparse.urlsplit('tel:+31-641044153')
+        self.assertEqual(p1.scheme, 'tel')
+        self.assertEqual(p1.path, '+31-641044153')
+        p2 = urlparse.urlsplit('tel:+31641044153')
+        self.assertEqual(p2.scheme, 'tel')
+        self.assertEqual(p2.path, '+31641044153')
 
     def test_attributes_bad_port(self):
         """Check handling of non-integer ports."""
diff --git a/Lib/urlparse.py b/Lib/urlparse.py
--- a/Lib/urlparse.py
+++ b/Lib/urlparse.py
@@ -185,12 +185,12 @@
             if c not in scheme_chars:
                 break
         else:
-            try:
-                # make sure "url" is not actually a port number (in which case
-                # "scheme" is really part of the path
-                _testportnum = int(url[i+1:])
-            except ValueError:
-                scheme, url = url[:i].lower(), url[i+1:]
+            # make sure "url" is not actually a port number (in which case
+            # "scheme" is really part of the path)
+            rest = url[i+1:]
+            if not rest or any(c not in '0123456789' for c in rest):
+                # not a port number
+                scheme, url = url[:i].lower(), rest
 
     if url[:2] == '//':
         netloc, url = _splitnetloc(url, 2)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -63,6 +63,9 @@
 - Issue #14721: Send proper header, Content-length: 0 when the body is an empty
   string ''. Initial Patch contributed by Arve Knudsen.
 
+- Issue #14072: Fix parsing of 'tel' URIs in urlparse by making the check for
+  ports stricter.
+
 - Issue #9374: Generic parsing of query and fragment portions of url for any
   scheme. Supported both by RFC3986 and RFC2396.
 

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


More information about the Python-checkins mailing list