[Python-checkins] r80199 - in python/branches/release31-maint: Lib/urllib/request.py Misc/NEWS Modules/_scproxy.c setup.py

ronald.oussoren python-checkins at python.org
Sun Apr 18 22:49:34 CEST 2010


Author: ronald.oussoren
Date: Sun Apr 18 22:49:34 2010
New Revision: 80199

Log:
Merged revisions 80198 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r80198 | ronald.oussoren | 2010-04-18 22:46:11 +0200 (Sun, 18 Apr 2010) | 4 lines
  
  For for issue #7154: Port the code that uses
  the SystemConfiguration framework to detect the
  proxy settings on OSX from the trunk to python 3.2
........


Added:
   python/branches/release31-maint/Modules/_scproxy.c
      - copied unchanged from r80198, /python/branches/py3k/Modules/_scproxy.c
Modified:
   python/branches/release31-maint/   (props changed)
   python/branches/release31-maint/Lib/urllib/request.py
   python/branches/release31-maint/Misc/NEWS
   python/branches/release31-maint/setup.py

Modified: python/branches/release31-maint/Lib/urllib/request.py
==============================================================================
--- python/branches/release31-maint/Lib/urllib/request.py	(original)
+++ python/branches/release31-maint/Lib/urllib/request.py	Sun Apr 18 22:49:34 2010
@@ -2142,44 +2142,82 @@
 
 
 if sys.platform == 'darwin':
-    def getproxies_internetconfig():
-        """Return a dictionary of scheme -> proxy server URL mappings.
+    from _scproxy import _get_proxy_settings, _get_proxies
 
-        By convention the mac uses Internet Config to store
-        proxies.  An HTTP proxy, for instance, is stored under
-        the HttpProxy key.
+    def proxy_bypass_macosx_sysconf(host):
+        """
+        Return True iff this host shouldn't be accessed using a proxy
 
+        This function uses the MacOSX framework SystemConfiguration
+        to fetch the proxy information.
         """
-        try:
-            import ic
-        except ImportError:
-            return {}
+        import re
+        import socket
+        from fnmatch import fnmatch
+
+        hostonly, port = splitport(host)
+
+        def ip2num(ipAddr):
+            parts = ipAddr.split('.')
+            parts = map(int, parts)
+            if len(parts) != 4:
+                parts = (parts + [0, 0, 0, 0])[:4]
+            return (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3]
+
+        proxy_settings = _get_proxy_settings()
+
+        # Check for simple host names:
+        if '.' not in host:
+            if proxy_settings['exclude_simple']:
+                return True
+
+        hostIP = None
+
+        for value in proxy_settings.get('exceptions', ()):
+            # Items in the list are strings like these: *.local, 169.254/16
+            if not value: continue
+
+            m = re.match(r"(\d+(?:\.\d+)*)(/\d+)?", value)
+            if m is not None:
+                if hostIP is None:
+                    try:
+                        hostIP = socket.gethostbyname(hostonly)
+                        hostIP = ip2num(hostIP)
+                    except socket.error:
+                        continue
+
+                base = ip2num(m.group(1))
+                mask = int(m.group(2)[1:])
+                mask = 32 - mask
+
+                if (hostIP >> mask) == (base >> mask):
+                    return True
+
+            elif fnmatch(host, value):
+                return True
+
+        return False
+
+
+    def getproxies_macosx_sysconf():
+        """Return a dictionary of scheme -> proxy server URL mappings.
+
+        This function uses the MacOSX framework SystemConfiguration
+        to fetch the proxy information.
+        """
+        return _get_proxies()
+
 
-        try:
-            config = ic.IC()
-        except ic.error:
-            return {}
-        proxies = {}
-        # HTTP:
-        if 'UseHTTPProxy' in config and config['UseHTTPProxy']:
-            try:
-                value = config['HTTPProxyHost']
-            except ic.error:
-                pass
-            else:
-                proxies['http'] = 'http://%s' % value
-        # FTP: XXX To be done.
-        # Gopher: XXX To be done.
-        return proxies
 
     def proxy_bypass(host):
         if getproxies_environment():
             return proxy_bypass_environment(host)
         else:
-            return 0
+            return proxy_bypass_macosx_sysconf(host)
 
     def getproxies():
-        return getproxies_environment() or getproxies_internetconfig()
+        return getproxies_environment() or getproxies_macosx_sysconf()
+
 
 elif os.name == 'nt':
     def getproxies_registry():

Modified: python/branches/release31-maint/Misc/NEWS
==============================================================================
--- python/branches/release31-maint/Misc/NEWS	(original)
+++ python/branches/release31-maint/Misc/NEWS	Sun Apr 18 22:49:34 2010
@@ -198,6 +198,9 @@
 Library
 -------
 
+- Issue #7154: urllib.request can now detect the proxy settings on OSX 10.6
+  (as long as the user didn't specify 'automatic proxy configuration').
+
 - Issue #8412: os.system() now accepts bytes, bytearray and str with
   surrogates.
 

Modified: python/branches/release31-maint/setup.py
==============================================================================
--- python/branches/release31-maint/setup.py	(original)
+++ python/branches/release31-maint/setup.py	Sun Apr 18 22:49:34 2010
@@ -1218,6 +1218,12 @@
                        Extension('_gestalt', ['_gestalt.c'],
                        extra_link_args=['-framework', 'Carbon'])
                        )
+            exts.append(
+                       Extension('_scproxy', ['_scproxy.c'],
+                       extra_link_args=[
+                           '-framework', 'SystemConfiguration',
+                           '-framework', 'CoreFoundation',
+                        ]))
 
         self.extensions.extend(exts)
 


More information about the Python-checkins mailing list