Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from

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

........
  r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line

  Move .setupterm() output so that we don't try to call endwin() if it fails
........
  r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line

  Use file descriptor for real stdout
........
  r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines


  Issue 2117.  Update compiler module to handle class decorators.
  Thanks Thomas Herve
........
  r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines

  Rename sphinx.addons to sphinx.ext.
........
  r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines

  Revert r61029.
........
  r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines


  Issue 2168. gdbm and dbm needs to be iterable; this fixes a
  failure in the shelve module.  Thanks Thomas Herve.
........
  r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line

  Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
  r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line

  Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
  r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines


  Coerced PyBool_Type to be able to compare it.
........
  r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line

  Docs for itertools.combinations().  Implementation in forthcoming checkin.
........
  r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines

  Don't use a hard coded port.  This test could hang/fail if the port is in use.
  Speed this test up by avoiding a sleep and using the event.
........
  r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line

  Whitespace normalization
........
  r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line

  Whitespace normalization
........
  r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines

  Banish tab.
........
This commit is contained in:
Christian Heimes 2008-02-26 08:18:30 +00:00
parent 34f8d3a4dd
commit 836baa53d8
13 changed files with 99 additions and 37 deletions

View File

@ -13,7 +13,7 @@ sys.path.append('tools/sphinxext')
# General configuration # General configuration
# --------------------- # ---------------------
extensions = ['sphinx.addons.refcounting', 'sphinx.addons.coverage'] extensions = ['sphinx.ext.refcounting', 'sphinx.ext.coverage']
# General substitutions. # General substitutions.
project = 'Python' project = 'Python'

View File

@ -74,6 +74,45 @@ loops that truncate the stream.
yield element yield element
.. function:: combinations(iterable, r)
Return successive *r* length combinations of elements in the *iterable*.
Combinations are emitted in a lexicographic sort order. So, if the
input *iterable* is sorted, the combination tuples will be produced
in sorted order.
Elements are treated as unique based on their position, not on their
value. So if the input elements are unique, there will be no repeat
values within a single combination.
Each result tuple is ordered to match the input order. So, every
combination is a subsequence of the input *iterable*.
Example: ``combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)``
Equivalent to::
def combinations(iterable, r):
pool = tuple(iterable)
if pool:
n = len(pool)
vec = range(r)
yield tuple(pool[i] for i in vec)
while 1:
for i in reversed(range(r)):
if vec[i] == i + n-r:
continue
vec[i] += 1
for j in range(i+1, r):
vec[j] = vec[j-1] + 1
yield tuple(pool[i] for i in vec)
break
else:
return
.. versionadded:: 2.6
.. function:: count([n]) .. function:: count([n])
Make an iterator that returns consecutive integers starting with *n*. If not Make an iterator that returns consecutive integers starting with *n*. If not
@ -298,9 +337,12 @@ loops that truncate the stream.
The leftmost iterators are in the outermost for-loop, so the output tuples The leftmost iterators are in the outermost for-loop, so the output tuples
cycle in a manner similar to an odometer (with the rightmost element cycle in a manner similar to an odometer (with the rightmost element
changing on every iteration). changing on every iteration). This results in a lexicographic ordering
so that if the inputs iterables are sorted, the product tuples are emitted
in sorted order.
Equivalent to (but without building the entire result in memory):: Equivalent to the following except that the actual implementation does not
build-up intermediate results in memory::
def product(*args): def product(*args):
pools = map(tuple, args) pools = map(tuple, args)

View File

@ -147,11 +147,6 @@ In addition to these methods, lock objects can also be used via the
exception will be received by an arbitrary thread. (When the :mod:`signal` exception will be received by an arbitrary thread. (When the :mod:`signal`
module is available, interrupts always go to the main thread.) module is available, interrupts always go to the main thread.)
* The import machinery is not thread safe. In general, an import may not
have the side effect of importing a module, and only the main thread
should import modules. Imports within or caused by a thread other than
the main thread isn't safe.
* Calling :func:`sys.exit` or raising the :exc:`SystemExit` exception is * Calling :func:`sys.exit` or raising the :exc:`SystemExit` exception is
equivalent to calling :func:`exit`. equivalent to calling :func:`exit`.
@ -172,3 +167,4 @@ In addition to these methods, lock objects can also be used via the
* When the main thread exits, it does not do any of its usual cleanup (except * When the main thread exits, it does not do any of its usual cleanup (except
that :keyword:`try` ... :keyword:`finally` clauses are honored), and the that :keyword:`try` ... :keyword:`finally` clauses are honored), and the
standard I/O files are not flushed. standard I/O files are not flushed.

