@@ -94,7 +94,6 @@ public class PythonProvider implements LanguageProvider {
94
94
private static final TypeDescriptor DICT = dict (ANY , ANY );
95
95
private static final TypeDescriptor LIST = list (ANY );
96
96
private static final TypeDescriptor TUPLE = tuple (ANY );
97
- private static final TypeDescriptor NON_INSTANTIABLE_TYPE = intersection (OBJECT , META_OBJECT );
98
97
private static final TypeDescriptor DATETIME_DATE = intersection (OBJECT , DATE );
99
98
private static final TypeDescriptor DATETIME_TIME = intersection (OBJECT , TIME );
100
99
private static final TypeDescriptor DATETIME_DATETIME = intersection (OBJECT , DATE , TIME );
@@ -103,7 +102,7 @@ public class PythonProvider implements LanguageProvider {
103
102
private static final TypeDescriptor BASE_EXCEPTION = intersection (OBJECT , EXCEPTION );
104
103
105
104
private static final TypeDescriptor type (TypeDescriptor instance , boolean varargs , TypeDescriptor ... params ) {
106
- return intersection (OBJECT , instantiable (instance , varargs , params ), META_OBJECT );
105
+ return intersection (OBJECT , instantiable (instance , varargs , params ), executable ( instance , varargs , params ), META_OBJECT );
107
106
}
108
107
109
108
private static final TypeDescriptor list (TypeDescriptor componentType ) {
@@ -219,14 +218,11 @@ public Collection<? extends Snippet> createExpressions(Context context) {
219
218
220
219
// @formatter:off
221
220
addExpressionSnippet (context , snippets , "+" , "lambda x, y: x + y" , NUMBER , union (BOOLEAN , NUMBER ), union (BOOLEAN , NUMBER ));
222
- addExpressionSnippet (context , snippets , "+" , "lambda x, y: x + y" , array (ANY ), PNoListCoercionVerifier .INSTANCE , array (ANY ), array (ANY ));
221
+ addExpressionSnippet (context , snippets , "+ str" , "lambda x, y: x + y" , NUMBER , union (BOOLEAN , NUMBER ), union (BOOLEAN , NUMBER ));
222
+ addExpressionSnippet (context , snippets , "+ list" , "lambda x, y: x + y" , array (ANY ), PNoListCoercionVerifier .INSTANCE , array (ANY ), array (ANY ));
223
223
224
224
addExpressionSnippet (context , snippets , "-" , "lambda x, y: x - y" , NUMBER , union (BOOLEAN , NUMBER ), union (BOOLEAN , NUMBER ));
225
225
226
- addExpressionSnippet (context , snippets , "*" , "lambda x, y: x * y" , NUMBER , union (BOOLEAN , NUMBER ), union (BOOLEAN , NUMBER ));
227
- addExpressionSnippet (context , snippets , "*" , "lambda x, y: x * y" , union (LIST , STRING , STR ), PSequenceMultiplicationVerifier .INSTANCE , array (ANY ), union (BOOLEAN , NUMBER ));
228
- addExpressionSnippet (context , snippets , "*" , "lambda x, y: x * y" , union (LIST , STRING , STR ), PSequenceMultiplicationVerifier .INSTANCE , union (BOOLEAN , NUMBER ), array (ANY ));
229
-
230
226
addExpressionSnippet (context , snippets , "/" , "lambda x, y: x / y" , NUMBER , PDivByZeroVerifier .INSTANCE , union (BOOLEAN , NUMBER ), union (BOOLEAN , NUMBER ));
231
227
232
228
addExpressionSnippet (context , snippets , "list-from-foreign" , "lambda x: list(x)" , array (ANY ), union (STRING , iterable (ANY ), iterator (ANY ), array (ANY )));
@@ -238,7 +234,7 @@ public Collection<? extends Snippet> createExpressions(Context context) {
238
234
addExpressionSnippet (context , snippets , "<" , "lambda x, y: x < y" , BOOLEAN , union (BOOLEAN , NUMBER ), union (BOOLEAN , NUMBER ));
239
235
addExpressionSnippet (context , snippets , "<=" , "lambda x, y: x <= y" , BOOLEAN , union (BOOLEAN , NUMBER ), union (BOOLEAN , NUMBER ));
240
236
241
- addExpressionSnippet (context , snippets , "isinstance" , "lambda x, y: isinstance(x, y)" , BOOLEAN , OBJECT , META_OBJECT );
237
+ addExpressionSnippet (context , snippets , "isinstance" , "lambda x, y: isinstance(x, y)" , BOOLEAN , ANY , META_OBJECT );
242
238
addExpressionSnippet (context , snippets , "issubclass" , "lambda x, y: issubclass(x, y)" , BOOLEAN , META_OBJECT , META_OBJECT );
243
239
// @formatter:on
244
240
return snippets ;
@@ -267,7 +263,7 @@ public Collection<? extends Snippet> createStatements(Context context) {
267
263
addStatementSnippet (context , snippets , "for" , "def gen_for(l):\n " +
268
264
" for x in l:\n " +
269
265
" return x\n \n " +
270
- "gen_for" , ANY , union (array (ANY ), iterable (ANY ), iterator (ANY )));
266
+ "gen_for" , ANY , union (array (ANY ), iterable (ANY ), iterator (ANY ), STRING ));
271
267
272
268
// any exception honours the finally block, but non-exception cannot be raised
273
269
addStatementSnippet (context , snippets , "try-finally" , "def gen_tryfinally(exc):\n " +
@@ -345,87 +341,6 @@ private static Source createSource(String resourceName) throws IOException {
345
341
private abstract static class PResultVerifier implements ResultVerifier {
346
342
}
347
343
348
- private static class PSequenceMultiplicationVerifier extends PResultVerifier {
349
-
350
- public void accept (SnippetRun snippetRun ) throws PolyglotException {
351
- List <? extends Value > parameters = snippetRun .getParameters ();
352
- assert parameters .size () == 2 ;
353
-
354
- Value par0 = parameters .get (0 );
355
- Value par1 = parameters .get (1 );
356
-
357
- // Just restrict 'number' to integer value space
358
- if (isSequence (par0 ) && isNumber (par1 )) {
359
- if (!hasMemoryError (snippetRun )) {
360
- if (isInteger (par1 ) || isNegativeNumber (par1 )) {
361
- ResultVerifier .getDefaultResultVerifier ().accept (snippetRun );
362
- } else {
363
- if (snippetRun .getException () == null ) {
364
- throw new AssertionError ("<sequence> * <non-integer> should give an error." );
365
- }
366
- }
367
- }
368
- } else if (isNumber (par0 ) && isSequence (par1 )) {
369
- if (!hasMemoryError (snippetRun )) {
370
- if (isInteger (par0 ) || isNegativeNumber (par0 )) {
371
- ResultVerifier .getDefaultResultVerifier ().accept (snippetRun );
372
- } else {
373
- if (snippetRun .getException () == null ) {
374
- throw new AssertionError ("<non-integer> * <sequence> should give an error." );
375
- }
376
- }
377
- }
378
- } else if (isNumber (par0 ) && isMapping (par1 ) || isNumber (par1 ) && isMapping (par0 )) {
379
- if (snippetRun .getException () != null ) {
380
- throw new AssertionError ("Multipliation with mapping should give an error." );
381
- }
382
- } else if (isSequence (par0 ) && isSequence (par1 )) {
383
- if (snippetRun .getException () == null ) {
384
- throw new AssertionError ("<sequence> * <sequence> should give an error." );
385
- } else {
386
- throw snippetRun .getException ();
387
- }
388
- } else if (!(isNumber (par0 ) && isScalarVector (par1 ) || isScalarVector (par0 ) && isNumber (par1 ))) {
389
- ResultVerifier .getDefaultResultVerifier ().accept (snippetRun );
390
- }
391
- }
392
-
393
- protected static boolean isScalarVector (Value val ) {
394
- return isNumber (val ) && val .hasArrayElements () && val .getArraySize () == 1 && !isMapping (val );
395
- }
396
-
397
- protected static boolean isNumber (Value par0 ) {
398
- return par0 .isNumber () || par0 .isBoolean ();
399
- }
400
-
401
- protected static boolean isNegativeNumber (Value par0 ) {
402
- return par0 .isNumber () && par0 .fitsInLong () && par0 .asLong () < 0L ;
403
- }
404
-
405
- protected static boolean isSequence (Value par0 ) {
406
- return !isNumber (par0 ) && (par0 .isString () || (par0 .hasArrayElements () && !isMapping (par0 )));
407
- }
408
-
409
- protected static boolean isMapping (Value par0 ) {
410
- return par0 .hasMembers () && par0 .getMetaObject ().toString ().contains ("dict" );
411
- }
412
-
413
- private static boolean hasMemoryError (SnippetRun snippetRun ) {
414
- PolyglotException exception = snippetRun .getException ();
415
- if (exception != null && exception .isGuestException ()) {
416
- return "MemoryError" .equals (exception .getMessage ()) || exception .getMessage ().contains ("OverflowError" );
417
- }
418
- return false ;
419
- }
420
-
421
- private static boolean isInteger (Value par0 ) {
422
- return (par0 .isNumber () && par0 .fitsInInt () || par0 .isBoolean ());
423
- }
424
-
425
- private static final PSequenceMultiplicationVerifier INSTANCE = new PSequenceMultiplicationVerifier ();
426
-
427
- }
428
-
429
344
/**
430
345
* Only accepts exact matches of types.
431
346
*/
@@ -452,35 +367,6 @@ public void accept(SnippetRun snippetRun) throws PolyglotException {
452
367
private static final PNoListCoercionVerifier INSTANCE = new PNoListCoercionVerifier ();
453
368
}
454
369
455
- /**
456
- * Foreign objects may be array-ish and boxed (e.g. if they have just one element). In this
457
- * case, we still treat them as arrays.
458
- */
459
- private static class PNoArrayVerifier extends PResultVerifier {
460
-
461
- public void accept (SnippetRun snippetRun ) throws PolyglotException {
462
- List <? extends Value > parameters = snippetRun .getParameters ();
463
- assert parameters .size () == 2 ;
464
-
465
- Value par0 = parameters .get (0 );
466
- Value par1 = parameters .get (1 );
467
-
468
- if (isNumber (par0 ) && isNumber (par1 )) {
469
- if (!(par0 .hasArrayElements () || par1 .hasArrayElements ())) {
470
- ResultVerifier .getDefaultResultVerifier ().accept (snippetRun );
471
- }
472
- } else {
473
- ResultVerifier .getDefaultResultVerifier ().accept (snippetRun );
474
- }
475
- }
476
-
477
- private static boolean isNumber (Value val ) {
478
- return val .isNumber () || val .isBoolean ();
479
- }
480
-
481
- private static final PNoArrayVerifier INSTANCE = new PNoArrayVerifier ();
482
- }
483
-
484
370
/**
485
371
* Only accepts exact matches of types.
486
372
*/
@@ -501,41 +387,4 @@ public void accept(SnippetRun snippetRun) throws PolyglotException {
501
387
502
388
private static final PDivByZeroVerifier INSTANCE = new PDivByZeroVerifier ();
503
389
}
504
-
505
- /**
506
- * Only accepts exact matches of types.
507
- */
508
- private static class PDictMemberVerifier extends PResultVerifier {
509
-
510
- private final String [] expectedMembers ;
511
- private final String [] unexpectedMembers ;
512
-
513
- public PDictMemberVerifier (String [] expectedMembers , String [] unexpectedMembers ) {
514
- this .expectedMembers = expectedMembers ;
515
- this .unexpectedMembers = unexpectedMembers ;
516
- }
517
-
518
- public void accept (SnippetRun snippetRun ) throws PolyglotException {
519
- ResultVerifier .getDefaultResultVerifier ().accept (snippetRun );
520
-
521
- Value result = snippetRun .getResult ();
522
- if (result .hasMembers ()) {
523
- for (String expectedMember : expectedMembers ) {
524
- if (!result .hasMember (expectedMember )) {
525
- throw new AssertionError ("Expected member missing: " + expectedMember );
526
- }
527
- }
528
- for (String unexpectedMember : unexpectedMembers ) {
529
- if (result .hasMember (unexpectedMember )) {
530
- throw new AssertionError ("Unexpected member present: " + unexpectedMember );
531
- }
532
- }
533
- }
534
- }
535
- }
536
-
537
- private static String [] arr (String ... strings ) {
538
- return strings ;
539
- }
540
-
541
390
}
0 commit comments