Eric Smith | a73fbe7 | 2008-02-23 03:09:44 +0000 | [diff] [blame] | 1 | |
| 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 | |
| 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\ |
| 14 | \n\ |
| 15 | Functions:\n\ |
| 16 | \n\ |
Senthil Kumaran | 9a4acdf | 2010-10-14 13:15:33 +0000 | [diff] [blame] | 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\ |
| 26 | argument.\n\ |
Eric Smith | a73fbe7 | 2008-02-23 03:09:44 +0000 | [diff] [blame] | 27 | \n\ |
| 28 | The typical usage of this module is to replace existing builtins in a\n\ |
| 29 | module's namespace:\n \n\ |
Senthil Kumaran | 9a4acdf | 2010-10-14 13:15:33 +0000 | [diff] [blame] | 30 | from future_builtins import ascii, filter, map, hex, oct, zip\n"); |
Eric Smith | a73fbe7 | 2008-02-23 03:09:44 +0000 | [diff] [blame] | 31 | |
| 32 | static PyObject * |
| 33 | builtin_hex(PyObject *self, PyObject *v) |
| 34 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 35 | return PyNumber_ToBase(v, 16); |
Eric Smith | a73fbe7 | 2008-02-23 03:09:44 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | PyDoc_STRVAR(hex_doc, |
| 39 | "hex(number) -> string\n\ |
| 40 | \n\ |
| 41 | Return the hexadecimal representation of an integer or long integer."); |
| 42 | |
| 43 | |
| 44 | static PyObject * |
| 45 | builtin_oct(PyObject *self, PyObject *v) |
| 46 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 47 | return PyNumber_ToBase(v, 8); |
Eric Smith | a73fbe7 | 2008-02-23 03:09:44 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | PyDoc_STRVAR(oct_doc, |
| 51 | "oct(number) -> string\n\ |
| 52 | \n\ |
| 53 | Return the octal representation of an integer or long integer."); |
| 54 | |
| 55 | |
Georg Brandl | 89f4887 | 2008-06-11 18:55:38 +0000 | [diff] [blame] | 56 | static PyObject * |
| 57 | builtin_ascii(PyObject *self, PyObject *v) |
| 58 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 59 | return PyObject_Repr(v); |
Georg Brandl | 89f4887 | 2008-06-11 18:55:38 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | PyDoc_STRVAR(ascii_doc, |
| 63 | "ascii(object) -> string\n\ |
| 64 | \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."); |
| 68 | |
Eric Smith | a73fbe7 | 2008-02-23 03:09:44 +0000 | [diff] [blame] | 69 | /* List of functions exported by this module */ |
| 70 | |
| 71 | static PyMethodDef module_functions[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 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 */ |
Eric Smith | a73fbe7 | 2008-02-23 03:09:44 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | |
| 79 | /* Initialize this module. */ |
| 80 | |
| 81 | PyMODINIT_FUNC |
| 82 | initfuture_builtins(void) |
| 83 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 84 | PyObject *m, *itertools, *iter_func; |
| 85 | char *it_funcs[] = {"imap", "ifilter", "izip", NULL}; |
| 86 | char **cur_func; |
Eric Smith | a73fbe7 | 2008-02-23 03:09:44 +0000 | [diff] [blame] | 87 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 88 | m = Py_InitModule3("future_builtins", module_functions, module_doc); |
| 89 | if (m == NULL) |
| 90 | return; |
Eric Smith | a73fbe7 | 2008-02-23 03:09:44 +0000 | [diff] [blame] | 91 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 92 | itertools = PyImport_ImportModuleNoBlock("itertools"); |
| 93 | if (itertools == NULL) |
| 94 | return; |
David Wolever | 2724ab9 | 2008-03-19 02:35:45 +0000 | [diff] [blame] | 95 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 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) |
| 101 | break; |
| 102 | } |
| 103 | Py_DECREF(itertools); |
| 104 | /* any other initialization needed */ |
Eric Smith | a73fbe7 | 2008-02-23 03:09:44 +0000 | [diff] [blame] | 105 | } |