2 /* future_builtins module */
4 /* This module provides functions that will be builtins in Python 3.0,
5 but that conflict with builtins that already exist in Python
11 PyDoc_STRVAR(module_doc
,
12 "This module provides functions that will be builtins in Python 3.0,\n\
13 but that conflict with builtins that already exist in Python 2.x.\n\
17 ascii(arg) -- Returns the canonical string representation of an object.\n\
18 filter(pred, iterable) -- Returns an iterator yielding those items of \n\
19 iterable for which pred(item) is true.\n\
20 hex(arg) -- Returns the hexadecimal representation of an integer.\n\
21 map(func, *iterables) -- Returns an iterator that computes the function \n\
22 using arguments from each of the iterables.\n\
23 oct(arg) -- Returns the octal representation of an integer.\n\
24 zip(iter1 [,iter2 [...]]) -- Returns a zip object whose .next() method \n\
25 returns a tuple where the i-th element comes from the i-th iterable \n\
28 The typical usage of this module is to replace existing builtins in a\n\
29 module's namespace:\n \n\
30 from future_builtins import ascii, filter, map, hex, oct, zip\n");
33 builtin_hex(PyObject
*self
, PyObject
*v
)
35 return PyNumber_ToBase(v
, 16);
39 "hex(number) -> string\n\
41 Return the hexadecimal representation of an integer or long integer.");
45 builtin_oct(PyObject
*self
, PyObject
*v
)
47 return PyNumber_ToBase(v
, 8);
51 "oct(number) -> string\n\
53 Return the octal representation of an integer or long integer.");
57 builtin_ascii(PyObject
*self
, PyObject
*v
)
59 return PyObject_Repr(v
);
62 PyDoc_STRVAR(ascii_doc
,
63 "ascii(object) -> string\n\
65 Return the same as repr(). In Python 3.x, the repr() result will\n\
66 contain printable characters unescaped, while the ascii() result\n\
67 will have such characters backslash-escaped.");
69 /* List of functions exported by this module */
71 static PyMethodDef module_functions
[] = {
72 {"hex", builtin_hex
, METH_O
, hex_doc
},
73 {"oct", builtin_oct
, METH_O
, oct_doc
},
74 {"ascii", builtin_ascii
, METH_O
, ascii_doc
},
75 {NULL
, NULL
} /* Sentinel */
79 /* Initialize this module. */
82 initfuture_builtins(void)
84 PyObject
*m
, *itertools
, *iter_func
;
85 char *it_funcs
[] = {"imap", "ifilter", "izip", NULL
};
88 m
= Py_InitModule3("future_builtins", module_functions
, module_doc
);
92 itertools
= PyImport_ImportModuleNoBlock("itertools");
93 if (itertools
== NULL
)
96 /* If anything in the following loop fails, we fall through. */
97 for (cur_func
= it_funcs
; *cur_func
; ++cur_func
){
98 iter_func
= PyObject_GetAttrString(itertools
, *cur_func
);
99 if (iter_func
== NULL
||
100 PyModule_AddObject(m
, *cur_func
+1, iter_func
) < 0)
103 Py_DECREF(itertools
);
104 /* any other initialization needed */