Home | History | Annotate | Download | only in Modules
      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\
     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\
     27 \n\
     28 The typical usage of this module is to replace existing builtins in a\n\
     29 module's namespace:\n \n\
     30 from future_builtins import ascii, filter, map, hex, oct, zip\n");
     31 
     32 static PyObject *
     33 builtin_hex(PyObject *self, PyObject *v)
     34 {
     35     return PyNumber_ToBase(v, 16);
     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 {
     47     return PyNumber_ToBase(v, 8);
     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 
     56 static PyObject *
     57 builtin_ascii(PyObject *self, PyObject *v)
     58 {
     59     return PyObject_Repr(v);
     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 
     69 /* List of functions exported by this module */
     70 
     71 static PyMethodDef module_functions[] = {
     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 */
     76 };
     77 
     78 
     79 /* Initialize this module. */
     80 
     81 PyMODINIT_FUNC
     82 initfuture_builtins(void)
     83 {
     84     PyObject *m, *itertools, *iter_func;
     85     char *it_funcs[] = {"imap", "ifilter", "izip", NULL};
     86     char **cur_func;
     87 
     88     m = Py_InitModule3("future_builtins", module_functions, module_doc);
     89     if (m == NULL)
     90         return;
     91 
     92     itertools = PyImport_ImportModuleNoBlock("itertools");
     93     if (itertools == NULL)
     94         return;
     95 
     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 */
    105 }
    106