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