--- /dev/null
+\r
+/* future_builtins module */\r
+\r
+/* This module provides functions that will be builtins in Python 3.0,\r
+ but that conflict with builtins that already exist in Python\r
+ 2.x. */\r
+\r
+\r
+#include "Python.h"\r
+\r
+PyDoc_STRVAR(module_doc,\r
+"This module provides functions that will be builtins in Python 3.0,\n\\r
+but that conflict with builtins that already exist in Python 2.x.\n\\r
+\n\\r
+Functions:\n\\r
+\n\\r
+ascii(arg) -- Returns the canonical string representation of an object.\n\\r
+filter(pred, iterable) -- Returns an iterator yielding those items of \n\\r
+ iterable for which pred(item) is true.\n\\r
+hex(arg) -- Returns the hexadecimal representation of an integer.\n\\r
+map(func, *iterables) -- Returns an iterator that computes the function \n\\r
+ using arguments from each of the iterables.\n\\r
+oct(arg) -- Returns the octal representation of an integer.\n\\r
+zip(iter1 [,iter2 [...]]) -- Returns a zip object whose .next() method \n\\r
+ returns a tuple where the i-th element comes from the i-th iterable \n\\r
+ argument.\n\\r
+\n\\r
+The typical usage of this module is to replace existing builtins in a\n\\r
+module's namespace:\n \n\\r
+from future_builtins import ascii, filter, map, hex, oct, zip\n");\r
+\r
+static PyObject *\r
+builtin_hex(PyObject *self, PyObject *v)\r
+{\r
+ return PyNumber_ToBase(v, 16);\r
+}\r
+\r
+PyDoc_STRVAR(hex_doc,\r
+"hex(number) -> string\n\\r
+\n\\r
+Return the hexadecimal representation of an integer or long integer.");\r
+\r
+\r
+static PyObject *\r
+builtin_oct(PyObject *self, PyObject *v)\r
+{\r
+ return PyNumber_ToBase(v, 8);\r
+}\r
+\r
+PyDoc_STRVAR(oct_doc,\r
+"oct(number) -> string\n\\r
+\n\\r
+Return the octal representation of an integer or long integer.");\r
+\r
+\r
+static PyObject *\r
+builtin_ascii(PyObject *self, PyObject *v)\r
+{\r
+ return PyObject_Repr(v);\r
+}\r
+\r
+PyDoc_STRVAR(ascii_doc,\r
+"ascii(object) -> string\n\\r
+\n\\r
+Return the same as repr(). In Python 3.x, the repr() result will\n\\r
+contain printable characters unescaped, while the ascii() result\n\\r
+will have such characters backslash-escaped.");\r
+\r
+/* List of functions exported by this module */\r
+\r
+static PyMethodDef module_functions[] = {\r
+ {"hex", builtin_hex, METH_O, hex_doc},\r
+ {"oct", builtin_oct, METH_O, oct_doc},\r
+ {"ascii", builtin_ascii, METH_O, ascii_doc},\r
+ {NULL, NULL} /* Sentinel */\r
+};\r
+\r
+\r
+/* Initialize this module. */\r
+\r
+PyMODINIT_FUNC\r
+initfuture_builtins(void)\r
+{\r
+ PyObject *m, *itertools, *iter_func;\r
+ char *it_funcs[] = {"imap", "ifilter", "izip", NULL};\r
+ char **cur_func;\r
+\r
+ m = Py_InitModule3("future_builtins", module_functions, module_doc);\r
+ if (m == NULL)\r
+ return;\r
+\r
+ itertools = PyImport_ImportModuleNoBlock("itertools");\r
+ if (itertools == NULL)\r
+ return;\r
+\r
+ /* If anything in the following loop fails, we fall through. */\r
+ for (cur_func = it_funcs; *cur_func; ++cur_func){\r
+ iter_func = PyObject_GetAttrString(itertools, *cur_func);\r
+ if (iter_func == NULL ||\r
+ PyModule_AddObject(m, *cur_func+1, iter_func) < 0)\r
+ break;\r
+ }\r
+ Py_DECREF(itertools);\r
+ /* any other initialization needed */\r
+}\r