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

Side by Side Diff: Python/bltinmodule.c

Issue 18111: Add a default argument to min & max
Patch Set: Created 1 year, 2 months 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:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_builtin.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 1311 matching lines...) Expand 10 before | Expand all | Expand 10 after
1322 PyDoc_STRVAR(locals_doc, 1322 PyDoc_STRVAR(locals_doc,
1323 "locals() -> dictionary\n\ 1323 "locals() -> dictionary\n\
1324 \n\ 1324 \n\
1325 Update and return a dictionary containing the current scope's local variables.") ; 1325 Update and return a dictionary containing the current scope's local variables.") ;
1326 1326
1327 1327
1328 static PyObject * 1328 static PyObject *
1329 min_max(PyObject *args, PyObject *kwds, int op) 1329 min_max(PyObject *args, PyObject *kwds, int op)
1330 { 1330 {
1331 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL; 1331 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
1332 PyObject *emptytuple, *defaultval = NULL;
1333 static char *kwlist[] = {"key", "default", NULL};
1332 const char *name = op == Py_LT ? "min" : "max"; 1334 const char *name = op == Py_LT ? "min" : "max";
1335 const int positional = PyTuple_Size(args) > 1;
1336 int ret;
1333 1337
1334 if (PyTuple_Size(args) > 1) 1338 if (positional)
1335 v = args; 1339 v = args;
1336 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v)) 1340 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
1337 return NULL; 1341 return NULL;
1338 1342
1339 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) { 1343 emptytuple = PyTuple_New(0);
1340 keyfunc = PyDict_GetItemString(kwds, "key"); 1344 if (emptytuple == NULL)
1341 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) { 1345 return NULL;
1342 PyErr_Format(PyExc_TypeError, 1346 ret = PyArg_ParseTupleAndKeywords(emptytuple, kwds, "|$OO", kwlist,
1343 "%s() got an unexpected keyword argument", name); 1347 &keyfunc, &defaultval);
1344 return NULL; 1348 Py_DECREF(emptytuple);
1345 } 1349 if (!ret)
1346 Py_INCREF(keyfunc); 1350 return NULL;
1351
1352 if (positional && defaultval != NULL) {
1353 PyErr_Format(PyExc_TypeError,
1354 "Cannot specify a default for %s() with multiple "
1355 "positional arguments", name);
1356 return NULL;
1347 } 1357 }
1348 1358
1349 it = PyObject_GetIter(v); 1359 it = PyObject_GetIter(v);
1350 if (it == NULL) { 1360 if (it == NULL) {
1351 Py_XDECREF(keyfunc);
1352 return NULL; 1361 return NULL;
1353 } 1362 }
1354 1363
1355 maxitem = NULL; /* the result */ 1364 maxitem = NULL; /* the result */
1356 maxval = NULL; /* the value associated with the result */ 1365 maxval = NULL; /* the value associated with the result */
1357 while (( item = PyIter_Next(it) )) { 1366 while (( item = PyIter_Next(it) )) {
1358 /* get the value from the key function */ 1367 /* get the value from the key function */
1359 if (keyfunc != NULL) { 1368 if (keyfunc != NULL) {
1360 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL); 1369 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1361 if (val == NULL) 1370 if (val == NULL)
(...skipping 23 matching lines...) Expand all
1385 } 1394 }
1386 else { 1395 else {
1387 Py_DECREF(item); 1396 Py_DECREF(item);
1388 Py_DECREF(val); 1397 Py_DECREF(val);
1389 } 1398 }
1390 } 1399 }
1391 } 1400 }
1392 if (PyErr_Occurred()) 1401 if (PyErr_Occurred())
1393 goto Fail_it; 1402 goto Fail_it;
1394 if (maxval == NULL) { 1403 if (maxval == NULL) {
1395 PyErr_Format(PyExc_ValueError,
1396 "%s() arg is an empty sequence", name);
1397 assert(maxitem == NULL); 1404 assert(maxitem == NULL);
1405 if (defaultval != NULL) {
1406 Py_INCREF(defaultval);
1407 maxitem = defaultval;
1408 } else {
1409 PyErr_Format(PyExc_ValueError,
1410 "%s() arg is an empty sequence", name);
1411 }
1398 } 1412 }
1399 else 1413 else
1400 Py_DECREF(maxval); 1414 Py_DECREF(maxval);
1401 Py_DECREF(it); 1415 Py_DECREF(it);
1402 Py_XDECREF(keyfunc);
1403 return maxitem; 1416 return maxitem;
1404 1417
1405 Fail_it_item_and_val: 1418 Fail_it_item_and_val:
1406 Py_DECREF(val); 1419 Py_DECREF(val);
1407 Fail_it_item: 1420 Fail_it_item:
1408 Py_DECREF(item); 1421 Py_DECREF(item);
1409 Fail_it: 1422 Fail_it:
1410 Py_XDECREF(maxval); 1423 Py_XDECREF(maxval);
1411 Py_XDECREF(maxitem); 1424 Py_XDECREF(maxitem);
1412 Py_DECREF(it); 1425 Py_DECREF(it);
1413 Py_XDECREF(keyfunc);
1414 return NULL; 1426 return NULL;
1415 } 1427 }
1416 1428
1417 static PyObject * 1429 static PyObject *
1418 builtin_min(PyObject *self, PyObject *args, PyObject *kwds) 1430 builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
1419 { 1431 {
1420 return min_max(args, kwds, Py_LT); 1432 return min_max(args, kwds, Py_LT);
1421 } 1433 }
1422 1434
1423 PyDoc_STRVAR(min_doc, 1435 PyDoc_STRVAR(min_doc,
(...skipping 1051 matching lines...) Expand 10 before | Expand all | Expand 10 after
2475 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) { 2487 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2476 Py_XDECREF(debug); 2488 Py_XDECREF(debug);
2477 return NULL; 2489 return NULL;
2478 } 2490 }
2479 Py_XDECREF(debug); 2491 Py_XDECREF(debug);
2480 2492
2481 return mod; 2493 return mod;
2482 #undef ADD_TO_ALL 2494 #undef ADD_TO_ALL
2483 #undef SETBUILTIN 2495 #undef SETBUILTIN
2484 } 2496 }
OLDNEW
« no previous file with comments | « Lib/test/test_builtin.py ('k') | no next file » | no next file with comments »

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