blob: b2a3ed88558fb20af3eb9b7816dcd2e39149baef [file] [log] [blame]
Eric Smitha73fbe72008-02-23 03:09:44 +00001
2/* future_builtins module */
3
4/* This module provides functions that will be builtins in Python 3.0,
5 but that conflict with builtins that already exist in Python
6 2.x. */
7
8
9#include "Python.h"
10
11PyDoc_STRVAR(module_doc,
12"This module provides functions that will be builtins in Python 3.0,\n\
13but that conflict with builtins that already exist in Python 2.x.\n\
14\n\
15Functions:\n\
16\n\
Senthil Kumaran9a4acdf2010-10-14 13:15:33 +000017ascii(arg) -- Returns the canonical string representation of an object.\n\
18filter(pred, iterable) -- Returns an iterator yielding those items of \n\
19 iterable for which pred(item) is true.\n\
20hex(arg) -- Returns the hexadecimal representation of an integer.\n\
21map(func, *iterables) -- Returns an iterator that computes the function \n\
22 using arguments from each of the iterables.\n\
23oct(arg) -- Returns the octal representation of an integer.\n\
24zip(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\
26 argument.\n\
Eric Smitha73fbe72008-02-23 03:09:44 +000027\n\
28The typical usage of this module is to replace existing builtins in a\n\
29module's namespace:\n \n\
Senthil Kumaran9a4acdf2010-10-14 13:15:33 +000030from future_builtins import ascii, filter, map, hex, oct, zip\n");
Eric Smitha73fbe72008-02-23 03:09:44 +000031
32static PyObject *
33builtin_hex(PyObject *self, PyObject *v)
34{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000035 return PyNumber_ToBase(v, 16);
Eric Smitha73fbe72008-02-23 03:09:44 +000036}
37
38PyDoc_STRVAR(hex_doc,
39"hex(number) -> string\n\
40\n\
41Return the hexadecimal representation of an integer or long integer.");
42
43
44static PyObject *
45builtin_oct(PyObject *self, PyObject *v)
46{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000047 return PyNumber_ToBase(v, 8);
Eric Smitha73fbe72008-02-23 03:09:44 +000048}
49
50PyDoc_STRVAR(oct_doc,
51"oct(number) -> string\n\
52\n\
53Return the octal representation of an integer or long integer.");
54
55
Georg Brandl89f48872008-06-11 18:55:38 +000056static PyObject *
57builtin_ascii(PyObject *self, PyObject *v)
58{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000059 return PyObject_Repr(v);
Georg Brandl89f48872008-06-11 18:55:38 +000060}
61
62PyDoc_STRVAR(ascii_doc,
63"ascii(object) -> string\n\
64\n\
65Return the same as repr(). In Python 3.x, the repr() result will\n\
66contain printable characters unescaped, while the ascii() result\n\
67will have such characters backslash-escaped.");
68
Eric Smitha73fbe72008-02-23 03:09:44 +000069/* List of functions exported by this module */
70
71static PyMethodDef module_functions[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000072 {"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 */
Eric Smitha73fbe72008-02-23 03:09:44 +000076};
77
78
79/* Initialize this module. */
80
81PyMODINIT_FUNC
82initfuture_builtins(void)
83{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000084 PyObject *m, *itertools, *iter_func;
85 char *it_funcs[] = {"imap", "ifilter", "izip", NULL};
86 char **cur_func;
Eric Smitha73fbe72008-02-23 03:09:44 +000087
Antoine Pitrouc83ea132010-05-09 14:46:46 +000088 m = Py_InitModule3("future_builtins", module_functions, module_doc);
89 if (m == NULL)
90 return;
Eric Smitha73fbe72008-02-23 03:09:44 +000091
Antoine Pitrouc83ea132010-05-09 14:46:46 +000092 itertools = PyImport_ImportModuleNoBlock("itertools");
93 if (itertools == NULL)
94 return;
David Wolever2724ab92008-03-19 02:35:45 +000095
Antoine Pitrouc83ea132010-05-09 14:46:46 +000096 /* 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)
101 break;
102 }
103 Py_DECREF(itertools);
104 /* any other initialization needed */
Eric Smitha73fbe72008-02-23 03:09:44 +0000105}