View File

@ -555,13 +555,6 @@ the :meth:`setDaemon` method and retrieved with the :meth:`isDaemon` method.
There is a "main thread" object; this corresponds to the initial thread of There is a "main thread" object; this corresponds to the initial thread of
control in the Python program. It is not a daemon thread. control in the Python program. It is not a daemon thread.
.. warning::
The import machinery is not thread safe. In general, an import may not
have the side effect of importing a module, and only the main thread
should import modules. Imports within or caused by a thread other than
the main thread isn't safe.
There is the possibility that "dummy thread objects" are created. These are There is the possibility that "dummy thread objects" are created. These are
thread objects corresponding to "alien threads", which are threads of control thread objects corresponding to "alien threads", which are threads of control
started outside the threading module, such as directly from C code. Dummy started outside the threading module, such as directly from C code. Dummy

View File

@ -15,6 +15,7 @@ __revision__ = "$Id$"
from _curses import * from _curses import *
from curses.wrapper import wrapper from curses.wrapper import wrapper
import os as _os import os as _os
import sys as _sys
# Some constants, most notably the ACS_* ones, are only added to the C # Some constants, most notably the ACS_* ones, are only added to the C
# _curses module's dictionary after initscr() is called. (Some # _curses module's dictionary after initscr() is called. (Some
@ -28,7 +29,8 @@ def initscr():
import _curses, curses import _curses, curses
# we call setupterm() here because it raises an error # we call setupterm() here because it raises an error
# instead of calling exit() in error cases. # instead of calling exit() in error cases.
setupterm(term=_os.environ.get("TERM", "unknown")) setupterm(term=_os.environ.get("TERM", "unknown"),
fd=_sys.__stdout__.fileno())
stdscr = _curses.initscr() stdscr = _curses.initscr()
for key, value in _curses.__dict__.items(): for key, value in _curses.__dict__.items():
if key[0:4] == 'ACS_' or key in ('LINES', 'COLS'): if key[0:4] == 'ACS_' or key in ('LINES', 'COLS'):

View File

@ -269,13 +269,12 @@ if __name__ == '__main__':
curses.wrapper(main) curses.wrapper(main)
unit_tests() unit_tests()
else: else:
try:
# testing setupterm() inside initscr/endwin # testing setupterm() inside initscr/endwin
# causes terminal breakage # causes terminal breakage
curses.setupterm(fd=sys.__stdout__.fileno()) curses.setupterm(fd=sys.__stdout__.fileno())
try:
stdscr = curses.initscr() stdscr = curses.initscr()
main(stdscr) main(stdscr)
finally: finally:
curses.endwin() curses.endwin()
unit_tests() unit_tests()

View File

