@@ -1281,32 +1281,38 @@ protected final BinaryComparisonNode createComparison() {
1281
1281
}
1282
1282
1283
1283
@ Specialization (guards = "args.length == 0" )
1284
- Object maxSequence (VirtualFrame frame , Object arg1 , Object [] args , @ SuppressWarnings ("unused" ) PNone keywordArg ,
1285
- @ Cached ( "create()" ) GetIteratorNode getIterator ,
1286
- @ Cached ( "create()" ) GetNextNode next ,
1284
+ Object maxSequence (VirtualFrame frame , Object arg1 , Object [] args , @ SuppressWarnings ("unused" ) PNone key , Object defaultVal ,
1285
+ @ Cached GetIteratorNode getIterator ,
1286
+ @ Cached GetNextNode next ,
1287
1287
@ Cached ("createComparison()" ) BinaryComparisonNode compare ,
1288
1288
@ Shared ("castToBooleanNode" ) @ Cached ("createIfTrueNode()" ) CoerceToBooleanNode castToBooleanNode ,
1289
- @ Cached ("create()" ) IsBuiltinClassProfile errorProfile1 ,
1290
- @ Cached ("create()" ) IsBuiltinClassProfile errorProfile2 ) {
1291
- return minmaxSequenceWithKey (frame , arg1 , args , null , getIterator , next , compare , castToBooleanNode , null , errorProfile1 , errorProfile2 );
1289
+ @ Cached IsBuiltinClassProfile errorProfile1 ,
1290
+ @ Cached IsBuiltinClassProfile errorProfile2 ,
1291
+ @ Shared ("hasDefaultProfile" ) @ Cached ("createBinaryProfile()" ) ConditionProfile hasDefaultProfile ) {
1292
+ return minmaxSequenceWithKey (frame , arg1 , args , null , defaultVal , getIterator , next , compare , castToBooleanNode , null , errorProfile1 , errorProfile2 , hasDefaultProfile );
1292
1293
}
1293
1294
1294
1295
@ Specialization (guards = "args.length == 0" )
1295
- Object minmaxSequenceWithKey (VirtualFrame frame , Object arg1 , @ SuppressWarnings ("unused" ) Object [] args , Object keywordArg ,
1296
- @ Cached ( "create()" ) GetIteratorNode getIterator ,
1297
- @ Cached ( "create()" ) GetNextNode next ,
1296
+ Object minmaxSequenceWithKey (VirtualFrame frame , Object arg1 , @ SuppressWarnings ("unused" ) Object [] args , Object keywordArg , Object defaultVal ,
1297
+ @ Cached GetIteratorNode getIterator ,
1298
+ @ Cached GetNextNode next ,
1298
1299
@ Cached ("createComparison()" ) BinaryComparisonNode compare ,
1299
1300
@ Shared ("castToBooleanNode" ) @ Cached ("createIfTrueNode()" ) CoerceToBooleanNode castToBooleanNode ,
1300
- @ Cached ("create()" ) CallNode keyCall ,
1301
- @ Cached ("create()" ) IsBuiltinClassProfile errorProfile1 ,
1302
- @ Cached ("create()" ) IsBuiltinClassProfile errorProfile2 ) {
1301
+ @ Cached CallNode keyCall ,
1302
+ @ Cached IsBuiltinClassProfile errorProfile1 ,
1303
+ @ Cached IsBuiltinClassProfile errorProfile2 ,
1304
+ @ Shared ("hasDefaultProfile" ) @ Cached ("createBinaryProfile()" ) ConditionProfile hasDefaultProfile ) {
1303
1305
Object iterator = getIterator .executeWith (frame , arg1 );
1304
1306
Object currentValue ;
1305
1307
try {
1306
1308
currentValue = next .execute (frame , iterator );
1307
1309
} catch (PException e ) {
1308
1310
e .expectStopIteration (errorProfile1 );
1309
- throw raise (PythonErrorType .ValueError , ErrorMessages .ARG_IS_EMPTY_SEQ , this instanceof MaxNode ? "max" : "min" );
1311
+ if (hasDefaultProfile .profile (PGuards .isNoValue (defaultVal ))) {
1312
+ throw raise (PythonErrorType .ValueError , ErrorMessages .ARG_IS_EMPTY_SEQ , getName ());
1313
+ } else {
1314
+ currentValue = defaultVal ;
1315
+ }
1310
1316
}
1311
1317
Object currentKey = applyKeyFunction (frame , keywordArg , keyCall , currentValue );
1312
1318
while (true ) {
@@ -1338,20 +1344,30 @@ Object minmaxSequenceWithKey(VirtualFrame frame, Object arg1, @SuppressWarnings(
1338
1344
return currentValue ;
1339
1345
}
1340
1346
1347
+ private String getName () {
1348
+ return this instanceof MaxNode ? "max" : "min" ;
1349
+ }
1350
+
1341
1351
@ Specialization (guards = "args.length != 0" )
1342
- Object minmaxBinary (VirtualFrame frame , Object arg1 , Object [] args , @ SuppressWarnings ("unused" ) PNone keywordArg ,
1352
+ Object minmaxBinary (VirtualFrame frame , Object arg1 , Object [] args , @ SuppressWarnings ("unused" ) PNone keywordArg , Object defaultVal ,
1343
1353
@ Cached ("createComparison()" ) BinaryComparisonNode compare ,
1344
1354
@ Cached ("createBinaryProfile()" ) ConditionProfile moreThanTwo ,
1345
- @ Shared ("castToBooleanNode" ) @ Cached ("createIfTrueNode()" ) CoerceToBooleanNode castToBooleanNode ) {
1346
- return minmaxBinaryWithKey (frame , arg1 , args , null , compare , null , moreThanTwo , castToBooleanNode );
1355
+ @ Shared ("castToBooleanNode" ) @ Cached ("createIfTrueNode()" ) CoerceToBooleanNode castToBooleanNode ,
1356
+ @ Shared ("hasDefaultProfile" ) @ Cached ("createBinaryProfile()" ) ConditionProfile hasDefaultProfile ) {
1357
+ return minmaxBinaryWithKey (frame , arg1 , args , null , defaultVal , compare , null , moreThanTwo , castToBooleanNode , hasDefaultProfile );
1347
1358
}
1348
1359
1349
1360
@ Specialization (guards = "args.length != 0" )
1350
- Object minmaxBinaryWithKey (VirtualFrame frame , Object arg1 , Object [] args , Object keywordArg ,
1361
+ Object minmaxBinaryWithKey (VirtualFrame frame , Object arg1 , Object [] args , Object keywordArg , @ SuppressWarnings ( "unused" ) Object defaultVal ,
1351
1362
@ Cached ("createComparison()" ) BinaryComparisonNode compare ,
1352
1363
@ Cached CallNode keyCall ,
1353
1364
@ Cached ("createBinaryProfile()" ) ConditionProfile moreThanTwo ,
1354
- @ Shared ("castToBooleanNode" ) @ Cached ("createIfTrueNode()" ) CoerceToBooleanNode castToBooleanNode ) {
1365
+ @ Shared ("castToBooleanNode" ) @ Cached ("createIfTrueNode()" ) CoerceToBooleanNode castToBooleanNode ,
1366
+ @ Shared ("hasDefaultProfile" ) @ Cached ("createBinaryProfile()" ) ConditionProfile hasDefaultProfile ) {
1367
+
1368
+ if (!hasDefaultProfile .profile (PGuards .isNoValue (defaultVal ))) {
1369
+ throw raise (PythonBuiltinClassType .TypeError , "Cannot specify a default for %s with multiple positional arguments" , getName ());
1370
+ }
1355
1371
Object currentValue = arg1 ;
1356
1372
Object currentKey = applyKeyFunction (frame , keywordArg , keyCall , currentValue );
1357
1373
Object nextValue = args [0 ];
@@ -1399,15 +1415,15 @@ private static Object applyKeyFunction(VirtualFrame frame, Object keywordArg, Ca
1399
1415
1400
1416
// max(iterable, *[, key])
1401
1417
// max(arg1, arg2, *args[, key])
1402
- @ Builtin (name = MAX , minNumOfPositionalArgs = 1 , takesVarArgs = true , keywordOnlyNames = {"key" })
1418
+ @ Builtin (name = MAX , minNumOfPositionalArgs = 1 , takesVarArgs = true , keywordOnlyNames = {"key" , "default" })
1403
1419
@ GenerateNodeFactory
1404
1420
public abstract static class MaxNode extends MinMaxNode {
1405
1421
1406
1422
}
1407
1423
1408
1424
// min(iterable, *[, key])
1409
1425
// min(arg1, arg2, *args[, key])
1410
- @ Builtin (name = MIN , minNumOfPositionalArgs = 1 , takesVarArgs = true , keywordOnlyNames = {"key" })
1426
+ @ Builtin (name = MIN , minNumOfPositionalArgs = 1 , takesVarArgs = true , keywordOnlyNames = {"key" , "default" })
1411
1427
@ GenerateNodeFactory
1412
1428
public abstract static class MinNode extends MinMaxNode {
1413
1429
0 commit comments