diff options
author | Alexandre Vassalotti <alexandre@peadrop.com> | 2008-05-16 02:54:33 (GMT) |
---|---|---|
committer | Alexandre Vassalotti <alexandre@peadrop.com> | 2008-05-16 02:54:33 (GMT) |
commit | eca20b611433fac88057da092efc0b6d6d2a7fb3 (patch) | |
tree | b7c6ce421f22a464f28152ea7620e09cde051f1f /Lib | |
parent | 18bf893935343a918f9d00b3b934d858607bdc40 (diff) | |
download | cpython-eca20b611433fac88057da092efc0b6d6d2a7fb3.zip cpython-eca20b611433fac88057da092efc0b6d6d2a7fb3.tar.gz cpython-eca20b611433fac88057da092efc0b6d6d2a7fb3.tar.bz2 |
Merged revisions 63119-63128,63130-63131,63133,63135-63144,63146-63148,63151-63152,63155-63165,63167-63176,63181-63186,63188-63189 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63119 | benjamin.peterson | 2008-05-11 20:41:23 -0400 (Sun, 11 May 2008) | 2 lines
#2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
........
r63122 | benjamin.peterson | 2008-05-11 20:46:49 -0400 (Sun, 11 May 2008) | 2 lines
make message slightly more informative, so there's no chance of misunderstanding it
........
r63158 | ronald.oussoren | 2008-05-12 07:24:33 -0400 (Mon, 12 May 2008) | 5 lines
Remove references to platform 'mac'
The 'mac' platform (that is, os.name == 'mac') was used for the MacOS 9 port,
which is no longer supported (as of Python 2.4 IIRC).
........
r63159 | ronald.oussoren | 2008-05-12 07:31:05 -0400 (Mon, 12 May 2008) | 8 lines
MacOSX: remove dependency on Carbon package for urllib
This patch removes the dependency on the Carbon package from urllib.
The mac-specific code for getting proxy configuration is now writting in
Python using ctypes and uses the SystemConfiguration framework instead of
InternetConfig. Also provides a mac-specific implementation of proxy_bypass.
........
r63162 | eric.smith | 2008-05-12 10:00:01 -0400 (Mon, 12 May 2008) | 1 line
Added 'n' presentation type for integers.
........
r63164 | georg.brandl | 2008-05-12 12:26:52 -0400 (Mon, 12 May 2008) | 2 lines
#1713041: fix pprint's handling of maximum depth.
........
r63170 | georg.brandl | 2008-05-12 12:53:42 -0400 (Mon, 12 May 2008) | 2 lines
Fix parameter name for enumerate().
........
r63173 | georg.brandl | 2008-05-12 13:01:58 -0400 (Mon, 12 May 2008) | 2 lines
#2766: remove code without effect.
........
r63174 | georg.brandl | 2008-05-12 13:04:10 -0400 (Mon, 12 May 2008) | 3 lines
#2767: don't clear globs in run() call, since they could be needed in tearDown,
which clears them at the end.
........
r63175 | georg.brandl | 2008-05-12 13:14:51 -0400 (Mon, 12 May 2008) | 2 lines
#1760: try-except-finally is one statement since PEP 341.
........
r63186 | amaury.forgeotdarc | 2008-05-12 17:30:24 -0400 (Mon, 12 May 2008) | 2 lines
Sync code with documentation, and remove Win95 support in winsound module.
........
r63189 | amaury.forgeotdarc | 2008-05-12 18:21:39 -0400 (Mon, 12 May 2008) | 3 lines
Adapt test_pyclbr to the new version of urllib.py:
The new mac-specific functions must be ignored.
........
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/doctest.py | 4 | ||||
-rw-r--r-- | Lib/os.py | 20 | ||||
-rw-r--r-- | Lib/pprint.py | 8 | ||||
-rw-r--r-- | Lib/tempfile.py | 14 | ||||
-rw-r--r-- | Lib/test/test_builtin.py | 10 | ||||
-rw-r--r-- | Lib/test/test_pprint.py | 15 | ||||
-rw-r--r-- | Lib/test/test_pyclbr.py | 6 | ||||
-rw-r--r-- | Lib/urllib.py | 218 |
8 files changed, 235 insertions, 60 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py index 0322612..dad8333 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -2137,7 +2137,7 @@ class DocTestCase(unittest.TestCase): self.setUp() runner = DebugRunner(optionflags=self._dt_optionflags, checker=self._dt_checker, verbose=False) - runner.run(self._dt_test) + runner.run(self._dt_test, clear_globs=False) self.tearDown() def id(self): @@ -2194,8 +2194,6 @@ def DocTestSuite(module=None, globs=None, extraglobs=None, test_finder=None, module = _normalize_module(module) tests = test_finder.find(module, globs=globs, extraglobs=extraglobs) - if globs is None: - globs = module.__dict__ if not tests: # Why do we want to do this? Because it reveals a bug that might # otherwise be hidden. @@ -1,9 +1,9 @@ r"""OS routines for Mac, NT, or Posix depending on what system we're on. This exports: - - all functions from posix, nt, os2, mac, or ce, e.g. unlink, stat, etc. - - os.path is one of the modules posixpath, ntpath, or macpath - - os.name is 'posix', 'nt', 'os2', 'mac' or 'ce' + - all functions from posix, nt, os2, or ce, e.g. unlink, stat, etc. + - os.path is either posixpath or ntpath + - os.name is either 'posix', 'nt', 'os2' or 'ce'. - os.curdir is a string representing the current directory ('.' or ':') - os.pardir is a string representing the parent directory ('..' or '::') - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\') @@ -83,20 +83,6 @@ elif 'os2' in _names: __all__.extend(_get_exports_list(os2)) del os2 -elif 'mac' in _names: - name = 'mac' - linesep = '\r' - from mac import * - try: - from mac import _exit - except ImportError: - pass - import macpath as path - - import mac - __all__.extend(_get_exports_list(mac)) - del mac - elif 'ce' in _names: name = 'ce' linesep = '\r\n' diff --git a/Lib/pprint.py b/Lib/pprint.py index 43b6b0d..8bbc845 100644 --- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -131,6 +131,10 @@ class PrettyPrinter: sepLines = _len(rep) > (self._width - 1 - indent - allowance) write = stream.write + if self._depth and level > self._depth: + write(rep) + return + if sepLines: r = getattr(typ, "__repr__", None) if issubclass(typ, dict) and r is dict.__repr__: @@ -252,7 +256,7 @@ def _safe_repr(object, context, maxlevels, level): if not object: return "{}", True, False objid = _id(object) - if maxlevels and level > maxlevels: + if maxlevels and level >= maxlevels: return "{...}", False, objid in context if objid in context: return _recursion(object), False, True @@ -294,7 +298,7 @@ def _safe_repr(object, context, maxlevels, level): return "()", True, False format = "(%s)" objid = _id(object) - if maxlevels and level > maxlevels: + if maxlevels and level >= maxlevels: return format % "...", False, objid in context if objid in context: return _recursion(object), False, True diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 0e2a460..1041c14 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -34,10 +34,6 @@ import os as _os import errno as _errno from random import Random as _Random -if _os.name == 'mac': - import Carbon.Folder as _Folder - import Carbon.Folders as _Folders - try: import fcntl as _fcntl except ImportError: @@ -149,15 +145,7 @@ def _candidate_tempdir_list(): if dirname: dirlist.append(dirname) # Failing that, try OS-specific locations. - if _os.name == 'mac': - try: - fsr = _Folder.FSFindFolder(_Folders.kOnSystemDisk, - _Folders.kTemporaryFolderType, 1) - dirname = fsr.as_pathname() - dirlist.append(dirname) - except _Folder.error: - pass - elif _os.name == 'nt': + if _os.name == 'nt': dirlist.extend([ r'c:\temp', r'c:\tmp', r'\temp', r'\tmp' ]) else: dirlist.extend([ '/tmp', '/var/tmp', '/usr/tmp' ]) diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 52b337c..a74cc84 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -499,6 +499,16 @@ class BuiltinTest(unittest.TestCase): self.assertRaises(TypeError, hasattr) self.assertEqual(False, hasattr(sys, chr(sys.maxunicode))) + # Check that hasattr allows SystemExit and KeyboardInterrupts by + class A: + def __getattr__(self, what): + raise KeyboardInterrupt + self.assertRaises(KeyboardInterrupt, hasattr, A(), "b") + class B: + def __getattr__(self, what): + raise SystemExit + self.assertRaises(SystemExit, hasattr, B(), "b") + def test_hash(self): hash(None) self.assertEqual(hash(1), hash(1)) diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index a0cd01a..ed35287 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -381,6 +381,21 @@ class QueryTestCase(unittest.TestCase): cubo = test.test_set.linegraph(cube) self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt) + def test_depth(self): + nested_tuple = (1, (2, (3, (4, (5, 6))))) + nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}} + nested_list = [1, [2, [3, [4, [5, [6, []]]]]]] + self.assertEqual(pprint.pformat(nested_tuple), repr(nested_tuple)) + self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict)) + self.assertEqual(pprint.pformat(nested_list), repr(nested_list)) + + lv1_tuple = '(1, (...))' + lv1_dict = '{1: {...}}' + lv1_list = '[1, [...]]' + self.assertEqual(pprint.pformat(nested_tuple, depth=1), lv1_tuple) + self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict) + self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list) + class DottedPrettyPrinter(pprint.PrettyPrinter): diff --git a/Lib/test/test_pyclbr.py b/Lib/test/test_pyclbr.py index 64c8f18..57ede89 100644 --- a/Lib/test/test_pyclbr.py +++ b/Lib/test/test_pyclbr.py @@ -156,10 +156,14 @@ class PyclbrTest(TestCase): # These were once about the 10 longest modules cm('random', ignore=('Random',)) # from _random import Random as CoreGenerator cm('cgi', ignore=('log',)) # set with = in module - cm('urllib', ignore=('getproxies_registry', + cm('urllib', ignore=('_CFNumberToInt32', + '_CStringFromCFString', + 'getproxies_registry', 'proxy_bypass_registry', + 'proxy_bypass_macosx_sysconf', 'open_https', '_https_connection', + 'getproxies_macosx_sysconf', 'getproxies_internetconfig',)) # not on all platforms cm('pickle') cm('aifc', ignore=('openfp',)) # set with = in module diff --git a/Lib/urllib.py b/Lib/urllib.py index 0b66e59..f610e4b 100644 --- a/Lib/urllib.py +++ b/Lib/urllib.py @@ -1271,44 +1271,214 @@ def proxy_bypass_environment(host): if sys.platform == 'darwin': - def getproxies_internetconfig(): - """Return a dictionary of scheme -> proxy server URL mappings. - - By convention the mac uses Internet Config to store - proxies. An HTTP proxy, for instance, is stored under - the HttpProxy key. + def _CStringFromCFString(sc, value): + from ctypes import create_string_buffer + length = sc.CFStringGetLength(value) + 1 + buff = create_string_buffer(length) + sc.CFStringGetCString(value, buff, length, 0) + return buff.value + + def _CFNumberToInt32(sc, cfnum): + from ctypes import byref, c_int + val = c_int() + kCFNumberSInt32Type = 3 + sc.CFNumberGetValue(cfnum, kCFNumberSInt32Type, byref(val)) + return val.value + + + 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 {} + from ctypes import cdll + from ctypes.util import find_library + import re + import socket + from fnmatch import fnmatch + + 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] + + sc = cdll.LoadLibrary(find_library("SystemConfiguration")) + + hostIP = None + + if not sc: + return False + + kSCPropNetProxiesExceptionsList = sc.CFStringCreateWithCString(0, "ExceptionsList", 0) + kSCPropNetProxiesExcludeSimpleHostnames = sc.CFStringCreateWithCString(0, + "ExcludeSimpleHostnames", 0) + + + proxyDict = sc.SCDynamicStoreCopyProxies(None) try: - config = ic.IC() - except ic.error: + # Check for simple host names: + if '.' not in host: + exclude_simple = sc.CFDictionaryGetValue(proxyDict, + kSCPropNetProxiesExcludeSimpleHostnames) + if exclude_simple and _CFNumberToInt32(sc, exclude_simple): + return True + + + # Check the exceptions list: + exceptions = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesExceptionsList) + if exceptions: + # Items in the list are strings like these: *.local, 169.254/16 + for index in xrange(sc.CFArrayGetCount(exceptions)): + value = sc.CFArrayGetValueAtIndex(exceptions, index) + if not value: continue + value = _CStringFromCFString(sc, value) + + m = re.match(r"(\d+(?:\.\d+)*)(/\d+)?", value) + if m is not None: + if hostIP is None: + hostIP = socket.gethostbyname(host) + hostIP = ip2num(hostIP) + + 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 + + finally: + sc.CFRelease(kSCPropNetProxiesExceptionsList) + sc.CFRelease(kSCPropNetProxiesExcludeSimpleHostnames) + + + + 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. + """ + from ctypes import cdll + from ctypes.util import find_library + + sc = cdll.LoadLibrary(find_library("SystemConfiguration")) + + if not sc: return {} + + + kSCPropNetProxiesHTTPEnable = sc.CFStringCreateWithCString(0, "HTTPEnable", 0) + kSCPropNetProxiesHTTPProxy = sc.CFStringCreateWithCString(0, "HTTPProxy", 0) + kSCPropNetProxiesHTTPPort = sc.CFStringCreateWithCString(0, "HTTPPort", 0) + + kSCPropNetProxiesHTTPSEnable = sc.CFStringCreateWithCString(0, "HTTPSEnable", 0) + kSCPropNetProxiesHTTPSProxy = sc.CFStringCreateWithCString(0, "HTTPSProxy", 0) + kSCPropNetProxiesHTTPSPort = sc.CFStringCreateWithCString(0, "HTTPSPort", 0) + + kSCPropNetProxiesFTPEnable = sc.CFStringCreateWithCString(0, "FTPEnable", 0) + kSCPropNetProxiesFTPPassive = sc.CFStringCreateWithCString(0, "FTPPassive", 0) + kSCPropNetProxiesFTPPort = sc.CFStringCreateWithCString(0, "FTPPort", 0) + kSCPropNetProxiesFTPProxy = sc.CFStringCreateWithCString(0, "FTPProxy", 0) + + kSCPropNetProxiesGopherEnable = sc.CFStringCreateWithCString(0, "GopherEnable", 0) + kSCPropNetProxiesGopherPort = sc.CFStringCreateWithCString(0, "GopherPort", 0) + kSCPropNetProxiesGopherProxy = sc.CFStringCreateWithCString(0, "GopherProxy", 0) + 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. + proxyDict = sc.SCDynamicStoreCopyProxies(None) + + try: + # HTTP: + enabled = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesHTTPEnable) + if enabled and _CFNumberToInt32(sc, enabled): + proxy = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesHTTPProxy) + port = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesHTTPPort) + + if proxy: + proxy = _CStringFromCFString(sc, proxy) + if port: + port = _CFNumberToInt32(sc, port) + proxies["http"] = "http://%s:%i" % (proxy, port) + else: + proxies["http"] = "http://%s" % (proxy, ) + + # HTTPS: + enabled = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesHTTPSEnable) + if enabled and _CFNumberToInt32(sc, enabled): + proxy = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesHTTPSProxy) + port = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesHTTPSPort) + + if proxy: + proxy = _CStringFromCFString(sc, proxy) + if port: + port = _CFNumberToInt32(sc, port) + proxies["https"] = "http://%s:%i" % (proxy, port) + else: + proxies["https"] = "http://%s" % (proxy, ) + + # FTP: + enabled = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesFTPEnable) + if enabled and _CFNumberToInt32(sc, enabled): + proxy = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesFTPProxy) + port = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesFTPPort) + + if proxy: + proxy = _CStringFromCFString(sc, proxy) + if port: + port = _CFNumberToInt32(sc, port) + proxies["ftp"] = "http://%s:%i" % (proxy, port) + else: + proxies["ftp"] = "http://%s" % (proxy, ) + + # Gopher: + enabled = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesGopherEnable) + if enabled and _CFNumberToInt32(sc, enabled): + proxy = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesGopherProxy) + port = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesGopherPort) + + if proxy: + proxy = _CStringFromCFString(sc, proxy) + if port: + port = _CFNumberToInt32(sc, port) + proxies["gopher"] = "http://%s:%i" % (proxy, port) + else: + proxies["gopher"] = "http://%s" % (proxy, ) + finally: + sc.CFRelease(proxyDict) + + sc.CFRelease(kSCPropNetProxiesHTTPEnable) + sc.CFRelease(kSCPropNetProxiesHTTPProxy) + sc.CFRelease(kSCPropNetProxiesHTTPPort) + sc.CFRelease(kSCPropNetProxiesFTPEnable) + sc.CFRelease(kSCPropNetProxiesFTPPassive) + sc.CFRelease(kSCPropNetProxiesFTPPort) + sc.CFRelease(kSCPropNetProxiesFTPProxy) + sc.CFRelease(kSCPropNetProxiesGopherEnable) + sc.CFRelease(kSCPropNetProxiesGopherPort) + sc.CFRelease(kSCPropNetProxiesGopherProxy) + 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(): |