Skip to content

Commit f0bbabb

Browse files
author
david.wolever
committed
Added zip, map, filter to future_bultins (#2171)
git-svn-id: http://svn.python.org/projects/python/trunk@61587 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent c653797 commit f0bbabb

4 files changed

Lines changed: 49 additions & 4 deletions

File tree

Lib/test/test_future_builtins.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import test.test_support, unittest
22

33
# we're testing the behavior of these future builtins:
4-
from future_builtins import hex, oct
4+
from future_builtins import hex, oct, map, zip, filter
5+
from test import test_support
56

67
class BuiltinTest(unittest.TestCase):
78
def test_hex(self):
@@ -20,6 +21,17 @@ def test_oct(self):
2021
self.assertEqual(oct(-100L), '-0o144')
2122
self.assertRaises(TypeError, oct, ())
2223

24+
def test_itertools(self):
25+
from itertools import imap, izip, ifilter
26+
# We will assume that the itertools functions work, so provided
27+
# that we've got identical coppies, we will work!
28+
self.assertEqual(map, imap)
29+
self.assertEqual(zip, izip)
30+
self.assertEqual(filter, ifilter)
31+
# Testing that filter(None, stuff) raises a warning lives in
32+
# test_py3kwarn.py
33+
34+
2335
def test_main(verbose=None):
2436
test.test_support.run_unittest(BuiltinTest)
2537

Lib/test/test_py3kwarn.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ def g():
5050
with catch_warning() as w:
5151
self.assertWarning(cell0 < cell1, w, expected)
5252

53+
def test_filter(self):
54+
from itertools import ifilter
55+
from future_builtins import filter
56+
expected = 'ifilter with None as a first argument is not supported '\
57+
'in 3.x. Use a list comprehension instead.'
58+
59+
with catch_warning() as w:
60+
self.assertWarning(ifilter(None, []), w, expected)
61+
with catch_warning() as w:
62+
self.assertWarning(filter(None, []), w, expected)
63+
5364
def test_code_inequality_comparisons(self):
5465
expected = 'code inequality comparisons not supported in 3.x.'
5566
def f(x):

Modules/future_builtins.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,24 @@ static PyMethodDef module_functions[] = {
5959
PyMODINIT_FUNC
6060
initfuture_builtins(void)
6161
{
62-
PyObject *m;
62+
PyObject *m, *itertools, *iter_func;
63+
char *it_funcs[] = {"imap", "ifilter", "izip", NULL};
64+
char **cur_func;
6365

6466
m = Py_InitModule3("future_builtins", module_functions, module_doc);
6567
if (m == NULL)
6668
return;
6769

70+
itertools = PyImport_ImportModuleNoBlock("itertools");
71+
if (itertools == NULL)
72+
return;
73+
74+
for (cur_func = it_funcs; *cur_func; ++cur_func){
75+
iter_func = PyObject_GetAttrString(itertools, *cur_func);
76+
if (iter_func == NULL)
77+
return;
78+
PyModule_AddObject(m, *cur_func+1, iter_func);
79+
}
80+
Py_DECREF(itertools);
6881
/* any other initialization needed */
6982
}

Modules/itertoolsmodule.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2542,7 +2542,7 @@ ifilter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
25422542
ifilterobject *lz;
25432543

25442544
if (Py_Py3kWarningFlag &&
2545-
PyErr_Warn(PyExc_DeprecationWarning,
2545+
PyErr_Warn(PyExc_DeprecationWarning,
25462546
"In 3.x, itertools.ifilter() was moved to builtin filter().") < 0)
25472547
return NULL;
25482548

@@ -2552,6 +2552,15 @@ ifilter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
25522552
if (!PyArg_UnpackTuple(args, "ifilter", 2, 2, &func, &seq))
25532553
return NULL;
25542554

2555+
if (func == Py_None) {
2556+
if (Py_Py3kWarningFlag &&
2557+
PyErr_Warn(PyExc_DeprecationWarning,
2558+
"ifilter with None as a first argument "
2559+
"is not supported in 3.x. Use a list "
2560+
"comprehension instead.") < 0)
2561+
return NULL;
2562+
}
2563+
25552564
/* Get iterator. */
25562565
it = PyObject_GetIter(seq);
25572566
if (it == NULL)
@@ -3602,7 +3611,7 @@ inititertools(void)
36023611
&izip_type,
36033612
&iziplongest_type,
36043613
&permutations_type,
3605-
&product_type,
3614+
&product_type,
36063615
&repeat_type,
36073616
&groupby_type,
36083617
NULL

0 commit comments

Comments
 (0)