mirror of
https://github.com/python/cpython.git
synced 2025-07-29 06:05:00 +00:00
Added zip, map, filter to future_bultins (#2171)
This commit is contained in:
parent
fbe7c55905
commit
2724ab99c8
4 changed files with 49 additions and 4 deletions
|
@ -1,7 +1,8 @@
|
|||
import test.test_support, unittest
|
||||
|
||||
# we're testing the behavior of these future builtins:
|
||||
from future_builtins import hex, oct
|
||||
from future_builtins import hex, oct, map, zip, filter
|
||||
from test import test_support
|
||||
|
||||
class BuiltinTest(unittest.TestCase):
|
||||
def test_hex(self):
|
||||
|
@ -20,6 +21,17 @@ class BuiltinTest(unittest.TestCase):
|
|||
self.assertEqual(oct(-100L), '-0o144')
|
||||
self.assertRaises(TypeError, oct, ())
|
||||
|
||||
def test_itertools(self):
|
||||
from itertools import imap, izip, ifilter
|
||||
# We will assume that the itertools functions work, so provided
|
||||
# that we've got identical coppies, we will work!
|
||||
self.assertEqual(map, imap)
|
||||
self.assertEqual(zip, izip)
|
||||
self.assertEqual(filter, ifilter)
|
||||
# Testing that filter(None, stuff) raises a warning lives in
|
||||
# test_py3kwarn.py
|
||||
|
||||
|
||||
def test_main(verbose=None):
|
||||
test.test_support.run_unittest(BuiltinTest)
|
||||
|
||||
|
|
|
@ -50,6 +50,17 @@ class TestPy3KWarnings(unittest.TestCase):
|
|||
with catch_warning() as w:
|
||||
self.assertWarning(cell0 < cell1, w, expected)
|
||||
|
||||
def test_filter(self):
|
||||
from itertools import ifilter
|
||||
from future_builtins import filter
|
||||
expected = 'ifilter with None as a first argument is not supported '\
|
||||
'in 3.x. Use a list comprehension instead.'
|
||||
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(ifilter(None, []), w, expected)
|
||||
with catch_warning() as w:
|
||||
self.assertWarning(filter(None, []), w, expected)
|
||||
|
||||
def test_code_inequality_comparisons(self):
|
||||
expected = 'code inequality comparisons not supported in 3.x.'
|
||||
def f(x):
|
||||
|
|
|
@ -59,11 +59,24 @@ static PyMethodDef module_functions[] = {
|
|||
PyMODINIT_FUNC
|
||||
initfuture_builtins(void)
|
||||
{
|
||||
PyObject *m;
|
||||
PyObject *m, *itertools, *iter_func;
|
||||
char *it_funcs[] = {"imap", "ifilter", "izip", NULL};
|
||||
char **cur_func;
|
||||
|
||||
m = Py_InitModule3("future_builtins", module_functions, module_doc);
|
||||
if (m == NULL)
|
||||
return;
|
||||
|
||||
itertools = PyImport_ImportModuleNoBlock("itertools");
|
||||
if (itertools == NULL)
|
||||
return;
|
||||
|
||||
for (cur_func = it_funcs; *cur_func; ++cur_func){
|
||||
iter_func = PyObject_GetAttrString(itertools, *cur_func);
|
||||
if (iter_func == NULL)
|
||||
return;
|
||||
PyModule_AddObject(m, *cur_func+1, iter_func);
|
||||
}
|
||||
Py_DECREF(itertools);
|
||||
/* any other initialization needed */
|
||||
}
|
||||
|
|
|
@ -2542,7 +2542,7 @@ ifilter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|||
ifilterobject *lz;
|
||||
|
||||
if (Py_Py3kWarningFlag &&
|
||||
PyErr_Warn(PyExc_DeprecationWarning,
|
||||
PyErr_Warn(PyExc_DeprecationWarning,
|
||||
"In 3.x, itertools.ifilter() was moved to builtin filter().") < 0)
|
||||
return NULL;
|
||||
|
||||
|
@ -2552,6 +2552,15 @@ ifilter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|||
if (!PyArg_UnpackTuple(args, "ifilter", 2, 2, &func, &seq))
|
||||
return NULL;
|
||||
|
||||
if (func == Py_None) {
|
||||
if (Py_Py3kWarningFlag &&
|
||||
PyErr_Warn(PyExc_DeprecationWarning,
|
||||
"ifilter with None as a first argument "
|
||||
"is not supported in 3.x. Use a list "
|
||||
"comprehension instead.") < 0)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Get iterator. */
|
||||
it = PyObject_GetIter(seq);
|
||||
if (it == NULL)
|
||||
|
@ -3602,7 +3611,7 @@ inititertools(void)
|
|||
&izip_type,
|
||||
&iziplongest_type,
|
||||
&permutations_type,
|
||||
&product_type,
|
||||
&product_type,
|
||||
&repeat_type,
|
||||
&groupby_type,
|
||||
NULL
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue