[Python-3000-checkins] r60977 - in python/branches/py3k: Doc/library/configparser.rst Doc/library/itertools.rst Lib/ConfigParser.py Lib/smtplib.py Lib/test/test_builtin.py Lib/test/test_cfgparser.py Lib/test/test_itertools.py Lib/test/test_parser.py Lib/test/test_smtplib.py Modules/itertoolsmodule.c Parser/parser.h
christian.heimes
python-3000-checkins at python.org
Sat Feb 23 14:18:04 CET 2008
Author: christian.heimes
Date: Sat Feb 23 14:18:03 2008
New Revision: 60977
Modified:
python/branches/py3k/ (props changed)
python/branches/py3k/Doc/library/configparser.rst
python/branches/py3k/Doc/library/itertools.rst
python/branches/py3k/Lib/ConfigParser.py
python/branches/py3k/Lib/smtplib.py
python/branches/py3k/Lib/test/test_builtin.py
python/branches/py3k/Lib/test/test_cfgparser.py
python/branches/py3k/Lib/test/test_itertools.py
python/branches/py3k/Lib/test/test_parser.py
python/branches/py3k/Lib/test/test_smtplib.py
python/branches/py3k/Modules/itertoolsmodule.c
python/branches/py3k/Parser/parser.h
Log:
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60965 | eric.smith | 2008-02-22 18:43:17 +0100 (Fri, 22 Feb 2008) | 1 line
Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
........
r60968 | raymond.hettinger | 2008-02-22 20:50:06 +0100 (Fri, 22 Feb 2008) | 1 line
Document itertools.product().
........
r60969 | raymond.hettinger | 2008-02-23 03:20:41 +0100 (Sat, 23 Feb 2008) | 9 lines
Improve the implementation of itertools.product()
* Fix-up issues pointed-out by Neal Norwitz.
* Add extensive comments.
* The lz->result variable is now a tuple instead of a list.
* Use fast macro getitem/setitem calls so most code is in-line.
* Re-use the result tuple if available (modify in-place instead of copy).
........
r60972 | raymond.hettinger | 2008-02-23 05:03:50 +0100 (Sat, 23 Feb 2008) | 1 line
Add more comments
........
r60973 | raymond.hettinger | 2008-02-23 11:04:15 +0100 (Sat, 23 Feb 2008) | 1 line
Add recipe using itertools.product().
........
r60974 | facundo.batista | 2008-02-23 13:01:13 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1881. Increased the stack limit from 500 to 1500. Also added
a test for this (and because of this test you'll see in stderr a
message that parser.c sends before raising MemoryError).
Thanks Ralf Schmitt.
........
r60975 | facundo.batista | 2008-02-23 13:27:17 +0100 (Sat, 23 Feb 2008) | 4 lines
Issue 1776581. Minor corrections to smtplib, and two small tests.
Thanks Alan McIntyre.
........
r60976 | facundo.batista | 2008-02-23 13:46:10 +0100 (Sat, 23 Feb 2008) | 5 lines
Issue 1781. Now ConfigParser.add_section does not let you add a
DEFAULT section any more, because it duplicated sections with
the rest of the machinery. Thanks Tim Lesher and Manuel Kaufmann.
........
Modified: python/branches/py3k/Doc/library/configparser.rst
==============================================================================
--- python/branches/py3k/Doc/library/configparser.rst (original)
+++ python/branches/py3k/Doc/library/configparser.rst Sat Feb 23 14:18:03 2008
@@ -176,8 +176,9 @@
.. method:: RawConfigParser.add_section(section)
Add a section named *section* to the instance. If a section by the given name
- already exists, :exc:`DuplicateSectionError` is raised.
-
+ already exists, :exc:`DuplicateSectionError` is raised. If the name
+ ``DEFAULT`` (or any of it's case-insensitive variants) is passed,
+ :exc:`ValueError` is raised.
.. method:: RawConfigParser.has_section(section)
Modified: python/branches/py3k/Doc/library/itertools.rst
==============================================================================
--- python/branches/py3k/Doc/library/itertools.rst (original)
+++ python/branches/py3k/Doc/library/itertools.rst Sat Feb 23 14:18:03 2008
@@ -289,6 +289,29 @@
example :func:`islice` or :func:`takewhile`).
+.. function:: product(*iterables)
+
+ Cartesian product of input iterables.
+
+ Equivalent to nested for-loops in a generator expression. For example,
+ ``product(A, B)`` returns the same as ``((x,y) for x in A for y in B)``.
+
+ 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
+ changing on every iteration).
+
+ Equivalent to (but without building the entire result in memory)::
+
+ def product(*args):
+ pools = map(tuple, args)
+ if pools:
+ result = [[]]
+ for pool in pools:
+ result = [x+[y] for x in result for y in pool]
+ for prod in result:
+ yield tuple(prod)
+
+
.. function:: repeat(object[, times])
Make an iterator that returns *object* over and over again. Runs indefinitely
@@ -526,3 +549,9 @@
pending -= 1
nexts = cycle(islice(nexts, pending))
+ def powerset(iterable):
+ "powerset('ab') --> set([]), set(['b']), set(['a']), set(['a', 'b'])"
+ skip = object()
+ for t in product(*izip(repeat(skip), iterable)):
+ yield set(e for e in t if e is not skip)
+
Modified: python/branches/py3k/Lib/ConfigParser.py
==============================================================================
--- python/branches/py3k/Lib/ConfigParser.py (original)
+++ python/branches/py3k/Lib/ConfigParser.py Sat Feb 23 14:18:03 2008
@@ -235,8 +235,12 @@
"""Create a new section in the configuration.
Raise DuplicateSectionError if a section by the specified name
- already exists.
+ already exists. Raise ValueError if name is DEFAULT or any of it's
+ case-insensitive variants.
"""
+ if section.lower() == "default":
+ raise ValueError('Invalid section name: %s' % section)
+
if section in self._sections:
raise DuplicateSectionError(section)
self._sections[section] = self._dict()
Modified: python/branches/py3k/Lib/smtplib.py
==============================================================================
--- python/branches/py3k/Lib/smtplib.py (original)
+++ python/branches/py3k/Lib/smtplib.py Sat Feb 23 14:18:03 2008
@@ -298,7 +298,7 @@
def send(self, s):
"""Send `s' to the server."""
if self.debuglevel > 0: print('send:', repr(s), file=stderr)
- if self.sock:
+ if hasattr(self, 'sock') and self.sock:
if isinstance(s, str):
s = s.encode("ascii")
try:
@@ -489,7 +489,7 @@
vrfy=verify
def expn(self, address):
- """SMTP 'verify' command -- checks for address validity."""
+ """SMTP 'expn' command -- expands a mailing list."""
self.putcmd("expn", quoteaddr(address))
return self.getreply()
Modified: python/branches/py3k/Lib/test/test_builtin.py
==============================================================================
--- python/branches/py3k/Lib/test/test_builtin.py (original)
+++ python/branches/py3k/Lib/test/test_builtin.py Sat Feb 23 14:18:03 2008
@@ -1829,6 +1829,15 @@
return i
self.assertRaises(ValueError, list, zip(BadSeq(), BadSeq()))
+ def test_bin(self):
+ self.assertEqual(bin(0), '0b0')
+ self.assertEqual(bin(1), '0b1')
+ self.assertEqual(bin(-1), '-0b1')
+ self.assertEqual(bin(2**65), '0b1' + '0' * 65)
+ self.assertEqual(bin(2**65-1), '0b' + '1' * 65)
+ self.assertEqual(bin(-(2**65)), '-0b1' + '0' * 65)
+ self.assertEqual(bin(-(2**65-1)), '-0b' + '1' * 65)
+
class TestSorted(unittest.TestCase):
def test_basic(self):
Modified: python/branches/py3k/Lib/test/test_cfgparser.py
==============================================================================
--- python/branches/py3k/Lib/test/test_cfgparser.py (original)
+++ python/branches/py3k/Lib/test/test_cfgparser.py Sat Feb 23 14:18:03 2008
@@ -436,6 +436,14 @@
self.assertRaises(TypeError, cf.set, "sect", "option2", 1.0)
self.assertRaises(TypeError, cf.set, "sect", "option2", object())
+ def test_add_section_default_1(self):
+ cf = self.newconfig()
+ self.assertRaises(ValueError, cf.add_section, "default")
+
+ def test_add_section_default_2(self):
+ cf = self.newconfig()
+ self.assertRaises(ValueError, cf.add_section, "DEFAULT")
+
class SortedTestCase(RawConfigParserTestCase):
def newconfig(self, defaults=None):
self.cf = self.config_class(defaults=defaults, dict_type=SortedDict)
Modified: python/branches/py3k/Lib/test/test_itertools.py
==============================================================================
--- python/branches/py3k/Lib/test/test_itertools.py (original)
+++ python/branches/py3k/Lib/test/test_itertools.py Sat Feb 23 14:18:03 2008
@@ -283,6 +283,9 @@
args = map(iter, args)
self.assertEqual(len(list(product(*args))), n)
+ # Test implementation detail: tuple re-use
+ self.assertEqual(len(set(map(id, product('abc', 'def')))), 1)
+ self.assertNotEqual(len(set(map(id, list(product('abc', 'def'))))), 1)
def test_repeat(self):
self.assertEqual(lzip(range(3),repeat('a')),
Modified: python/branches/py3k/Lib/test/test_parser.py
==============================================================================
--- python/branches/py3k/Lib/test/test_parser.py (original)
+++ python/branches/py3k/Lib/test/test_parser.py Sat Feb 23 14:18:03 2008
@@ -450,11 +450,29 @@
st = parser.suite('a = "\\u1"')
self.assertRaises(SyntaxError, parser.compilest, st)
+class ParserStackLimitTestCase(unittest.TestCase):
+ """try to push the parser to/over it's limits.
+ see http://bugs.python.org/issue1881 for a discussion
+ """
+ def _nested_expression(self, level):
+ return "["*level+"]"*level
+
+ def test_deeply_nested_list(self):
+ # XXX used to be 99 levels in 2.x
+ e = self._nested_expression(93)
+ st = parser.expr(e)
+ st.compile()
+
+ def test_trigger_memory_error(self):
+ e = self._nested_expression(100)
+ self.assertRaises(MemoryError, parser.expr, e)
+
def test_main():
test_support.run_unittest(
RoundtripLegalSyntaxTestCase,
IllegalSyntaxTestCase,
CompileTestCase,
+ ParserStackLimitTestCase,
)
Modified: python/branches/py3k/Lib/test/test_smtplib.py
==============================================================================
--- python/branches/py3k/Lib/test/test_smtplib.py (original)
+++ python/branches/py3k/Lib/test/test_smtplib.py Sat Feb 23 14:18:03 2008
@@ -82,8 +82,9 @@
# to reference the nonexistent 'sock' attribute of the SMTP object
# causes an AttributeError)
smtp = smtplib.SMTP()
- self.assertRaises(AttributeError, smtp.ehlo)
- self.assertRaises(AttributeError, smtp.send, 'test msg')
+ self.assertRaises(smtplib.SMTPServerDisconnected, smtp.ehlo)
+ self.assertRaises(smtplib.SMTPServerDisconnected,
+ smtp.send, 'test msg')
def testLocalHostName(self):
# check that supplied local_hostname is used
Modified: python/branches/py3k/Modules/itertoolsmodule.c
==============================================================================
--- python/branches/py3k/Modules/itertoolsmodule.c (original)
+++ python/branches/py3k/Modules/itertoolsmodule.c Sat Feb 23 14:18:03 2008
@@ -1716,10 +1716,10 @@
typedef struct {
PyObject_HEAD
PyObject *pools; /* tuple of pool tuples */
- Py_ssize_t *maxvec;
- Py_ssize_t *indices;
- PyObject *result;
- int stopped;
+ Py_ssize_t *maxvec; /* size of each pool */
+ Py_ssize_t *indices; /* one index per pool */
+ PyObject *result; /* most recently returned result tuple */
+ int stopped; /* set to 1 when the product iterator is exhausted */
} productobject;
static PyTypeObject product_type;
@@ -1766,7 +1766,7 @@
lz = (productobject *)type->tp_alloc(type, 0);
if (lz == NULL) {
Py_DECREF(pools);
- return NULL;
+ goto error;
}
lz->pools = pools;
@@ -1810,7 +1810,7 @@
{
PyObject *pool;
PyObject *elem;
- PyObject *tuple_result;
+ PyObject *oldelem;
PyObject *pools = lz->pools;
PyObject *result = lz->result;
Py_ssize_t npools = PyTuple_GET_SIZE(pools);
@@ -1818,10 +1818,14 @@
if (lz->stopped)
return NULL;
+
if (result == NULL) {
+ /* On the first pass, return an initial tuple filled with the
+ first element from each pool. If any pool is empty, then
+ whole product is empty and we're already done */
if (npools == 0)
goto empty;
- result = PyList_New(npools);
+ result = PyTuple_New(npools);
if (result == NULL)
goto empty;
lz->result = result;
@@ -1831,34 +1835,61 @@
goto empty;
elem = PyTuple_GET_ITEM(pool, 0);
Py_INCREF(elem);
- PyList_SET_ITEM(result, i, elem);
+ PyTuple_SET_ITEM(result, i, elem);
}
} else {
Py_ssize_t *indices = lz->indices;
Py_ssize_t *maxvec = lz->maxvec;
+
+ /* Copy the previous result tuple or re-use it if available */
+ if (Py_REFCNT(result) > 1) {
+ PyObject *old_result = result;
+ result = PyTuple_New(npools);
+ if (result == NULL)
+ goto empty;
+ lz->result = result;
+ for (i=0; i < npools; i++) {
+ elem = PyTuple_GET_ITEM(old_result, i);
+ Py_INCREF(elem);
+ PyTuple_SET_ITEM(result, i, elem);
+ }
+ Py_DECREF(old_result);
+ }
+ /* Now, we've got the only copy so we can update it in-place */
+ assert (Py_REFCNT(result) == 1);
+
+ /* Update the pool indices right-to-left. Only advance to the
+ next pool when the previous one rolls-over */
for (i=npools-1 ; i >= 0 ; i--) {
pool = PyTuple_GET_ITEM(pools, i);
indices[i]++;
if (indices[i] == maxvec[i]) {
+ /* Roll-over and advance to next pool */
indices[i] = 0;
elem = PyTuple_GET_ITEM(pool, 0);
Py_INCREF(elem);
- PyList_SetItem(result, i, elem);
+ oldelem = PyTuple_GET_ITEM(result, i);
+ PyTuple_SET_ITEM(result, i, elem);
+ Py_DECREF(oldelem);
} else {
+ /* No rollover. Just increment and stop here. */
elem = PyTuple_GET_ITEM(pool, indices[i]);
Py_INCREF(elem);
- PyList_SetItem(result, i, elem);
+ oldelem = PyTuple_GET_ITEM(result, i);
+ PyTuple_SET_ITEM(result, i, elem);
+ Py_DECREF(oldelem);
break;
}
}
+
+ /* If i is negative, then the indices have all rolled-over
+ and we're done. */
if (i < 0)
- return NULL;
+ goto empty;
}
- tuple_result = PySequence_Tuple(result);
- if (tuple_result == NULL)
- lz->stopped = 1;
- return tuple_result;
+ Py_INCREF(result);
+ return result;
empty:
lz->stopped = 1;
@@ -1868,7 +1899,7 @@
PyDoc_STRVAR(product_doc,
"product(*iterables) --> product object\n\
\n\
-Cartesian product of input interables. Equivalent to nested for-loops.\n\n\
+Cartesian product of input iterables. Equivalent to nested for-loops.\n\n\
For example, product(A, B) returns the same as: ((x,y) for x in A for y in B).\n\
The leftmost iterators are in the outermost for-loop, so the output tuples\n\
cycle in a manner similar to an odometer (with the rightmost element changing\n\
Modified: python/branches/py3k/Parser/parser.h
==============================================================================
--- python/branches/py3k/Parser/parser.h (original)
+++ python/branches/py3k/Parser/parser.h Sat Feb 23 14:18:03 2008
@@ -7,7 +7,7 @@
/* Parser interface */
-#define MAXSTACK 500
+#define MAXSTACK 1500
typedef struct {
int s_state; /* State in current DFA */
More information about the Python-3000-checkins
mailing list