From 94eceeb89c153d8bf77dc194f8896a66cc25519a Mon Sep 17 00:00:00 2001 From: briancurtin Date: Mon, 14 Mar 2011 15:35:35 -0400 Subject: [PATCH 1/3] Fix #11491. When dbm.open was called with a file which already exists and the "flag" argument is "n", dbm.error was being raised. As documented, dbm.open(...,flag='n') will now "Always create a new, empty database, open for reading and writing", regardless of a previous file existing. --- Lib/dbm/__init__.py | 6 +++--- Lib/test/test_dbm.py | 8 ++++++++ Misc/ACKS | 1 + Misc/NEWS | 4 ++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Lib/dbm/__init__.py b/Lib/dbm/__init__.py index c224847597f..99c16379a95 100644 --- a/Lib/dbm/__init__.py +++ b/Lib/dbm/__init__.py @@ -68,10 +68,10 @@ def open(file, flag = 'r', mode = 0o666): if not _defaultmod: raise ImportError("no dbm clone found; tried %s" % _names) - # guess the type of an existing database - result = whichdb(file) + # guess the type of an existing database, if not creating a new one + result = whichdb(file) if 'n' not in flag else None if result is None: - # db doesn't exist + # db doesn't exist or 'n' flag was specified to create a new db if 'c' in flag or 'n' in flag: # file doesn't exist and the new flag was used so use default type mod = _defaultmod diff --git a/Lib/test/test_dbm.py b/Lib/test/test_dbm.py index 818be453f47..ce48cfd5ced 100644 --- a/Lib/test/test_dbm.py +++ b/Lib/test/test_dbm.py @@ -70,6 +70,14 @@ class AnyDBMTestCase(unittest.TestCase): self.read_helper(f) f.close() + def test_anydbm_creation_n_file_exists_with_invalid_contents(self): + with open(_fname, "w") as w: + pass # create an empty file + + f = dbm.open(_fname, 'n') + self.addCleanup(f.close) + self.assertEqual(len(f), 0) + def test_anydbm_modification(self): self.init_db() f = dbm.open(_fname, 'c') diff --git a/Misc/ACKS b/Misc/ACKS index a18dd18efeb..c95e76dff73 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -154,6 +154,7 @@ Terrence Cole Benjamin Collar Jeffery Collins Paul Colomiets +Denver Coneybeare Matt Conway David M. Cooke Greg Copeland diff --git a/Misc/NEWS b/Misc/NEWS index 9eb0537dd52..37b247730d0 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -40,6 +40,10 @@ Core and Builtins Library ------- +- Issue #11491: dbm.error is no longer raised when dbm.open is called with + the "n" as the flag argument and the file exists. The behavior matches + the documentation and general logic. + - Issue #11131: Fix sign of zero in decimal.Decimal plus and minus operations when the rounding mode is ROUND_FLOOR. From e72e161851cb5d0bddef3d24e56bb5d2969a28e5 Mon Sep 17 00:00:00 2001 From: Ronald Oussoren Date: Mon, 14 Mar 2011 18:15:25 -0400 Subject: [PATCH 2/3] Issue #11500: Fixed a bug in the os x proxy bypass code for fully qualified IP addresses in the proxy exception list Patch by Scott Wilson. --- Lib/test/test_urllib2.py | 35 ++++++++++- Lib/urllib/request.py | 122 +++++++++++++++++++++------------------ Misc/NEWS | 3 + 3 files changed, 102 insertions(+), 58 deletions(-) diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 9320e61c4ea..1704683b4dd 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -6,7 +6,9 @@ import io import socket import urllib.request -from urllib.request import Request, OpenerDirector +# The proxy bypass method imported below has logic specific to the OSX +# proxy config data structure but is testable on all platforms. +from urllib.request import Request, OpenerDirector, _proxy_bypass_macosx_sysconf # XXX # Request @@ -1030,6 +1032,17 @@ class HandlerTests(unittest.TestCase): self.assertEqual(req.get_host(), "www.python.org") del os.environ['no_proxy'] + def test_proxy_no_proxy_all(self): + os.environ['no_proxy'] = '*' + o = OpenerDirector() + ph = urllib.request.ProxyHandler(dict(http="proxy.example.com")) + o.add_handler(ph) + req = Request("http://www.python.org") + self.assertEqual(req.get_host(), "www.python.org") + r = o.open(req) + self.assertEqual(req.get_host(), "www.python.org") + del os.environ['no_proxy'] + def test_proxy_https(self): o = OpenerDirector() @@ -1070,6 +1083,26 @@ class HandlerTests(unittest.TestCase): self.assertEqual(req.get_host(), "proxy.example.com:3128") self.assertEqual(req.get_header("Proxy-authorization"),"FooBar") + def test_osx_proxy_bypass(self): + bypass = { + 'exclude_simple': False, + 'exceptions': ['foo.bar', '*.bar.com', '127.0.0.1', '10.10', + '10.0/16'] + } + # Check hosts that should trigger the proxy bypass + for host in ('foo.bar', 'www.bar.com', '127.0.0.1', '10.10.0.1', + '10.0.0.1'): + self.assertTrue(_proxy_bypass_macosx_sysconf(host, bypass), + 'expected bypass of %s to be True' % host) + # Check hosts that should not trigger the proxy bypass + for host in ('abc.foo.bar', 'bar.com', '127.0.0.2', '10.11.0.1', 'test'): + self.assertFalse(_proxy_bypass_macosx_sysconf(host, bypass), + 'expected bypass of %s to be False' % host) + + # Check the exclude_simple flag + bypass = {'exclude_simple': True, 'exceptions': []} + self.assertTrue(_proxy_bypass_macosx_sysconf('test', bypass)) + def test_basic_auth(self, quote_char='"'): opener = OpenerDirector() password_manager = MockPasswordManager() diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index e13381c3bca..087e9a606c0 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -2175,68 +2175,76 @@ def proxy_bypass_environment(host): return 0 +# This code tests an OSX specific data structure but is testable on all +# platforms +def _proxy_bypass_macosx_sysconf(host, proxy_settings): + """ + Return True iff this host shouldn't be accessed using a proxy + + This function uses the MacOSX framework SystemConfiguration + to fetch the proxy information. + + proxy_settings come from _scproxy._get_proxy_settings or get mocked ie: + { 'exclude_simple': bool, + 'exceptions': ['foo.bar', '*.bar.com', '127.0.0.1', '10.1', '10.0/16'] + } + """ + import re + import socket + from fnmatch import fnmatch + + hostonly, port = splitport(host) + + def ip2num(ipAddr): + parts = ipAddr.split('.') + parts = list(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] + + # 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 = m.group(2) + if mask is None: + mask = 8 * (m.group(1).count('.') + 1) + else: + mask = int(mask[1:]) + mask = 32 - mask + + if (hostIP >> mask) == (base >> mask): + return True + + elif fnmatch(host, value): + return True + + return False + + if sys.platform == 'darwin': from _scproxy import _get_proxy_settings, _get_proxies 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. - """ - import re - import socket - from fnmatch import fnmatch - - hostonly, port = splitport(host) - - def ip2num(ipAddr): - parts = ipAddr.split('.') - parts = list(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 = m.group(2) - if mask is None: - mask = 8 * (m.group(1).count('.') + 1) - - else: - mask = int(mask[1:]) - mask = 32 - mask - - if (hostIP >> mask) == (base >> mask): - return True - - elif fnmatch(host, value): - return True - - return False - + return _proxy_bypass_macosx_sysconf(host, proxy_settings) def getproxies_macosx_sysconf(): """Return a dictionary of scheme -> proxy server URL mappings. diff --git a/Misc/NEWS b/Misc/NEWS index 37b247730d0..d4ce939119f 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -197,6 +197,9 @@ Library OSError exception when The OS had been told to ignore SIGCLD in our process or otherwise not wait for exiting child processes. +- Issue #11500: Fixed a bug in the os x proxy bypass code for fully qualified + IP addresses in the proxy exception list. + Extensions ---------- From cd37dfcfac956d6400659dd2d208bcf81f32aebc Mon Sep 17 00:00:00 2001 From: R David Murray Date: Mon, 14 Mar 2011 18:35:56 -0400 Subject: [PATCH 3/3] Harmonize linesep docstrings, and fix the quoting of \r\n --- Lib/email/generator.py | 7 +++++-- Lib/email/header.py | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Lib/email/generator.py b/Lib/email/generator.py index 531fa9a7a43..f0e7a95477d 100644 --- a/Lib/email/generator.py +++ b/Lib/email/generator.py @@ -59,7 +59,7 @@ class Generator: self._fp.write(s) def flatten(self, msg, unixfrom=False, linesep='\n'): - """Print the message object tree rooted at msg to the output file + r"""Print the message object tree rooted at msg to the output file specified when the Generator instance was created. unixfrom is a flag that forces the printing of a Unix From_ delimiter @@ -70,7 +70,10 @@ class Generator: Note that for subobjects, no From_ line is printed. linesep specifies the characters used to indicate a new line in - the output. + the output. The default value is the most useful for typical + Python applications, but it can be set to \r\n to produce RFC-compliant + line separators when needed. + """ # We use the _XXX constants for operating on data that comes directly # from the msg, and _encoded_XXX constants for operating on data that diff --git a/Lib/email/header.py b/Lib/email/header.py index 35cdb2b4056..e1716176fa0 100644 --- a/Lib/email/header.py +++ b/Lib/email/header.py @@ -276,7 +276,7 @@ class Header: self._chunks.append((s, charset)) def encode(self, splitchars=';, \t', maxlinelen=None, linesep='\n'): - """Encode a message header into an RFC-compliant format. + r"""Encode a message header into an RFC-compliant format. There are many issues involved in converting a given string for use in an email header. Only certain character sets are readable in most