[Python-3000-checkins] r56135 - in python/branches/p3yk: Lib/distutils/dist.py Lib/distutils/extension.py Lib/distutils/sysconfig.py Lib/optparse.py Lib/string.py Lib/test/test_builtin.py Lib/textwrap.py Lib/unittest.py Python/bltinmodule.c setup.py
guido.van.rossum
python-3000-checkins at python.org
Sun Jul 1 06:13:55 CEST 2007
Author: guido.van.rossum
Date: Sun Jul 1 06:13:54 2007
New Revision: 56135
Modified:
python/branches/p3yk/Lib/distutils/dist.py
python/branches/p3yk/Lib/distutils/extension.py
python/branches/p3yk/Lib/distutils/sysconfig.py
python/branches/p3yk/Lib/optparse.py
python/branches/p3yk/Lib/string.py
python/branches/p3yk/Lib/test/test_builtin.py
python/branches/p3yk/Lib/textwrap.py
python/branches/p3yk/Lib/unittest.py
python/branches/p3yk/Python/bltinmodule.c
python/branches/p3yk/setup.py
Log:
Make map() and filter() identical to itertools.imap() and .ifilter(),
respectively.
I fixed two bootstrap issues, due to the dynamic import of itertools:
1. Starting python requires that map() and filter() are not used until
site.py has added build/lib.<arch> to sys.path.
2. Building python requires that setup.py and distutils and everything
they use is free of map() and filter() calls.
Beyond this, I only fixed the tests in test_builtin.py.
Others, please help fixing the remaining tests that are now broken!
The fixes are usually simple:
a. map(None, X) -> list(X)
b. map(F, X) -> list(map(F, X))
c. map(lambda x: F(x), X) -> [F(x) for x in X]
d. filter(F, X) -> list(filter(F, X))
e. filter(lambda x: P(x), X) -> [x for x in X if P(x)]
Someone, please also contribute a fixer for 2to3 to do this.
It can leave map()/filter() calls alone that are already
inside a list() or sorted() call or for-loop.
Only in rare cases have I seen code that depends on map() of lists
of different lengths going to the end of the longest, or on filter()
of a string or tuple returning an object of the same type; these
will need more thought to fix.
Modified: python/branches/p3yk/Lib/distutils/dist.py
==============================================================================
--- python/branches/p3yk/Lib/distutils/dist.py (original)
+++ python/branches/p3yk/Lib/distutils/dist.py Sun Jul 1 06:13:54 2007
@@ -112,8 +112,7 @@
('obsoletes', None,
"print the list of packages/modules made obsolete")
]
- display_option_names = map(lambda x: translate_longopt(x[0]),
- display_options)
+ display_option_names = [translate_longopt(x[0]) for x in display_options]
# negative options are options that exclude other options
negative_opt = {'quiet': 'verbose'}
@@ -805,7 +804,7 @@
pkgs = (pkgs or "").split(",")
for i in range(len(pkgs)):
pkgs[i] = pkgs[i].strip()
- pkgs = filter(None, pkgs)
+ pkgs = [p for p in pkgs if p]
if "distutils.command" not in pkgs:
pkgs.insert(0, "distutils.command")
self.command_packages = pkgs
Modified: python/branches/p3yk/Lib/distutils/extension.py
==============================================================================
--- python/branches/p3yk/Lib/distutils/extension.py (original)
+++ python/branches/p3yk/Lib/distutils/extension.py Sun Jul 1 06:13:54 2007
@@ -104,7 +104,7 @@
):
assert isinstance(name, str), "'name' must be a string"
assert (isinstance(sources, list) and
- map(type, sources) == [str]*len(sources)), \
+ all(isinstance(s, str) for s in sources)), \
"'sources' must be a list of strings"
self.name = name
Modified: python/branches/p3yk/Lib/distutils/sysconfig.py
==============================================================================
--- python/branches/p3yk/Lib/distutils/sysconfig.py (original)
+++ python/branches/p3yk/Lib/distutils/sysconfig.py Sun Jul 1 06:13:54 2007
@@ -371,7 +371,7 @@
if cur_target == '':
cur_target = cfg_target
os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target)
- elif map(int, cfg_target.split('.')) > map(int, cur_target.split('.')):
+ elif [int(x) for x in cfg_target.split('.')] > [int(x) for x in cur_target.split('.')]:
my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
% (cur_target, cfg_target))
raise DistutilsPlatformError(my_msg)
Modified: python/branches/p3yk/Lib/optparse.py
==============================================================================
--- python/branches/p3yk/Lib/optparse.py (original)
+++ python/branches/p3yk/Lib/optparse.py Sun Jul 1 06:13:54 2007
@@ -573,7 +573,7 @@
# Filter out None because early versions of Optik had exactly
# one short option and one long option, either of which
# could be None.
- opts = filter(None, opts)
+ opts = [opt for opt in opts if opt]
if not opts:
raise TypeError("at least one option string must be supplied")
return opts
Modified: python/branches/p3yk/Lib/string.py
==============================================================================
--- python/branches/p3yk/Lib/string.py (original)
+++ python/branches/p3yk/Lib/string.py Sun Jul 1 06:13:54 2007
@@ -30,9 +30,7 @@
# Case conversion helpers
# Use str to convert Unicode literal in case of -U
-l = map(chr, range(256))
-_idmap = str('').join(l)
-del l
+_idmap = str('').join(chr(c) for c in range(256))
# Functions which aren't available as string methods.
@@ -63,11 +61,10 @@
raise ValueError, "maketrans arguments must have same length"
global _idmapL
if not _idmapL:
- _idmapL = map(None, _idmap)
+ _idmapL = list(_idmap)
L = _idmapL[:]
- fromstr = map(ord, fromstr)
- for i in range(len(fromstr)):
- L[fromstr[i]] = tostr[i]
+ for i, c in enumerate(fromstr):
+ L[ord(c)] = tostr[i]
return ''.join(L)
Modified: python/branches/p3yk/Lib/test/test_builtin.py
==============================================================================
--- python/branches/p3yk/Lib/test/test_builtin.py (original)
+++ python/branches/p3yk/Lib/test/test_builtin.py Sun Jul 1 06:13:54 2007
@@ -490,11 +490,11 @@
self.assertEqual((g, l), ({'a': 1}, {'b': 2}))
def test_filter(self):
- self.assertEqual(filter(lambda c: 'a' <= c <= 'z', 'Hello World'), 'elloorld')
- self.assertEqual(filter(None, [1, 'hello', [], [3], '', None, 9, 0]), [1, 'hello', [3], 9])
- self.assertEqual(filter(lambda x: x > 0, [1, -3, 9, 0, 2]), [1, 9, 2])
- self.assertEqual(filter(None, Squares(10)), [1, 4, 9, 16, 25, 36, 49, 64, 81])
- self.assertEqual(filter(lambda x: x%2, Squares(10)), [1, 9, 25, 49, 81])
+ self.assertEqual(list(filter(lambda c: 'a' <= c <= 'z', 'Hello World')), list('elloorld'))
+ self.assertEqual(list(filter(None, [1, 'hello', [], [3], '', None, 9, 0])), [1, 'hello', [3], 9])
+ self.assertEqual(list(filter(lambda x: x > 0, [1, -3, 9, 0, 2])), [1, 9, 2])
+ self.assertEqual(list(filter(None, Squares(10))), [1, 4, 9, 16, 25, 36, 49, 64, 81])
+ self.assertEqual(list(filter(lambda x: x%2, Squares(10))), [1, 9, 25, 49, 81])
def identity(item):
return 1
filter(identity, Squares(5))
@@ -504,101 +504,15 @@
if index<4:
return 42
raise ValueError
- self.assertRaises(ValueError, filter, lambda x: x, BadSeq())
+ self.assertRaises(ValueError, list, filter(lambda x: x, BadSeq()))
def badfunc():
pass
- self.assertRaises(TypeError, filter, badfunc, range(5))
+ self.assertRaises(TypeError, list, filter(badfunc, range(5)))
# test bltinmodule.c::filtertuple()
- self.assertEqual(filter(None, (1, 2)), (1, 2))
- self.assertEqual(filter(lambda x: x>=3, (1, 2, 3, 4)), (3, 4))
- self.assertRaises(TypeError, filter, 42, (1, 2))
-
- # test bltinmodule.c::filterstring()
- self.assertEqual(filter(None, "12"), "12")
- self.assertEqual(filter(lambda x: x>="3", "1234"), "34")
- self.assertRaises(TypeError, filter, 42, "12")
- class badstr(str):
- def __getitem__(self, index):
- raise ValueError
- self.assertRaises(ValueError, filter, lambda x: x >="3", badstr("1234"))
-
- class badstr2(str):
- def __getitem__(self, index):
- return 42
- self.assertRaises(TypeError, filter, lambda x: x >=42, badstr2("1234"))
-
- class weirdstr(str):
- def __getitem__(self, index):
- return weirdstr(2*str.__getitem__(self, index))
- self.assertEqual(filter(lambda x: x>="33", weirdstr("1234")), "3344")
-
- class shiftstr(str):
- def __getitem__(self, index):
- return chr(ord(str.__getitem__(self, index))+1)
- self.assertEqual(filter(lambda x: x>="3", shiftstr("1234")), "345")
-
- if have_unicode:
- # test bltinmodule.c::filterunicode()
- self.assertEqual(filter(None, unicode("12")), unicode("12"))
- self.assertEqual(filter(lambda x: x>="3", unicode("1234")), unicode("34"))
- self.assertRaises(TypeError, filter, 42, unicode("12"))
- self.assertRaises(ValueError, filter, lambda x: x >="3", badstr(unicode("1234")))
-
- class badunicode(unicode):
- def __getitem__(self, index):
- return 42
- self.assertRaises(TypeError, filter, lambda x: x >=42, badunicode("1234"))
-
- class weirdunicode(unicode):
- def __getitem__(self, index):
- return weirdunicode(2*unicode.__getitem__(self, index))
- self.assertEqual(
- filter(lambda x: x>=unicode("33"), weirdunicode("1234")), unicode("3344"))
-
- class shiftunicode(unicode):
- def __getitem__(self, index):
- return unichr(ord(unicode.__getitem__(self, index))+1)
- self.assertEqual(
- filter(lambda x: x>=unicode("3"), shiftunicode("1234")),
- unicode("345")
- )
-
- def test_filter_subclasses(self):
- # test that filter() never returns tuple, str or unicode subclasses
- # and that the result always goes through __getitem__
- funcs = (None, bool, lambda x: True)
- class tuple2(tuple):
- def __getitem__(self, index):
- return 2*tuple.__getitem__(self, index)
- class str2(str):
- def __getitem__(self, index):
- return 2*str.__getitem__(self, index)
- inputs = {
- tuple2: {(): (), (1, 2, 3): (2, 4, 6)},
- str2: {"": "", "123": "112233"}
- }
- if have_unicode:
- class unicode2(unicode):
- def __getitem__(self, index):
- return 2*unicode.__getitem__(self, index)
- inputs[unicode2] = {
- unicode(): unicode(),
- unicode("123"): unicode("112233")
- }
-
- for (cls, inps) in inputs.items():
- for (inp, exp) in inps.items():
- # make sure the output goes through __getitem__
- # even if func is None
- self.assertEqual(
- filter(funcs[0], cls(inp)),
- filter(funcs[1], cls(inp))
- )
- for func in funcs:
- outp = filter(func, cls(inp))
- self.assertEqual(outp, exp)
- self.assert_(not isinstance(outp, cls))
+ self.assertEqual(list(filter(None, (1, 2))), [1, 2])
+ self.assertEqual(list(filter(lambda x: x>=3, (1, 2, 3, 4))), [3, 4])
+ self.assertRaises(TypeError, list, filter(42, (1, 2)))
def test_float(self):
self.assertEqual(float(3.14), 3.14)
@@ -1181,19 +1095,19 @@
def test_map(self):
self.assertEqual(
- map(None, 'hello world'),
- ['h','e','l','l','o',' ','w','o','r','l','d']
+ list(map(None, 'hello')),
+ [('h',), ('e',), ('l',), ('l',), ('o',)]
)
self.assertEqual(
- map(None, 'abcd', 'efg'),
- [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]
+ list(map(None, 'abcd', 'efg')),
+ [('a', 'e'), ('b', 'f'), ('c', 'g')]
)
self.assertEqual(
- map(None, range(10)),
- [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
+ list(map(None, range(3))),
+ [(0,), (1,), (2,)]
)
self.assertEqual(
- map(lambda x: x*x, range(1,4)),
+ list(map(lambda x: x*x, range(1,4))),
[1, 4, 9]
)
try:
@@ -1202,11 +1116,11 @@
def sqrt(x):
return pow(x, 0.5)
self.assertEqual(
- map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]),
+ list(map(lambda x: list(map(sqrt, x)), [[16, 4], [81, 9]])),
[[4.0, 2.0], [9.0, 3.0]]
)
self.assertEqual(
- map(lambda x, y: x+y, [1,3,2], [9,1,4]),
+ list(map(lambda x, y: x+y, [1,3,2], [9,1,4])),
[10, 4, 6]
)
@@ -1215,28 +1129,28 @@
for i in v: accu = accu + i
return accu
self.assertEqual(
- map(plus, [1, 3, 7]),
+ list(map(plus, [1, 3, 7])),
[1, 3, 7]
)
self.assertEqual(
- map(plus, [1, 3, 7], [4, 9, 2]),
+ list(map(plus, [1, 3, 7], [4, 9, 2])),
[1+4, 3+9, 7+2]
)
self.assertEqual(
- map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]),
+ list(map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])),
[1+4+1, 3+9+1, 7+2+0]
)
self.assertEqual(
- map(None, Squares(10)),
- [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
+ list(map(None, Squares(10))),
+ [(0,), (1,), (4,), (9,), (16,), (25,), (36,), (49,), (64,), (81,)]
)
self.assertEqual(
- map(int, Squares(10)),
+ list(map(int, Squares(10))),
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
)
self.assertEqual(
- map(None, Squares(3), Squares(2)),
- [(0,0), (1,1), (4,None)]
+ list(map(None, Squares(3), Squares(2))),
+ [(0,0), (1,1)]
)
def Max(a, b):
if a is None:
@@ -1245,19 +1159,20 @@
return a
return max(a, b)
self.assertEqual(
- map(Max, Squares(3), Squares(2)),
- [0, 1, 4]
+ list(map(Max, Squares(3), Squares(2))),
+ [0, 1]
)
self.assertRaises(TypeError, map)
self.assertRaises(TypeError, map, lambda x: x, 42)
- self.assertEqual(map(None, [42]), [42])
+ self.assertEqual(list(map(None, [42])), [(42,)])
class BadSeq:
- def __getitem__(self, index):
+ def __iter__(self):
raise ValueError
- self.assertRaises(ValueError, map, lambda x: x, BadSeq())
+ yield None
+ self.assertRaises(ValueError, list, map(lambda x: x, BadSeq()))
def badfunc(x):
raise RuntimeError
- self.assertRaises(RuntimeError, map, badfunc, range(5))
+ self.assertRaises(RuntimeError, list, map(badfunc, range(5)))
def test_max(self):
self.assertEqual(max('123123'), '3')
Modified: python/branches/p3yk/Lib/textwrap.py
==============================================================================
--- python/branches/p3yk/Lib/textwrap.py (original)
+++ python/branches/p3yk/Lib/textwrap.py Sun Jul 1 06:13:54 2007
@@ -63,8 +63,8 @@
unicode_whitespace_trans = {}
uspace = ord(u' ')
- for x in map(ord, _whitespace):
- unicode_whitespace_trans[x] = uspace
+ for x in _whitespace:
+ unicode_whitespace_trans[ord(x)] = uspace
# This funky little regex is just the trick for splitting
# text up into word-wrappable chunks. E.g.
@@ -136,7 +136,7 @@
'use', ' ', 'the', ' ', '-b', ' ', 'option!'
"""
chunks = self.wordsep_re.split(text)
- chunks = filter(None, chunks) # remove empty chunks
+ chunks = [c for c in chunks if c]
return chunks
def _fix_sentence_endings(self, chunks):
Modified: python/branches/p3yk/Lib/unittest.py
==============================================================================
--- python/branches/p3yk/Lib/unittest.py (original)
+++ python/branches/p3yk/Lib/unittest.py Sun Jul 1 06:13:54 2007
@@ -586,7 +586,7 @@
"""
def isTestMethod(attrname, testCaseClass=testCaseClass, prefix=self.testMethodPrefix):
return attrname.startswith(prefix) and hasattr(getattr(testCaseClass, attrname), '__call__')
- testFnNames = filter(isTestMethod, dir(testCaseClass))
+ testFnNames = list(filter(isTestMethod, dir(testCaseClass)))
if self.sortTestMethodsUsing:
testFnNames.sort(self.sortTestMethodsUsing)
return testFnNames
@@ -725,7 +725,7 @@
self.stream.writeln()
if not result.wasSuccessful():
self.stream.write("FAILED (")
- failed, errored = map(len, (result.failures, result.errors))
+ failed, errored = len(result.failures), len(result.errors)
if failed:
self.stream.write("failures=%d" % failed)
if errored:
Modified: python/branches/p3yk/Python/bltinmodule.c
==============================================================================
--- python/branches/p3yk/Python/bltinmodule.c (original)
+++ python/branches/p3yk/Python/bltinmodule.c Sun Jul 1 06:13:54 2007
@@ -23,13 +23,6 @@
const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
#endif
-/* Forward */
-static PyObject *filterstring(PyObject *, PyObject *);
-#ifdef Py_USING_UNICODE
-static PyObject *filterunicode(PyObject *, PyObject *);
-#endif
-static PyObject *filtertuple (PyObject *, PyObject *);
-
static PyObject *
builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
{
@@ -265,123 +258,26 @@
static PyObject *
builtin_filter(PyObject *self, PyObject *args)
{
- PyObject *func, *seq, *result, *it, *arg;
- Py_ssize_t len; /* guess for result list size */
- register Py_ssize_t j;
-
- if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
+ PyObject *itertools, *ifilter, *result;
+ itertools = PyImport_ImportModule("itertools");
+ if (itertools == NULL)
+ return NULL;
+ ifilter = PyObject_GetAttrString(itertools, "ifilter");
+ Py_DECREF(itertools);
+ if (ifilter == NULL)
return NULL;
-
- /* Strings and tuples return a result of the same type. */
- if (PyString_Check(seq))
- return filterstring(func, seq);
-#ifdef Py_USING_UNICODE
- if (PyUnicode_Check(seq))
- return filterunicode(func, seq);
-#endif
- if (PyTuple_Check(seq))
- return filtertuple(func, seq);
-
- /* Pre-allocate argument list tuple. */
- arg = PyTuple_New(1);
- if (arg == NULL)
- return NULL;
-
- /* Get iterator. */
- it = PyObject_GetIter(seq);
- if (it == NULL)
- goto Fail_arg;
-
- /* Guess a result list size. */
- len = _PyObject_LengthHint(seq);
- if (len < 0) {
- if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
- !PyErr_ExceptionMatches(PyExc_AttributeError)) {
- goto Fail_it;
- }
- PyErr_Clear();
- len = 8; /* arbitrary */
- }
-
- /* Get a result list. */
- if (PyList_Check(seq) && seq->ob_refcnt == 1) {
- /* Eww - can modify the list in-place. */
- Py_INCREF(seq);
- result = seq;
- }
- else {
- result = PyList_New(len);
- if (result == NULL)
- goto Fail_it;
- }
-
- /* Build the result list. */
- j = 0;
- for (;;) {
- PyObject *item;
- int ok;
-
- item = PyIter_Next(it);
- if (item == NULL) {
- if (PyErr_Occurred())
- goto Fail_result_it;
- break;
- }
-
- if (func == (PyObject *)&PyBool_Type || func == Py_None) {
- ok = PyObject_IsTrue(item);
- }
- else {
- PyObject *good;
- PyTuple_SET_ITEM(arg, 0, item);
- good = PyObject_Call(func, arg, NULL);
- PyTuple_SET_ITEM(arg, 0, NULL);
- if (good == NULL) {
- Py_DECREF(item);
- goto Fail_result_it;
- }
- ok = PyObject_IsTrue(good);
- Py_DECREF(good);
- }
- if (ok) {
- if (j < len)
- PyList_SET_ITEM(result, j, item);
- else {
- int status = PyList_Append(result, item);
- Py_DECREF(item);
- if (status < 0)
- goto Fail_result_it;
- }
- ++j;
- }
- else
- Py_DECREF(item);
- }
-
-
- /* Cut back result list if len is too big. */
- if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
- goto Fail_result_it;
-
- Py_DECREF(it);
- Py_DECREF(arg);
+ result = PyObject_Call(ifilter, args, NULL);
+ Py_DECREF(ifilter);
return result;
-
-Fail_result_it:
- Py_DECREF(result);
-Fail_it:
- Py_DECREF(it);
-Fail_arg:
- Py_DECREF(arg);
- return NULL;
}
PyDoc_STRVAR(filter_doc,
-"filter(function or None, sequence) -> list, tuple, or string\n"
-"\n"
-"Return those items of sequence for which function(item) is true. If\n"
-"function is None, return the items that are true. If sequence is a tuple\n"
-"or string, return the same type, else return a list.");
+"filter(predicate, iterable) -> iterator\n\
+\n\
+Return an iterator yielding only those elements of the input iterable\n\
+for which the predicate (a Boolean function) returns true.\n\
+If the predicate is None, 'lambda x: bool(x)' is assumed.\n\
+(This is identical to itertools.ifilter().)");
static PyObject *
builtin_chr(PyObject *self, PyObject *args)
@@ -978,168 +874,28 @@
static PyObject *
builtin_map(PyObject *self, PyObject *args)
{
- typedef struct {
- PyObject *it; /* the iterator object */
- int saw_StopIteration; /* bool: did the iterator end? */
- } sequence;
-
- PyObject *func, *result;
- sequence *seqs = NULL, *sqp;
- Py_ssize_t n, len;
- register int i, j;
-
- n = PyTuple_Size(args);
- if (n < 2) {
- PyErr_SetString(PyExc_TypeError,
- "map() requires at least two args");
+ PyObject *itertools, *imap, *result;
+ itertools = PyImport_ImportModule("itertools");
+ if (itertools == NULL)
+ return NULL;
+ imap = PyObject_GetAttrString(itertools, "imap");
+ Py_DECREF(itertools);
+ if (imap == NULL)
return NULL;
- }
-
- func = PyTuple_GetItem(args, 0);
- n--;
-
- if (func == Py_None && n == 1) {
- /* map(None, S) is the same as list(S). */
- return PySequence_List(PyTuple_GetItem(args, 1));
- }
-
- /* Get space for sequence descriptors. Must NULL out the iterator
- * pointers so that jumping to Fail_2 later doesn't see trash.
- */
- if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
- for (i = 0; i < n; ++i) {
- seqs[i].it = (PyObject*)NULL;
- seqs[i].saw_StopIteration = 0;
- }
-
- /* Do a first pass to obtain iterators for the arguments, and set len
- * to the largest of their lengths.
- */
- len = 0;
- for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
- PyObject *curseq;
- Py_ssize_t curlen;
-
- /* Get iterator. */
- curseq = PyTuple_GetItem(args, i+1);
- sqp->it = PyObject_GetIter(curseq);
- if (sqp->it == NULL) {
- static char errmsg[] =
- "argument %d to map() must support iteration";
- char errbuf[sizeof(errmsg) + 25];
- PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
- PyErr_SetString(PyExc_TypeError, errbuf);
- goto Fail_2;
- }
-
- /* Update len. */
- curlen = _PyObject_LengthHint(curseq);
- if (curlen < 0) {
- if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
- !PyErr_ExceptionMatches(PyExc_AttributeError)) {
- goto Fail_2;
- }
- PyErr_Clear();
- curlen = 8; /* arbitrary */
- }
- if (curlen > len)
- len = curlen;
- }
-
- /* Get space for the result list. */
- if ((result = (PyObject *) PyList_New(len)) == NULL)
- goto Fail_2;
-
- /* Iterate over the sequences until all have stopped. */
- for (i = 0; ; ++i) {
- PyObject *alist, *item=NULL, *value;
- int numactive = 0;
-
- if (func == Py_None && n == 1)
- alist = NULL;
- else if ((alist = PyTuple_New(n)) == NULL)
- goto Fail_1;
-
- for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
- if (sqp->saw_StopIteration) {
- Py_INCREF(Py_None);
- item = Py_None;
- }
- else {
- item = PyIter_Next(sqp->it);
- if (item)
- ++numactive;
- else {
- if (PyErr_Occurred()) {
- Py_XDECREF(alist);
- goto Fail_1;
- }
- Py_INCREF(Py_None);
- item = Py_None;
- sqp->saw_StopIteration = 1;
- }
- }
- if (alist)
- PyTuple_SET_ITEM(alist, j, item);
- else
- break;
- }
-
- if (!alist)
- alist = item;
-
- if (numactive == 0) {
- Py_DECREF(alist);
- break;
- }
-
- if (func == Py_None)
- value = alist;
- else {
- value = PyEval_CallObject(func, alist);
- Py_DECREF(alist);
- if (value == NULL)
- goto Fail_1;
- }
- if (i >= len) {
- int status = PyList_Append(result, value);
- Py_DECREF(value);
- if (status < 0)
- goto Fail_1;
- }
- else if (PyList_SetItem(result, i, value) < 0)
- goto Fail_1;
- }
-
- if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
- goto Fail_1;
-
- goto Succeed;
-
-Fail_1:
- Py_DECREF(result);
-Fail_2:
- result = NULL;
-Succeed:
- assert(seqs);
- for (i = 0; i < n; ++i)
- Py_XDECREF(seqs[i].it);
- PyMem_DEL(seqs);
+ result = PyObject_Call(imap, args, NULL);
+ Py_DECREF(imap);
return result;
}
PyDoc_STRVAR(map_doc,
-"map(function, sequence[, sequence, ...]) -> list\n\
+"map(function, iterable[, iterable, ...]) -> iterator\n\
\n\
-Return a list of the results of applying the function to the items of\n\
-the argument sequence(s). If more than one sequence is given, the\n\
-function is called with an argument list consisting of the corresponding\n\
-item of each sequence, substituting None for missing values when not all\n\
-sequences have the same length. If the function is None, return a list of\n\
-the items of the sequence (or a list of tuples if more than one sequence).");
+Return an iterator yielding the results of applying the function to the\n\
+items of the argument iterables(s). If more than one iterable is given,\n\
+the function is called with an argument list consisting of the\n\
+corresponding item of each iterable, until an iterable is exhausted.\n\
+If the function is None, 'lambda *a: a' is assumed.\n\
+(This is identical to itertools.imap().)");
static PyObject *
@@ -1921,7 +1677,7 @@
Return an iterator yielding tuples, where each tuple contains the\n\
corresponding element from each of the argument iterables.\n\
The returned iterator ends when the shortest argument iterable is exhausted.\n\
-NOTE: This is implemented using itertools.izip().");
+(This is identical to itertools.izip().)");
static PyMethodDef builtin_methods[] = {
@@ -2054,264 +1810,3 @@
#undef ADD_TO_ALL
#undef SETBUILTIN
}
-
-/* Helper for filter(): filter a tuple through a function */
-
-static PyObject *
-filtertuple(PyObject *func, PyObject *tuple)
-{
- PyObject *result;
- Py_ssize_t i, j;
- Py_ssize_t len = PyTuple_Size(tuple);
-
- if (len == 0) {
- if (PyTuple_CheckExact(tuple))
- Py_INCREF(tuple);
- else
- tuple = PyTuple_New(0);
- return tuple;
- }
-
- if ((result = PyTuple_New(len)) == NULL)
- return NULL;
-
- for (i = j = 0; i < len; ++i) {
- PyObject *item, *good;
- int ok;
-
- if (tuple->ob_type->tp_as_sequence &&
- tuple->ob_type->tp_as_sequence->sq_item) {
- item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
- if (item == NULL)
- goto Fail_1;
- } else {
- PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
- goto Fail_1;
- }
- if (func == Py_None) {
- Py_INCREF(item);
- good = item;
- }
- else {
- PyObject *arg = PyTuple_Pack(1, item);
- if (arg == NULL) {
- Py_DECREF(item);
- goto Fail_1;
- }
- good = PyEval_CallObject(func, arg);
- Py_DECREF(arg);
- if (good == NULL) {
- Py_DECREF(item);
- goto Fail_1;
- }
- }
- ok = PyObject_IsTrue(good);
- Py_DECREF(good);
- if (ok) {
- if (PyTuple_SetItem(result, j++, item) < 0)
- goto Fail_1;
- }
- else
- Py_DECREF(item);
- }
-
- if (_PyTuple_Resize(&result, j) < 0)
- return NULL;
-
- return result;
-
-Fail_1:
- Py_DECREF(result);
- return NULL;
-}
-
-
-/* Helper for filter(): filter a string through a function */
-
-static PyObject *
-filterstring(PyObject *func, PyObject *strobj)
-{
- PyObject *result;
- Py_ssize_t i, j;
- Py_ssize_t len = PyString_Size(strobj);
- Py_ssize_t outlen = len;
-
- if (func == Py_None) {
- /* If it's a real string we can return the original,
- * as no character is ever false and __getitem__
- * does return this character. If it's a subclass
- * we must go through the __getitem__ loop */
- if (PyString_CheckExact(strobj)) {
- Py_INCREF(strobj);
- return strobj;
- }
- }
- if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
- return NULL;
-
- for (i = j = 0; i < len; ++i) {
- PyObject *item;
- int ok;
-
- item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
- if (item == NULL)
- goto Fail_1;
- if (func==Py_None) {
- ok = 1;
- } else {
- PyObject *arg, *good;
- arg = PyTuple_Pack(1, item);
- if (arg == NULL) {
- Py_DECREF(item);
- goto Fail_1;
- }
- good = PyEval_CallObject(func, arg);
- Py_DECREF(arg);
- if (good == NULL) {
- Py_DECREF(item);
- goto Fail_1;
- }
- ok = PyObject_IsTrue(good);
- Py_DECREF(good);
- }
- if (ok) {
- Py_ssize_t reslen;
- if (!PyString_Check(item)) {
- PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
- " __getitem__ returned different type");
- Py_DECREF(item);
- goto Fail_1;
- }
- reslen = PyString_GET_SIZE(item);
- if (reslen == 1) {
- PyString_AS_STRING(result)[j++] =
- PyString_AS_STRING(item)[0];
- } else {
- /* do we need more space? */
- Py_ssize_t need = j + reslen + len-i-1;
- if (need > outlen) {
- /* overallocate, to avoid reallocations */
- if (need<2*outlen)
- need = 2*outlen;
- if (_PyString_Resize(&result, need)) {
- Py_DECREF(item);
- return NULL;
- }
- outlen = need;
- }
- memcpy(
- PyString_AS_STRING(result) + j,
- PyString_AS_STRING(item),
- reslen
- );
- j += reslen;
- }
- }
- Py_DECREF(item);
- }
-
- if (j < outlen)
- _PyString_Resize(&result, j);
-
- return result;
-
-Fail_1:
- Py_DECREF(result);
- return NULL;
-}
-
-#ifdef Py_USING_UNICODE
-/* Helper for filter(): filter a Unicode object through a function */
-
-static PyObject *
-filterunicode(PyObject *func, PyObject *strobj)
-{
- PyObject *result;
- register Py_ssize_t i, j;
- Py_ssize_t len = PyUnicode_GetSize(strobj);
- Py_ssize_t outlen = len;
-
- if (func == Py_None) {
- /* If it's a real string we can return the original,
- * as no character is ever false and __getitem__
- * does return this character. If it's a subclass
- * we must go through the __getitem__ loop */
- if (PyUnicode_CheckExact(strobj)) {
- Py_INCREF(strobj);
- return strobj;
- }
- }
- if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
- return NULL;
-
- for (i = j = 0; i < len; ++i) {
- PyObject *item, *arg, *good;
- int ok;
-
- item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
- if (item == NULL)
- goto Fail_1;
- if (func == Py_None) {
- ok = 1;
- } else {
- arg = PyTuple_Pack(1, item);
- if (arg == NULL) {
- Py_DECREF(item);
- goto Fail_1;
- }
- good = PyEval_CallObject(func, arg);
- Py_DECREF(arg);
- if (good == NULL) {
- Py_DECREF(item);
- goto Fail_1;
- }
- ok = PyObject_IsTrue(good);
- Py_DECREF(good);
- }
- if (ok) {
- Py_ssize_t reslen;
- if (!PyUnicode_Check(item)) {
- PyErr_SetString(PyExc_TypeError,
- "can't filter unicode to unicode:"
- " __getitem__ returned different type");
- Py_DECREF(item);
- goto Fail_1;
- }
- reslen = PyUnicode_GET_SIZE(item);
- if (reslen == 1)
- PyUnicode_AS_UNICODE(result)[j++] =
- PyUnicode_AS_UNICODE(item)[0];
- else {
- /* do we need more space? */
- Py_ssize_t need = j + reslen + len - i - 1;
- if (need > outlen) {
- /* overallocate,
- to avoid reallocations */
- if (need < 2 * outlen)
- need = 2 * outlen;
- if (PyUnicode_Resize(
- &result, need) < 0) {
- Py_DECREF(item);
- goto Fail_1;
- }
- outlen = need;
- }
- memcpy(PyUnicode_AS_UNICODE(result) + j,
- PyUnicode_AS_UNICODE(item),
- reslen*sizeof(Py_UNICODE));
- j += reslen;
- }
- }
- Py_DECREF(item);
- }
-
- if (j < outlen)
- PyUnicode_Resize(&result, j);
-
- return result;
-
-Fail_1:
- Py_DECREF(result);
- return NULL;
-}
-#endif
Modified: python/branches/p3yk/setup.py
==============================================================================
--- python/branches/p3yk/setup.py (original)
+++ python/branches/p3yk/setup.py Sun Jul 1 06:13:54 2007
@@ -736,7 +736,7 @@
db_incdir.replace("include", 'lib64'),
db_incdir.replace("include", 'lib'),
]
- db_dirs_to_check = filter(os.path.isdir, db_dirs_to_check)
+ db_dirs_to_check = [x for x in db_dirs_to_check if os.path.isdir(x)]
# Look for a version specific db-X.Y before an ambiguoius dbX
# XXX should we -ever- look for a dbX name? Do any
@@ -1568,7 +1568,7 @@
description = "A high-level object-oriented programming language",
long_description = SUMMARY.strip(),
license = "PSF license",
- classifiers = filter(None, CLASSIFIERS.split("\n")),
+ classifiers = [x for x in CLASSIFIERS.split("\n") if x],
platforms = ["Many"],
# Build info
More information about the Python-3000-checkins
mailing list