source: python/vendor/Python-2.7.6/Modules/future_builtins.c

Last change on this file was 388, checked in by dmik, 11 years ago

python: Update vendor to 2.7.6.

  • Property svn:eol-style set to native
File size: 3.0 KB
Line 
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
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\
17ascii(arg) -- Returns the canonical string representation of an object.\n\
18filter(pred, iterable) -- Returns an iterator yielding those items of \n\
19 iterable for which pred(item) is true.\n\
20hex(arg) -- Returns the hexadecimal representation of an integer.\n\
21map(func, *iterables) -- Returns an iterator that computes the function \n\
22 using arguments from each of the iterables.\n\
23oct(arg) -- Returns the octal representation of an integer.\n\
24zip(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\
28The typical usage of this module is to replace existing builtins in a\n\
29module's namespace:\n \n\
30from future_builtins import ascii, filter, map, hex, oct, zip\n");
31
32static PyObject *
33builtin_hex(PyObject *self, PyObject *v)
34{
35 return PyNumber_ToBase(v, 16);
36}
37
38PyDoc_STRVAR(hex_doc,
39"hex(number) -> string\n\
40\n\
41Return the hexadecimal representation of an integer or long integer.");
42
43
44static PyObject *
45builtin_oct(PyObject *self, PyObject *v)
46{
47 return PyNumber_ToBase(v, 8);
48}
49
50PyDoc_STRVAR(oct_doc,
51"oct(number) -> string\n\
52\n\
53Return the octal representation of an integer or long integer.");
54
55
56static PyObject *
57builtin_ascii(PyObject *self, PyObject *v)
58{
59 return PyObject_Repr(v);
60}
61
62PyDoc_STRVAR(ascii_doc,
63"ascii(object) -> string\n\
64\n\
65Return the same as repr(). In Python 3.x, the repr() result will\n\
66contain printable characters unescaped, while the ascii() result\n\
67will have such characters backslash-escaped.");
68
69/* List of functions exported by this module */
70
71static 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
81PyMODINIT_FUNC
82initfuture_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}
Note: See TracBrowser for help on using the repository browser.