@ -6,34 +6,48 @@ import time
from unittest import TestCase from unittest import TestCase
from test import test_support from test import test_support
def server(evt, ready): server_port = None
# This function sets the evt 3 times:
# 1) when the connection is ready to be accepted.
# 2) when it is safe for the caller to close the connection
# 3) when we have closed the socket
def server(evt):
global server_port
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serv.settimeout(3) serv.settimeout(3)
serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serv.bind(("", 9091)) server_port = test_support.bind_port(serv, "", 9091)
serv.listen(5) serv.listen(5)
ready.set()
# (1) Signal the caller that we are ready to accept the connection.
evt.set()
try: try:
conn, addr = serv.accept() conn, addr = serv.accept()
except socket.timeout: except socket.timeout:
pass pass
else: else:
conn.send(b"1 Hola mundo\n") conn.send(b"1 Hola mundo\n")
# (2) Signal the caller that it is safe to close the socket.
evt.set()
conn.close() conn.close()
finally: finally:
serv.close() serv.close()
# (3) Signal the caller that we are done.
evt.set() evt.set()
class GeneralTests(TestCase): class GeneralTests(TestCase):
def setUp(self): def setUp(self):
ftplib.FTP.port = 9091
self.evt = threading.Event() self.evt = threading.Event()
ready = threading.Event() threading.Thread(target=server, args=(self.evt,)).start()
threading.Thread(target=server, args=(self.evt, ready)).start() # Wait for the server to be ready.
ready.wait() self.evt.wait()
self.evt.clear()
ftplib.FTP.port = server_port
def tearDown(self): def tearDown(self):
# Wait on the closing of the socket (this shouldn't be necessary).
self.evt.wait() self.evt.wait()
def testBasic(self): def testBasic(self):
@ -42,30 +56,35 @@ class GeneralTests(TestCase):
# connects # connects
ftp = ftplib.FTP("localhost") ftp = ftplib.FTP("localhost")
self.evt.wait()
ftp.sock.close() ftp.sock.close()
def testTimeoutDefault(self): def testTimeoutDefault(self):
# default # default
ftp = ftplib.FTP("localhost") ftp = ftplib.FTP("localhost")
self.assertTrue(ftp.sock.gettimeout() is None) self.assertTrue(ftp.sock.gettimeout() is None)
self.evt.wait()
ftp.sock.close() ftp.sock.close()
def testTimeoutValue(self): def testTimeoutValue(self):
# a value # a value
ftp = ftplib.FTP("localhost", timeout=30) ftp = ftplib.FTP("localhost", timeout=30)
self.assertEqual(ftp.sock.gettimeout(), 30) self.assertEqual(ftp.sock.gettimeout(), 30)
self.evt.wait()
ftp.sock.close() ftp.sock.close()
def testTimeoutConnect(self): def testTimeoutConnect(self):
ftp = ftplib.FTP() ftp = ftplib.FTP()
ftp.connect("localhost", timeout=30) ftp.connect("localhost", timeout=30)
self.assertEqual(ftp.sock.gettimeout(), 30) self.assertEqual(ftp.sock.gettimeout(), 30)
self.evt.wait()
ftp.sock.close() ftp.sock.close()
def testTimeoutDifferentOrder(self): def testTimeoutDifferentOrder(self):
ftp = ftplib.FTP(timeout=30) ftp = ftplib.FTP(timeout=30)
ftp.connect("localhost") ftp.connect("localhost")
self.assertEqual(ftp.sock.gettimeout(), 30) self.assertEqual(ftp.sock.gettimeout(), 30)
self.evt.wait()
ftp.sock.close() ftp.sock.close()
def testTimeoutDirectAccess(self): def testTimeoutDirectAccess(self):
@ -73,6 +92,7 @@ class GeneralTests(TestCase):
ftp.timeout = 30 ftp.timeout = 30
ftp.connect("localhost") ftp.connect("localhost")
self.assertEqual(ftp.sock.gettimeout(), 30) self.assertEqual(ftp.sock.gettimeout(), 30)
self.evt.wait()
ftp.sock.close() ftp.sock.close()
def testTimeoutNone(self): def testTimeoutNone(self):
@ -84,10 +104,10 @@ class GeneralTests(TestCase):
finally: finally:
socket.setdefaulttimeout(previous) socket.setdefaulttimeout(previous)
self.assertEqual(ftp.sock.gettimeout(), 30) self.assertEqual(ftp.sock.gettimeout(), 30)
self.evt.wait()
ftp.close() ftp.close()
def test_main(verbose=None): def test_main(verbose=None):
test_support.run_unittest(GeneralTests) test_support.run_unittest(GeneralTests)

View File

@ -177,6 +177,7 @@ class TestBasicOps(unittest.TestCase):
def test_ifilter(self): def test_ifilter(self):
self.assertEqual(list(ifilter(isEven, range(6))), [0,2,4]) self.assertEqual(list(ifilter(isEven, range(6))), [0,2,4])
self.assertEqual(list(ifilter(None, [0,1,0,2,0])), [1,2]) self.assertEqual(list(ifilter(None, [0,1,0,2,0])), [1,2])
self.assertEqual(list(ifilter(bool, [0,1,0,2,0])), [1,2])
self.assertEqual(take(4, ifilter(isEven, count())), [0,2,4,6]) self.assertEqual(take(4, ifilter(isEven, count())), [0,2,4,6])
self.assertRaises(TypeError, ifilter) self.assertRaises(TypeError, ifilter)
self.assertRaises(TypeError, ifilter, lambda x:x) self.assertRaises(TypeError, ifilter, lambda x:x)
@ -187,6 +188,7 @@ class TestBasicOps(unittest.TestCase):
def test_ifilterfalse(self): def test_ifilterfalse(self):
self.assertEqual(list(ifilterfalse(isEven, range(6))), [1,3,5]) self.assertEqual(list(ifilterfalse(isEven, range(6))), [1,3,5])
self.assertEqual(list(ifilterfalse(None, [0,1,0,2,0])), [0,0,0]) self.assertEqual(list(ifilterfalse(None, [0,1,0,2,0])), [0,0,0])
self.assertEqual(list(ifilterfalse(bool, [0,1,0,2,0])), [0,0,0])
self.assertEqual(take(4, ifilterfalse(isEven, count())), [1,3,5,7]) self.assertEqual(take(4, ifilterfalse(isEven, count())), [1,3,5,7])
self.assertRaises(TypeError, ifilterfalse) self.assertRaises(TypeError, ifilterfalse)
self.assertRaises(TypeError, ifilterfalse, lambda x:x) self.assertRaises(TypeError, ifilterfalse, lambda x:x)

