blob: 4c840fb161ed2380679064f6e15214eab141600d [file] [log] [blame]
Eric Smitha73fbe72008-02-23 03:09:44 +00001
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
11PyDoc_STRVAR(module_doc,
12"This module provides functions that will be builtins in Python 3.0,\n\
13but that conflict with builtins that already exist in Python 2.x.\n\
14\n\
15Functions:\n\
16\n\
Neal Norwitz114dd942008-02-24 08:27:49 +000017hex(arg) -- Returns the hexadecimal representation of an integer\n\
Eric Smitha73fbe72008-02-23 03:09:44 +000018oct(arg) -- Returns the octal representation of an integer\n\
19\n\
20The typical usage of this module is to replace existing builtins in a\n\
21module's namespace:\n \n\
22from future_builtins import hex, oct\n");
23
24static PyObject *
25builtin_hex(PyObject *self, PyObject *v)
26{
27 return PyNumber_ToBase(v, 16);
28}
29
30PyDoc_STRVAR(hex_doc,
31"hex(number) -> string\n\
32\n\
33Return the hexadecimal representation of an integer or long integer.");
34
35
36static PyObject *
37builtin_oct(PyObject *self, PyObject *v)
38{
39 return PyNumber_ToBase(v, 8);
40}
41
42PyDoc_STRVAR(oct_doc,
43"oct(number) -> string\n\
44\n\
45Return the octal representation of an integer or long integer.");
46
47
Georg Brandl89f48872008-06-11 18:55:38 +000048static PyObject *
49builtin_ascii(PyObject *self, PyObject *v)
50{
51 return PyObject_Repr(v);
52}
53
54PyDoc_STRVAR(ascii_doc,
55"ascii(object) -> string\n\
56\n\
57Return the same as repr(). In Python 3.x, the repr() result will\n\
58contain printable characters unescaped, while the ascii() result\n\
59will have such characters backslash-escaped.");
60
Eric Smitha73fbe72008-02-23 03:09:44 +000061/* List of functions exported by this module */
62
63static PyMethodDef module_functions[] = {
64 {"hex", builtin_hex, METH_O, hex_doc},
65 {"oct", builtin_oct, METH_O, oct_doc},
Georg Brandl89f48872008-06-11 18:55:38 +000066 {"ascii", builtin_ascii, METH_O, ascii_doc},
Eric Smitha73fbe72008-02-23 03:09:44 +000067 {NULL, NULL} /* Sentinel */
68};
69
70
71/* Initialize this module. */
72
73PyMODINIT_FUNC
74initfuture_builtins(void)
75{
David Wolever2724ab92008-03-19 02:35:45 +000076 PyObject *m, *itertools, *iter_func;
77 char *it_funcs[] = {"imap", "ifilter", "izip", NULL};
78 char **cur_func;
Eric Smitha73fbe72008-02-23 03:09:44 +000079
80 m = Py_InitModule3("future_builtins", module_functions, module_doc);
81 if (m == NULL)
82 return;
83
David Wolever2724ab92008-03-19 02:35:45 +000084 itertools = PyImport_ImportModuleNoBlock("itertools");
85 if (itertools == NULL)
86 return;
87
88 for (cur_func = it_funcs; *cur_func; ++cur_func){
89 iter_func = PyObject_GetAttrString(itertools, *cur_func);
90 if (iter_func == NULL)
91 return;
92 PyModule_AddObject(m, *cur_func+1, iter_func);
93 }
94 Py_DECREF(itertools);
Eric Smitha73fbe72008-02-23 03:09:44 +000095 /* any other initialization needed */
96}