Merged revisions 53451-53537 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r53454 | brett.cannon | 2007-01-15 20:12:08 +0100 (Mon, 15 Jan 2007) | 3 lines

  Add a note for strptime that just because strftime supports some extra
  directive that is not documented that strptime will as well.
........
  r53458 | vinay.sajip | 2007-01-16 10:50:07 +0100 (Tue, 16 Jan 2007) | 1 line

  Updated rotating file handlers to use _open().
........
  r53459 | marc-andre.lemburg | 2007-01-16 14:03:06 +0100 (Tue, 16 Jan 2007) | 2 lines

  Add news items for the recent pybench and platform changes.
........
  r53460 | sjoerd.mullender | 2007-01-16 17:42:38 +0100 (Tue, 16 Jan 2007) | 4 lines

  Fixed ntpath.expandvars to not replace references to non-existing
  variables with nothing.  Also added tests.
  This fixes bug #494589.
........
  r53464 | neal.norwitz | 2007-01-17 07:23:51 +0100 (Wed, 17 Jan 2007) | 1 line

  Give Calvin Spealman access for python-dev summaries.
........
  r53465 | neal.norwitz | 2007-01-17 09:37:26 +0100 (Wed, 17 Jan 2007) | 1 line

  Remove Calvin since he only has access to the website currently.
........
  r53466 | thomas.heller | 2007-01-17 10:40:34 +0100 (Wed, 17 Jan 2007) | 2 lines

  Replace C++ comments with C comments.
