summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Doc/library/configparser.rst5
-rw-r--r--Doc/library/itertools.rst29
-rw-r--r--Lib/ConfigParser.py6
-rwxr-xr-xLib/smtplib.py4
-rw-r--r--Lib/test/test_builtin.py9
-rw-r--r--Lib/test/test_cfgparser.py8
-rw-r--r--Lib/test/test_itertools.py3
-rw-r--r--Lib/test/test_parser.py18
-rw-r--r--Lib/test/test_smtplib.py5
-rw-r--r--Modules/itertoolsmodule.c63
-rw-r--r--Parser/parser.h2
11 files changed, 128 insertions, 24 deletions
diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst
index 89bd441..aa4dc7c 100644
--- a/Doc/library/configparser.rst
+++ b/Doc/library/configparser.rst
@@ -176,8 +176,9 @@ RawConfigParser Objects
.. 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)
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 013ed38..af73b57 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -289,6 +289,29 @@ loops that truncate the stream.
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 @@ which incur interpreter overhead. ::
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)
+
diff --git a/Lib/ConfigParser.py b/Lib/ConfigParser.py
index 167b963..5e36cc9 100644
--- a/Lib/ConfigParser.py
+++ b/Lib/ConfigParser.py
@@ -235,8 +235,12 @@ class RawConfigParser:
"""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()
diff --git a/Lib/smtplib.py b/Lib/smtplib.py
index 2b7befb..da5e767 100755
--- a/Lib/smtplib.py
+++ b/Lib/smtplib.py
@@ -298,7 +298,7 @@ class SMTP:
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 @@ class SMTP:
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()
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 42c55cc..111090c 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -1829,6 +1829,15 @@ class BuiltinTest(unittest.TestCase):
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):
diff --git a/Lib/test/test_cfgparser.py b/Lib/test/test_cfgparser.py
index 3ef82be..a2da556 100644
--- a/Lib/test/test_cfgparser.py
+++ b/Lib/test/test_cfgparser.py
@@ -436,6 +436,14 @@ class SafeConfigParserTestCase(ConfigParserTestCase):
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)
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index 380d121..744b344 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -283,6 +283,9 @@ class TestBasicOps(unittest.TestCase):
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')),
diff --git a/Lib/test/test_parser.py b/Lib/test/test_parser.py
index 3bebe7e..054d6d2 100644
--- a/Lib/test/test_parser.py
+++ b/Lib/test/test_parser.py
@@ -450,11 +450,29 @@ class CompileTestCase(unittest.TestCase):
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,
)
diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py
index 4151d6b..6b00b80 100644
--- a/Lib/test/test_smtplib.py
+++ b/Lib/test/test_smtplib.py
@@ -82,8 +82,9 @@ class GeneralTests(TestCase):
# 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
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 613515a..5b6aec3 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -1716,10 +1716,10 @@ static PyTypeObject chain_type = {
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 @@ product_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
lz = (productobject *)type->tp_alloc(type, 0);
if (lz == NULL) {
Py_DECREF(pools);
- return NULL;
+ goto error;
}
lz->pools = pools;
@@ -1810,7 +1810,7 @@ product_next(productobject *lz)
{
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 @@ product_next(productobject *lz)
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 @@ product_next(productobject *lz)
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 @@ empty:
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\
diff --git a/Parser/parser.h b/Parser/parser.h
index bdca3e9..403236d 100644
--- a/Parser/parser.h
+++ b/Parser/parser.h
@@ -7,7 +7,7 @@ extern "C" {
/* Parser interface */
-#define MAXSTACK 500
+#define MAXSTACK 1500
typedef struct {
int s_state; /* State in current DFA */