Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(148740)

Delta Between Two Patch Sets: Python/bltinmodule.c

Issue 23722: During metaclass.__init__, super() of the constructed class does not work
Left Patch Set: Created 3 years, 4 months ago
Right Patch Set: Created 3 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Objects/typeobject.c ('k') | Python/compile.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* Built-in functions */ 1 /* Built-in functions */
2 2
3 #include "Python.h" 3 #include "Python.h"
4 #include "Python-ast.h" 4 #include "Python-ast.h"
5 5
6 #include "node.h" 6 #include "node.h"
7 #include "code.h" 7 #include "code.h"
8 8
9 #include "asdl.h" 9 #include "asdl.h"
10 #include "ast.h" 10 #include "ast.h"
11 11
12 #include <ctype.h> 12 #include <ctype.h>
13 13
14 #ifdef HAVE_LANGINFO_H 14 #ifdef HAVE_LANGINFO_H
15 #include <langinfo.h> /* CODESET */ 15 #include <langinfo.h> /* CODESET */
16 #endif 16 #endif
17 17
18 /* The default encoding used by the platform file system APIs 18 /* The default encoding used by the platform file system APIs
19 Can remain NULL for all platforms that don't have such a concept 19 Can remain NULL for all platforms that don't have such a concept
20 20
21 Don't forget to modify PyUnicode_DecodeFSDefault() if you touch any of the 21 Don't forget to modify PyUnicode_DecodeFSDefault() if you touch any of the
22 values for Py_FileSystemDefaultEncoding! 22 values for Py_FileSystemDefaultEncoding!
23 */ 23 */
24 #ifdef HAVE_MBCS 24 #if defined(__APPLE__)
25 const char *Py_FileSystemDefaultEncoding = "mbcs"; 25 const char *Py_FileSystemDefaultEncoding = "utf-8";
26 int Py_HasFileSystemDefaultEncoding = 1; 26 int Py_HasFileSystemDefaultEncoding = 1;
27 #elif defined(__APPLE__) 27 #elif defined(MS_WINDOWS)
28 /* may be changed by initfsencoding(), but should never be free()d */
28 const char *Py_FileSystemDefaultEncoding = "utf-8"; 29 const char *Py_FileSystemDefaultEncoding = "utf-8";
29 int Py_HasFileSystemDefaultEncoding = 1; 30 int Py_HasFileSystemDefaultEncoding = 1;
30 #else 31 #else
31 const char *Py_FileSystemDefaultEncoding = NULL; /* set by initfsencoding() */ 32 const char *Py_FileSystemDefaultEncoding = NULL; /* set by initfsencoding() */
32 int Py_HasFileSystemDefaultEncoding = 0; 33 int Py_HasFileSystemDefaultEncoding = 0;
33 #endif 34 #endif
35 const char *Py_FileSystemDefaultEncodeErrors = "surrogateescape";
34 36
35 _Py_IDENTIFIER(__builtins__); 37 _Py_IDENTIFIER(__builtins__);
36 _Py_IDENTIFIER(__dict__); 38 _Py_IDENTIFIER(__dict__);
37 _Py_IDENTIFIER(__prepare__); 39 _Py_IDENTIFIER(__prepare__);
38 _Py_IDENTIFIER(__round__); 40 _Py_IDENTIFIER(__round__);
39 _Py_IDENTIFIER(encoding); 41 _Py_IDENTIFIER(encoding);
40 _Py_IDENTIFIER(errors); 42 _Py_IDENTIFIER(errors);
41 _Py_IDENTIFIER(fileno); 43 _Py_IDENTIFIER(fileno);
42 _Py_IDENTIFIER(flush); 44 _Py_IDENTIFIER(flush);
43 _Py_IDENTIFIER(metaclass); 45 _Py_IDENTIFIER(metaclass);
44 _Py_IDENTIFIER(sort); 46 _Py_IDENTIFIER(sort);
45 _Py_IDENTIFIER(stdin); 47 _Py_IDENTIFIER(stdin);
46 _Py_IDENTIFIER(stdout); 48 _Py_IDENTIFIER(stdout);
47 _Py_IDENTIFIER(stderr); 49 _Py_IDENTIFIER(stderr);
48 50
49 #include "clinic/bltinmodule.c.h" 51 #include "clinic/bltinmodule.c.h"
50 52
51 /* AC: cannot convert yet, waiting for *args support */ 53 /* AC: cannot convert yet, waiting for *args support */
52 static PyObject * 54 static PyObject *
53 builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds) 55 builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
54 { 56 {
55 PyObject *func, *name, *bases, *mkw, *meta, *winner, *prep, *ns, *cell; 57 PyObject *func, *name, *bases, *mkw, *meta, *winner, *prep, *ns;
56 PyObject *cls = NULL; 58 PyObject *cls = NULL, *cell = NULL;
57 Py_ssize_t nargs; 59 Py_ssize_t nargs;
58 int isclass = 0; /* initialize to prevent gcc warning */ 60 int isclass = 0; /* initialize to prevent gcc warning */
59 61
60 assert(args != NULL); 62 assert(args != NULL);
61 if (!PyTuple_Check(args)) { 63 if (!PyTuple_Check(args)) {
62 PyErr_SetString(PyExc_TypeError, 64 PyErr_SetString(PyExc_TypeError,
63 "__build_class__: args is not a tuple"); 65 "__build_class__: args is not a tuple");
64 return NULL; 66 return NULL;
65 } 67 }
66 nargs = PyTuple_GET_SIZE(args); 68 nargs = PyTuple_GET_SIZE(args);
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 ns = PyDict_New(); 150 ns = PyDict_New();
149 } 151 }
150 else { 152 else {
151 Py_DECREF(meta); 153 Py_DECREF(meta);
152 Py_XDECREF(mkw); 154 Py_XDECREF(mkw);
153 Py_DECREF(bases); 155 Py_DECREF(bases);
154 return NULL; 156 return NULL;
155 } 157 }
156 } 158 }
157 else { 159 else {
158 PyObject *pargs = PyTuple_Pack(2, name, bases); 160 PyObject *pargs[2] = {name, bases};
159 if (pargs == NULL) { 161 ns = _PyObject_FastCallDict(prep, pargs, 2, mkw);
160 Py_DECREF(prep);
161 Py_DECREF(meta);
162 Py_XDECREF(mkw);
163 Py_DECREF(bases);
164 return NULL;
165 }
166 ns = PyEval_CallObjectWithKeywords(prep, pargs, mkw);
167 Py_DECREF(pargs);
168 Py_DECREF(prep); 162 Py_DECREF(prep);
169 } 163 }
170 if (ns == NULL) { 164 if (ns == NULL) {
171 Py_DECREF(meta); 165 Py_DECREF(meta);
172 Py_XDECREF(mkw); 166 Py_XDECREF(mkw);
173 Py_DECREF(bases); 167 Py_DECREF(bases);
174 return NULL; 168 return NULL;
175 } 169 }
176 cell = PyEval_EvalCodeEx(PyFunction_GET_CODE(func), PyFunction_GET_GLOBALS(f unc), ns, 170 cell = PyEval_EvalCodeEx(PyFunction_GET_CODE(func), PyFunction_GET_GLOBALS(f unc), ns,
177 NULL, 0, NULL, 0, NULL, 0, NULL, 171 NULL, 0, NULL, 0, NULL, 0, NULL,
178 PyFunction_GET_CLOSURE(func)); 172 PyFunction_GET_CLOSURE(func));
179 if (cell != NULL) { 173 if (cell != NULL) {
180 PyObject *margs; 174 PyObject *margs[3] = {name, bases, ns};
181 margs = PyTuple_Pack(3, name, bases, ns); 175 cls = _PyObject_FastCallDict(meta, margs, 3, mkw);
182 if (margs != NULL) { 176 if (cls != NULL && PyType_Check(cls) && PyCell_Check(cell)) {
183 cls = PyEval_CallObjectWithKeywords(meta, margs, mkw); 177 PyObject *cell_cls = PyCell_GET(cell);
184 Py_DECREF(margs); 178 if (cell_cls != cls) {
185 } 179 /* TODO: In 3.7, the deprecation warning becomes a RuntimeError
storchaka 2016/12/04 14:10:59 A dot is needed since the next line is a different
186 Py_DECREF(cell); 180 * At that point, cell_error won't be needed.
187 } 181 */
182 int cell_error;
183 if (cell_cls == NULL) {
184 const char *msg =
185 "__class__ not set defining %.200R as %.200R. "
186 "Was __classcell__ propagated to type.__new__?";
187 cell_error = PyErr_WarnFormat(
188 PyExc_DeprecationWarning, 1, msg, name, cls);
189 } else {
190 const char *msg =
191 "__class__ set to %.200R defining %.200R as %.200R.";
storchaka 2016/12/04 14:10:59 What about the ending dot? It usually is not used
192 PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls);
193 cell_error = 1;
194 }
195 if (cell_error) {
196 Py_DECREF(cls);
197 cls = NULL;
198 goto error;
199 } else {
200 /* Fill in the cell, since type.__new__ didn't do it */
201 PyCell_Set(cell, cls);
202 }
203 }
204 }
205 }
206 error:
207 Py_XDECREF(cell);
188 Py_DECREF(ns); 208 Py_DECREF(ns);
189 Py_DECREF(meta); 209 Py_DECREF(meta);
190 Py_XDECREF(mkw); 210 Py_XDECREF(mkw);
191 Py_DECREF(bases); 211 Py_DECREF(bases);
192 return cls; 212 return cls;
193 } 213 }
194 214
195 PyDoc_STRVAR(build_class_doc, 215 PyDoc_STRVAR(build_class_doc,
196 "__build_class__(func, name, *bases, metaclass=None, **kwds) -> class\n\ 216 "__build_class__(func, name, *bases, metaclass=None, **kwds) -> class\n\
197 \n\ 217 \n\
(...skipping 961 matching lines...) Expand 10 before | Expand all | Expand 10 after
1159 map_traverse(mapobject *lz, visitproc visit, void *arg) 1179 map_traverse(mapobject *lz, visitproc visit, void *arg)
1160 { 1180 {
1161 Py_VISIT(lz->iters); 1181 Py_VISIT(lz->iters);
1162 Py_VISIT(lz->func); 1182 Py_VISIT(lz->func);
1163 return 0; 1183 return 0;
1164 } 1184 }
1165 1185
1166 static PyObject * 1186 static PyObject *
1167 map_next(mapobject *lz) 1187 map_next(mapobject *lz)
1168 { 1188 {
1169 PyObject *val; 1189 PyObject *small_stack[5];
1170 PyObject *argtuple; 1190 PyObject **stack;
1171 PyObject *result; 1191 Py_ssize_t niters, nargs, i;
1172 Py_ssize_t numargs, i; 1192 PyObject *result = NULL;
1173 1193
1174 numargs = PyTuple_GET_SIZE(lz->iters); 1194 niters = PyTuple_GET_SIZE(lz->iters);
1175 argtuple = PyTuple_New(numargs); 1195 if (niters <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
1176 if (argtuple == NULL) 1196 stack = small_stack;
1177 return NULL; 1197 }
1178 1198 else {
1179 for (i=0 ; i<numargs ; i++) { 1199 stack = PyMem_Malloc(niters * sizeof(stack[0]));
1200 if (stack == NULL) {
1201 PyErr_NoMemory();
1202 return NULL;
1203 }
1204 }
1205
1206 nargs = 0;
1207 for (i=0; i < niters; i++) {
1180 PyObject *it = PyTuple_GET_ITEM(lz->iters, i); 1208 PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1181 val = Py_TYPE(it)->tp_iternext(it); 1209 PyObject *val = Py_TYPE(it)->tp_iternext(it);
1182 if (val == NULL) { 1210 if (val == NULL) {
1183 Py_DECREF(argtuple); 1211 goto exit;
1184 return NULL; 1212 }
1185 } 1213 stack[i] = val;
1186 PyTuple_SET_ITEM(argtuple, i, val); 1214 nargs++;
1187 } 1215 }
1188 result = PyObject_Call(lz->func, argtuple, NULL); 1216
1189 Py_DECREF(argtuple); 1217 result = _PyObject_FastCall(lz->func, stack, nargs);
1218
1219 exit:
1220 for (i=0; i < nargs; i++) {
1221 Py_DECREF(stack[i]);
1222 }
1223 if (stack != small_stack) {
1224 PyMem_Free(stack);
1225 }
1190 return result; 1226 return result;
1191 } 1227 }
1192 1228
1193 static PyObject * 1229 static PyObject *
1194 map_reduce(mapobject *lz) 1230 map_reduce(mapobject *lz)
1195 { 1231 {
1196 Py_ssize_t numargs = PyTuple_GET_SIZE(lz->iters); 1232 Py_ssize_t numargs = PyTuple_GET_SIZE(lz->iters);
1197 PyObject *args = PyTuple_New(numargs+1); 1233 PyObject *args = PyTuple_New(numargs+1);
1198 Py_ssize_t i; 1234 Py_ssize_t i;
1199 if (args == NULL) 1235 if (args == NULL)
(...skipping 571 matching lines...) Expand 10 before | Expand all | Expand 10 after
1771 err = PyFile_WriteObject(end, file, Py_PRINT_RAW); 1807 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1772 if (err) 1808 if (err)
1773 return NULL; 1809 return NULL;
1774 1810
1775 if (flush != NULL) { 1811 if (flush != NULL) {
1776 PyObject *tmp; 1812 PyObject *tmp;
1777 int do_flush = PyObject_IsTrue(flush); 1813 int do_flush = PyObject_IsTrue(flush);
1778 if (do_flush == -1) 1814 if (do_flush == -1)
1779 return NULL; 1815 return NULL;
1780 else if (do_flush) { 1816 else if (do_flush) {
1781 tmp = _PyObject_CallMethodId(file, &PyId_flush, ""); 1817 tmp = _PyObject_CallMethodId(file, &PyId_flush, NULL);
1782 if (tmp == NULL) 1818 if (tmp == NULL)
1783 return NULL; 1819 return NULL;
1784 else 1820 else
1785 Py_DECREF(tmp); 1821 Py_DECREF(tmp);
1786 } 1822 }
1787 } 1823 }
1788 1824
1789 Py_RETURN_NONE; 1825 Py_RETURN_NONE;
1790 } 1826 }
1791 1827
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1837 "input(): lost sys.stdout"); 1873 "input(): lost sys.stdout");
1838 return NULL; 1874 return NULL;
1839 } 1875 }
1840 if (ferr == NULL || ferr == Py_None) { 1876 if (ferr == NULL || ferr == Py_None) {
1841 PyErr_SetString(PyExc_RuntimeError, 1877 PyErr_SetString(PyExc_RuntimeError,
1842 "input(): lost sys.stderr"); 1878 "input(): lost sys.stderr");
1843 return NULL; 1879 return NULL;
1844 } 1880 }
1845 1881
1846 /* First of all, flush stderr */ 1882 /* First of all, flush stderr */
1847 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, ""); 1883 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
1848 if (tmp == NULL) 1884 if (tmp == NULL)
1849 PyErr_Clear(); 1885 PyErr_Clear();
1850 else 1886 else
1851 Py_DECREF(tmp); 1887 Py_DECREF(tmp);
1852 1888
1853 /* We should only use (GNU) readline if Python's sys.stdin and 1889 /* We should only use (GNU) readline if Python's sys.stdin and
1854 sys.stdout are the same as C's stdin and stdout, because we 1890 sys.stdout are the same as C's stdin and stdout, because we
1855 need to pass it those. */ 1891 need to pass it those. */
1856 tmp = _PyObject_CallMethodId(fin, &PyId_fileno, ""); 1892 tmp = _PyObject_CallMethodId(fin, &PyId_fileno, NULL);
1857 if (tmp == NULL) { 1893 if (tmp == NULL) {
1858 PyErr_Clear(); 1894 PyErr_Clear();
1859 tty = 0; 1895 tty = 0;
1860 } 1896 }
1861 else { 1897 else {
1862 fd = PyLong_AsLong(tmp); 1898 fd = PyLong_AsLong(tmp);
1863 Py_DECREF(tmp); 1899 Py_DECREF(tmp);
1864 if (fd < 0 && PyErr_Occurred()) 1900 if (fd < 0 && PyErr_Occurred())
1865 return NULL; 1901 return NULL;
1866 tty = fd == fileno(stdin) && isatty(fd); 1902 tty = fd == fileno(stdin) && isatty(fd);
1867 } 1903 }
1868 if (tty) { 1904 if (tty) {
1869 tmp = _PyObject_CallMethodId(fout, &PyId_fileno, ""); 1905 tmp = _PyObject_CallMethodId(fout, &PyId_fileno, NULL);
1870 if (tmp == NULL) { 1906 if (tmp == NULL) {
1871 PyErr_Clear(); 1907 PyErr_Clear();
1872 tty = 0; 1908 tty = 0;
1873 } 1909 }
1874 else { 1910 else {
1875 fd = PyLong_AsLong(tmp); 1911 fd = PyLong_AsLong(tmp);
1876 Py_DECREF(tmp); 1912 Py_DECREF(tmp);
1877 if (fd < 0 && PyErr_Occurred()) 1913 if (fd < 0 && PyErr_Occurred())
1878 return NULL; 1914 return NULL;
1879 tty = fd == fileno(stdout) && isatty(fd); 1915 tty = fd == fileno(stdout) && isatty(fd);
(...skipping 10 matching lines...) Expand all
1890 char *stdin_encoding_str, *stdin_errors_str; 1926 char *stdin_encoding_str, *stdin_errors_str;
1891 PyObject *result; 1927 PyObject *result;
1892 size_t len; 1928 size_t len;
1893 1929
1894 stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding); 1930 stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);
1895 stdin_errors = _PyObject_GetAttrId(fin, &PyId_errors); 1931 stdin_errors = _PyObject_GetAttrId(fin, &PyId_errors);
1896 if (!stdin_encoding || !stdin_errors) 1932 if (!stdin_encoding || !stdin_errors)
1897 /* stdin is a text stream, so it must have an 1933 /* stdin is a text stream, so it must have an
1898 encoding. */ 1934 encoding. */
1899 goto _readline_errors; 1935 goto _readline_errors;
1900 stdin_encoding_str = _PyUnicode_AsString(stdin_encoding); 1936 stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding);
1901 stdin_errors_str = _PyUnicode_AsString(stdin_errors); 1937 stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
1902 if (!stdin_encoding_str || !stdin_errors_str) 1938 if (!stdin_encoding_str || !stdin_errors_str)
1903 goto _readline_errors; 1939 goto _readline_errors;
1904 tmp = _PyObject_CallMethodId(fout, &PyId_flush, ""); 1940 tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
1905 if (tmp == NULL) 1941 if (tmp == NULL)
1906 PyErr_Clear(); 1942 PyErr_Clear();
1907 else 1943 else
1908 Py_DECREF(tmp); 1944 Py_DECREF(tmp);
1909 if (prompt != NULL) { 1945 if (prompt != NULL) {
1910 /* We have a prompt, encode it as stdout would */ 1946 /* We have a prompt, encode it as stdout would */
1911 char *stdout_encoding_str, *stdout_errors_str; 1947 char *stdout_encoding_str, *stdout_errors_str;
1912 PyObject *stringpo; 1948 PyObject *stringpo;
1913 stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding); 1949 stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding);
1914 stdout_errors = _PyObject_GetAttrId(fout, &PyId_errors); 1950 stdout_errors = _PyObject_GetAttrId(fout, &PyId_errors);
1915 if (!stdout_encoding || !stdout_errors) 1951 if (!stdout_encoding || !stdout_errors)
1916 goto _readline_errors; 1952 goto _readline_errors;
1917 stdout_encoding_str = _PyUnicode_AsString(stdout_encoding); 1953 stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding);
1918 stdout_errors_str = _PyUnicode_AsString(stdout_errors); 1954 stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
1919 if (!stdout_encoding_str || !stdout_errors_str) 1955 if (!stdout_encoding_str || !stdout_errors_str)
1920 goto _readline_errors; 1956 goto _readline_errors;
1921 stringpo = PyObject_Str(prompt); 1957 stringpo = PyObject_Str(prompt);
1922 if (stringpo == NULL) 1958 if (stringpo == NULL)
1923 goto _readline_errors; 1959 goto _readline_errors;
1924 po = PyUnicode_AsEncodedString(stringpo, 1960 po = PyUnicode_AsEncodedString(stringpo,
1925 stdout_encoding_str, stdout_errors_str); 1961 stdout_encoding_str, stdout_errors_str);
1926 Py_CLEAR(stdout_encoding); 1962 Py_CLEAR(stdout_encoding);
1927 Py_CLEAR(stdout_errors); 1963 Py_CLEAR(stdout_errors);
1928 Py_CLEAR(stringpo); 1964 Py_CLEAR(stringpo);
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1974 Py_XDECREF(stdout_errors); 2010 Py_XDECREF(stdout_errors);
1975 Py_XDECREF(po); 2011 Py_XDECREF(po);
1976 return NULL; 2012 return NULL;
1977 } 2013 }
1978 2014
1979 /* Fallback if we're not interactive */ 2015 /* Fallback if we're not interactive */
1980 if (prompt != NULL) { 2016 if (prompt != NULL) {
1981 if (PyFile_WriteObject(prompt, fout, Py_PRINT_RAW) != 0) 2017 if (PyFile_WriteObject(prompt, fout, Py_PRINT_RAW) != 0)
1982 return NULL; 2018 return NULL;
1983 } 2019 }
1984 tmp = _PyObject_CallMethodId(fout, &PyId_flush, ""); 2020 tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
1985 if (tmp == NULL) 2021 if (tmp == NULL)
1986 PyErr_Clear(); 2022 PyErr_Clear();
1987 else 2023 else
1988 Py_DECREF(tmp); 2024 Py_DECREF(tmp);
1989 return PyFile_GetLine(fin, -1); 2025 return PyFile_GetLine(fin, -1);
1990 } 2026 }
1991 2027
1992 2028
1993 /*[clinic input] 2029 /*[clinic input]
1994 repr as builtin_repr 2030 repr as builtin_repr
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
2030 2066
2031 round = _PyObject_LookupSpecial(number, &PyId___round__); 2067 round = _PyObject_LookupSpecial(number, &PyId___round__);
2032 if (round == NULL) { 2068 if (round == NULL) {
2033 if (!PyErr_Occurred()) 2069 if (!PyErr_Occurred())
2034 PyErr_Format(PyExc_TypeError, 2070 PyErr_Format(PyExc_TypeError,
2035 "type %.100s doesn't define __round__ method", 2071 "type %.100s doesn't define __round__ method",
2036 Py_TYPE(number)->tp_name); 2072 Py_TYPE(number)->tp_name);
2037 return NULL; 2073 return NULL;
2038 } 2074 }
2039 2075
2040 if (ndigits == NULL) 2076 if (ndigits == NULL || ndigits == Py_None)
2041 result = PyObject_CallFunctionObjArgs(round, NULL); 2077 result = PyObject_CallFunctionObjArgs(round, NULL);
2042 else 2078 else
2043 result = PyObject_CallFunctionObjArgs(round, ndigits, NULL); 2079 result = PyObject_CallFunctionObjArgs(round, ndigits, NULL);
2044 Py_DECREF(round); 2080 Py_DECREF(round);
2045 return result; 2081 return result;
2046 } 2082 }
2047 2083
2048 PyDoc_STRVAR(round_doc, 2084 PyDoc_STRVAR(round_doc,
2049 "round(number[, ndigits]) -> number\n\ 2085 "round(number[, ndigits]) -> number\n\
2050 \n\ 2086 \n\
2051 Round a number to a given precision in decimal digits (default 0 digits).\n\ 2087 Round a number to a given precision in decimal digits (default 0 digits).\n\
2052 This returns an int when called with one argument, otherwise the\n\ 2088 This returns an int when called with one argument, otherwise the\n\
2053 same type as the number. ndigits may be negative."); 2089 same type as the number. ndigits may be negative.");
2054 2090
2055 2091
2056 /*AC: we need to keep the kwds dict intact to easily call into the 2092 /*AC: we need to keep the kwds dict intact to easily call into the
2057 * list.sort method, which isn't currently supported in AC. So we just use 2093 * list.sort method, which isn't currently supported in AC. So we just use
2058 * the initially generated signature with a custom implementation. 2094 * the initially generated signature with a custom implementation.
2059 */ 2095 */
2060 /* [disabled clinic input] 2096 /* [disabled clinic input]
2061 sorted as builtin_sorted 2097 sorted as builtin_sorted
2062 2098
2063 iterable as seq: object 2099 iterable as seq: object
2064 key as keyfunc: object = None 2100 key as keyfunc: object = None
2065 reverse: object = False 2101 reverse: object = False
2066 2102
2067 Return a new list containing all items from the iterable in ascending order. 2103 Return a new list containing all items from the iterable in ascending order.
2068 2104
2069 A custom key function can be supplied to customise the sort order, and the 2105 A custom key function can be supplied to customize the sort order, and the
2070 reverse flag can be set to request the result in descending order. 2106 reverse flag can be set to request the result in descending order.
2071 [end disabled clinic input]*/ 2107 [end disabled clinic input]*/
2072 2108
2073 PyDoc_STRVAR(builtin_sorted__doc__, 2109 PyDoc_STRVAR(builtin_sorted__doc__,
2074 "sorted($module, iterable, key=None, reverse=False)\n" 2110 "sorted($module, iterable, key=None, reverse=False)\n"
2075 "--\n" 2111 "--\n"
2076 "\n" 2112 "\n"
2077 "Return a new list containing all items from the iterable in ascending order.\n" 2113 "Return a new list containing all items from the iterable in ascending order.\n"
2078 "\n" 2114 "\n"
2079 "A custom key function can be supplied to customise the sort order, and the\n" 2115 "A custom key function can be supplied to customize the sort order, and the\n"
2080 "reverse flag can be set to request the result in descending order."); 2116 "reverse flag can be set to request the result in descending order.");
2081 2117
2082 #define BUILTIN_SORTED_METHODDEF \ 2118 #define BUILTIN_SORTED_METHODDEF \
2083 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS|METH_KEYWORDS, builtin_ sorted__doc__}, 2119 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS|METH_KEYWORDS, builtin_ sorted__doc__},
2084 2120
2085 static PyObject * 2121 static PyObject *
2086 builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds) 2122 builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
2087 { 2123 {
2088 PyObject *newlist, *v, *seq, *keyfunc=NULL, *newargs; 2124 PyObject *newlist, *v, *seq, *keyfunc=NULL, **newargs;
2089 PyObject *callable; 2125 PyObject *callable;
2090 static char *kwlist[] = {"iterable", "key", "reverse", 0}; 2126 static char *kwlist[] = {"iterable", "key", "reverse", 0};
2091 int reverse; 2127 int reverse;
2128 Py_ssize_t nargs;
2092 2129
2093 /* args 1-3 should match listsort in Objects/listobject.c */ 2130 /* args 1-3 should match listsort in Objects/listobject.c */
2094 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oi:sorted", 2131 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oi:sorted",
2095 kwlist, &seq, &keyfunc, &reverse)) 2132 kwlist, &seq, &keyfunc, &reverse))
2096 return NULL; 2133 return NULL;
2097 2134
2098 newlist = PySequence_List(seq); 2135 newlist = PySequence_List(seq);
2099 if (newlist == NULL) 2136 if (newlist == NULL)
2100 return NULL; 2137 return NULL;
2101 2138
2102 callable = _PyObject_GetAttrId(newlist, &PyId_sort); 2139 callable = _PyObject_GetAttrId(newlist, &PyId_sort);
2103 if (callable == NULL) { 2140 if (callable == NULL) {
2104 Py_DECREF(newlist); 2141 Py_DECREF(newlist);
2105 return NULL; 2142 return NULL;
2106 } 2143 }
2107 2144
2108 newargs = PyTuple_GetSlice(args, 1, 4); 2145 newargs = &PyTuple_GET_ITEM(args, 1);
2109 if (newargs == NULL) { 2146 nargs = PyTuple_GET_SIZE(args) - 1;
2110 Py_DECREF(newlist); 2147 v = _PyObject_FastCallDict(callable, newargs, nargs, kwds);
2111 Py_DECREF(callable);
2112 return NULL;
2113 }
2114
2115 v = PyObject_Call(callable, newargs, kwds);
2116 Py_DECREF(newargs);
2117 Py_DECREF(callable); 2148 Py_DECREF(callable);
2118 if (v == NULL) { 2149 if (v == NULL) {
2119 Py_DECREF(newlist); 2150 Py_DECREF(newlist);
2120 return NULL; 2151 return NULL;
2121 } 2152 }
2122 Py_DECREF(v); 2153 Py_DECREF(v);
2123 return newlist; 2154 return newlist;
2124 } 2155 }
2125 2156
2126 2157
(...skipping 583 matching lines...) Expand 10 before | Expand all | Expand 10 after
2710 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) { 2741 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2711 Py_DECREF(debug); 2742 Py_DECREF(debug);
2712 return NULL; 2743 return NULL;
2713 } 2744 }
2714 Py_DECREF(debug); 2745 Py_DECREF(debug);
2715 2746
2716 return mod; 2747 return mod;
2717 #undef ADD_TO_ALL 2748 #undef ADD_TO_ALL
2718 #undef SETBUILTIN 2749 #undef SETBUILTIN
2719 } 2750 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+