........
  r53472 | andrew.kuchling | 2007-01-17 20:55:06 +0100 (Wed, 17 Jan 2007) | 1 line

  [Part of bug #1599254] Add suggestion to Mailbox docs to use Maildir, and warn user to lock/unlock mailboxes when modifying them
........
  r53475 | georg.brandl | 2007-01-17 22:09:04 +0100 (Wed, 17 Jan 2007) | 2 lines

  Bug #1637967: missing //= operator in list.
........
  r53477 | georg.brandl | 2007-01-17 22:19:58 +0100 (Wed, 17 Jan 2007) | 2 lines

  Bug #1629125: fix wrong data type (int -> Py_ssize_t) in PyDict_Next docs.
........
  r53481 | neal.norwitz | 2007-01-18 06:40:58 +0100 (Thu, 18 Jan 2007) | 1 line

  Try reverting part of r53145 that seems to cause the Windows buildbots to fail in test_uu.UUFileTest.test_encode
........
  r53482 | fred.drake | 2007-01-18 06:42:30 +0100 (Thu, 18 Jan 2007) | 1 line

  add missing version entry
........
  r53483 | neal.norwitz | 2007-01-18 07:20:55 +0100 (Thu, 18 Jan 2007) | 7 lines

  This test doesn't pass on Windows.  The cause seems to be that chmod
  doesn't support the same funcationality as on Unix.  I'm not sure if
  this fix is the best (or if it will even work)--it's a test to see
  if the buildbots start passing again.

  It might be better to not even run this test if it's windows (or non-posix).
........
  r53488 | neal.norwitz | 2007-01-19 06:53:33 +0100 (Fri, 19 Jan 2007) | 1 line

  SF #1635217, Fix unbalanced paren
........
  r53489 | martin.v.loewis | 2007-01-19 07:42:22 +0100 (Fri, 19 Jan 2007) | 3 lines

  Prefix AST symbols with _Py_. Fixes #1637022.
  Will backport.
........
  r53497 | martin.v.loewis | 2007-01-19 19:01:38 +0100 (Fri, 19 Jan 2007) | 2 lines

  Add UUIDs for 2.5.1 and 2.5.2
........
  r53499 | raymond.hettinger | 2007-01-19 19:07:18 +0100 (Fri, 19 Jan 2007) | 1 line

  SF# 1635892:  Fix docs for betavariate's input parameters .
........
  r53503 | martin.v.loewis | 2007-01-20 15:05:39 +0100 (Sat, 20 Jan 2007) | 2 lines

  Merge 53501 and 53502 from 25 branch:
  Add /GS- for AMD64 and Itanium builds where missing.
........
  r53504 | walter.doerwald | 2007-01-20 18:28:31 +0100 (Sat, 20 Jan 2007) | 2 lines

  Port test_resource.py to unittest.
........
  r53505 | walter.doerwald | 2007-01-20 19:19:33 +0100 (Sat, 20 Jan 2007) | 2 lines

  Add argument tests an calls of resource.getrusage().
........
  r53506 | walter.doerwald | 2007-01-20 20:03:17 +0100 (Sat, 20 Jan 2007) | 2 lines

  resource.RUSAGE_BOTH might not exist.
........
  r53507 | walter.doerwald | 2007-01-21 00:07:28 +0100 (Sun, 21 Jan 2007) | 2 lines

  Port test_new.py to unittest.
........
  r53508 | martin.v.loewis | 2007-01-21 10:33:07 +0100 (Sun, 21 Jan 2007) | 2 lines

  Patch #1610575: Add support for _Bool to struct.
........
  r53509 | georg.brandl | 2007-01-21 11:28:43 +0100 (Sun, 21 Jan 2007) | 3 lines

  Bug #1486663: don't reject keyword arguments for subclasses of builtin
  types.
........
  r53511 | georg.brandl | 2007-01-21 11:35:10 +0100 (Sun, 21 Jan 2007) | 2 lines

  Patch #1627441: close sockets properly in urllib2.
........
  r53517 | georg.brandl | 2007-01-22 20:40:21 +0100 (Mon, 22 Jan 2007) | 3 lines

  Use new email module names (#1637162, #1637159, #1637157).
........
  r53518 | andrew.kuchling | 2007-01-22 21:26:40 +0100 (Mon, 22 Jan 2007) | 1 line

  Improve pattern used for mbox 'From' lines; add a simple test
........
  r53519 | andrew.kuchling | 2007-01-22 21:27:50 +0100 (Mon, 22 Jan 2007) | 1 line

  Make comment match the code
........
  r53522 | georg.brandl | 2007-01-22 22:10:33 +0100 (Mon, 22 Jan 2007) | 2 lines

  Bug #1249573: fix rfc822.parsedate not accepting a certain date format
........
  r53524 | georg.brandl | 2007-01-22 22:23:41 +0100 (Mon, 22 Jan 2007) | 2 lines

  Bug #1627316: handle error in condition/ignore pdb commands more gracefully.
........
  r53526 | lars.gustaebel | 2007-01-23 12:17:33 +0100 (Tue, 23 Jan 2007) | 4 lines

  Patch #1507247: tarfile.py: use current umask for intermediate
  directories.
........
  r53527 | thomas.wouters | 2007-01-23 14:42:00 +0100 (Tue, 23 Jan 2007) | 13 lines


  SF patch #1630975: Fix crash when replacing sys.stdout in sitecustomize

  When running the interpreter in an environment that would cause it to set
  stdout/stderr/stdin's encoding, having a sitecustomize that would replace
  them with something other than PyFile objects would crash the interpreter.
  Fix it by simply ignoring the encoding-setting for non-files.

  This could do with a test, but I can think of no maintainable and portable
  way to test this bug, short of adding a sitecustomize.py to the buildsystem
  and have it always run with it (hmmm....)
........
  r53528 | thomas.wouters | 2007-01-23 14:50:49 +0100 (Tue, 23 Jan 2007) | 4 lines


  Add news entry about last checkin (oops.)
........
  r53531 | martin.v.loewis | 2007-01-23 22:11:47 +0100 (Tue, 23 Jan 2007) | 4 lines

  Make PyTraceBack_Here use the current thread, not the
  frame's thread state. Fixes #1579370.
  Will backport.
........
  r53535 | brett.cannon | 2007-01-24 00:21:22 +0100 (Wed, 24 Jan 2007) | 5 lines

  Fix crasher for when an object's __del__ creates a new weakref to itself.
  Patch only fixes new-style classes; classic classes still buggy.

  Closes bug #1377858.  Already backported.
........
  r53536 | walter.doerwald | 2007-01-24 01:42:19 +0100 (Wed, 24 Jan 2007) | 2 lines

  Port test_popen.py to unittest.
........
This commit is contained in:
Thomas Wouters 2007-02-01 18:02:27 +00:00
parent ec7027fc5b
commit b213704f3c
77 changed files with 1417 additions and 495 deletions

View file

@ -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)

View file

@ -1,6 +0,0 @@
test_new
new.module()
new.classobj()
new.instancemethod()
new.function()
new.code()

View file

@ -1,3 +0,0 @@
test_popen
Test popen:
popen seemed to process the command-line correctly

View file

@ -1,2 +0,0 @@
test_resource
True

View file

@ -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):

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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))

