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\ |
Neal Norwitz | 114dd94 | 2008-02-24 08:27:49 +0000 | [diff] [blame] | 17 | hex(arg) -- Returns the hexadecimal representation of an integer\n\ |
Eric Smith | a73fbe7 | 2008-02-23 03:09:44 +0000 | [diff] [blame] | 18 | oct(arg) -- Returns the octal representation of an integer\n\ |
| 19 | \n\ |
| 20 | The typical usage of this module is to replace existing builtins in a\n\ |
| 21 | module's namespace:\n \n\ |
| 22 | from future_builtins import hex, oct\n"); |
| 23 | |
| 24 | static PyObject * |
| 25 | builtin_hex(PyObject *self, PyObject *v) |
| 26 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 27 | return PyNumber_ToBase(v, 16); |
Eric Smith | a73fbe7 | 2008-02-23 03:09:44 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | PyDoc_STRVAR(hex_doc, |
| 31 | "hex(number) -> string\n\ |
| 32 | \n\ |
| 33 | Return the hexadecimal representation of an integer or long integer."); |
| 34 | |
| 35 | |
| 36 | static PyObject * |
| 37 | builtin_oct(PyObject *self, PyObject *v) |
| 38 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 39 | return PyNumber_ToBase(v, 8); |
Eric Smith | a73fbe7 | 2008-02-23 03:09:44 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | PyDoc_STRVAR(oct_doc, |
| 43 | "oct(number) -> string\n\ |
| 44 | \n\ |
| 45 | Return the octal representation of an integer or long integer."); |
| 46 | |
| 47 | |
Georg Brandl | 89f4887 | 2008-06-11 18:55:38 +0000 | [diff] [blame] | 48 | static PyObject * |
| 49 | builtin_ascii(PyObject *self, PyObject *v) |
| 50 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 51 | return PyObject_Repr(v); |
Georg Brandl | 89f4887 | 2008-06-11 18:55:38 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | PyDoc_STRVAR(ascii_doc, |
| 55 | "ascii(object) -> string\n\ |
| 56 | \n\ |
| 57 | Return the same as repr(). In Python 3.x, the repr() result will\n\ |
| 58 | contain printable characters unescaped, while the ascii() result\n\ |
| 59 | will have such characters backslash-escaped."); |
| 60 | |
Eric Smith | a73fbe7 | 2008-02-23 03:09:44 +0000 | [diff] [blame] | 61 | /* List of functions exported by this module */ |
| 62 | |
| 63 | static PyMethodDef module_functions[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 64 | {"hex", builtin_hex, METH_O, hex_doc}, |
| 65 | {"oct", builtin_oct, METH_O, oct_doc}, |
| 66 | {"ascii", builtin_ascii, METH_O, ascii_doc}, |
| 67 | {NULL, NULL} /* Sentinel */ |
Eric Smith | a73fbe7 | 2008-02-23 03:09:44 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | |
| 71 | /* Initialize this module. */ |
| 72 | |
| 73 | PyMODINIT_FUNC |
| 74 | initfuture_builtins(void) |
| 75 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 76 | PyObject *m, *itertools, *iter_func; |
| 77 | char *it_funcs[] = {"imap", "ifilter", "izip", NULL}; |
| 78 | char **cur_func; |
Eric Smith | a73fbe7 | 2008-02-23 03:09:44 +0000 | [diff] [blame] | 79 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 80 | m = Py_InitModule3("future_builtins", module_functions, module_doc); |
| 81 | if (m == NULL) |
| 82 | return; |
Eric Smith | a73fbe7 | 2008-02-23 03:09:44 +0000 | [diff] [blame] | 83 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 84 | itertools = PyImport_ImportModuleNoBlock("itertools"); |
| 85 | if (itertools == NULL) |
| 86 | return; |
David Wolever | 2724ab9 | 2008-03-19 02:35:45 +0000 | [diff] [blame] | 87 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 88 | /* If anything in the following loop fails, we fall through. */ |
| 89 | for (cur_func = it_funcs; *cur_func; ++cur_func){ |
| 90 | iter_func = PyObject_GetAttrString(itertools, *cur_func); |
| 91 | if (iter_func == NULL || |
| 92 | PyModule_AddObject(m, *cur_func+1, iter_func) < 0) |
| 93 | break; |
| 94 | } |
| 95 | Py_DECREF(itertools); |
| 96 | /* any other initialization needed */ |
Eric Smith | a73fbe7 | 2008-02-23 03:09:44 +0000 | [diff] [blame] | 97 | } |