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