View file

@ -1,180 +1,158 @@
from test.test_support import verbose, verify, TestFailed
import sys
import new
import unittest
from test import test_support
import sys, new
class Eggs:
def get_yolks(self):
return self.yolks
class NewTest(unittest.TestCase):
def test_spam(self):
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
m = new.module('Spam')
m.Eggs = Eggs
sys.modules['Spam'] = m
import Spam
def get_more_yolks(self):
return self.yolks + 3
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
# new.classobj()
C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks})
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
def break_yolks(self):
self.yolks = self.yolks - 2
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')
# new.instancemethod()
c = C()
c.yolks = 3
im = new.instancemethod(break_yolks, c, C)
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"
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')
# 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"
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')
# 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
'''
im = new.instancemethod(break_yolks, c)
im()
self.assertEqual(c.get_yolks(), -1)
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')
# Verify that dangerous instance method creation is forbidden
self.assertRaises(TypeError, new.instancemethod, break_yolks, None)
# 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"
# Verify that instancemethod() doesn't allow keyword args
self.assertRaises(TypeError, new.instancemethod, break_yolks, c, kw=1)
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
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
'''
print 'new.code()'
# bogus test of new.code()
# Note: Jython will never have new.code()
if hasattr(new, 'code'):
def f(a): pass
codestr = "\n".join(l.strip() for l in codestr.splitlines())
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
ccode = compile(codestr, '<string>', 'exec')
# Jython doesn't have a __builtins__, so use a portable alternative
import __builtin__
g = {'c': 0, '__builtins__': __builtin__}
d = new.code(argcount, kwonlyargcount,
nlocals, stacksize, flags, codestring,
constants, names, varnames, filename, name,
firstlineno, lnotab, freevars, cellvars)
# this test could be more robust
func = new.function(ccode, g)
func()
self.assertEqual(g['c'], 3, 'Could not create a proper function object')
# 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)
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)
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"
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
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"
# Note: Jython will never have new.code()
if hasattr(new, 'code'):
def test_code(self):
# bogus test of new.code()
def f(a): pass
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"
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
# 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!")
d = new.code(argcount, kwonlyargcount, nlocals, stacksize, flags,
codestring, constants, names, varnames, filename,
name, firstlineno, lnotab, freevars, cellvars)
if verbose:
print d
# 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()

View file

@ -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

View file

@ -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__":

View file

@ -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()

View file

@ -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)

View file

@ -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,

View file

@ -1,56 +1,96 @@
import os
import resource
import unittest
from test import test_support
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.
import os, resource
try:
cur, max = resource.getrlimit(resource.RLIMIT_FSIZE)
except AttributeError:
pass
else:
print resource.RLIM_INFINITY == max
resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
# This test is checking a few specific problem spots with the resource module.
# 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.
class ResourceTest(unittest.TestCase):
# At least one platform has an unlimited RLIMIT_FSIZE and attempts to
# change it raise ValueError instead.
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)
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))
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))
# 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
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()

View file

@ -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)

View file

@ -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()

View file

@ -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()

View file

@ -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()

View file

@ -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):