View File

@ -72,6 +72,7 @@ tok_name = {}
for _name, _value in list(globals().items()): for _name, _value in list(globals().items()):
if type(_value) is type(0): if type(_value) is type(0):
tok_name[_value] = _name tok_name[_value] = _name
del _name, _value
def ISTERMINAL(x): def ISTERMINAL(x):

View File

@ -344,6 +344,13 @@ static PyTypeObject Dbmtype = {
0, /*tp_as_number*/ 0, /*tp_as_number*/
&dbm_as_sequence, /*tp_as_sequence*/ &dbm_as_sequence, /*tp_as_sequence*/
&dbm_as_mapping, /*tp_as_mapping*/ &dbm_as_mapping, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_xxx4*/
}; };
/* ----------------------------------------------------------------- */ /* ----------------------------------------------------------------- */

View File

@ -407,7 +407,7 @@ static PyTypeObject Dbmtype = {
0, /*tp_getattro*/ 0, /*tp_getattro*/
0, /*tp_setattro*/ 0, /*tp_setattro*/
0, /*tp_as_buffer*/ 0, /*tp_as_buffer*/
0, /*tp_xxx4*/ Py_TPFLAGS_DEFAULT, /*tp_xxx4*/
gdbm_object__doc__, /*tp_doc*/ gdbm_object__doc__, /*tp_doc*/
}; };

View File

@ -2025,7 +2025,7 @@ ifilter_next(ifilterobject *lz)
if (item == NULL) if (item == NULL)
return NULL; return NULL;
if (lz->func == Py_None) { if (lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type) {
ok = PyObject_IsTrue(item); ok = PyObject_IsTrue(item);
} else { } else {
PyObject *good; PyObject *good;
@ -2169,7 +2169,7 @@ ifilterfalse_next(ifilterfalseobject *lz)
if (item == NULL) if (item == NULL)
return NULL; return NULL;
if (lz->func == Py_None) { if (lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type) {
ok = PyObject_IsTrue(item); ok = PyObject_IsTrue(item);
} else { } else {
PyObject *good; PyObject *good;

View File

@ -154,7 +154,7 @@ addcleanup(void *ptr, PyObject **freelist)
PyMem_FREE(ptr); PyMem_FREE(ptr);
return -1; return -1;
} }
if(PyList_Append(*freelist, cobj)) { if (PyList_Append(*freelist, cobj)) {
PyMem_FREE(ptr); PyMem_FREE(ptr);
Py_DECREF(cobj); Py_DECREF(cobj);
return -1; return -1;
@ -166,8 +166,8 @@ addcleanup(void *ptr, PyObject **freelist)
static int static int
cleanreturn(int retval, PyObject *freelist) cleanreturn(int retval, PyObject *freelist)
{ {
if(freelist) { if (freelist) {
if((retval) == 0) { if (retval == 0) {
Py_ssize_t len = PyList_GET_SIZE(freelist), i; Py_ssize_t len = PyList_GET_SIZE(freelist), i;
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
PyMem_FREE(PyCObject_AsVoidPtr( PyMem_FREE(PyCObject_AsVoidPtr(
@ -708,7 +708,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
case 'L': {/* PY_LONG_LONG */ case 'L': {/* PY_LONG_LONG */
PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * ); PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
PY_LONG_LONG ival = PyLong_AsLongLong( arg ); PY_LONG_LONG ival = PyLong_AsLongLong( arg );
if( ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) { if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
return converterr("long<L>", arg, msgbuf, bufsize); return converterr("long<L>", arg, msgbuf, bufsize);
} else { } else {
*p = ival; *p = ival;
@ -1045,7 +1045,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
"(memory error)", "(memory error)",
arg, msgbuf, bufsize); arg, msgbuf, bufsize);
} }
if(addcleanup(*buffer, freelist)) { if (addcleanup(*buffer, freelist)) {
Py_DECREF(s); Py_DECREF(s);
return converterr( return converterr(
"(cleanup problem)", "(cleanup problem)",
@ -1087,7 +1087,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
return converterr("(memory error)", return converterr("(memory error)",
arg, msgbuf, bufsize); arg, msgbuf, bufsize);
} }
if(addcleanup(*buffer, freelist)) { if (addcleanup(*buffer, freelist)) {
Py_DECREF(s); Py_DECREF(s);
return converterr("(cleanup problem)", return converterr("(cleanup problem)",
arg, msgbuf, bufsize); arg, msgbuf, bufsize);