diff options
Diffstat (limited to 'Lib')
32 files changed, 596 insertions, 348 deletions
diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py index 41cbca1..e8c3591 100644 --- a/Lib/logging/handlers.py +++ b/Lib/logging/handlers.py @@ -1,4 +1,4 @@ -# Copyright 2001-2005 by Vinay Sajip. All Rights Reserved. +# Copyright 2001-2007 by Vinay Sajip. All Rights Reserved. # # Permission to use, copy, modify, and distribute this software and its # documentation for any purpose and without fee is hereby granted, @@ -22,7 +22,7 @@ Apache's log4j system. Should work under Python versions >= 1.5.2, except that source line information is not available unless 'sys._getframe()' is. -Copyright (C) 2001-2004 Vinay Sajip. All Rights Reserved. +Copyright (C) 2001-2007 Vinay Sajip. All Rights Reserved. To use, simply 'import logging' and log away! """ @@ -135,10 +135,8 @@ class RotatingFileHandler(BaseRotatingHandler): os.remove(dfn) os.rename(self.baseFilename, dfn) #print "%s -> %s" % (self.baseFilename, dfn) - if self.encoding: - self.stream = codecs.open(self.baseFilename, 'w', self.encoding) - else: - self.stream = open(self.baseFilename, 'w') + self.mode = 'w' + self.stream = self._open() def shouldRollover(self, record): """ @@ -281,10 +279,8 @@ class TimedRotatingFileHandler(BaseRotatingHandler): s.sort() os.remove(s[0]) #print "%s -> %s" % (self.baseFilename, dfn) - if self.encoding: - self.stream = codecs.open(self.baseFilename, 'w', self.encoding) - else: - self.stream = open(self.baseFilename, 'w') + self.mode = 'w' + self.stream = self._open() self.rolloverAt = self.rolloverAt + self.interval class WatchedFileHandler(logging.FileHandler): @@ -786,7 +782,7 @@ class SMTPHandler(logging.Handler): try: import smtplib try: - from email.Utils import formatdate + from email.utils import formatdate except ImportError: formatdate = self.date_time port = self.mailport diff --git a/Lib/mailbox.py b/Lib/mailbox.py index d55a3dd..cbe6aa8 100755 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -16,8 +16,8 @@ import socket import errno import copy import email -import email.Message -import email.Generator +import email.message +import email.generator import rfc822 import StringIO try: @@ -193,9 +193,9 @@ class Mailbox: # To get native line endings on disk, the user-friendly \n line endings # used in strings and by email.Message are translated here. """Dump message contents to target file.""" - if isinstance(message, email.Message.Message): + if isinstance(message, email.message.Message): buffer = StringIO.StringIO() - gen = email.Generator.Generator(buffer, mangle_from_, 0) + gen = email.generator.Generator(buffer, mangle_from_, 0) gen.flatten(message) buffer.seek(0) target.write(buffer.read().replace('\n', os.linesep)) @@ -704,7 +704,7 @@ class _mboxMMDF(_singlefileMailbox): message = '' elif isinstance(message, _mboxMMDFMessage): from_line = 'From ' + message.get_from() - elif isinstance(message, email.Message.Message): + elif isinstance(message, email.message.Message): from_line = message.get_unixfrom() # May be None. if from_line is None: from_line = 'From MAILER-DAEMON %s' % time.asctime(time.gmtime()) @@ -1254,9 +1254,9 @@ class Babyl(_singlefileMailbox): self._file.write(os.linesep) else: self._file.write('1,,' + os.linesep) - if isinstance(message, email.Message.Message): + if isinstance(message, email.message.Message): orig_buffer = StringIO.StringIO() - orig_generator = email.Generator.Generator(orig_buffer, False, 0) + orig_generator = email.generator.Generator(orig_buffer, False, 0) orig_generator.flatten(message) orig_buffer.seek(0) while True: @@ -1267,7 +1267,7 @@ class Babyl(_singlefileMailbox): self._file.write('*** EOOH ***' + os.linesep) if isinstance(message, BabylMessage): vis_buffer = StringIO.StringIO() - vis_generator = email.Generator.Generator(vis_buffer, False, 0) + vis_generator = email.generator.Generator(vis_buffer, False, 0) vis_generator.flatten(message.get_visible()) while True: line = vis_buffer.readline() @@ -1323,12 +1323,12 @@ class Babyl(_singlefileMailbox): return (start, stop) -class Message(email.Message.Message): +class Message(email.message.Message): """Message with mailbox-format-specific properties.""" def __init__(self, message=None): """Initialize a Message instance.""" - if isinstance(message, email.Message.Message): + if isinstance(message, email.message.Message): self._become_message(copy.deepcopy(message)) if isinstance(message, Message): message._explain_to(self) @@ -1337,7 +1337,7 @@ class Message(email.Message.Message): elif hasattr(message, "read"): self._become_message(email.message_from_file(message)) elif message is None: - email.Message.Message.__init__(self) + email.message.Message.__init__(self) else: raise TypeError('Invalid message type: %s' % type(message)) @@ -1468,7 +1468,7 @@ class _mboxMMDFMessage(Message): def __init__(self, message=None): """Initialize an mboxMMDFMessage instance.""" self.set_from('MAILER-DAEMON', True) - if isinstance(message, email.Message.Message): + if isinstance(message, email.message.Message): unixfrom = message.get_unixfrom() if unixfrom is not None and unixfrom.startswith('From '): self.set_from(unixfrom[5:]) @@ -1990,10 +1990,12 @@ class UnixMailbox(_Mailbox): # that the two characters preceding "From " are \n\n or the beginning of # the file. Fixing this would require a more extensive rewrite than is # necessary. For convenience, we've added a PortableUnixMailbox class - # which uses the more lenient _fromlinepattern regular expression. + # which does no checking of the format of the 'From' line. - _fromlinepattern = r"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+" \ - r"\d?\d:\d\d(:\d\d)?(\s+[^\s]+)?\s+\d\d\d\d\s*$" + _fromlinepattern = (r"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+" + r"\d?\d:\d\d(:\d\d)?(\s+[^\s]+)?\s+\d\d\d\d\s*" + r"[^\s]*\s*" + "$") _regexp = None def _strict_isrealfromline(self, line): diff --git a/Lib/ntpath.py b/Lib/ntpath.py index b32ec16..23d5127 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -344,8 +344,10 @@ def expandvars(path): var = path[:index] if var in os.environ: res = res + os.environ[var] + else: + res = res + '${' + var + '}' except ValueError: - res = res + path + res = res + '${' + path index = pathlen - 1 else: var = '' @@ -357,8 +359,10 @@ def expandvars(path): c = path[index:index + 1] if var in os.environ: res = res + os.environ[var] + else: + res = res + '$' + var if c != '': - res = res + c + index = index - 1 else: res = res + c index = index + 1 @@ -479,7 +479,12 @@ class Pdb(bdb.Bdb, cmd.Cmd): def do_condition(self, arg): # arg is breakpoint number and condition args = arg.split(' ', 1) - bpnum = int(args[0].strip()) + try: + bpnum = int(args[0].strip()) + except ValueError: + # something went wrong + print >>self.stdout, \ + 'Breakpoint index %r is not a number' % args[0] try: cond = args[1] except: @@ -494,7 +499,12 @@ class Pdb(bdb.Bdb, cmd.Cmd): def do_ignore(self,arg): """arg is bp number followed by ignore count.""" args = arg.split() - bpnum = int(args[0].strip()) + try: + bpnum = int(args[0].strip()) + except ValueError: + # something went wrong + print >>self.stdout, \ + 'Breakpoint index %r is not a number' % args[0] try: count = int(args[1].strip()) except: diff --git a/Lib/random.py b/Lib/random.py index 0a65400..79ffb55 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -569,7 +569,7 @@ class Random(_random.Random): def betavariate(self, alpha, beta): """Beta distribution. - Conditions on the parameters are alpha > -1 and beta} > -1. + Conditions on the parameters are alpha > 0 and beta > 0. Returned values range between 0 and 1. """ diff --git a/Lib/rfc822.py b/Lib/rfc822.py index 0ef0d97..a9109cd 100644 --- a/Lib/rfc822.py +++ b/Lib/rfc822.py @@ -850,6 +850,11 @@ def parsedate_tz(data): if data[0][-1] in (',', '.') or data[0].lower() in _daynames: # There's a dayname here. Skip it del data[0] + else: + # no space after the "weekday,"? + i = data[0].rfind(',') + if i >= 0: + data[0] = data[0][i+1:] if len(data) == 3: # RFC 850 date, deprecated stuff = data[0].split('-') if len(stuff) == 3: diff --git a/Lib/smtplib.py b/Lib/smtplib.py index a96082a..701306d 100755 --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -43,10 +43,10 @@ Example: import socket import re -import email.Utils +import email.utils import base64 import hmac -from email.base64MIME import encode as encode_base64 +from email.base64mime import encode as encode_base64 from sys import stderr __all__ = ["SMTPException","SMTPServerDisconnected","SMTPResponseException", @@ -172,7 +172,7 @@ def quoteaddr(addr): """ m = (None, None) try: - m = email.Utils.parseaddr(addr)[1] + m = email.utils.parseaddr(addr)[1] except AttributeError: pass if m == (None, None): # Indicates parse failure or AttributeError diff --git a/Lib/socket.py b/Lib/socket.py index 08605f8..eff47d2 100644 --- a/Lib/socket.py +++ b/Lib/socket.py @@ -204,9 +204,10 @@ class _fileobject(object): __slots__ = ["mode", "bufsize", "softspace", # "closed" is a property, see below - "_sock", "_rbufsize", "_wbufsize", "_rbuf", "_wbuf"] + "_sock", "_rbufsize", "_wbufsize", "_rbuf", "_wbuf", + "_close"] - def __init__(self, sock, mode='rb', bufsize=-1): + def __init__(self, sock, mode='rb', bufsize=-1, close=False): self._sock = sock self.mode = mode # Not actually used in this version if bufsize < 0: @@ -222,6 +223,7 @@ class _fileobject(object): self._wbufsize = bufsize self._rbuf = "" # A string self._wbuf = [] # A list of strings + self._close = close def _getclosed(self): return self._sock is None @@ -232,6 +234,8 @@ class _fileobject(object): if self._sock: self.flush() finally: + if self._close: + self._sock.close() self._sock = None def __del__(self): diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 64e1f88..1685426 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1632,19 +1632,7 @@ class TarFile(object): # Create all upper directories. upperdirs = os.path.dirname(targetpath) if upperdirs and not os.path.exists(upperdirs): - ti = TarInfo() - ti.name = upperdirs - ti.type = DIRTYPE - ti.mode = 0777 - ti.mtime = tarinfo.mtime - ti.uid = tarinfo.uid - ti.gid = tarinfo.gid - ti.uname = tarinfo.uname - ti.gname = tarinfo.gname - try: - self._extract_member(ti, ti.name) - except: - pass + os.makedirs(upperdirs) if tarinfo.islnk() or tarinfo.issym(): self._dbg(1, "%s -> %s" % (tarinfo.name, tarinfo.linkname)) diff --git a/Lib/test/crashers/weakref_in_del.py b/Lib/test/crashers/weakref_in_del.py index 3bbcfc3..2e9b186 100644 --- a/Lib/test/crashers/weakref_in_del.py +++ b/Lib/test/crashers/weakref_in_del.py @@ -1,11 +1,12 @@ import weakref # http://python.org/sf/1377858 +# Fixed for new-style classes in 2.5c1. ref = None def test_weakref_in_del(): - class Target(object): + class Target(): def __del__(self): global ref ref = weakref.ref(self) diff --git a/Lib/test/output/test_new b/Lib/test/output/test_new deleted file mode 100644 index fd225f3..0000000 --- a/Lib/test/output/test_new +++ /dev/null @@ -1,6 +0,0 @@ -test_new -new.module() -new.classobj() -new.instancemethod() -new.function() -new.code() diff --git a/Lib/test/output/test_popen b/Lib/test/output/test_popen deleted file mode 100644 index db2ac06..0000000 --- a/Lib/test/output/test_popen +++ /dev/null @@ -1,3 +0,0 @@ -test_popen -Test popen: -popen seemed to process the command-line correctly diff --git a/Lib/test/output/test_resource b/Lib/test/output/test_resource deleted file mode 100644 index aafed83..0000000 --- a/Lib/test/output/test_resource +++ /dev/null @@ -1,2 +0,0 @@ -test_resource -True diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index 17d494c..02215e5 100755 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -12,6 +12,10 @@ from pickle import loads, dumps class ArraySubclass(array.array): pass +class ArraySubclassWithKwargs(array.array): + def __init__(self, typecode, newarg=None): + array.array.__init__(typecode) + tests = [] # list to accumulate all tests typecodes = "cubBhHiIlLfd" @@ -683,6 +687,9 @@ class BaseTest(unittest.TestCase): b = array.array('B', range(64)) self.assertEqual(rc, sys.getrefcount(10)) + def test_subclass_with_kwargs(self): + # SF bug #1486663 -- this used to erroneously raise a TypeError + ArraySubclassWithKwargs('b', newarg=1) class StringTest(BaseTest): diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py index 56031a7..a98339b 100644 --- a/Lib/test/test_deque.py +++ b/Lib/test/test_deque.py @@ -486,6 +486,16 @@ class TestSubclass(unittest.TestCase): d1 == d2 # not clear if this is supposed to be True or False, # but it used to give a SystemError + +class SubclassWithKwargs(deque): + def __init__(self, newarg=1): + deque.__init__(self) + +class TestSubclassWithKwargs(unittest.TestCase): + def test_subclass_with_kwargs(self): + # SF bug #1486663 -- this used to erroneously raise a TypeError + SubclassWithKwargs(newarg=1) + #============================================================================== libreftest = """ @@ -599,6 +609,7 @@ def test_main(verbose=None): TestBasic, TestVariousIteratorArgs, TestSubclass, + TestSubclassWithKwargs, ) test_support.run_unittest(*test_classes) diff --git a/Lib/test/test_dumbdbm.py b/Lib/test/test_dumbdbm.py index e5dfe1d..62fa3dd 100644 --- a/Lib/test/test_dumbdbm.py +++ b/Lib/test/test_dumbdbm.py @@ -50,11 +50,17 @@ class DumbDBMTestCase(unittest.TestCase): finally: os.umask(old_umask) + expected_mode = 0635 + if os.name != 'posix': + # Windows only supports setting the read-only attribute. + # This shouldn't fail, but doesn't work like Unix either. + expected_mode = 0666 + import stat st = os.stat(_fname + '.dat') - self.assertEqual(stat.S_IMODE(st.st_mode), 0635) + self.assertEqual(stat.S_IMODE(st.st_mode), expected_mode) st = os.stat(_fname + '.dir') - self.assertEqual(stat.S_IMODE(st.st_mode), 0635) + self.assertEqual(stat.S_IMODE(st.st_mode), expected_mode) def test_close_twice(self): f = dumbdbm.open(_fname) diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index 8e1118a..0b9f165 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -744,6 +744,21 @@ class RegressionTests(unittest.TestCase): self.assertRaises(AssertionError, list, cycle(gen1())) self.assertEqual(hist, [0,1]) +class SubclassWithKwargsTest(unittest.TestCase): + def test_keywords_in_subclass(self): + # count is not subclassable... + for cls in (repeat, izip, ifilter, ifilterfalse, chain, imap, + starmap, islice, takewhile, dropwhile, cycle): + class Subclass(cls): + def __init__(self, newarg=None, *args): + cls.__init__(self, *args) + try: + Subclass(newarg=1) + except TypeError as err: + # we expect type errors because of wrong argument count + self.failIf("does not take keyword arguments" in err.args[0]) + + libreftest = """ Doctest for examples in the library reference: libitertools.tex @@ -938,7 +953,8 @@ __test__ = {'libreftest' : libreftest} def test_main(verbose=None): test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC, - RegressionTests, LengthTransparency) + RegressionTests, LengthTransparency, + SubclassWithKwargsTest) test_support.run_unittest(*test_classes) # verify reference counting diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index def58cc..41ca7c2 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -4,7 +4,7 @@ import time import stat import socket import email -import email.Message +import email.message import rfc822 import re import StringIO @@ -22,7 +22,7 @@ class TestBase(unittest.TestCase): def _check_sample(self, msg): # Inspect a mailbox.Message representation of the sample message - self.assert_(isinstance(msg, email.Message.Message)) + self.assert_(isinstance(msg, email.message.Message)) self.assert_(isinstance(msg, mailbox.Message)) for key, value in _sample_headers.iteritems(): self.assert_(value in msg.get_all(key)) @@ -30,7 +30,7 @@ class TestBase(unittest.TestCase): self.assert_(len(msg.get_payload()) == len(_sample_payloads)) for i, payload in enumerate(_sample_payloads): part = msg.get_payload(i) - self.assert_(isinstance(part, email.Message.Message)) + self.assert_(isinstance(part, email.message.Message)) self.assert_(not isinstance(part, mailbox.Message)) self.assert_(part.get_payload() == payload) @@ -939,7 +939,7 @@ class TestMessage(TestBase): self._delete_recursively(self._path) def test_initialize_with_eMM(self): - # Initialize based on email.Message.Message instance + # Initialize based on email.message.Message instance eMM = email.message_from_string(_sample_message) msg = self._factory(eMM) self._post_initialize_hook(msg) @@ -965,7 +965,7 @@ class TestMessage(TestBase): # Initialize without arguments msg = self._factory() self._post_initialize_hook(msg) - self.assert_(isinstance(msg, email.Message.Message)) + self.assert_(isinstance(msg, email.message.Message)) self.assert_(isinstance(msg, mailbox.Message)) self.assert_(isinstance(msg, self._factory)) self.assert_(msg.keys() == []) @@ -992,7 +992,7 @@ class TestMessage(TestBase): mailbox.BabylMessage, mailbox.MMDFMessage): other_msg = class_() msg._explain_to(other_msg) - other_msg = email.Message.Message() + other_msg = email.message.Message() self.assertRaises(TypeError, lambda: msg._explain_to(other_msg)) def _post_initialize_hook(self, msg): @@ -1732,11 +1732,11 @@ class MaildirTestCase(unittest.TestCase): def test_unix_mbox(self): ### should be better! - import email.Parser + import email.parser fname = self.createMessage("cur", True) n = 0 for msg in mailbox.PortableUnixMailbox(open(fname), - email.Parser.Parser().parse): + email.parser.Parser().parse): n += 1 self.assertEqual(msg["subject"], "Simple Test") self.assertEqual(len(str(msg)), len(FROM_)+len(DUMMY_MESSAGE)) diff --git a/Lib/test/test_new.py b/Lib/test/test_new.py index e90fd66..e2d26fa 100644 --- a/Lib/test/test_new.py +++ b/Lib/test/test_new.py @@ -1,180 +1,158 @@ -from test.test_support import verbose, verify, TestFailed -import sys -import new - -class Eggs: - def get_yolks(self): - return self.yolks - -print 'new.module()' -m = new.module('Spam') -if verbose: - print m -m.Eggs = Eggs -sys.modules['Spam'] = m -import Spam - -def get_more_yolks(self): - return self.yolks + 3 - -print 'new.classobj()' -C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks}) -if verbose: - print C - -def break_yolks(self): - self.yolks = self.yolks - 2 -print 'new.instancemethod()' -c = C() -c.yolks = 3 -im = new.instancemethod(break_yolks, c, C) -if verbose: - print im - -verify(c.get_yolks() == 3 and c.get_more_yolks() == 6, - 'Broken call of hand-crafted class instance') -im() -verify(c.get_yolks() == 1 and c.get_more_yolks() == 4, - 'Broken call of hand-crafted instance method') - -im = new.instancemethod(break_yolks, c) -im() -verify(c.get_yolks() == -1) -try: - new.instancemethod(break_yolks, None) -except TypeError: - pass -else: - raise TestFailed, "dangerous instance method creation allowed" - -# Verify that instancemethod() doesn't allow keyword args -try: - new.instancemethod(break_yolks, c, kw=1) -except TypeError: - pass -else: - raise TestFailed, "instancemethod shouldn't accept keyword args" - -# It's unclear what the semantics should be for a code object compiled at -# module scope, but bound and run in a function. In CPython, `c' is global -# (by accident?) while in Jython, `c' is local. The intent of the test -# clearly is to make `c' global, so let's be explicit about it. -codestr = ''' -global c -a = 1 -b = 2 -c = a + b -''' - -ccode = compile(codestr, '<string>', 'exec') -# Jython doesn't have a __builtins__, so use a portable alternative -import __builtin__ -g = {'c': 0, '__builtins__': __builtin__} -# this test could be more robust -print 'new.function()' -func = new.function(ccode, g) -if verbose: - print func -func() -verify(g['c'] == 3, - 'Could not create a proper function object') - -# test the various extended flavors of function.new -def f(x): - def g(y): - return x + y - return g -g = f(4) -new.function(f.func_code, {}, "blah") -g2 = new.function(g.func_code, {}, "blah", (2,), g.func_closure) -verify(g2() == 6) -g3 = new.function(g.func_code, {}, "blah", None, g.func_closure) -verify(g3(5) == 9) -def test_closure(func, closure, exc): - try: - new.function(func.func_code, {}, "", None, closure) - except exc: - pass - else: - print "corrupt closure accepted" - -test_closure(g, None, TypeError) # invalid closure -test_closure(g, (1,), TypeError) # non-cell in closure -test_closure(g, (1, 1), ValueError) # closure is wrong size -test_closure(f, g.func_closure, ValueError) # no closure needed - -print 'new.code()' -# bogus test of new.code() -# Note: Jython will never have new.code() -if hasattr(new, 'code'): - def f(a): pass - - c = f.func_code - argcount = c.co_argcount - kwonlyargcount = c.co_kwonlyargcount - nlocals = c.co_nlocals - stacksize = c.co_stacksize - flags = c.co_flags - codestring = c.co_code - constants = c.co_consts - names = c.co_names - varnames = c.co_varnames - filename = c.co_filename - name = c.co_name - firstlineno = c.co_firstlineno - lnotab = c.co_lnotab - freevars = c.co_freevars - cellvars = c.co_cellvars - - d = new.code(argcount, kwonlyargcount, - nlocals, stacksize, flags, codestring, - constants, names, varnames, filename, name, - firstlineno, lnotab, freevars, cellvars) - - # test backwards-compatibility version with no freevars or cellvars - d = new.code(argcount, kwonlyargcount, - nlocals, stacksize, flags, codestring, - constants, names, varnames, filename, name, - firstlineno, lnotab) - - try: # this used to trigger a SystemError - d = new.code(-argcount, kwonlyargcount, - nlocals, stacksize, flags, codestring, - constants, names, varnames, filename, name, - firstlineno, lnotab) - except ValueError: - pass - else: - raise TestFailed, "negative co_argcount didn't trigger an exception" - - try: # this used to trigger a SystemError - d = new.code(argcount, kwonlyargcount, - -nlocals, stacksize, flags, codestring, - constants, names, varnames, filename, name, - firstlineno, lnotab) - except ValueError: - pass - else: - raise TestFailed, "negative co_nlocals didn't trigger an exception" - - try: # this used to trigger a Py_FatalError! - d = new.code(argcount, kwonlyargcount, - nlocals, stacksize, flags, codestring, - constants, (5,), varnames, filename, name, - firstlineno, lnotab) - except TypeError: - pass - else: - raise TestFailed, "non-string co_name didn't trigger an exception" - - # new.code used to be a way to mutate a tuple... - class S(str): pass - t = (S("ab"),) - d = new.code(argcount, kwonlyargcount, - nlocals, stacksize, flags, codestring, - constants, t, varnames, filename, name, - firstlineno, lnotab) - verify(type(t[0]) is S, "eek, tuple changed under us!") - - if verbose: - print d +import unittest +from test import test_support +import sys, new + +class NewTest(unittest.TestCase): + def test_spam(self): + class Eggs: + def get_yolks(self): + return self.yolks + + m = new.module('Spam') + m.Eggs = Eggs + sys.modules['Spam'] = m + import Spam + + def get_more_yolks(self): + return self.yolks + 3 + + # new.classobj() + C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks}) + + def break_yolks(self): + self.yolks = self.yolks - 2 + + # new.instancemethod() + c = C() + c.yolks = 3 + im = new.instancemethod(break_yolks, c, C) + + self.assertEqual(c.get_yolks(), 3, + 'Broken call of hand-crafted class instance') + self.assertEqual(c.get_more_yolks(), 6, + 'Broken call of hand-crafted class instance') + + im() + self.assertEqual(c.get_yolks(), 1, + 'Broken call of hand-crafted instance method') + self.assertEqual(c.get_more_yolks(), 4, + 'Broken call of hand-crafted instance method') + + im = new.instancemethod(break_yolks, c) + im() + self.assertEqual(c.get_yolks(), -1) + + # Verify that dangerous instance method creation is forbidden + self.assertRaises(TypeError, new.instancemethod, break_yolks, None) + + # Verify that instancemethod() doesn't allow keyword args + self.assertRaises(TypeError, new.instancemethod, break_yolks, c, kw=1) + + def test_scope(self): + # It's unclear what the semantics should be for a code object compiled + # at module scope, but bound and run in a function. In CPython, `c' is + # global (by accident?) while in Jython, `c' is local. The intent of + # the test clearly is to make `c' global, so let's be explicit about it. + codestr = ''' + global c + a = 1 + b = 2 + c = a + b + ''' + + codestr = "\n".join(l.strip() for l in codestr.splitlines()) + + ccode = compile(codestr, '<string>', 'exec') + # Jython doesn't have a __builtins__, so use a portable alternative + import __builtin__ + g = {'c': 0, '__builtins__': __builtin__} + + # this test could be more robust + func = new.function(ccode, g) + func() + self.assertEqual(g['c'], 3, 'Could not create a proper function object') + + def test_function(self): + # test the various extended flavors of function.new + def f(x): + def g(y): + return x + y + return g + g = f(4) + new.function(f.func_code, {}, "blah") + g2 = new.function(g.func_code, {}, "blah", (2,), g.func_closure) + self.assertEqual(g2(), 6) + g3 = new.function(g.func_code, {}, "blah", None, g.func_closure) + self.assertEqual(g3(5), 9) + def test_closure(func, closure, exc): + self.assertRaises(exc, new.function, func.func_code, {}, "", None, closure) + + test_closure(g, None, TypeError) # invalid closure + test_closure(g, (1,), TypeError) # non-cell in closure + test_closure(g, (1, 1), ValueError) # closure is wrong size + test_closure(f, g.func_closure, ValueError) # no closure needed + + # Note: Jython will never have new.code() + if hasattr(new, 'code'): + def test_code(self): + # bogus test of new.code() + def f(a): pass + + c = f.func_code + argcount = c.co_argcount + kwonlyargcount = c.co_kwonlyargcount + nlocals = c.co_nlocals + stacksize = c.co_stacksize + flags = c.co_flags + codestring = c.co_code + constants = c.co_consts + names = c.co_names + varnames = c.co_varnames + filename = c.co_filename + name = c.co_name + firstlineno = c.co_firstlineno + lnotab = c.co_lnotab + freevars = c.co_freevars + cellvars = c.co_cellvars + + d = new.code(argcount, kwonlyargcount, nlocals, stacksize, flags, + codestring, constants, names, varnames, filename, + name, firstlineno, lnotab, freevars, cellvars) + + # test backwards-compatibility version with no freevars or cellvars + d = new.code(argcount, kwonlyargcount, nlocals, stacksize, + flags, codestring, constants, names, varnames, + filename, name, firstlineno, lnotab) + + # negative co_argcount used to trigger a SystemError + self.assertRaises(ValueError, new.code, + -argcount, kwonlyargcount, nlocals, stacksize, flags, + codestring, constants, names, varnames, filename, name, + firstlineno, lnotab) + + # negative co_nlocals used to trigger a SystemError + self.assertRaises(ValueError, new.code, + argcount, kwonlyargcount, -nlocals, stacksize, flags, + codestring, constants, names, varnames, filename, name, + firstlineno, lnotab) + + # non-string co_name used to trigger a Py_FatalError + self.assertRaises(TypeError, new.code, + argcount, kwonlyargcount, nlocals, stacksize, flags, + codestring, constants, (5,), varnames, filename, name, + firstlineno, lnotab) + + # new.code used to be a way to mutate a tuple... + class S(str): + pass + t = (S("ab"),) + d = new.code(argcount, kwonlyargcount, nlocals, stacksize, + flags, codestring, constants, t, varnames, + filename, name, firstlineno, lnotab) + self.assert_(type(t[0]) is S, "eek, tuple changed under us!") + +def test_main(): + test_support.run_unittest(NewTest) + +if __name__ == "__main__": + test_main() diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py index 139aa1f..6bc2a05 100644 --- a/Lib/test/test_ntpath.py +++ b/Lib/test/test_ntpath.py @@ -115,6 +115,28 @@ tester("ntpath.normpath('K:../.././..')", r'K:..\..\..') tester("ntpath.normpath('C:////a/b')", r'C:\a\b') tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b') +oldenv = os.environ.copy() +try: + os.environ.clear() + os.environ["foo"] = "bar" + os.environ["{foo"] = "baz1" + os.environ["{foo}"] = "baz2" + tester('ntpath.expandvars("foo")', "foo") + tester('ntpath.expandvars("$foo bar")', "bar bar") + tester('ntpath.expandvars("${foo}bar")', "barbar") + tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar") + tester('ntpath.expandvars("$bar bar")', "$bar bar") + tester('ntpath.expandvars("$?bar")', "$?bar") + tester('ntpath.expandvars("${foo}bar")', "barbar") + tester('ntpath.expandvars("$foo}bar")', "bar}bar") + tester('ntpath.expandvars("${foo")', "${foo") + tester('ntpath.expandvars("${{foo}}")', "baz1}") + tester('ntpath.expandvars("$foo$foo")', "barbar") + tester('ntpath.expandvars("$bar$bar")', "$bar$bar") +finally: + os.environ.clear() + os.environ.update(oldenv) + # ntpath.abspath() can only be used on a system with the "nt" module # (reasonably), so we protect this test with "import nt". This allows # the rest of the tests for the ntpath module to be run to completion diff --git a/Lib/test/test_old_mailbox.py b/Lib/test/test_old_mailbox.py index cca6897..c8f6bac 100644 --- a/Lib/test/test_old_mailbox.py +++ b/Lib/test/test_old_mailbox.py @@ -109,11 +109,44 @@ class MaildirTestCase(unittest.TestCase): self.assertEqual(len(str(msg)), len(FROM_)+len(DUMMY_MESSAGE)) self.assertEqual(n, 1) +class MboxTestCase(unittest.TestCase): + def setUp(self): + # create a new maildir mailbox to work with: + self._path = test_support.TESTFN + + def tearDown(self): + os.unlink(self._path) + + def test_from_regex (self): + # Testing new regex from bug #1633678 + f = open(self._path, 'w') + f.write("""From fred@example.com Mon May 31 13:24:50 2004 +0200 +Subject: message 1 + +body1 +From fred@example.com Mon May 31 13:24:50 2004 -0200 +Subject: message 2 + +body2 +From fred@example.com Mon May 31 13:24:50 2004 +Subject: message 3 + +body3 +From fred@example.com Mon May 31 13:24:50 2004 +Subject: message 4 + +body4 +""") + f.close() + box = mailbox.UnixMailbox(open(self._path, 'r')) + self.assert_(len(list(iter(box))) == 4) + + # XXX We still need more tests! def test_main(): - test_support.run_unittest(MaildirTestCase) + test_support.run_unittest(MaildirTestCase, MboxTestCase) if __name__ == "__main__": diff --git a/Lib/test/test_popen.py b/Lib/test/test_popen.py index fbf5e05..069f370 100644 --- a/Lib/test/test_popen.py +++ b/Lib/test/test_popen.py @@ -4,10 +4,9 @@ Particularly useful for platforms that fake popen. """ -import os -import sys -from test.test_support import TestSkipped, reap_children -from os import popen +import unittest +from test import test_support +import os, sys # Test that command-lines get down as we expect. # To do this we execute: @@ -17,24 +16,32 @@ from os import popen python = sys.executable if ' ' in python: python = '"' + python + '"' # quote embedded space for cmdline -def _do_test_commandline(cmdline, expected): - cmd = '%s -c "import sys;print sys.argv" %s' % (python, cmdline) - data = popen(cmd).read() - got = eval(data)[1:] # strip off argv[0] - if got != expected: - print "Error in popen commandline handling." - print " executed '%s', expected '%r', but got '%r'" \ - % (cmdline, expected, got) -def _test_commandline(): - _do_test_commandline("foo bar", ["foo", "bar"]) - _do_test_commandline('foo "spam and eggs" "silly walk"', ["foo", "spam and eggs", "silly walk"]) - _do_test_commandline('foo "a \\"quoted\\" arg" bar', ["foo", 'a "quoted" arg', "bar"]) - print "popen seemed to process the command-line correctly" +class PopenTest(unittest.TestCase): + def _do_test_commandline(self, cmdline, expected): + cmd = '%s -c "import sys;print sys.argv" %s' % (python, cmdline) + data = os.popen(cmd).read() + got = eval(data)[1:] # strip off argv[0] + self.assertEqual(got, expected) -def main(): - print "Test popen:" - _test_commandline() - reap_children() + def test_popen(self): + self.assertRaises(TypeError, os.popen) + self._do_test_commandline( + "foo bar", + ["foo", "bar"] + ) + self._do_test_commandline( + 'foo "spam and eggs" "silly walk"', + ["foo", "spam and eggs", "silly walk"] + ) + self._do_test_commandline( + 'foo "a \\"quoted\\" arg" bar', + ["foo", 'a "quoted" arg', "bar"] + ) + test_support.reap_children() -main() +def test_main(): + test_support.run_unittest(PopenTest) + +if __name__ == "__main__": + test_main() diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 3984157..20a1fc5 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -374,6 +374,8 @@ class PosixPathTest(unittest.TestCase): self.assertEqual(posixpath.expandvars("$foo}bar"), "bar}bar") self.assertEqual(posixpath.expandvars("${foo"), "${foo") self.assertEqual(posixpath.expandvars("${{foo}}"), "baz1}") + self.assertEqual(posixpath.expandvars("$foo$foo"), "barbar") + self.assertEqual(posixpath.expandvars("$bar$bar"), "$bar$bar") finally: os.environ.clear() os.environ.update(oldenv) diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index 0e0a273..5a76b1e 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -517,6 +517,14 @@ class TestModule(unittest.TestCase): # tests validity but not completeness of the __all__ list self.failUnless(set(random.__all__) <= set(dir(random))) + def test_random_subclass_with_kwargs(self): + # SF bug #1486663 -- this used to erroneously raise a TypeError + class Subclass(random.Random): + def __init__(self, newarg=None): + random.Random.__init__(self) + Subclass(newarg=1) + + def test_main(verbose=None): testclasses = [WichmannHill_TestBasicOps, MersenneTwister_TestBasicOps, diff --git a/Lib/test/test_resource.py b/Lib/test/test_resource.py index 31aa370..dd66e35 100644 --- a/Lib/test/test_resource.py +++ b/Lib/test/test_resource.py @@ -1,56 +1,96 @@ -import os -import resource - -from test.test_support import TESTFN - -# This test is checking a few specific problem spots. RLIMIT_FSIZE -# should be RLIM_INFINITY, which will be a really big number on a -# platform with large file support. On these platforms, we need to -# test that the get/setrlimit functions properly convert the number to -# a C long long and that the conversion doesn't raise an error. - -try: - cur, max = resource.getrlimit(resource.RLIMIT_FSIZE) -except AttributeError: - pass -else: - print resource.RLIM_INFINITY == max - resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) - -# Now check to see what happens when the RLIMIT_FSIZE is small. Some -# versions of Python were terminated by an uncaught SIGXFSZ, but -# pythonrun.c has been fixed to ignore that exception. If so, the -# write() should return EFBIG when the limit is exceeded. - -# At least one platform has an unlimited RLIMIT_FSIZE and attempts to -# change it raise ValueError instead. - -try: - try: - resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max)) - limit_set = 1 - except ValueError: - limit_set = 0 - f = open(TESTFN, "wb") - f.write("X" * 1024) - try: - f.write("Y") - f.flush() - except IOError: - if not limit_set: - raise - f.close() - os.unlink(TESTFN) -finally: - resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) - -# And be sure that setrlimit is checking for really large values -too_big = 10**50 -try: - resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max)) -except (OverflowError, ValueError): - pass -try: - resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big)) -except (OverflowError, ValueError): - pass +import unittest +from test import test_support + + +import os, resource + +# This test is checking a few specific problem spots with the resource module. + +class ResourceTest(unittest.TestCase): + + def test_args(self): + self.assertRaises(TypeError, resource.getrlimit) + self.assertRaises(TypeError, resource.getrlimit, 42, 42) + self.assertRaises(TypeError, resource.setrlimit) + self.assertRaises(TypeError, resource.setrlimit, 42, 42, 42) + + def test_fsize_ismax(self): + + try: + (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE) + except AttributeError: + pass + else: + # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big + # number on a platform with large file support. On these platforms, + # we need to test that the get/setrlimit functions properly convert + # the number to a C long long and that the conversion doesn't raise + # an error. + self.assertEqual(resource.RLIM_INFINITY, max) + resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) + + def test_fsize_enforced(self): + try: + (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE) + except AttributeError: + pass + else: + # Check to see what happens when the RLIMIT_FSIZE is small. Some + # versions of Python were terminated by an uncaught SIGXFSZ, but + # pythonrun.c has been fixed to ignore that exception. If so, the + # write() should return EFBIG when the limit is exceeded. + + # At least one platform has an unlimited RLIMIT_FSIZE and attempts + # to change it raise ValueError instead. + try: + try: + resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max)) + limit_set = True + except ValueError: + limit_set = False + f = open(test_support.TESTFN, "wb") + f.write("X" * 1024) + try: + f.write("Y") + f.flush() + except IOError: + if not limit_set: + raise + f.close() + os.unlink(test_support.TESTFN) + finally: + resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) + + def test_fsize_toobig(self): + # Be sure that setrlimit is checking for really large values + too_big = 10**50 + try: + (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE) + except AttributeError: + pass + else: + try: + resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max)) + except (OverflowError, ValueError): + pass + try: + resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big)) + except (OverflowError, ValueError): + pass + + def test_getrusage(self): + self.assertRaises(TypeError, resource.getrusage) + self.assertRaises(TypeError, resource.getrusage, 42, 42) + usageself = resource.getrusage(resource.RUSAGE_SELF) + usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN) + # May not be available on all systems. + try: + usageboth = resource.getrusage(resource.RUSAGE_BOTH) + except (ValueError, AttributeError): + pass + +def test_main(verbose=None): + test_support.run_unittest(ResourceTest) + +if __name__ == "__main__": + test_main() diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 1f26b4e..4f74186 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -809,6 +809,31 @@ class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase): bufsize = 2 # Exercise the buffering code + +class Urllib2FileobjectTest(unittest.TestCase): + + # urllib2.HTTPHandler has "borrowed" socket._fileobject, and requires that + # it close the socket if the close c'tor argument is true + + def testClose(self): + class MockSocket: + closed = False + def flush(self): pass + def close(self): self.closed = True + + # must not close unless we request it: the original use of _fileobject + # by module socket requires that the underlying socket not be closed until + # the _socketobject that created the _fileobject is closed + s = MockSocket() + f = socket._fileobject(s) + f.close() + self.assert_(not s.closed) + + s = MockSocket() + f = socket._fileobject(s, close=True) + f.close() + self.assert_(s.closed) + class TCPTimeoutTest(SocketTCPTest): def testTCPTimeout(self): @@ -961,7 +986,8 @@ def test_main(): FileObjectClassTestCase, UnbufferedFileObjectClassTestCase, LineBufferedFileObjectClassTestCase, - SmallBufferedFileObjectClassTestCase + SmallBufferedFileObjectClassTestCase, + Urllib2FileobjectTest, ]) if hasattr(socket, "socketpair"): tests.append(BasicSocketPairTest) diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index f1bef4b..8b241a6 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -84,8 +84,8 @@ sz = struct.calcsize('i') if sz * 3 != struct.calcsize('iii'): raise TestFailed, 'inconsistent sizes' -fmt = 'cbxxxxxxhhhhiillffd' -fmt3 = '3c3b18x12h6i6l6f3d' +fmt = 'cbxxxxxxhhhhiillffdt' +fmt3 = '3c3b18x12h6i6l6f3d3t' sz = struct.calcsize(fmt) sz3 = struct.calcsize(fmt3) if sz * 3 != sz3: @@ -108,19 +108,21 @@ i = 65535 l = 65536 f = 3.1415 d = 3.1415 +t = True for prefix in ('', '@', '<', '>', '=', '!'): - for format in ('xcbhilfd', 'xcBHILfd'): + for format in ('xcbhilfdt', 'xcBHILfdt'): format = prefix + format if verbose: print "trying:", format - s = struct.pack(format, c, b, h, i, l, f, d) - cp, bp, hp, ip, lp, fp, dp = struct.unpack(format, s) + s = struct.pack(format, c, b, h, i, l, f, d, t) + cp, bp, hp, ip, lp, fp, dp, tp = struct.unpack(format, s) if (cp != c or bp != b or hp != h or ip != i or lp != l or - int(100 * fp) != int(100 * f) or int(100 * dp) != int(100 * d)): + int(100 * fp) != int(100 * f) or int(100 * dp) != int(100 * d) or + tp != t): # ^^^ calculate only to two decimal places raise TestFailed, "unpack/pack not transitive (%s, %s)" % ( - str(format), str((cp, bp, hp, ip, lp, fp, dp))) + str(format), str((cp, bp, hp, ip, lp, fp, dp, tp))) # Test some of the new features in detail @@ -158,6 +160,11 @@ tests = [ ('f', -2.0, '\300\000\000\000', '\000\000\000\300', 0), ('d', -2.0, '\300\000\000\000\000\000\000\000', '\000\000\000\000\000\000\000\300', 0), + ('t', 0, '\0', '\0', 0), + ('t', 3, '\1', '\1', 1), + ('t', True, '\1', '\1', 0), + ('t', [], '\0', '\0', 1), + ('t', (1,), '\1', '\1', 1), ] for fmt, arg, big, lil, asy in tests: @@ -612,3 +619,50 @@ def test_pack_into_fn(): test_unpack_from() test_pack_into() test_pack_into_fn() + +def test_bool(): + for prefix in tuple("<>!=")+('',): + false = (), [], [], '', 0 + true = [1], 'test', 5, -1, 0xffffffff+1, 0xffffffff/2 + + falseFormat = prefix + 't' * len(false) + if verbose: + print 'trying bool pack/unpack on', false, 'using format', falseFormat + packedFalse = struct.pack(falseFormat, *false) + unpackedFalse = struct.unpack(falseFormat, packedFalse) + + trueFormat = prefix + 't' * len(true) + if verbose: + print 'trying bool pack/unpack on', true, 'using format', trueFormat + packedTrue = struct.pack(trueFormat, *true) + unpackedTrue = struct.unpack(trueFormat, packedTrue) + + if len(true) != len(unpackedTrue): + raise TestFailed('unpacked true array is not of same size as input') + if len(false) != len(unpackedFalse): + raise TestFailed('unpacked false array is not of same size as input') + + for t in unpackedFalse: + if t is not False: + raise TestFailed('%r did not unpack as False' % t) + for t in unpackedTrue: + if t is not True: + raise TestFailed('%r did not unpack as false' % t) + + if prefix and verbose: + print 'trying size of bool with format %r' % (prefix+'t') + packed = struct.pack(prefix+'t', 1) + + if len(packed) != struct.calcsize(prefix+'t'): + raise TestFailed('packed length is not equal to calculated size') + + if len(packed) != 1 and prefix: + raise TestFailed('encoded bool is not one byte: %r' % packed) + elif not prefix and verbose: + print 'size of bool in native format is %i' % (len(packed)) + + for c in '\x01\x7f\xff\x0f\xf0': + if struct.unpack('>t', c)[0] is not True: + raise TestFailed('%c did not unpack as True' % c) + +test_bool() diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py index 50acff8..60d5f48 100644 --- a/Lib/test/test_urllib2net.py +++ b/Lib/test/test_urllib2net.py @@ -64,6 +64,27 @@ class AuthTests(unittest.TestCase): # urllib2.urlopen, "http://evil:thing@example.com") +class CloseSocketTest(unittest.TestCase): + + def test_close(self): + import socket, httplib, gc + + # calling .close() on urllib2's response objects should close the + # underlying socket + + # delve deep into response to fetch socket._socketobject + response = urllib2.urlopen("http://www.python.org/") + abused_fileobject = response.fp + self.assert_(abused_fileobject.__class__ is socket._fileobject) + httpresponse = abused_fileobject._sock + self.assert_(httpresponse.__class__ is httplib.HTTPResponse) + fileobject = httpresponse.fp + self.assert_(fileobject.__class__ is socket._fileobject) + + self.assert_(not fileobject.closed) + response.close() + self.assert_(fileobject.closed) + class urlopenNetworkTests(unittest.TestCase): """Tests urllib2.urlopen using the network. @@ -263,8 +284,12 @@ class OtherNetworkTests(unittest.TestCase): def test_main(): test_support.requires("network") - test_support.run_unittest(URLTimeoutTest, urlopenNetworkTests, - AuthTests, OtherNetworkTests) + test_support.run_unittest(URLTimeoutTest, + urlopenNetworkTests, + AuthTests, + OtherNetworkTests, + CloseSocketTest, + ) if __name__ == "__main__": test_main() diff --git a/Lib/test/test_uu.py b/Lib/test/test_uu.py index 7128148..548cafb 100644 --- a/Lib/test/test_uu.py +++ b/Lib/test/test_uu.py @@ -114,11 +114,11 @@ class UUFileTest(unittest.TestCase): def test_encode(self): try: - fin = open(self.tmpin, 'w') + fin = open(self.tmpin, 'wb') fin.write(plaintext) fin.close() - fin = open(self.tmpin, 'r') + fin = open(self.tmpin, 'rb') fout = open(self.tmpout, 'w') uu.encode(fin, fout, self.tmpin, mode=0644) fin.close() diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py index 06f4537..f81d168 100644 --- a/Lib/test/test_weakref.py +++ b/Lib/test/test_weakref.py @@ -6,6 +6,8 @@ import weakref from test import test_support +# Used in ReferencesTestCase.test_ref_created_during_del() . +ref_from_del = None class C: def method(self): @@ -630,6 +632,18 @@ class ReferencesTestCase(TestBase): finally: gc.set_threshold(*thresholds) + def test_ref_created_during_del(self): + # Bug #1377858 + # A weakref created in an object's __del__() would crash the + # interpreter when the weakref was cleaned up since it would refer to + # non-existent memory. This test should not segfault the interpreter. + class Target(object): + def __del__(self): + global ref_from_del + ref_from_del = weakref.ref(self) + + w = Target() + class SubclassableWeakrefTestCase(unittest.TestCase): diff --git a/Lib/urllib.py b/Lib/urllib.py index aacc373..bb9472b 100644 --- a/Lib/urllib.py +++ b/Lib/urllib.py @@ -452,7 +452,7 @@ class URLopener: def open_local_file(self, url): """Use local file.""" - import mimetypes, mimetools, email.Utils + import mimetypes, mimetools, email.utils try: from cStringIO import StringIO except ImportError: @@ -464,7 +464,7 @@ class URLopener: except OSError as e: raise IOError(e.errno, e.strerror, e.filename) size = stats.st_size - modified = email.Utils.formatdate(stats.st_mtime, usegmt=True) + modified = email.utils.formatdate(stats.st_mtime, usegmt=True) mtype = mimetypes.guess_type(url)[0] headers = mimetools.Message(StringIO( 'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' % diff --git a/Lib/urllib2.py b/Lib/urllib2.py index d14996d..058397d 100644 --- a/Lib/urllib2.py +++ b/Lib/urllib2.py @@ -1087,7 +1087,7 @@ class AbstractHTTPHandler(BaseHandler): # out of socket._fileobject() and into a base class. r.recv = r.read - fp = socket._fileobject(r) + fp = socket._fileobject(r, close=True) resp = addinfourl(fp, r.msg, req.get_full_url()) resp.code = r.status @@ -1209,14 +1209,14 @@ class FileHandler(BaseHandler): # not entirely sure what the rules are here def open_local_file(self, req): - import email.Utils + import email.utils import mimetypes host = req.get_host() file = req.get_selector() localfile = url2pathname(file) stats = os.stat(localfile) size = stats.st_size - modified = email.Utils.formatdate(stats.st_mtime, usegmt=True) + modified = email.utils.formatdate(stats.st_mtime, usegmt=True) mtype = mimetypes.guess_type(file)[0] headers = mimetools.Message(StringIO( 'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' % |